@adguard/agtree 1.0.1 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,14 +2,45 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
- The format is based on [Keep a Changelog][keepachangelog], and this project
6
- adheres to [Semantic Versioning][semver].
5
+ The format is based on [Keep a Changelog][keepachangelog], and this project adheres to [Semantic Versioning][semver].
7
6
 
8
- [keepachangelog]: https://keepachangelog.com/en/1.0.0/
9
- [semver]: https://semver.org/spec/v2.0.0.html
7
+ ## 1.1.1 - 2023-08-11
8
+
9
+ ### Fixed
10
+
11
+ - Validation of assignable modifiers which may be used without a value
10
12
 
11
- ## [Unreleased]
13
+ ## 1.1.0 - 2023-08-10
14
+
15
+ ### Added
16
+
17
+ - Compatibility tables for modifiers
18
+ - Validator for modifiers
19
+ - Basic rule converter
20
+ - New utils (regex, quotes)
21
+
22
+ ### Changed
23
+
24
+ - Updated dependencies
25
+ - Improved library build
26
+ - Improved CSSTree utils
27
+ - Export CSSTree utils
28
+ - Store raw data while parsing
29
+ - General code improvements
30
+
31
+ ### Fixed
32
+
33
+ - Package metadata
34
+ - Type import/export
35
+ - Modifier list parsing
36
+ - Scriptlet parsing
37
+ - Metadata parsing
38
+
39
+ ## 1.0.1 - 2023-05-24
12
40
 
13
41
  ### Added
14
42
 
15
43
  - Migrated parser from AGLint to a separate package.
44
+
45
+ [keepachangelog]: https://keepachangelog.com/en/1.0.0/
46
+ [semver]: https://semver.org/spec/v2.0.0.html
package/README.md CHANGED
@@ -1,76 +1,50 @@
1
+ <!-- markdownlint-disable -->
1
2
  &nbsp;
2
-
3
3
  <p align="center">
4
- <picture>
5
- <source
6
- media="(prefers-color-scheme: dark)"
7
- srcset="
8
- https://cdn.adtidy.org/website/github.com/AGTree/agtree_darkmode.svg
9
- "
10
- />
11
- <img
12
- alt="AGTree"
13
- src="https://cdn.adtidy.org/website/github.com/AGTree/agtree_lightmode.svg"
14
- width="350px"
15
- />
16
- </picture>
4
+ <picture>
5
+ <source media="(prefers-color-scheme: dark)" srcset="https://cdn.adtidy.org/website/github.com/AGTree/agtree_darkmode.svg" />
6
+ <img alt="AGTree" src="https://cdn.adtidy.org/website/github.com/AGTree/agtree_lightmode.svg" width="350px" />
7
+ </picture>
17
8
  </p>
18
9
  <h3 align="center">Universal adblock filter list parser</h3>
19
10
  <p align="center">Supported syntaxes:</p>
20
11
  <p align="center">
21
- <a href="https://adguard.com"
22
- ><img
23
- src="https://cdn.adguard.com/website/github.com/AGLint/adg_logo.svg"
24
- width="14px"
25
- />
26
- AdGuard</a
27
- >
28
- |
29
- <a href="https://github.com/gorhill/uBlock"
30
- ><img
31
- src="https://cdn.adguard.com/website/github.com/AGLint/ubo_logo.svg"
32
- width="14px"
33
- />
34
- uBlock Origin</a
35
- >
36
- |
37
- <a href="https://getadblock.com"
38
- ><img
39
- src="https://cdn.adguard.com/website/github.com/AGLint/ab_logo.svg"
40
- width="14px"
41
- />
42
- AdBlock</a
43
- >
44
- |
45
- <a href="https://adblockplus.org"
46
- ><img
47
- src="https://cdn.adguard.com/website/github.com/AGLint/abp_logo.svg"
48
- width="14px"
49
- />
50
- Adblock Plus</a
51
- >
12
+ <a href="https://adguard.com">
13
+ <img src="https://cdn.adguard.com/website/github.com/AGLint/adg_logo.svg" width="14px" />
14
+ AdGuard
15
+ </a>
16
+ |
17
+ <a href="https://github.com/gorhill/uBlock">
18
+ <img src="https://cdn.adguard.com/website/github.com/AGLint/ubo_logo.svg" width="14px" />
19
+ uBlock Origin
20
+ </a>
21
+ |
22
+ <a href="https://getadblock.com">
23
+ <img src="https://cdn.adguard.com/website/github.com/AGLint/ab_logo.svg" width="14px" />
24
+ AdBlock
25
+ </a>
26
+ |
27
+ <a href="https://adblockplus.org">
28
+ <img src="https://cdn.adguard.com/website/github.com/AGLint/abp_logo.svg" width="14px" />
29
+ Adblock Plus
30
+ </a>
52
31
  </p>
