Gathering detailed insights and metrics for @selderee/plugin-htmlparser2
Gathering detailed insights and metrics for @selderee/plugin-htmlparser2
Gathering detailed insights and metrics for @selderee/plugin-htmlparser2
Gathering detailed insights and metrics for @selderee/plugin-htmlparser2
Selectors decision tree - pick matching selectors, fast.
npm install @selderee/plugin-htmlparser2
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2 Stars
38 Commits
1 Forks
2 Watching
6 Branches
1 Contributors
Updated on 25 Aug 2023
TypeScript (93.2%)
JavaScript (6.8%)
Cumulative downloads
Total Downloads
Last day
-8.1%
320,417
Compared to previous day
Last week
3.7%
1,883,430
Compared to previous week
Last month
9.9%
7,832,105
Compared to previous month
Last year
76.5%
76,139,520
Compared to previous year
2
1
Selectors decision tree - pick matching selectors, fast.
The problem statement: there are multiple CSS selectors with attached handlers, and a HTML DOM to process. For each HTML Element a matching handler has to be found and applied.
The naive approach is to walk through the DOM and test each and every selector against each Element. This means O(n*m) complexity.
It is pretty clear though that if we have selectors that share something in common then we can reduce the number of checks.
The main selderee
package offers the selectors tree structure. Runnable decision functions for specific DOM implementations are built via plugins.
~
), descendants (
) and same column combinators (||
) are also not supported.selderee
vs css-select
css-select - a CSS selector compiler & engine.
Feature | selderee | css-select |
---|---|---|
Support for htmlparser2 DOM AST | plugin | + |
"Compiles" into a function | + | + |
Pick selector(s) for a given Element | + | |
Query Element(s) for a given selector | + |
Package | Version | Folder | Changelog |
---|---|---|---|
selderee | /packages/selderee | changelog | |
@selderee/plugin-htmlparser2 | /packages/plugin-htmlparser2 | changelog |
1> npm i selderee @selderee/plugin-htmlparser2
1const htmlparser2 = require('htmlparser2'); 2const util = require('util'); 3 4const { DecisionTree, Treeify } = require('selderee'); 5const { hp2Builder } = require('@selderee/plugin-htmlparser2'); 6 7const selectorValuePairs = [ 8 ['p', 'A'], 9 ['p.foo[bar]', 'B'], 10 ['p[class~=foo]', 'C'], 11 ['div.foo', 'D'], 12 ['div > p.foo', 'E'], 13 ['div > p', 'F'], 14 ['#baz', 'G'] 15]; 16 17// Make a tree structure from all given selectors. 18const selectorsDecisionTree = new DecisionTree(selectorValuePairs); 19 20// `treeify` builder produces a string output for testing and debug purposes. 21// `treeify` expects string values attached to each selector. 22const prettyTree = selectorsDecisionTree.build(Treeify.treeify); 23console.log(prettyTree); 24 25const html = /*html*/`<html><body> 26 <div><p class="foo qux">second</p></div> 27</body></html>`; 28const dom = htmlparser2.parseDocument(html); 29const element = dom.children[0].children[0].children[1].children[0]; 30 31// `hp2Builder` produces a picker that can pick values 32// from the selectors tree. 33const picker = selectorsDecisionTree.build(hp2Builder); 34 35// Get all matches 36const allMatches = picker.pickAll(element); 37console.log(util.inspect(allMatches, { breakLength: 70, depth: null })); 38 39// or get the value from the most specific match. 40const bestMatch = picker.pick1(element); 41console.log(`Best matched value: ${bestMatch}`);
1▽ 2├─◻ Tag name 3│ ╟─◇ = p 4│ ║ ┠─▣ Attr value: class 5│ ║ ┃ ╙─◈ ~= "foo" 6│ ║ ┃ ┠─◨ Attr presence: bar 7│ ║ ┃ ┃ ┖─◁ #1 [0,2,1] B 8│ ║ ┃ ┠─◁ #2 [0,1,1] C 9│ ║ ┃ ┖─◉ Push element: > 10│ ║ ┃ └─◻ Tag name 11│ ║ ┃ ╙─◇ = div 12│ ║ ┃ ┖─◁ #4 [0,1,2] E 13│ ║ ┠─◁ #0 [0,0,1] A 14│ ║ ┖─◉ Push element: > 15│ ║ └─◻ Tag name 16│ ║ ╙─◇ = div 17│ ║ ┖─◁ #5 [0,0,2] F 18│ ╙─◇ = div 19│ ┖─▣ Attr value: class 20│ ╙─◈ ~= "foo" 21│ ┖─◁ #3 [0,1,1] D 22└─▣ Attr value: id 23 ╙─◈ = "baz" 24 ┖─◁ #6 [1,0,0] G 25[ { index: 2, value: 'C', specificity: [ 0, 1, 1 ] }, 26 { index: 4, value: 'E', specificity: [ 0, 1, 2 ] }, 27 { index: 0, value: 'A', specificity: [ 0, 0, 1 ] }, 28 { index: 5, value: 'F', specificity: [ 0, 0, 2 ] } ] 29Best matched value: E
Some gotcha: you may notice the check for #baz
has to be performed every time the decision tree is called. If it happens to be p#baz
or div#baz
or even .foo#baz
- it would be much better to write it like this. Deeper, narrower tree means less checks on average. (in case of .foo#baz
the class check might finally outweigh the tag name check and rebalance the tree.)
Targeting Node.js version >=14.
Monorepo uses NPM v7 workspaces (make sure v7 is installed when used with Node.js v14.)
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
6 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
Found 0/30 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 SAST tool detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
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
Score
Last Scanned on 2024-11-18
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