Gathering detailed insights and metrics for @adguard/ecss-tree
Gathering detailed insights and metrics for @adguard/ecss-tree
Gathering detailed insights and metrics for @adguard/ecss-tree
Gathering detailed insights and metrics for @adguard/ecss-tree
npm install @adguard/ecss-tree
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
10 Stars
42 Commits
1 Forks
13 Watching
3 Branches
94 Contributors
Updated on 13 Sept 2024
JavaScript (98.9%)
Shell (1.02%)
TypeScript (0.08%)
Cumulative downloads
Total Downloads
Last day
-72.5%
103
Compared to previous day
Last week
-3.8%
4,755
Compared to previous week
Last month
520.8%
12,752
Compared to previous month
Last year
1,176.6%
26,286
Compared to previous year
1
22
Adblock Extended CSS supplement for CSSTree. Our primary goal is to change the internal behavior of the CSSTree parser to support Extended CSS (ECSS) language elements, but we don't change the API or the AST structure. Therefore ECSSTree fully backwards compatible with CSSTree, so you can pass our AST to CSSTree functions and vice versa without any problems.
[!NOTE] If you are looking for a library that can parse CSS, but you don't know what is Adblock or Extended CSS, you should probably use CSSTree instead of this library :)
You can install the library using
yarn add @adguard/ecss-tree
npm install @adguard/ecss-tree
pnpm add @adguard/ecss-tree
Or you can use it via esm.run: https://esm.run/@adguard/ecss-tree
Currently, the following Extended CSS pseudo classes are supported:
:-abp-contains(raw)
: [ABP reference]:-abp-has(selector list)
: [ABP reference]:contains(raw)
: [ADG reference]:has-text(raw)
: [uBO reference]:if-not(selector)
: [ADG reference]:matches-css-after(raw)
: [uBO reference]:matches-css-before(raw)
: [uBO reference]:matches-css(raw)
: [ADG reference], [uBO reference]:matches-media(media query list)
: [uBO reference]:min-text-length(number)
: [uBO reference]:nth-ancestor(number)
: [ADG reference]:style(declaration list)
: [uBO reference]:upward(selector / number)
: [ADG reference], [uBO reference]:xpath(raw)
: [ADG reference], [uBO reference]In addition, CSSTree supports the following pseudo classes by default:
:has(selector list)
: [W3C reference], [ADG reference], [uBO reference]:not(selector list)
: [W3C reference], [ADG reference], [uBO reference]:is(selector list)
: [W3C reference], [ADG reference], [uBO reference]Also, CSSTree supports legacy Extended CSS elements by default (attribute selectors):
[-ext-name="value"]
, where name
is the name of the Extended CSS element and value
is its value.
For example, the following selector can be parsed by CSSTree:
1[-ext-has="selector list"]
If a pseudo class is unknown to CSSTree, it tries to parse it as a Raw
element
(if possible - see problematic cases).
The CSSTree library itself is quite flexible and error-tolerant, so it basically manages well the Extended CSS elements that are not (yet) included here.
For example, the following selector
1div:-abp-has(> section)
will be parsed by the default CSSTree as follows
1{ 2 "type": "Selector", 3 "loc": null, 4 "children": [ 5 { 6 "type": "PseudoClassSelector", 7 "loc": null, 8 "name": "-abp-has", 9 "children": [ 10 { 11 "type": "Raw", 12 "loc": null, 13 "value": "> section" 14 } 15 ] 16 } 17 ] 18}
The problem with this is that the -abp-has
parameter is parsed as Raw
, not as a Selector
,
since -abp-has
is an unknown pseudo class in CSS / CSSTree.
This is where the ECSSTree library comes into play. It detects that -abp-has
expects a selector as a parameter,
i.e. it parses the passed parameter as a Selector
. This means that the selector above will be parsed as follows:
1{ 2 "type": "Selector", 3 "loc": null, 4 "children": [ 5 { 6 "type": "PseudoClassSelector", 7 "loc": null, 8 "name": "-abp-has", 9 "children": [ 10 { 11 "type": "Selector", 12 "loc": null, 13 "children": [ 14 { 15 "type": "Combinator", 16 "loc": null, 17 "name": ">" 18 }, 19 { 20 "type": "TypeSelector", 21 "loc": null, 22 "name": "section" 23 } 24 ] 25 } 26 ] 27 } 28 ] 29}
Combinator
and similar Nodes are part of CSSTree, this fork simply specifies that the -abp-has
parameter
should be parsed as a selector. The nodes themselves are part of the CSSTree.
In addition, this approach enables a more advanced validation. For example, the default CSSTree does not throw an error when parsing the following selector:
1div:-abp-has(42)
since it doesn't know what -abp-has
is, it simply parses 42 as Raw
. ECSSTree parses the parameter as a selector,
which will throw an error, since 42 is simply an invalid selector.
The library also handles problematic selectors, such as the following:
1div:contains(aaa'bbb)
This selector doesn't fully meet with CSS standards, so even if CSSTree is flexible, it will not be able to parse it properly, because it will tokenize it as follows:
Token type | Start index | End index | Source part |
---|---|---|---|
ident-token | 0 | 3 | div |
colon-token | 3 | 4 | : |
function-token | 4 | 13 | contains( |
ident-token | 13 | 16 | aaa |
string-token | 16 | 21 | 'bbb) |
At quote mark ('
) tokenizer will think that a string is starting, and it tokenizes the rest of the input as a string.
This is the normal behavior for the tokenizer, but it is wrong for us, since the parser will fail with an
")" is expected
error, as it doesn't found the closing parenthesis, since it thinks that the string is still open.
ECSSTree will handle this case by a special re-tokenization algorithm during the parsing process, when parser reaches
this problematic point. This way, ECSSTree's parser will be able to parse this selector properly.
It is also true for xpath
.
Note: ECSSTree parses :contains
and :xpath
parameters as Raw
. The main goal of this library is changing the
internal behavior of the CSSTree's parser to make it able to parse the Extended CSS selectors properly,
not to change the AST itself. The AST should be the same as in CSSTree, so that the library can be used
as a drop-in replacement for CSSTree.
Parsing :xpath
expressions or regular expressions in detail would be a huge task, and requires new AST nodes,
which would be a breaking change. But it always parses the correct raw expression for you,
so you can parse/validate these expressions yourself if you want. There are many libraries for this,
such as xpath or regexpp.
See example codes for more details.
Here are a very simple example to show how to use ECSSTree:
1const { parse, generate, toPlainObject, fromPlainObject } = require("@adguard/ecss-tree"); 2const { inspect } = require("util"); 3 4// Some inputs to test 5const inputs = [ 6 // Valid selectors 7 `div:-abp-has(> .some-class > a[href^="https://example.com"])`, 8 `body:style(padding-top: 0 !important;):matches-media((min-width: 500px) and (max-width: 1000px))`, 9 `section:upward(2):contains(aaa'bbb):xpath(//*[contains(text(),"()(cc")])`, 10 11 // Missing closing bracket at the end 12 `div:-abp-has(> .some-class > a[href^="https://example.com"]`, 13]; 14 15// Iterate over inputs 16for (const input of inputs) { 17 try { 18 // Parse raw input to AST. This will throw an error if the input is not valid. 19 // Don't forget to set context to 'selector', because CSSTree will try to parse 20 // 'stylesheet' by default. 21 const ast = parse(input, { context: "selector" }); 22 23 // By default, AST uses a doubly linked list. To convert it to plain object, you can 24 // use toPlainObject() function. 25 // If you want to convert AST back to doubly linked list version, you can use 26 // fromPlainObject() function. 27 const astPlain = toPlainObject(ast); 28 const astAgain = fromPlainObject(astPlain); 29 30 // Print AST to console 31 console.log(inspect(astPlain, { colors: true, depth: null })); 32 33 // You can also generate string from AST (don't use plain object here) 34 console.log(generate(astAgain)); 35 } catch (e) { 36 // Mark invalid selector 37 console.log(`Invalid selector: ${input}`); 38 39 // Show CSSTree's formatted error message 40 console.log(e.formattedMessage); 41 } 42}
The API is the same as in CSSTree, so you can use the CSSTree documentation as a reference.
You can find more examples in the examples folder.
If you find a bug or want to request a new feature, please please open an issue on GitHub. Please provide a detailed description of the problem or the feature you want to request, and if possible, a code example that demonstrates the problem or the feature.
You can contribute to the project by opening a pull request. People who contribute to AdGuard projects can receive various rewards, see this page for details.
Make sure you have the following tools installed:
During development, you can use the following commands (listed in package.json
):
yarn lint
- lint the code with ESLintyarn test
- run tests with Jest (you can also run a specific test with yarn test <test-name>
)yarn build
- build the library to the dist
folder by using RollupThis library is licensed under the MIT license. See the LICENSE file for more info.
In this section, we would like to thank the following people for their work:
Here are some useful links to learn more about Extended CSS selectors:
No vulnerabilities found.
No security vulnerabilities found.