Gathering detailed insights and metrics for cheerio-advanced-selectors
Gathering detailed insights and metrics for cheerio-advanced-selectors
npm install cheerio-advanced-selectors
Typescript
Module System
Node Version
NPM Version
73.7
Supply Chain
100
Quality
75.5
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
2,907,134
Last Day
319
Last Week
1,207
Last Month
5,292
Last Year
158,113
30 Stars
32 Commits
11 Forks
5 Watching
1 Branches
2 Contributors
Minified
Minified + Gzipped
Latest Version
2.0.1
Package Id
cheerio-advanced-selectors@2.0.1
Size
4.14 kB
NPM Version
5.7.1
Node Version
9.6.1
Publised On
12 Mar 2018
Cumulative downloads
Total Downloads
Last day
-5.3%
319
Compared to previous day
Last week
-35.5%
1,207
Compared to previous week
Last month
-16.9%
5,292
Compared to previous month
Last year
-31.3%
158,113
Compared to previous year
4
Add support for the following selectors to cheerio:
:first
:last
:eq(index)
More selectors can easily be added: Just open an issue and I'll look into it :)
This module is inspired by cheerio-eq with the added support for many different selectors.
Supports cheerio version 0.18.0 and above.
npm install cheerio-advanced-selectors
Use the .wrap()
function to make cheerio-advanced-selectors take care
of everything for you:
1var cheerio = require('cheerio') 2var cheerioAdv = require('cheerio-advanced-selectors') 3 4cheerio = cheerioAdv.wrap(cheerio) 5 6var $ = cheerio.load('<div>foo</div><div>bar</div>') 7 8$('div:first').text() // => 'foo'
Alternatively use the .find()
function to only use
cheerio-advanced-selectors for a specific selector:
1var cheerio = require('cheerio') 2var cheerioAdv = require('cheerio-advanced-selectors') 3 4var $ = cheerio.load('<div><span>foo</span></div><div><span>bar</span></div>') 5 6cheerioAdv.find($, 'div:eq(1)').text() // => 'bar'
If you need to run the same selector on a lot of different HTML
documents, you can speed things up by pre-compiling the selector using
the .compile()
function:
1var cheerio = require('cheerio') 2var cheerioAdv = require('cheerio-advanced-selectors') 3 4var myH1 = cheerioAdv.compile('div:first span:eq(1) h1') 5 6var html1 = cheerio.load('<div><span><h1>foo1</h1></span><span><h1>bar1</h1></span></div>') 7var html2 = cheerio.load('<div><span><h1>foo2</h1></span><span><h1>bar2</h1></span></div>') 8 9myH1(html1).text() // => 'bar1' 10myH1(html2).text() // => 'bar2'
Cheerio sacrifices advanced CSS selector support for speed. This means
for instance that the :eq()
selector isn't supported. The work-around
is normally to use the .eq()
function instead:
1// this will not work: 2$('div:eq(1)'); 3 4// use this instead: 5$('div').eq(1);
This is a good alternative if you write the CSS selectors from scrach,
but what if you are working with selectors that already contain :eq()
?
Don't fear, cheerio-advanced-selectors is here :)
The solution to the problem is to automatically parse the selector
string at run-time. So if you give cheerio-advanced-selectors a selector
like div:eq(1)
it will return the following cheerio cursor:
$('div').eq(1)
.
It also works for complex selectors, so that div:eq(1) h2:first span
will be converted and interpreted as
$('div').eq(1).find('h2').first().find('span')
.
This module currently only support a minimal subset of the possible advanced selectors:
:first
:last
:eq(index)
But don't fear :) It's easy to add support for other selectors. Just open an issue or make a pull request.
.wrap(cheerio)
Wraps the main cheerio module to overload the standard load
function
so it knows how to handle the advanced selectors.
Returns the cheerio
module.
.find(cheerio, selector [, context [, root]])
Run the selector
on the given cheerio object optionally within the
given context
and optionally on the given root
.
The cheerio
object is usually called $
.
.compile(selector)
Compiles the selector
and returns a function which take 3 arguments:
fn(cheerio [, context [, root]])
:
cheerio
- a reference to the cheerio object (usually called $
)context
- the context in which to run the selector (optional)root
- the HTML root on which to run the selector (optional)MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 1/29 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More