53
-
54
32
  <p align="center">
55
- <a href="https://www.npmjs.com/package/@adguard/agtree"
56
- ><img src="https://img.shields.io/npm/v/@adguard/agtree" alt="NPM version"
57
- /></a>
58
- <a href="https://www.npmjs.com/package/@adguard/agtree"
59
- ><img
60
- src="https://img.shields.io/npm/dm/@adguard/agtree"
61
- alt="NPM Downloads"
62
- /></a>
63
- <a href="https://github.com/AdguardTeam/AGTree/blob/master/LICENSE"
64
- ><img src="https://img.shields.io/npm/l/@adguard/agtree" alt="License"
65
- /></a>
33
+ <a href="https://www.npmjs.com/package/@adguard/agtree">
34
+ <img src="https://img.shields.io/npm/v/@adguard/agtree" alt="NPM version" />
35
+ </a>
36
+ <a href="https://www.npmjs.com/package/@adguard/agtree">
37
+ <img src="https://img.shields.io/npm/dm/@adguard/agtree" alt="NPM Downloads" />
38
+ </a>
39
+ <a href="https://github.com/AdguardTeam/tsurlfilter/blob/master/packages/agtree/LICENSE">
40
+ <img src="https://img.shields.io/npm/l/@adguard/agtree" alt="License" />
41
+ </a>
66
42
  </p>
43
+ <!-- markdownlint-restore -->
67
44
 
68
45
  Table of Contents:
69
46
 
