You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
:first: Selects the first element in the set of elements.
:last: Selects the last element in the set of elements.
:eq(index): Selects the element with the specified index.
:nth(index): Selects the element with the specified index. This pseudo-class
is equivalent to :eq.
:gt(index): Selects elements with a higher index than the specified value.
:lt(index): Selects elements with a lower index than the specified value.
:even: Selects even elements, zero-indexed. For example, :even will select
the second, fourth, and sixth elements.
:odd: Selects odd elements, zero-indexed. For example, :odd will select the
first, third, and fifth elements.
:not(:positional), where :positional is any of the above: Excludes
elements that match the specified selector.
Installation
To install cheerio-select, use npm:
npm install cheerio-select
Usage
import{parseDocument}from"htmlparser2";import{select,filter,is,some}from"cheerio-select";constdocument=parseDocument("<html><body><div></div></body></html>");constdom=parseDocument("<div><p>First<p>Second");// Select all divsexpect(select("div",dom)).toHaveLength(1);// Accepts a function as a selectorexpect(select((elem)=>elem.name==="p",dom)).toHaveLength(2);// Supports positionalsexpect(select("p:first",dom)).toHaveLength(1);// Supports filteringexpect(filter("p:contains(First)",dom.children)).toHaveLength(1);// Supports checking whether an element matches a selectorexpect(is("p",dom.children[0])).toBe(true);// Supports checking whether any element in a list matches a selectorexpect(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.
About
CSS selector engine supporting jQuery selectors, based on css-select