Home

Awesome

cheerio-select NPM version Node.js CI Downloads Coverage

cheerio-select is a CSS selector engine that supports jQuery selectors, based on the css-select library. This library is a thin wrapper around css-select that adds support for all of the jQuery positional pseudo-selectors:

Installation

To install cheerio-select, use npm:

npm install cheerio-select

Usage

import { parseDocument } from "htmlparser2";
import { select, filter, is, some } from "cheerio-select";

const document = parseDocument("<html><body><div></div></body></html>");

const dom = parseDocument("<div><p>First<p>Second");

// Select all divs
expect(select("div", dom)).toHaveLength(1);

// Accepts a function as a selector
expect(select((elem) => elem.name === "p", dom)).toHaveLength(2);

// Supports positionals
expect(select("p:first", dom)).toHaveLength(1);

// Supports filtering
expect(filter("p:contains(First)", dom.children)).toHaveLength(1);

// Supports checking whether an element matches a selector
expect(is("p", dom.children[0])).toBe(true);

// Supports checking whether any element in a list matches a selector
expect(some("p", dom.children)).toBe(true);

Note

Only use this module if you will actually use jQuery positional selectors in your project. If you do not need these specific selectors, it is recommended to use the css-select library directly.

License

This project is licensed under the BSD-2-Clause license. See the LICENSE file for more info.