70
47
  - [Introduction](#introduction)
71
- - [Usage](#usage)
72
- - [Parsing rules](#parsing-rules)
73
- - [Parsing filter lists](#parsing-filter-lists)
74
48
  - [Development \& Contribution](#development--contribution)
75
49
  - [Ideas \& Questions](#ideas--questions)
76
50
  - [License](#license)
@@ -78,259 +52,76 @@ Table of Contents:
78
52
 
79
53
  ## Introduction
80
54
 
81
- AGTree is a universal adblock filter list parser. It supports all syntaxes
82
- currently in use: AdGuard, uBlock Origin and AdBlock / Adblock Plus.
83
-
84
- ## Usage
85
-
86
- AGTree provides a powerful, error-tolerant parser for all kinds of ad blocking
87
- rules. It fully supports AdGuard, uBlock Origin and Adblock Plus syntaxes, and
88
- provides a high-detail AST for all rules.
89
-
90
- Basically, the parser API has two main parts:
91
-
92
- - Parser: parsing rules (string &#8594; AST)
93
- - Generator: serialization of ASTs (AST &#8594; string)
94
-
95
- ### Parsing rules
96
-
97
- You can parse individual rules using the `RuleParser` class. It has a `parse`
98
- method that takes a rule string and returns an AST. For example, this code:
99
-
100
- ```typescript
101
- import { RuleParser } from "@adguard/agtree";
102
-
103
- // RuleParser automatically determines the rule type
104
- const ast = RuleParser.parse("/ads.js^$script");
105
- ```
106
-
107
- will gives you this AST:
108
-
109
- ```json
110
- {
111
- "type": "NetworkRule",
112
- "category": "Network",
113
- "syntax": "Common",
114
- "exception": false,
115
- "pattern": {
116
- "type": "Value",
117
- "value": "/ads.js^"
118
- },
119
- "modifiers": {
120
- "type": "ModifierList",
121
- "children": [
122
- {
123
- "type": "Modifier",
124
- "modifier": {
125
- "type": "Value",
126
- "value": "script"
127
- },
128
- "exception": false
129
- }
130
- ]
131
- }
132
- }
133
- ```
134
-
135
- <details>
136
-
137
- <summary>Show full AST (with locations)</summary>
138
-
139
- ```json
140
- {
141
- "type": "NetworkRule",
142
- "loc": {
143
- "start": {
144
- "offset": 0,
145
- "line": 1,
146
- "column": 1
147
- },
148
- "end": {
149
- "offset": 15,
150
- "line": 1,
151
- "column": 16
152
- }
153
- },
154
- "raws": {
155
- "text": "/ads.js^$script"
156
- },
157
- "category": "Network",
158
- "syntax": "Common",
159
- "exception": false,
160
- "pattern": {
161
- "type": "Value",
162
- "loc": {
163
- "start": {
164
- "offset": 0,
165
- "line": 1,
166
- "column": 1
167
- },
168
- "end": {
169
- "offset": 8,
170
- "line": 1,
171
- "column": 9
172
- }
173
- },
174
- "value": "/ads.js^"
175
- },
176
- "modifiers": {
177
- "type": "ModifierList",
178
- "loc": {
179
- "start": {
180
- "offset": 9,
181
- "line": 1,
182
- "column": 10
183
- },
184
- "end": {
185
- "offset": 15,
186
- "line": 1,
187
- "column": 16
188
- }
189
- },
190
- "children": [
191
- {
192
- "type": "Modifier",
193
- "loc": {
194
- "start": {
195
- "offset": 9,
196
- "line": 1,
197
- "column": 10
198
- },
199
- "end": {
200
- "offset": 15,
201
- "line": 1,
202
- "column": 16
203
- }
204
- },
205
- "modifier": {
206
- "type": "Value",
207
- "loc": {
208
- "start": {
209
- "offset": 9,
210
- "line": 1,
211
- "column": 10
212
- },
213
- "end": {
214
- "offset": 15,
215
- "line": 1,
216
- "column": 16
217
- }
218
- },
219
- "value": "script"
220
- },
221
- "exception": false
222
- }
223
- ]
224
- }
225
- }
226
- ```
227
-
228
- </details>
229
-
230
- As you can see, this AST is very detailed and contains all the information about
231
- the rule: syntax, category, exception, modifiers, node locations, and so on.
232
- Locations are especially useful for linters, since they allow you to point to
233
- the exact place in the rule where the error occurred.
234
-
235
- And this code:
236
-
237
- ```typescript
238
- RuleParser.generate(ast);
239
- ```
240
-
241
- will generate the adblock rule from the AST (serialization):
242
-
243
- ```adblock
244
- /ads.js^$script
245
- ```
246
-
247
- Please keep in mind that the parser omits unnecessary whitespaces, so the
248
- generated rule may not match with the original rule character by character.
249
- Only the formatting can change, the rule itself remains the same. You can
250
- pass any rule to the parser, it automatically determines the type and category
251
- of the rule. If the rule is syntactically incorrect, the parser will throw an
252
- error.
253
-
254
- ### Parsing filter lists
255
-
256
- You can also parse complete filter lists using the `FilterListParser` class.
257
- It works the same way as the `RuleParser` class. Here is an example of parsing
258
- [EasyList](https://easylist.to/easylist/easylist.txt) and generating it back:
259
-
260
- ```typescript
261
- import { FilterListParser } from "@adguard/agtree";
262
- import { writeFile } from "fs/promises";
263
- // Requires installing "node-fetch" package
264
- import fetch from "node-fetch";
265
-
266
- // Download EasyList
267
- const easyList = await (
268
- await fetch("https://easylist.to/easylist/easylist.txt")
269
- ).text();
270
-
271
- // Or read it from file
272
- // const easyList = await readFile('easylist.txt', 'utf-8');
55
+ AGTree is a universal tool for working with adblock filter lists. It contains the following modules:
273
56
 
274
- // Parse EasyList to AST. By default, parser is very tolerant,
275
- // if it can't parse some rules, it will just mark them as "raw".
276
- // If you want to disable this behavior, you can pass the second
277
- // argument as "false" to the "parse" method, like this:
278
- // const ast = FilterListParser.parse(easyList, false);
279
- const ast = FilterListParser.parse(easyList);
57
+ - [Adblock rule converter][converter-url]
58
+ - [Adblock rule parser][parser-url]
59
+ - [Compatibility tables][compatibility-tables-url]
280
60
 
281
- // Generate filter list from filter list AST
282
- const easyListGenerated = FilterListParser.generate(ast);
61
+ AGTree supports all syntaxes currently in use:
283
62
 
284
- // Write generated filter list to file
285
- await writeFile("easylist-generated.txt", easyListGenerated);
286
- ```
63
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/adg_logo.svg" width="14px"> [AdGuard][adg-url]
64
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/ubo_logo.svg" width="14px"> [uBlock Origin][ubo-url]
65
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/abp_logo.svg" width="14px"> [Adblock Plus][abp-url]
66
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/ab_logo.svg" width="14px"> [AdBlock][ab-url]
287
67
 
288
68
  ## Development & Contribution
289
69
 
290
- Please read the [CONTRIBUTING.md](CONTRIBUTING.md) file for details on how to
291
- contribute to this project.
70
+ Please read the [CONTRIBUTING.md][contributing-url] file for details on how to contribute to this project.
292
71
 
293
72
  ## Ideas & Questions
294
73
 
295
- If you have any questions or ideas for new features, please open an issue or a
296
- discussion. We will be happy to discuss it with you.
74
+ If you have any questions or ideas for new features, please [open an issue][new-issue-url] or a
75
+ [discussion][discussions-url]. We will be happy to discuss it with you.
297
76
 
298
77
  ## License
299
78
 
300
- AGTree is licensed under the MIT License. See the [LICENSE](LICENSE) file for
301
- details.
79
+ AGTree is licensed under the MIT License. See the [LICENSE][license-url] file for details.
302
80
 
303
81
  ## References
304
82
 
305
- Here are some useful links to help you write adblock rules. This list is not
306
- exhaustive, so if you know any other useful resources, please let us know.
83
+ Here are some useful links to help you write adblock rules. This list is not exhaustive, so if you know any other useful
84
+ resources, please let us know.
307
85
 
86
+ <!--markdownlint-disable MD013-->
308
87
  - Syntax documentation:
309
- - [AdGuard: _How to create your own ad filters_][adg-filters]
310
- - [uBlock Origin: _Static filter syntax_][ubo-filters]
311
- - [Adblock Plus: _How to write filters_][abp-filters]
88
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/adg_logo.svg" width="14px"> [AdGuard: *How to create your own ad filters*][adg-filters]
89
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/ubo_logo.svg" width="14px"> [uBlock Origin: *Static filter syntax*][ubo-filters]
90
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/abp_logo.svg" width="14px"> [Adblock Plus: *How to write filters*][abp-filters]
312
91
  - Extended CSS documentation:
313
- - [MDN: _CSS selectors_][mdn-css-selectors]
314
- - [AdGuard: _Extended CSS capabilities_][adg-ext-css]
315
- - [uBlock Origin: _Procedural cosmetic filters_][ubo-procedural]
316
- - [Adblock Plus: _Extended CSS selectors_][abp-ext-css]
92
+ - [MDN: *CSS selectors*][mdn-css-selectors]
93
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/adg_logo.svg" width="14px"> [AdGuard: *Extended CSS capabilities*][adg-ext-css]
94
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/ubo_logo.svg" width="14px"> [uBlock Origin: *Procedural cosmetic filters*][ubo-procedural]
95
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/abp_logo.svg" width="14px"> [Adblock Plus: *Extended CSS selectors*][abp-ext-css]
317
96
  - Scriptlets:
318
- - [AdGuard scriptlets][adg-scriptlets]
319
- - [uBlock Origin scriptlets][ubo-scriptlets]
320
- - [Adblock Plus snippets][abp-snippets]
97
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/adg_logo.svg" width="14px"> [AdGuard scriptlets][adg-scriptlets]
98
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/ubo_logo.svg" width="14px"> [uBlock Origin scriptlets][ubo-scriptlets]
99
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/abp_logo.svg" width="14px"> [Adblock Plus snippets][abp-snippets]
321
100
  - Third party libraries:
322
- - [CSSTree docs][css-tree-docs]
323
- - [AdGuard's compatibility table][adg-compatibility-table]
101
+ - <img src="https://raw.githubusercontent.com/csstree/csstree/master/assets/csstree-logo-rounded.svg" width="14px"> [CSSTree docs][css-tree-docs]
102
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/adg_logo.svg" width="14px"> [AdGuard's compatibility table][adg-compatibility-table]
103
+ <!--markdownlint-enable MD013-->
324
104
 
105
+ [ab-url]: https://getadblock.com
325
106
  [abp-ext-css]: https://help.eyeo.com/adblockplus/how-to-write-filters#elemhide-emulation
326
107
  [abp-filters]: https://help.eyeo.com/adblockplus/how-to-write-filters
327
108
  [abp-snippets]: https://help.eyeo.com/adblockplus/snippet-filters-tutorial#snippets-ref
109
+ [abp-url]: https://adblockplus.org
328
110
  [adg-compatibility-table]: https://github.com/AdguardTeam/Scriptlets/blob/master/wiki/compatibility-table.md
329
111
  [adg-ext-css]: https://github.com/AdguardTeam/ExtendedCss/blob/master/README.md
330
112
  [adg-filters]: https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters
331
113
  [adg-scriptlets]: https://github.com/AdguardTeam/Scriptlets/blob/master/wiki/about-scriptlets.md#scriptlets
114
+ [adg-url]: https://adguard.com
115
+ [compatibility-tables-url]: https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree/src/compatibility-tables
116
+ [contributing-url]: https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree/CONTRIBUTING.md
117
+ [converter-url]: https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree/src/converter
332
118
  [css-tree-docs]: https://github.com/csstree/csstree/tree/master/docs
119
+ [discussions-url]: https://github.com/AdguardTeam/tsurlfilter/discussions
120
+ [license-url]: https://github.com/AdguardTeam/tsurlfilter/blob/master/packages/agtree/LICENSE
333
121
  [mdn-css-selectors]: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors
122
+ [new-issue-url]: https://github.com/AdguardTeam/tsurlfilter/issues/new
123
+ [parser-url]: https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree/src/parser
334
124
  [ubo-filters]: https://github.com/gorhill/uBlock/wiki/Static-filter-syntax
335
125
  [ubo-procedural]: https://github.com/gorhill/uBlock/wiki/Procedural-cosmetic-filters
336
126
  [ubo-scriptlets]: https://github.com/gorhill/uBlock/wiki/Resources-Library#available-general-purpose-scriptlets
127
+ [ubo-url]: https://github.com/gorhill/uBlock