@adguard/agtree 1.0.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 +15 -0
- package/LICENSE +21 -0
- package/README.md +336 -0
- package/dist/agtree.cjs +4985 -0
- package/dist/agtree.d.ts +1717 -0
- package/dist/agtree.esm.js +4963 -0
- package/dist/agtree.iife.min.js +7 -0
- package/dist/agtree.umd.min.js +7 -0
- package/dist/build.txt +1 -0
- package/package.json +87 -0
package/dist/agtree.d.ts
ADDED
|
@@ -0,0 +1,1717 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* AGTree v1.0.1 (build date: Wed, 24 May 2023 17:09:26 GMT)
|
|
3
|
+
* (c) 2023 AdGuard Software Ltd.
|
|
4
|
+
* Released under the MIT license
|
|
5
|
+
* https://github.com/AdguardTeam/AGTree#readme
|
|
6
|
+
*/
|
|
7
|
+
import { MediaQueryListPlain, SelectorListPlain, DeclarationListPlain, FunctionNodePlain } from '@adguard/ecss-tree';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @file Possible adblock syntaxes are listed here.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Possible adblock syntaxes (supported by this library)
|
|
14
|
+
*/
|
|
15
|
+
declare enum AdblockSyntax {
|
|
16
|
+
/**
|
|
17
|
+
* Common syntax, which is supported by more than one adblocker (or by all adblockers).
|
|
18
|
+
*
|
|
19
|
+
* We typically use this syntax when we cannot determine the concrete syntax of the rule,
|
|
20
|
+
* because the syntax is used by more than one adblocker natively.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* - `||example.org^$important` is a common syntax, since it is used by all adblockers natively, and
|
|
24
|
+
* we cannot determine at parsing level whether `important` is a valid option or not, and if it is valid,
|
|
25
|
+
* then which adblocker supports it.
|
|
26
|
+
*/
|
|
27
|
+
Common = "Common",
|
|
28
|
+
/**
|
|
29
|
+
* Adblock Plus syntax.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* - `example.org#$#abort-on-property-read alert` is an Adblock Plus syntax, since it is not used by any other
|
|
33
|
+
* adblockers directly (probably supported by some on-the-fly conversion, but this is not the native syntax).
|
|
34
|
+
* @see {@link https://adblockplus.org/}
|
|
35
|
+
*/
|
|
36
|
+
Abp = "AdblockPlus",
|
|
37
|
+
/**
|
|
38
|
+
* uBlock Origin syntax.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* - `example.com##+js(set, atob, noopFunc)` is an uBlock Origin syntax, since it is not used by any other
|
|
42
|
+
* adblockers directly (probably supported by some on-the-fly conversion, but this is not the native syntax).
|
|
43
|
+
* @see {@link https://github.com/gorhill/uBlock}
|
|
44
|
+
*/
|
|
45
|
+
Ubo = "UblockOrigin",
|
|
46
|
+
/**
|
|
47
|
+
* AdGuard syntax.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* - `example.org#%#//scriptlet("abort-on-property-read", "alert")` is an AdGuard syntax, since it is not used
|
|
51
|
+
* by any other adblockers directly (probably supported by some on-the-fly conversion, but this is not the native
|
|
52
|
+
* syntax).
|
|
53
|
+
* @see {@link https://adguard.com/}
|
|
54
|
+
*/
|
|
55
|
+
Adg = "AdGuard"
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Classic domain separator.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```adblock
|
|
63
|
+
* ! Domains are separated by ",":
|
|
64
|
+
* example.com,~example.org##.ads
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
declare const CLASSIC_DOMAIN_SEPARATOR = ",";
|
|
68
|
+
/**
|
|
69
|
+
* Modifier domain separator.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```adblock
|
|
73
|
+
* ! Domains are separated by "|":
|
|
74
|
+
* ads.js^$script,domains=example.com|~example.org
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
declare const MODIFIER_DOMAIN_SEPARATOR = "|";
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Represents possible logical expression operators.
|
|
81
|
+
*/
|
|
82
|
+
type AnyOperator = '&&' | '||' | '!';
|
|
83
|
+
/**
|
|
84
|
+
* Represents possible new line types.
|
|
85
|
+
*/
|
|
86
|
+
type NewLine = 'crlf' | 'lf' | 'cr';
|
|
87
|
+
/**
|
|
88
|
+
* Represents any kind of logical expression node.
|
|
89
|
+
*/
|
|
90
|
+
type AnyExpressionNode = ExpressionVariableNode | ExpressionOperatorNode | ExpressionParenthesisNode;
|
|
91
|
+
/**
|
|
92
|
+
* Represents any kind of adblock rule.
|
|
93
|
+
*/
|
|
94
|
+
type AnyRule = EmptyRule | AnyCommentRule | AnyCosmeticRule | NetworkRule | InvalidRule;
|
|
95
|
+
/**
|
|
96
|
+
* Represents any comment-like adblock rule.
|
|
97
|
+
*/
|
|
98
|
+
type AnyCommentRule = AgentCommentRule | CommentRule | ConfigCommentRule | HintCommentRule | MetadataCommentRule | PreProcessorCommentRule;
|
|
99
|
+
/**
|
|
100
|
+
* Represents any cosmetic adblock rule.
|
|
101
|
+
*/
|
|
102
|
+
type AnyCosmeticRule = CssInjectionRule | ElementHidingRule | ScriptletInjectionRule | HtmlFilteringRule | JsInjectionRule;
|
|
103
|
+
/**
|
|
104
|
+
* Represents the different comment markers that can be used in an adblock rule.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* - If the rule is `! This is just a comment`, then the marker will be `!`.
|
|
108
|
+
* - If the rule is `# This is just a comment`, then the marker will be `#`.
|
|
109
|
+
*/
|
|
110
|
+
declare enum CommentMarker {
|
|
111
|
+
/**
|
|
112
|
+
* Regular comment marker. It is supported by all ad blockers.
|
|
113
|
+
*/
|
|
114
|
+
Regular = "!",
|
|
115
|
+
/**
|
|
116
|
+
* Hashmark comment marker. It is supported by uBlock Origin and AdGuard,
|
|
117
|
+
* and also used in hosts files.
|
|
118
|
+
*/
|
|
119
|
+
Hashmark = "#"
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Represents the main categories that an adblock rule can belong to.
|
|
123
|
+
* Of course, these include additional subcategories.
|
|
124
|
+
*/
|
|
125
|
+
declare enum RuleCategory {
|
|
126
|
+
/**
|
|
127
|
+
* Empty "rules" that are only containing whitespaces. These rules are handled just for convenience.
|
|
128
|
+
*/
|
|
129
|
+
Empty = "Empty",
|
|
130
|
+
/**
|
|
131
|
+
* Syntactically invalid rules (tolerant mode only).
|
|
132
|
+
*/
|
|
133
|
+
Invalid = "Invalid",
|
|
134
|
+
/**
|
|
135
|
+
* Comment rules, such as comment rules, metadata rules, preprocessor rules, etc.
|
|
136
|
+
*/
|
|
137
|
+
Comment = "Comment",
|
|
138
|
+
/**
|
|
139
|
+
* Cosmetic rules, such as element hiding rules, CSS rules, scriptlet rules, HTML rules, and JS rules.
|
|
140
|
+
*/
|
|
141
|
+
Cosmetic = "Cosmetic",
|
|
142
|
+
/**
|
|
143
|
+
* Network rules, such as basic network rules, header remover network rules, redirect network rules,
|
|
144
|
+
* response header filtering rules, etc.
|
|
145
|
+
*/
|
|
146
|
+
Network = "Network"
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Represents possible comment types.
|
|
150
|
+
*/
|
|
151
|
+
declare enum CommentRuleType {
|
|
152
|
+
AgentCommentRule = "AgentCommentRule",
|
|
153
|
+
CommentRule = "CommentRule",
|
|
154
|
+
ConfigCommentRule = "ConfigCommentRule",
|
|
155
|
+
HintCommentRule = "HintCommentRule",
|
|
156
|
+
MetadataCommentRule = "MetadataCommentRule",
|
|
157
|
+
PreProcessorCommentRule = "PreProcessorCommentRule"
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Represents possible cosmetic rule types.
|
|
161
|
+
*/
|
|
162
|
+
declare enum CosmeticRuleType {
|
|
163
|
+
ElementHidingRule = "ElementHidingRule",
|
|
164
|
+
CssInjectionRule = "CssInjectionRule",
|
|
165
|
+
ScriptletInjectionRule = "ScriptletInjectionRule",
|
|
166
|
+
HtmlFilteringRule = "HtmlFilteringRule",
|
|
167
|
+
JsInjectionRule = "JsInjectionRule"
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Represents possible cosmetic rule separators.
|
|
171
|
+
*/
|
|
172
|
+
type CosmeticRuleSeparator = '##' | '#@#' | '#?#' | '#@?#' | '#$#' | '#@$#' | '#$?#' | '#@$?#' | '##+' | '#@#+' | '#%#' | '#@%#' | '##^' | '#@#^' | '$$' | '$@$';
|
|
173
|
+
/**
|
|
174
|
+
* Represents a basic node in the AST.
|
|
175
|
+
*/
|
|
176
|
+
interface Node {
|
|
177
|
+
/**
|
|
178
|
+
* The type of the node. Every node should have a type.
|
|
179
|
+
*/
|
|
180
|
+
type: string;
|
|
181
|
+
/**
|
|
182
|
+
* Every node should support a loc property, which refers to the location of the node in the source code.
|
|
183
|
+
*/
|
|
184
|
+
loc?: LocationRange;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Represents a location range in the source code.
|
|
188
|
+
*/
|
|
189
|
+
interface LocationRange {
|
|
190
|
+
/**
|
|
191
|
+
* The start location of the node.
|
|
192
|
+
*/
|
|
193
|
+
start: Location;
|
|
194
|
+
/**
|
|
195
|
+
* The end location of the node.
|
|
196
|
+
*/
|
|
197
|
+
end: Location;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Represents a location in the source code.
|
|
201
|
+
*/
|
|
202
|
+
interface Location {
|
|
203
|
+
/**
|
|
204
|
+
* Zero-based index of the first character of the parsed source region.
|
|
205
|
+
*/
|
|
206
|
+
offset: number;
|
|
207
|
+
/**
|
|
208
|
+
* One-based line index of the first character of the parsed source region.
|
|
209
|
+
*/
|
|
210
|
+
line: number;
|
|
211
|
+
/**
|
|
212
|
+
* One-based column index of the first character of the parsed source region.
|
|
213
|
+
*/
|
|
214
|
+
column: number;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Represents a basic value node in the AST.
|
|
218
|
+
*/
|
|
219
|
+
interface Value<T = string> extends Node {
|
|
220
|
+
type: 'Value';
|
|
221
|
+
/**
|
|
222
|
+
* Value of the node.
|
|
223
|
+
*/
|
|
224
|
+
value: T;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Represents a basic parameter node in the AST.
|
|
228
|
+
*/
|
|
229
|
+
interface Parameter extends Node {
|
|
230
|
+
type: 'Parameter';
|
|
231
|
+
/**
|
|
232
|
+
* Value of the node.
|
|
233
|
+
*/
|
|
234
|
+
value: string;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Represents a list of parameters.
|
|
238
|
+
*/
|
|
239
|
+
interface ParameterList extends Node {
|
|
240
|
+
type: 'ParameterList';
|
|
241
|
+
/**
|
|
242
|
+
* List of values
|
|
243
|
+
*/
|
|
244
|
+
children: Parameter[];
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Represents a logical expression variable node in the AST.
|
|
248
|
+
*/
|
|
249
|
+
interface ExpressionVariableNode extends Node {
|
|
250
|
+
type: 'Variable';
|
|
251
|
+
name: string;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Represents a logical expression operator node in the AST.
|
|
255
|
+
*/
|
|
256
|
+
interface ExpressionOperatorNode extends Node {
|
|
257
|
+
type: 'Operator';
|
|
258
|
+
operator: AnyOperator;
|
|
259
|
+
left: AnyExpressionNode;
|
|
260
|
+
right?: AnyExpressionNode;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Represents a logical expression parenthesis node in the AST.
|
|
264
|
+
*/
|
|
265
|
+
interface ExpressionParenthesisNode extends Node {
|
|
266
|
+
type: 'Parenthesis';
|
|
267
|
+
expression: AnyExpressionNode;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Represents a filter list (list of rules).
|
|
271
|
+
*/
|
|
272
|
+
interface FilterList extends Node {
|
|
273
|
+
type: 'FilterList';
|
|
274
|
+
/**
|
|
275
|
+
* List of rules
|
|
276
|
+
*/
|
|
277
|
+
children: AnyRule[];
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Represents a basic adblock rule. Every adblock rule should extend this interface.
|
|
281
|
+
* We don't use this interface directly, so we don't specify the `type` property.
|
|
282
|
+
*/
|
|
283
|
+
interface RuleBase extends Node {
|
|
284
|
+
/**
|
|
285
|
+
* Syntax of the adblock rule. If we are not able to determine the syntax of the rule,
|
|
286
|
+
* we should use `AdblockSyntax.Common` as the value.
|
|
287
|
+
*/
|
|
288
|
+
syntax: AdblockSyntax;
|
|
289
|
+
/**
|
|
290
|
+
* Category of the adblock rule
|
|
291
|
+
*/
|
|
292
|
+
category: RuleCategory;
|
|
293
|
+
/**
|
|
294
|
+
* Raw data of the rule
|
|
295
|
+
*/
|
|
296
|
+
raws?: {
|
|
297
|
+
/**
|
|
298
|
+
* Original rule text
|
|
299
|
+
*/
|
|
300
|
+
text?: string;
|
|
301
|
+
/**
|
|
302
|
+
* Newline character used in the rule (if any)
|
|
303
|
+
*/
|
|
304
|
+
nl?: NewLine;
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Represents an invalid rule (used by tolerant mode).
|
|
309
|
+
*/
|
|
310
|
+
interface InvalidRule extends RuleBase {
|
|
311
|
+
type: 'InvalidRule';
|
|
312
|
+
/**
|
|
313
|
+
* Category of the adblock rule
|
|
314
|
+
*/
|
|
315
|
+
category: RuleCategory.Invalid;
|
|
316
|
+
/**
|
|
317
|
+
* Raw rule text
|
|
318
|
+
*/
|
|
319
|
+
raw: string;
|
|
320
|
+
/**
|
|
321
|
+
* Error details
|
|
322
|
+
*/
|
|
323
|
+
error: {
|
|
324
|
+
/**
|
|
325
|
+
* Error name
|
|
326
|
+
*/
|
|
327
|
+
name: string;
|
|
328
|
+
/**
|
|
329
|
+
* Error message
|
|
330
|
+
*/
|
|
331
|
+
message: string;
|
|
332
|
+
/**
|
|
333
|
+
* Error location (if any)
|
|
334
|
+
*/
|
|
335
|
+
loc?: LocationRange;
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Represents an "empty rule" (practically an empty line)
|
|
340
|
+
*/
|
|
341
|
+
interface EmptyRule extends RuleBase {
|
|
342
|
+
/**
|
|
343
|
+
* Type of the adblock rule (should be always present)
|
|
344
|
+
*/
|
|
345
|
+
type: 'EmptyRule';
|
|
346
|
+
/**
|
|
347
|
+
* Category of the adblock rule
|
|
348
|
+
*/
|
|
349
|
+
category: RuleCategory.Empty;
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Represents the basic comment rule interface.
|
|
353
|
+
*/
|
|
354
|
+
interface CommentBase extends RuleBase {
|
|
355
|
+
category: RuleCategory.Comment;
|
|
356
|
+
type: CommentRuleType;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Represents a simple comment.
|
|
360
|
+
*
|
|
361
|
+
* @example
|
|
362
|
+
* Example rules:
|
|
363
|
+
* - ```adblock
|
|
364
|
+
* ! This is just a comment
|
|
365
|
+
* ```
|
|
366
|
+
* - ```adblock
|
|
367
|
+
* # This is just a comment
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
370
|
+
interface CommentRule extends CommentBase {
|
|
371
|
+
type: CommentRuleType.CommentRule;
|
|
372
|
+
/**
|
|
373
|
+
* Comment marker.
|
|
374
|
+
*
|
|
375
|
+
* @example
|
|
376
|
+
* - If the rule is `! This is just a comment`, then the marker will be `!`.
|
|
377
|
+
* - If the rule is `# This is just a comment`, then the marker will be `#`.
|
|
378
|
+
*/
|
|
379
|
+
marker: Value<CommentMarker>;
|
|
380
|
+
/**
|
|
381
|
+
* Comment text.
|
|
382
|
+
*
|
|
383
|
+
* @example
|
|
384
|
+
* If the rule is `! This is just a comment`, then the text will be `This is just a comment`.
|
|
385
|
+
*/
|
|
386
|
+
text: Value;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Represents a metadata comment rule. This is a special comment that specifies
|
|
390
|
+
* the name and value of the metadata header.
|
|
391
|
+
*
|
|
392
|
+
* @example
|
|
393
|
+
* For example, in the case of
|
|
394
|
+
* ```adblock
|
|
395
|
+
* ! Title: My List
|
|
396
|
+
* ```
|
|
397
|
+
* the name of the header is `Title`, and the value is `My List`.
|
|
398
|
+
*/
|
|
399
|
+
interface MetadataCommentRule extends CommentBase {
|
|
400
|
+
type: CommentRuleType.MetadataCommentRule;
|
|
401
|
+
/**
|
|
402
|
+
* Comment marker.
|
|
403
|
+
*/
|
|
404
|
+
marker: Value<CommentMarker>;
|
|
405
|
+
/**
|
|
406
|
+
* Metadata header name.
|
|
407
|
+
*/
|
|
408
|
+
header: Value;
|
|
409
|
+
/**
|
|
410
|
+
* Metadata header value (always should present).
|
|
411
|
+
*/
|
|
412
|
+
value: Value;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Represents an inline linter configuration comment.
|
|
416
|
+
*
|
|
417
|
+
* @example
|
|
418
|
+
* For example, if the comment is
|
|
419
|
+
* ```adblock
|
|
420
|
+
* ! aglint-disable some-rule another-rule
|
|
421
|
+
* ```
|
|
422
|
+
* then the command is `aglint-disable` and its params is `["some-rule", "another-rule"]`.
|
|
423
|
+
*/
|
|
424
|
+
interface ConfigCommentRule extends CommentBase {
|
|
425
|
+
category: RuleCategory.Comment;
|
|
426
|
+
type: CommentRuleType.ConfigCommentRule;
|
|
427
|
+
/**
|
|
428
|
+
* The marker for the comment. It can be `!` or `#`. It is always the first non-whitespace character in the comment.
|
|
429
|
+
*/
|
|
430
|
+
marker: Value<CommentMarker>;
|
|
431
|
+
/**
|
|
432
|
+
* The command for the comment. It is always begins with the `aglint` prefix.
|
|
433
|
+
*
|
|
434
|
+
* @example
|
|
435
|
+
* ```adblock
|
|
436
|
+
* ! aglint-disable-next-line
|
|
437
|
+
* ```
|
|
438
|
+
*/
|
|
439
|
+
command: Value;
|
|
440
|
+
/**
|
|
441
|
+
* Params for the command. Can be a rule configuration object or a list of rule names.
|
|
442
|
+
*
|
|
443
|
+
* @example
|
|
444
|
+
* For the following comment:
|
|
445
|
+
* ```adblock
|
|
446
|
+
* ! aglint-disable some-rule another-rule
|
|
447
|
+
* ```
|
|
448
|
+
* the params would be `["some-rule", "another-rule"]`.
|
|
449
|
+
*/
|
|
450
|
+
params?: Value<object> | ParameterList;
|
|
451
|
+
/**
|
|
452
|
+
* Config comment text. The idea is generally the same as in ESLint.
|
|
453
|
+
*
|
|
454
|
+
* @example
|
|
455
|
+
* You can use the following syntax to specify a comment for a config comment:
|
|
456
|
+
* `! aglint-enable -- this is the comment`
|
|
457
|
+
*/
|
|
458
|
+
comment?: Value;
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Represents a preprocessor comment.
|
|
462
|
+
*
|
|
463
|
+
* @example
|
|
464
|
+
* For example, if the comment is
|
|
465
|
+
* ```adblock
|
|
466
|
+
* !#if (adguard)
|
|
467
|
+
* ```
|
|
468
|
+
* then the directive's name is `if` and its value is `(adguard)`.
|
|
469
|
+
*
|
|
470
|
+
* In such a case, the parameters must be submitted for further parsing and validation, as this parser only handles
|
|
471
|
+
* the general syntax.
|
|
472
|
+
*/
|
|
473
|
+
interface PreProcessorCommentRule extends CommentBase {
|
|
474
|
+
category: RuleCategory.Comment;
|
|
475
|
+
type: CommentRuleType.PreProcessorCommentRule;
|
|
476
|
+
/**
|
|
477
|
+
* Name of the directive
|
|
478
|
+
*/
|
|
479
|
+
name: Value;
|
|
480
|
+
/**
|
|
481
|
+
* Params (optional)
|
|
482
|
+
*/
|
|
483
|
+
params?: Value | ParameterList | AnyExpressionNode;
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Represents an adblock agent.
|
|
487
|
+
*/
|
|
488
|
+
interface Agent extends Node {
|
|
489
|
+
type: 'Agent';
|
|
490
|
+
/**
|
|
491
|
+
* Adblock name.
|
|
492
|
+
*/
|
|
493
|
+
adblock: Value;
|
|
494
|
+
/**
|
|
495
|
+
* Adblock version (if specified).
|
|
496
|
+
*/
|
|
497
|
+
version: Value | null;
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Represents an agent comment rule.
|
|
501
|
+
*
|
|
502
|
+
* @example
|
|
503
|
+
* - ```adblock
|
|
504
|
+
* [Adblock Plus 2.0]
|
|
505
|
+
* ```
|
|
506
|
+
* - ```adblock
|
|
507
|
+
* [uBlock Origin 1.16.4; AdGuard 1.0]
|
|
508
|
+
* ```
|
|
509
|
+
*/
|
|
510
|
+
interface AgentCommentRule extends RuleBase {
|
|
511
|
+
category: RuleCategory.Comment;
|
|
512
|
+
type: CommentRuleType.AgentCommentRule;
|
|
513
|
+
/**
|
|
514
|
+
* Agent list.
|
|
515
|
+
*/
|
|
516
|
+
children: Agent[];
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Represents a hint.
|
|
520
|
+
*
|
|
521
|
+
* @example
|
|
522
|
+
* ```adblock
|
|
523
|
+
* !+ PLATFORM(windows, mac)
|
|
524
|
+
* ```
|
|
525
|
+
* the name would be `PLATFORM` and the params would be `["windows", "mac"]`.
|
|
526
|
+
*/
|
|
527
|
+
interface Hint extends Node {
|
|
528
|
+
type: 'Hint';
|
|
529
|
+
/**
|
|
530
|
+
* Hint name.
|
|
531
|
+
*
|
|
532
|
+
* @example
|
|
533
|
+
* For `PLATFORM(windows, mac)` the name would be `PLATFORM`.
|
|
534
|
+
*/
|
|
535
|
+
name: Value;
|
|
536
|
+
/**
|
|
537
|
+
* Hint parameters.
|
|
538
|
+
*
|
|
539
|
+
* @example
|
|
540
|
+
* For `PLATFORM(windows, mac)` the params would be `["windows", "mac"]`.
|
|
541
|
+
*/
|
|
542
|
+
params?: ParameterList;
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Represents a hint comment rule.
|
|
546
|
+
*
|
|
547
|
+
* There can be several hints in a hint rule.
|
|
548
|
+
*
|
|
549
|
+
* @example
|
|
550
|
+
* If the rule is
|
|
551
|
+
* ```adblock
|
|
552
|
+
* !+ NOT_OPTIMIZED PLATFORM(windows)
|
|
553
|
+
* ```
|
|
554
|
+
* then there are two hint members: `NOT_OPTIMIZED` and `PLATFORM`.
|
|
555
|
+
*/
|
|
556
|
+
interface HintCommentRule extends RuleBase {
|
|
557
|
+
category: RuleCategory.Comment;
|
|
558
|
+
type: CommentRuleType.HintCommentRule;
|
|
559
|
+
/**
|
|
560
|
+
* Currently only AdGuard supports hints.
|
|
561
|
+
*/
|
|
562
|
+
syntax: AdblockSyntax.Adg;
|
|
563
|
+
/**
|
|
564
|
+
* List of hints.
|
|
565
|
+
*/
|
|
566
|
+
children: Hint[];
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Represents a modifier list.
|
|
570
|
+
*
|
|
571
|
+
* @example
|
|
572
|
+
* If the rule is
|
|
573
|
+
* ```adblock
|
|
574
|
+
* some-rule$script,domain=example.com
|
|
575
|
+
* ```
|
|
576
|
+
* then the list of modifiers will be `script,domain=example.com`.
|
|
577
|
+
*/
|
|
578
|
+
interface ModifierList extends Node {
|
|
579
|
+
type: 'ModifierList';
|
|
580
|
+
/**
|
|
581
|
+
* List of modifiers.
|
|
582
|
+
*/
|
|
583
|
+
children: Modifier[];
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Represents a modifier.
|
|
587
|
+
*
|
|
588
|
+
* @example
|
|
589
|
+
* If the modifier is `third-party`, the value of the modifier property
|
|
590
|
+
* will be `third-party`, but the value will remain undefined.
|
|
591
|
+
*
|
|
592
|
+
* But if the modifier is `domain=example.com`, then the modifier property will be
|
|
593
|
+
* `domain` and the value property will be `example.com`.
|
|
594
|
+
*/
|
|
595
|
+
interface Modifier extends Node {
|
|
596
|
+
/**
|
|
597
|
+
* Modifier name
|
|
598
|
+
*/
|
|
599
|
+
modifier: Value;
|
|
600
|
+
/**
|
|
601
|
+
* Is this modifier an exception? For example, `~third-party` is an exception
|
|
602
|
+
*/
|
|
603
|
+
exception?: boolean;
|
|
604
|
+
/**
|
|
605
|
+
* Modifier value (optional)
|
|
606
|
+
*/
|
|
607
|
+
value?: Value;
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Represents the separator used in a domain list.
|
|
611
|
+
*
|
|
612
|
+
* @example
|
|
613
|
+
* "," for the classic domain list, and "|" for the "domain" modifier parameter
|
|
614
|
+
*/
|
|
615
|
+
type DomainListSeparator = typeof CLASSIC_DOMAIN_SEPARATOR | typeof MODIFIER_DOMAIN_SEPARATOR;
|
|
616
|
+
/**
|
|
617
|
+
* Represents a list of domains
|
|
618
|
+
*
|
|
619
|
+
* @example
|
|
620
|
+
* `example.com,~example.net`.
|
|
621
|
+
*/
|
|
622
|
+
interface DomainList extends Node {
|
|
623
|
+
/**
|
|
624
|
+
* Type of the node. Basically, the idea is that each main AST part should have a type
|
|
625
|
+
*/
|
|
626
|
+
type: 'DomainList';
|
|
627
|
+
/**
|
|
628
|
+
* Separator used in the domain list.
|
|
629
|
+
*/
|
|
630
|
+
separator: DomainListSeparator;
|
|
631
|
+
/**
|
|
632
|
+
* List of domains
|
|
633
|
+
*/
|
|
634
|
+
children: Domain[];
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Represents an element of the domain list (a domain).
|
|
638
|
+
*/
|
|
639
|
+
interface Domain extends Node {
|
|
640
|
+
type: 'Domain';
|
|
641
|
+
/**
|
|
642
|
+
* Domain name
|
|
643
|
+
*/
|
|
644
|
+
value: string;
|
|
645
|
+
/**
|
|
646
|
+
* If the domain is an exception.
|
|
647
|
+
*
|
|
648
|
+
* @example
|
|
649
|
+
* `~example.com` is an exception, but `example.com` is not. `~` is the exception marker here.
|
|
650
|
+
*/
|
|
651
|
+
exception: boolean;
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Represents a CSS injection body.
|
|
655
|
+
*/
|
|
656
|
+
interface CssInjectionRuleBody extends Node {
|
|
657
|
+
type: 'CssInjectionRuleBody';
|
|
658
|
+
/**
|
|
659
|
+
* Media query list (if any)
|
|
660
|
+
*/
|
|
661
|
+
mediaQueryList?: MediaQueryListPlain;
|
|
662
|
+
/**
|
|
663
|
+
* Selector list
|
|
664
|
+
*/
|
|
665
|
+
selectorList: SelectorListPlain;
|
|
666
|
+
/**
|
|
667
|
+
* Declaration block / remove flag
|
|
668
|
+
*/
|
|
669
|
+
declarationList?: DeclarationListPlain;
|
|
670
|
+
/**
|
|
671
|
+
* Remove flag
|
|
672
|
+
*/
|
|
673
|
+
remove?: boolean;
|
|
674
|
+
}
|
|
675
|
+
/**
|
|
676
|
+
* Represents an element hiding rule body. There can even be several selectors in a rule,
|
|
677
|
+
* but the best practice is to place the selectors in separate rules.
|
|
678
|
+
*/
|
|
679
|
+
interface ElementHidingRuleBody extends Node {
|
|
680
|
+
type: 'ElementHidingRuleBody';
|
|
681
|
+
/**
|
|
682
|
+
* Element hiding rule selector(s).
|
|
683
|
+
*/
|
|
684
|
+
selectorList: SelectorListPlain;
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Represents a scriptlet injection rule body.
|
|
688
|
+
*/
|
|
689
|
+
interface ScriptletInjectionRuleBody extends Node {
|
|
690
|
+
type: 'ScriptletInjectionRuleBody';
|
|
691
|
+
/**
|
|
692
|
+
* List of scriptlets (list of parameter lists).
|
|
693
|
+
*/
|
|
694
|
+
children: ParameterList[];
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* Represents an HTML filtering rule body.
|
|
698
|
+
*/
|
|
699
|
+
interface HtmlFilteringRuleBody extends Node {
|
|
700
|
+
type: 'HtmlFilteringRuleBody';
|
|
701
|
+
/**
|
|
702
|
+
* HTML rule selector(s).
|
|
703
|
+
*/
|
|
704
|
+
body: SelectorListPlain | FunctionNodePlain;
|
|
705
|
+
}
|
|
706
|
+
/**
|
|
707
|
+
* A generic representation of a cosmetic rule.
|
|
708
|
+
*
|
|
709
|
+
* Regarding the categories, there is only a difference in the body,
|
|
710
|
+
* all other properties can be defined at this level.
|
|
711
|
+
*/
|
|
712
|
+
interface CosmeticRule extends RuleBase {
|
|
713
|
+
category: RuleCategory.Cosmetic;
|
|
714
|
+
type: CosmeticRuleType;
|
|
715
|
+
/**
|
|
716
|
+
* List of modifiers (optional)
|
|
717
|
+
*/
|
|
718
|
+
modifiers?: ModifierList;
|
|
719
|
+
/**
|
|
720
|
+
* List of domains.
|
|
721
|
+
*/
|
|
722
|
+
domains: DomainList;
|
|
723
|
+
/**
|
|
724
|
+
* Separator between pattern and body. For example, in the following rule:
|
|
725
|
+
* ```adblock
|
|
726
|
+
* example.com##.ads
|
|
727
|
+
* ```
|
|
728
|
+
* then the separator is `##`.
|
|
729
|
+
*/
|
|
730
|
+
separator: Value;
|
|
731
|
+
/**
|
|
732
|
+
* If the rule is an exception. For example, in the following rule:
|
|
733
|
+
* ```adblock
|
|
734
|
+
* example.com#@#.ads
|
|
735
|
+
* ```
|
|
736
|
+
* then the rule is an exception and @ is the exception marker.
|
|
737
|
+
*/
|
|
738
|
+
exception: boolean;
|
|
739
|
+
/**
|
|
740
|
+
* Body of the rule. It can be a CSS rule, an element hiding rule, a scriptlet rule, etc.
|
|
741
|
+
*/
|
|
742
|
+
body: unknown;
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* Representation of an element hiding rule.
|
|
746
|
+
*
|
|
747
|
+
* Example rules:
|
|
748
|
+
* - ```adblock
|
|
749
|
+
* example.com##.ads
|
|
750
|
+
* ```
|
|
751
|
+
* - ```adblock
|
|
752
|
+
* example.com#@#.ads
|
|
753
|
+
* ```
|
|
754
|
+
* - ```adblock
|
|
755
|
+
* example.com#?#.ads:has(> .something)
|
|
756
|
+
* ```
|
|
757
|
+
* - ```adblock
|
|
758
|
+
* example.com#@?#.ads:has(> .something)
|
|
759
|
+
* ```
|
|
760
|
+
*/
|
|
761
|
+
interface ElementHidingRule extends CosmeticRule {
|
|
762
|
+
type: CosmeticRuleType.ElementHidingRule;
|
|
763
|
+
body: ElementHidingRuleBody;
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* Representation of a CSS injection rule.
|
|
767
|
+
*
|
|
768
|
+
* Example rules (AdGuard):
|
|
769
|
+
* - ```adblock
|
|
770
|
+
* example.com#$#body { padding-top: 0 !important; }
|
|
771
|
+
* ```
|
|
772
|
+
* - ```adblock
|
|
773
|
+
* example.com#$#@media (min-width: 1024px) { body { padding-top: 0 !important; } }
|
|
774
|
+
* ```
|
|
775
|
+
* - ```adblock
|
|
776
|
+
* example.com#$?#@media (min-width: 1024px) { .something:has(.ads) { padding-top: 0 !important; } }
|
|
777
|
+
* ```
|
|
778
|
+
* - ```adblock
|
|
779
|
+
* example.com#$#.ads { remove: true; }
|
|
780
|
+
* ```
|
|
781
|
+
*
|
|
782
|
+
* Example rules (uBlock Origin):
|
|
783
|
+
* - ```adblock
|
|
784
|
+
* example.com##body:style(padding-top: 0 !important;)
|
|
785
|
+
* ```
|
|
786
|
+
* - ```adblock
|
|
787
|
+
* example.com##.ads:remove()
|
|
788
|
+
* ```
|
|
789
|
+
*/
|
|
790
|
+
interface CssInjectionRule extends CosmeticRule {
|
|
791
|
+
type: CosmeticRuleType.CssInjectionRule;
|
|
792
|
+
body: CssInjectionRuleBody;
|
|
793
|
+
}
|
|
794
|
+
/**
|
|
795
|
+
* Representation of a scriptlet injection rule.
|
|
796
|
+
*
|
|
797
|
+
* Example rules (AdGuard):
|
|
798
|
+
* - ```adblock
|
|
799
|
+
* example.com#%#//scriptlet('scriptlet-name', 'arg0', 'arg1')
|
|
800
|
+
* ```
|
|
801
|
+
* - ```adblock
|
|
802
|
+
* example.com#@%#//scriptlet('scriptlet-name', 'arg0', 'arg1')
|
|
803
|
+
* ```
|
|
804
|
+
*
|
|
805
|
+
* Example rules (uBlock Origin):
|
|
806
|
+
* - ```adblock
|
|
807
|
+
* example.com##+js(scriptlet-name, arg0, arg1)
|
|
808
|
+
* ```
|
|
809
|
+
* - ```adblock
|
|
810
|
+
* example.com#@#+js(scriptlet-name, arg0, arg1)
|
|
811
|
+
* ```
|
|
812
|
+
*
|
|
813
|
+
* Example rules (Adblock Plus):
|
|
814
|
+
* - ```adblock
|
|
815
|
+
* example.com#$#scriptlet-name arg0 arg1
|
|
816
|
+
* ```
|
|
817
|
+
* - ```adblock
|
|
818
|
+
* example.com#@$#scriptlet-name arg0 arg1
|
|
819
|
+
* ```
|
|
820
|
+
* - ```adblock
|
|
821
|
+
* example.com#$#scriptlet0 arg00 arg01; scriptlet1 arg10 arg11
|
|
822
|
+
* ```
|
|
823
|
+
*/
|
|
824
|
+
interface ScriptletInjectionRule extends CosmeticRule {
|
|
825
|
+
type: CosmeticRuleType.ScriptletInjectionRule;
|
|
826
|
+
body: ScriptletInjectionRuleBody;
|
|
827
|
+
}
|
|
828
|
+
/**
|
|
829
|
+
* Representation of a HTML filtering rule.
|
|
830
|
+
*
|
|
831
|
+
* Example rules (AdGuard):
|
|
832
|
+
* - ```adblock
|
|
833
|
+
* example.com$$script[tag-content="detect"]
|
|
834
|
+
* ```
|
|
835
|
+
* - ```adblock
|
|
836
|
+
* example.com$@$script[tag-content="detect"]
|
|
837
|
+
* ```
|
|
838
|
+
*
|
|
839
|
+
* Example rules (uBlock Origin):
|
|
840
|
+
* - ```adblock
|
|
841
|
+
* example.com##^script:has-text(detect)
|
|
842
|
+
* ```
|
|
843
|
+
* - ```adblock
|
|
844
|
+
* example.com#@#^script:has-text(detect)
|
|
845
|
+
* ```
|
|
846
|
+
*/
|
|
847
|
+
interface HtmlFilteringRule extends CosmeticRule {
|
|
848
|
+
type: CosmeticRuleType.HtmlFilteringRule;
|
|
849
|
+
body: HtmlFilteringRuleBody;
|
|
850
|
+
}
|
|
851
|
+
/**
|
|
852
|
+
* Representation of a JS injection rule.
|
|
853
|
+
*
|
|
854
|
+
* Example rules (AdGuard):
|
|
855
|
+
* - ```adblock
|
|
856
|
+
* example.com#%#let a = 2;
|
|
857
|
+
* ```
|
|
858
|
+
* - ```adblock
|
|
859
|
+
* example.com#@%#let a = 2;
|
|
860
|
+
* ```
|
|
861
|
+
*/
|
|
862
|
+
interface JsInjectionRule extends CosmeticRule {
|
|
863
|
+
type: CosmeticRuleType.JsInjectionRule;
|
|
864
|
+
body: Value;
|
|
865
|
+
}
|
|
866
|
+
/**
|
|
867
|
+
* Represents the common properties of network rules
|
|
868
|
+
*/
|
|
869
|
+
interface NetworkRule extends RuleBase {
|
|
870
|
+
category: RuleCategory.Network;
|
|
871
|
+
type: 'NetworkRule';
|
|
872
|
+
syntax: AdblockSyntax;
|
|
873
|
+
/**
|
|
874
|
+
* If the rule is an exception rule. If the rule begins with `@@`, it means that it is an exception rule.
|
|
875
|
+
*
|
|
876
|
+
* @example
|
|
877
|
+
* The following rule is an exception rule:
|
|
878
|
+
* ```adblock
|
|
879
|
+
* @@||example.org^
|
|
880
|
+
* ```
|
|
881
|
+
* since it begins with `@@`, which is the exception marker.
|
|
882
|
+
*
|
|
883
|
+
* But the following rule is not an exception rule:
|
|
884
|
+
* ```adblock
|
|
885
|
+
* ||example.org^
|
|
886
|
+
* ```
|
|
887
|
+
* since it does not begins with `@@`.
|
|
888
|
+
*/
|
|
889
|
+
exception: boolean;
|
|
890
|
+
/**
|
|
891
|
+
* The rule pattern.
|
|
892
|
+
*
|
|
893
|
+
* @example
|
|
894
|
+
* - Let's say we have the following rule:
|
|
895
|
+
* ```adblock
|
|
896
|
+
* ||example.org^
|
|
897
|
+
* ```
|
|
898
|
+
* then the pattern of this rule is `||example.org^`.
|
|
899
|
+
* - But let's say we have the following rule:
|
|
900
|
+
* ```adblock
|
|
901
|
+
* ||example.org^$third-party,script
|
|
902
|
+
* ```
|
|
903
|
+
* then the pattern of this rule is also `||example.org^`.
|
|
904
|
+
*/
|
|
905
|
+
pattern: Value;
|
|
906
|
+
/**
|
|
907
|
+
* The rule modifiers.
|
|
908
|
+
*
|
|
909
|
+
* @example
|
|
910
|
+
* - Let's say we have the following rule:
|
|
911
|
+
* ```adblock
|
|
912
|
+
* ||example.org^$third-party
|
|
913
|
+
* ```
|
|
914
|
+
* then the modifiers of this rule are `["third-party"]`.
|
|
915
|
+
*/
|
|
916
|
+
modifiers?: ModifierList;
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
/**
|
|
920
|
+
* `RuleParser` is responsible for parsing the rules.
|
|
921
|
+
*
|
|
922
|
+
* It automatically determines the category and syntax of the rule, so you can pass any kind of rule to it.
|
|
923
|
+
*/
|
|
924
|
+
declare class RuleParser {
|
|
925
|
+
/**
|
|
926
|
+
* Parse an adblock rule. You can pass any kind of rule to this method, since it will automatically determine
|
|
927
|
+
* the category and syntax. If the rule is syntactically invalid, then an error will be thrown. If the
|
|
928
|
+
* syntax / compatibility cannot be determined clearly, then the value of the `syntax` property will be
|
|
929
|
+
* `Common`.
|
|
930
|
+
*
|
|
931
|
+
* For example, let's have this network rule:
|
|
932
|
+
* ```adblock
|
|
933
|
+
* ||example.org^$important
|
|
934
|
+
* ```
|
|
935
|
+
* The `syntax` property will be `Common`, since the rule is syntactically correct in every adblockers, but we
|
|
936
|
+
* cannot determine at parsing level whether `important` is an existing option or not, nor if it exists, then
|
|
937
|
+
* which adblocker supports it. This is why the `syntax` property is simply `Common` at this point.
|
|
938
|
+
* The concrete COMPATIBILITY of the rule will be determined later, in a different, higher-level layer, called
|
|
939
|
+
* "Compatibility table".
|
|
940
|
+
*
|
|
941
|
+
* But we can determinate the concrete syntax of this rule:
|
|
942
|
+
* ```adblock
|
|
943
|
+
* example.org#%#//scriptlet("scriptlet0", "arg0")
|
|
944
|
+
* ```
|
|
945
|
+
* since it is clearly an AdGuard-specific rule and no other adblockers uses this syntax natively. However, we also
|
|
946
|
+
* cannot determine the COMPATIBILITY of this rule, as it is not clear at this point whether the `scriptlet0`
|
|
947
|
+
* scriptlet is supported by AdGuard or not. This is also the task of the "Compatibility table". Here, we simply
|
|
948
|
+
* mark the rule with the `AdGuard` syntax in this case.
|
|
949
|
+
*
|
|
950
|
+
* @param raw Raw adblock rule
|
|
951
|
+
* @param tolerant If `true`, then the parser will not throw if the rule is syntactically invalid, instead it will
|
|
952
|
+
* return an `InvalidRule` object with the error attached to it. Default is `false`.
|
|
953
|
+
* @param loc Base location of the rule
|
|
954
|
+
* @returns Adblock rule AST
|
|
955
|
+
* @throws If the input matches a pattern but syntactically invalid
|
|
956
|
+
* @example
|
|
957
|
+
* Take a look at the following example:
|
|
958
|
+
* ```js
|
|
959
|
+
* // Parse a network rule
|
|
960
|
+
* const ast1 = RuleParser.parse("||example.org^$important");
|
|
961
|
+
*
|
|
962
|
+
* // Parse another network rule
|
|
963
|
+
* const ast2 = RuleParser.parse("/ads.js^$important,third-party,domain=example.org|~example.com");
|
|
964
|
+
*
|
|
965
|
+
* // Parse a cosmetic rule
|
|
966
|
+
* const ast2 = RuleParser.parse("example.org##.banner");
|
|
967
|
+
*
|
|
968
|
+
* // Parse another cosmetic rule
|
|
969
|
+
* const ast3 = RuleParser.parse("example.org#?#.banner:-abp-has(.ad)");
|
|
970
|
+
*
|
|
971
|
+
* // Parse a comment rule
|
|
972
|
+
* const ast4 = RuleParser.parse("! Comment");
|
|
973
|
+
*
|
|
974
|
+
* // Parse an empty rule
|
|
975
|
+
* const ast5 = RuleParser.parse("");
|
|
976
|
+
*
|
|
977
|
+
* // Parse a comment rule (with metadata)
|
|
978
|
+
* const ast6 = RuleParser.parse("! Title: Example");
|
|
979
|
+
*
|
|
980
|
+
* // Parse a pre-processor rule
|
|
981
|
+
* const ast7 = RuleParser.parse("!#if (adguard)");
|
|
982
|
+
* ```
|
|
983
|
+
*/
|
|
984
|
+
static parse(raw: string, tolerant?: boolean, loc?: Location): AnyRule;
|
|
985
|
+
/**
|
|
986
|
+
* Converts a rule AST to a string.
|
|
987
|
+
*
|
|
988
|
+
* @param ast - Adblock rule AST
|
|
989
|
+
* @returns Raw string
|
|
990
|
+
* @example
|
|
991
|
+
* Take a look at the following example:
|
|
992
|
+
* ```js
|
|
993
|
+
* // Parse the rule to the AST
|
|
994
|
+
* const ast = RuleParser.parse("example.org##.banner");
|
|
995
|
+
* // Generate the rule from the AST
|
|
996
|
+
* const raw = RuleParser.generate(ast);
|
|
997
|
+
* // Print the generated rule
|
|
998
|
+
* console.log(raw); // "example.org##.banner"
|
|
999
|
+
* ```
|
|
1000
|
+
*/
|
|
1001
|
+
static generate(ast: AnyRule): string;
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
/**
|
|
1005
|
+
* `AgentParser` is responsible for parsing an Adblock agent rules.
|
|
1006
|
+
* Adblock agent comment marks that the filter list is supposed to
|
|
1007
|
+
* be used by the specified ad blockers.
|
|
1008
|
+
*
|
|
1009
|
+
* @example
|
|
1010
|
+
* - ```adblock
|
|
1011
|
+
* [AdGuard]
|
|
1012
|
+
* ```
|
|
1013
|
+
* - ```adblock
|
|
1014
|
+
* [Adblock Plus 2.0]
|
|
1015
|
+
* ```
|
|
1016
|
+
* - ```adblock
|
|
1017
|
+
* [uBlock Origin]
|
|
1018
|
+
* ```
|
|
1019
|
+
* - ```adblock
|
|
1020
|
+
* [uBlock Origin 1.45.3]
|
|
1021
|
+
* ```
|
|
1022
|
+
* - ```adblock
|
|
1023
|
+
* [Adblock Plus 2.0; AdGuard]
|
|
1024
|
+
* ```
|
|
1025
|
+
*/
|
|
1026
|
+
declare class AgentCommentRuleParser {
|
|
1027
|
+
/**
|
|
1028
|
+
* Checks if the raw rule is an adblock agent comment.
|
|
1029
|
+
*
|
|
1030
|
+
* @param raw Raw rule
|
|
1031
|
+
* @returns `true` if the rule is an adblock agent, `false` otherwise
|
|
1032
|
+
*/
|
|
1033
|
+
static isAgentRule(raw: string): boolean;
|
|
1034
|
+
/**
|
|
1035
|
+
* Parses a raw rule as an adblock agent comment.
|
|
1036
|
+
*
|
|
1037
|
+
* @param raw Raw rule
|
|
1038
|
+
* @param loc Base location
|
|
1039
|
+
* @returns Agent rule AST or null (if the raw rule cannot be parsed as an adblock agent comment)
|
|
1040
|
+
*/
|
|
1041
|
+
static parse(raw: string, loc?: Location): AgentCommentRule | null;
|
|
1042
|
+
/**
|
|
1043
|
+
* Converts an adblock agent AST to a string.
|
|
1044
|
+
*
|
|
1045
|
+
* @param ast Agent rule AST
|
|
1046
|
+
* @returns Raw string
|
|
1047
|
+
*/
|
|
1048
|
+
static generate(ast: AgentCommentRule): string;
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
/**
|
|
1052
|
+
* `AgentParser` is responsible for parsing single adblock agent elements.
|
|
1053
|
+
*
|
|
1054
|
+
* @example
|
|
1055
|
+
* If the adblock agent rule is
|
|
1056
|
+
* ```adblock
|
|
1057
|
+
* [Adblock Plus 2.0; AdGuard]
|
|
1058
|
+
* ```
|
|
1059
|
+
* then the adblock agents are `Adblock Plus 2.0` and `AdGuard`, and this
|
|
1060
|
+
* class is responsible for parsing them. The rule itself is parsed by
|
|
1061
|
+
* `AgentCommentRuleParser`, which uses this class to parse single agents.
|
|
1062
|
+
*/
|
|
1063
|
+
declare class AgentParser {
|
|
1064
|
+
/**
|
|
1065
|
+
* Checks if the string is a valid version.
|
|
1066
|
+
*
|
|
1067
|
+
* @param str String to check
|
|
1068
|
+
* @returns `true` if the string is a valid version, `false` otherwise
|
|
1069
|
+
*/
|
|
1070
|
+
private static isValidVersion;
|
|
1071
|
+
/**
|
|
1072
|
+
* Parses a raw rule as an adblock agent comment.
|
|
1073
|
+
*
|
|
1074
|
+
* @param raw Raw rule
|
|
1075
|
+
* @param loc Base location
|
|
1076
|
+
* @returns Agent rule AST
|
|
1077
|
+
* @throws {AdblockSyntaxError} If the raw rule cannot be parsed as an adblock agent
|
|
1078
|
+
*/
|
|
1079
|
+
static parse(raw: string, loc?: Location): Agent;
|
|
1080
|
+
/**
|
|
1081
|
+
* Converts an adblock agent AST to a string.
|
|
1082
|
+
*
|
|
1083
|
+
* @param ast Agent AST
|
|
1084
|
+
* @returns Raw string
|
|
1085
|
+
*/
|
|
1086
|
+
static generate(ast: Agent): string;
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
/**
|
|
1090
|
+
* @file AdGuard Hints
|
|
1091
|
+
* @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#hints}
|
|
1092
|
+
*/
|
|
1093
|
+
|
|
1094
|
+
/**
|
|
1095
|
+
* `HintParser` is responsible for parsing AdGuard hints.
|
|
1096
|
+
*
|
|
1097
|
+
* @example
|
|
1098
|
+
* If the hint rule is
|
|
1099
|
+
* ```adblock
|
|
1100
|
+
* !+ NOT_OPTIMIZED PLATFORM(windows)
|
|
1101
|
+
* ```
|
|
1102
|
+
* then the hints are `NOT_OPTIMIZED` and `PLATFORM(windows)`, and this
|
|
1103
|
+
* class is responsible for parsing them. The rule itself is parsed by
|
|
1104
|
+
* the `HintRuleParser`, which uses this class to parse single hints.
|
|
1105
|
+
*/
|
|
1106
|
+
declare class HintParser {
|
|
1107
|
+
/**
|
|
1108
|
+
* Parses a raw rule as a hint.
|
|
1109
|
+
*
|
|
1110
|
+
* @param raw Raw rule
|
|
1111
|
+
* @param loc Base location
|
|
1112
|
+
* @returns Hint rule AST or null
|
|
1113
|
+
* @throws If the syntax is invalid
|
|
1114
|
+
*/
|
|
1115
|
+
static parse(raw: string, loc?: Location): Hint;
|
|
1116
|
+
/**
|
|
1117
|
+
* Converts a single hint AST to a string.
|
|
1118
|
+
*
|
|
1119
|
+
* @param hint Hint AST
|
|
1120
|
+
* @returns Hint string
|
|
1121
|
+
*/
|
|
1122
|
+
static generate(hint: Hint): string;
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
/**
|
|
1126
|
+
* `HintRuleParser` is responsible for parsing AdGuard hint rules.
|
|
1127
|
+
*
|
|
1128
|
+
* @example
|
|
1129
|
+
* The following hint rule
|
|
1130
|
+
* ```adblock
|
|
1131
|
+
* !+ NOT_OPTIMIZED PLATFORM(windows)
|
|
1132
|
+
* ```
|
|
1133
|
+
* contains two hints: `NOT_OPTIMIZED` and `PLATFORM`.
|
|
1134
|
+
* @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#hints}
|
|
1135
|
+
*/
|
|
1136
|
+
declare class HintCommentRuleParser {
|
|
1137
|
+
/**
|
|
1138
|
+
* Checks if the raw rule is a hint rule.
|
|
1139
|
+
*
|
|
1140
|
+
* @param raw Raw rule
|
|
1141
|
+
* @returns `true` if the rule is a hint rule, `false` otherwise
|
|
1142
|
+
*/
|
|
1143
|
+
static isHintRule(raw: string): boolean;
|
|
1144
|
+
/**
|
|
1145
|
+
* Parses a raw rule as a hint comment.
|
|
1146
|
+
*
|
|
1147
|
+
* @param raw Raw rule
|
|
1148
|
+
* @param loc Base location
|
|
1149
|
+
* @returns Hint AST or null (if the raw rule cannot be parsed as a hint comment)
|
|
1150
|
+
* @throws If the input matches the HINT pattern but syntactically invalid
|
|
1151
|
+
* @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#hints-1}
|
|
1152
|
+
*/
|
|
1153
|
+
static parse(raw: string, loc?: Location): HintCommentRule | null;
|
|
1154
|
+
/**
|
|
1155
|
+
* Converts a hint rule AST to a raw string.
|
|
1156
|
+
*
|
|
1157
|
+
* @param ast Hint rule AST
|
|
1158
|
+
* @returns Raw string
|
|
1159
|
+
*/
|
|
1160
|
+
static generate(ast: HintCommentRule): string;
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
/**
|
|
1164
|
+
* @file Metadata comments
|
|
1165
|
+
*/
|
|
1166
|
+
|
|
1167
|
+
/**
|
|
1168
|
+
* `MetadataParser` is responsible for parsing metadata comments.
|
|
1169
|
+
* Metadata comments are special comments that specify some properties of the list.
|
|
1170
|
+
*
|
|
1171
|
+
* @example
|
|
1172
|
+
* For example, in the case of
|
|
1173
|
+
* ```adblock
|
|
1174
|
+
* ! Title: My List
|
|
1175
|
+
* ```
|
|
1176
|
+
* the name of the header is `Title`, and the value is `My List`, which means that
|
|
1177
|
+
* the list title is `My List`, and it can be used in the adblocker UI.
|
|
1178
|
+
* @see {@link https://help.eyeo.com/adblockplus/how-to-write-filters#special-comments}
|
|
1179
|
+
*/
|
|
1180
|
+
declare class MetadataCommentRuleParser {
|
|
1181
|
+
/**
|
|
1182
|
+
* Parses a raw rule as a metadata comment.
|
|
1183
|
+
*
|
|
1184
|
+
* @param raw Raw rule
|
|
1185
|
+
* @param loc Base location
|
|
1186
|
+
* @returns Metadata comment AST or null (if the raw rule cannot be parsed as a metadata comment)
|
|
1187
|
+
*/
|
|
1188
|
+
static parse(raw: string, loc?: Location): MetadataCommentRule | null;
|
|
1189
|
+
/**
|
|
1190
|
+
* Converts a metadata comment AST to a string.
|
|
1191
|
+
*
|
|
1192
|
+
* @param ast - Metadata comment AST
|
|
1193
|
+
* @returns Raw string
|
|
1194
|
+
*/
|
|
1195
|
+
static generate(ast: MetadataCommentRule): string;
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
/**
|
|
1199
|
+
* Pre-processor directives
|
|
1200
|
+
*
|
|
1201
|
+
* @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#pre-processor-directives}
|
|
1202
|
+
* @see {@link https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#pre-parsing-directives}
|
|
1203
|
+
*/
|
|
1204
|
+
|
|
1205
|
+
/**
|
|
1206
|
+
* `PreProcessorParser` is responsible for parsing preprocessor rules.
|
|
1207
|
+
* Pre-processor comments are special comments that are used to control the behavior of the filter list processor.
|
|
1208
|
+
* Please note that this parser only handles general syntax for now, and does not validate the parameters at
|
|
1209
|
+
* the parsing stage.
|
|
1210
|
+
*
|
|
1211
|
+
* @example
|
|
1212
|
+
* If your rule is
|
|
1213
|
+
* ```adblock
|
|
1214
|
+
* !#if (adguard)
|
|
1215
|
+
* ```
|
|
1216
|
+
* then the directive's name is `if` and its value is `(adguard)`, but the parameter list
|
|
1217
|
+
* is not parsed / validated further.
|
|
1218
|
+
* @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#pre-processor-directives}
|
|
1219
|
+
* @see {@link https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#pre-parsing-directives}
|
|
1220
|
+
*/
|
|
1221
|
+
declare class PreProcessorCommentRuleParser {
|
|
1222
|
+
/**
|
|
1223
|
+
* Determines whether the rule is a pre-processor rule.
|
|
1224
|
+
*
|
|
1225
|
+
* @param raw Raw rule
|
|
1226
|
+
* @returns `true` if the rule is a pre-processor rule, `false` otherwise
|
|
1227
|
+
*/
|
|
1228
|
+
static isPreProcessorRule(raw: string): boolean;
|
|
1229
|
+
/**
|
|
1230
|
+
* Parses a raw rule as a pre-processor comment.
|
|
1231
|
+
*
|
|
1232
|
+
* @param raw Raw rule
|
|
1233
|
+
* @param loc Base location
|
|
1234
|
+
* @returns
|
|
1235
|
+
* Pre-processor comment AST or null (if the raw rule cannot be parsed as a pre-processor comment)
|
|
1236
|
+
*/
|
|
1237
|
+
static parse(raw: string, loc?: Location): PreProcessorCommentRule | null;
|
|
1238
|
+
/**
|
|
1239
|
+
* Converts a pre-processor comment AST to a string.
|
|
1240
|
+
*
|
|
1241
|
+
* @param ast - Pre-processor comment AST
|
|
1242
|
+
* @returns Raw string
|
|
1243
|
+
*/
|
|
1244
|
+
static generate(ast: PreProcessorCommentRule): string;
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
/**
|
|
1248
|
+
* @file AGLint configuration comments. Inspired by ESLint inline configuration comments.
|
|
1249
|
+
* @see {@link https://eslint.org/docs/latest/user-guide/configuring/rules#using-configuration-comments}
|
|
1250
|
+
*/
|
|
1251
|
+
|
|
1252
|
+
/**
|
|
1253
|
+
* `ConfigCommentParser` is responsible for parsing inline AGLint configuration rules.
|
|
1254
|
+
* Generally, the idea is inspired by ESLint inline configuration comments.
|
|
1255
|
+
*
|
|
1256
|
+
* @see {@link https://eslint.org/docs/latest/user-guide/configuring/rules#using-configuration-comments}
|
|
1257
|
+
*/
|
|
1258
|
+
declare class ConfigCommentRuleParser {
|
|
1259
|
+
/**
|
|
1260
|
+
* Checks if the raw rule is an inline configuration comment rule.
|
|
1261
|
+
*
|
|
1262
|
+
* @param raw Raw rule
|
|
1263
|
+
* @returns `true` if the rule is an inline configuration comment rule, otherwise `false`.
|
|
1264
|
+
*/
|
|
1265
|
+
static isConfigComment(raw: string): boolean;
|
|
1266
|
+
/**
|
|
1267
|
+
* Parses a raw rule as an inline configuration comment.
|
|
1268
|
+
*
|
|
1269
|
+
* @param raw Raw rule
|
|
1270
|
+
* @param loc Base location
|
|
1271
|
+
* @returns
|
|
1272
|
+
* Inline configuration comment AST or null (if the raw rule cannot be parsed as configuration comment)
|
|
1273
|
+
*/
|
|
1274
|
+
static parse(raw: string, loc?: Location): ConfigCommentRule | null;
|
|
1275
|
+
/**
|
|
1276
|
+
* Converts an inline configuration comment AST to a string.
|
|
1277
|
+
*
|
|
1278
|
+
* @param ast Inline configuration comment AST
|
|
1279
|
+
* @returns Raw string
|
|
1280
|
+
*/
|
|
1281
|
+
static generate(ast: ConfigCommentRule): string;
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
/**
|
|
1285
|
+
* `CommentParser` is responsible for parsing any comment-like adblock rules.
|
|
1286
|
+
*
|
|
1287
|
+
* @example
|
|
1288
|
+
* Example rules:
|
|
1289
|
+
* - Adblock agent rules:
|
|
1290
|
+
* - ```adblock
|
|
1291
|
+
* [AdGuard]
|
|
1292
|
+
* ```
|
|
1293
|
+
* - ```adblock
|
|
1294
|
+
* [Adblock Plus 2.0]
|
|
1295
|
+
* ```
|
|
1296
|
+
* - etc.
|
|
1297
|
+
* - AdGuard hint rules:
|
|
1298
|
+
* - ```adblock
|
|
1299
|
+
* !+ NOT_OPTIMIZED
|
|
1300
|
+
* ```
|
|
1301
|
+
* - ```adblock
|
|
1302
|
+
* !+ NOT_OPTIMIZED PLATFORM(windows)
|
|
1303
|
+
* ```
|
|
1304
|
+
* - etc.
|
|
1305
|
+
* - Pre-processor rules:
|
|
1306
|
+
* - ```adblock
|
|
1307
|
+
* !#if (adguard)
|
|
1308
|
+
* ```
|
|
1309
|
+
* - ```adblock
|
|
1310
|
+
* !#endif
|
|
1311
|
+
* ```
|
|
1312
|
+
* - etc.
|
|
1313
|
+
* - Metadata rules:
|
|
1314
|
+
* - ```adblock
|
|
1315
|
+
* ! Title: My List
|
|
1316
|
+
* ```
|
|
1317
|
+
* - ```adblock
|
|
1318
|
+
* ! Version: 2.0.150
|
|
1319
|
+
* ```
|
|
1320
|
+
* - etc.
|
|
1321
|
+
* - AGLint inline config rules:
|
|
1322
|
+
* - ```adblock
|
|
1323
|
+
* ! aglint-enable some-rule
|
|
1324
|
+
* ```
|
|
1325
|
+
* - ```adblock
|
|
1326
|
+
* ! aglint-disable some-rule
|
|
1327
|
+
* ```
|
|
1328
|
+
* - etc.
|
|
1329
|
+
* - Simple comments:
|
|
1330
|
+
* - Regular version:
|
|
1331
|
+
* ```adblock
|
|
1332
|
+
* ! This is just a comment
|
|
1333
|
+
* ```
|
|
1334
|
+
* - uBlock Origin / "hostlist" version:
|
|
1335
|
+
* ```adblock
|
|
1336
|
+
* # This is just a comment
|
|
1337
|
+
* ```
|
|
1338
|
+
* - etc.
|
|
1339
|
+
*/
|
|
1340
|
+
declare class CommentRuleParser {
|
|
1341
|
+
/**
|
|
1342
|
+
* Checks whether a rule is a regular comment. Regular comments are the ones that start with
|
|
1343
|
+
* an exclamation mark (`!`).
|
|
1344
|
+
*
|
|
1345
|
+
* @param raw Raw rule
|
|
1346
|
+
* @returns `true` if the rule is a regular comment, `false` otherwise
|
|
1347
|
+
*/
|
|
1348
|
+
static isRegularComment(raw: string): boolean;
|
|
1349
|
+
/**
|
|
1350
|
+
* Checks whether a rule is a comment.
|
|
1351
|
+
*
|
|
1352
|
+
* @param raw Raw rule
|
|
1353
|
+
* @returns `true` if the rule is a comment, `false` otherwise
|
|
1354
|
+
*/
|
|
1355
|
+
static isCommentRule(raw: string): boolean;
|
|
1356
|
+
/**
|
|
1357
|
+
* Parses a raw rule as comment.
|
|
1358
|
+
*
|
|
1359
|
+
* @param raw Raw rule
|
|
1360
|
+
* @param loc Base location
|
|
1361
|
+
* @returns Comment AST or null (if the raw rule cannot be parsed as comment)
|
|
1362
|
+
*/
|
|
1363
|
+
static parse(raw: string, loc?: Location): AnyCommentRule | null;
|
|
1364
|
+
/**
|
|
1365
|
+
* Converts a comment AST to a string.
|
|
1366
|
+
*
|
|
1367
|
+
* @param ast Comment AST
|
|
1368
|
+
* @returns Raw string
|
|
1369
|
+
*/
|
|
1370
|
+
static generate(ast: AnyCommentRule): string;
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1373
|
+
/**
|
|
1374
|
+
* `NetworkRuleParser` is responsible for parsing network rules.
|
|
1375
|
+
*
|
|
1376
|
+
* Please note that this will parse all syntactically correct network rules.
|
|
1377
|
+
* Modifier compatibility is not checked at the parser level.
|
|
1378
|
+
*
|
|
1379
|
+
* @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#basic-rules}
|
|
1380
|
+
* @see {@link https://help.eyeo.com/adblockplus/how-to-write-filters#basic}
|
|
1381
|
+
*/
|
|
1382
|
+
declare class NetworkRuleParser {
|
|
1383
|
+
/**
|
|
1384
|
+
* Parses a network rule (also known as basic rule).
|
|
1385
|
+
*
|
|
1386
|
+
* @param raw Raw rule
|
|
1387
|
+
* @param loc Location of the rule
|
|
1388
|
+
* @returns Network rule AST
|
|
1389
|
+
*/
|
|
1390
|
+
static parse(raw: string, loc?: Location): NetworkRule;
|
|
1391
|
+
/**
|
|
1392
|
+
* Finds the index of the separator character in a network rule.
|
|
1393
|
+
*
|
|
1394
|
+
* @param rule Network rule to check
|
|
1395
|
+
* @returns The index of the separator character, or -1 if there is no separator
|
|
1396
|
+
*/
|
|
1397
|
+
private static findNetworkRuleSeparatorIndex;
|
|
1398
|
+
/**
|
|
1399
|
+
* Converts a network rule (basic rule) AST to a string.
|
|
1400
|
+
*
|
|
1401
|
+
* @param ast - Network rule AST
|
|
1402
|
+
* @returns Raw string
|
|
1403
|
+
*/
|
|
1404
|
+
static generate(ast: NetworkRule): string;
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1407
|
+
/**
|
|
1408
|
+
* `CosmeticRuleParser` is responsible for parsing cosmetic rules.
|
|
1409
|
+
*
|
|
1410
|
+
* Where possible, it automatically detects the difference between supported syntaxes:
|
|
1411
|
+
* - AdGuard
|
|
1412
|
+
* - uBlock Origin
|
|
1413
|
+
* - Adblock Plus
|
|
1414
|
+
*
|
|
1415
|
+
* If the syntax is common / cannot be determined, the parser gives `Common` syntax.
|
|
1416
|
+
*
|
|
1417
|
+
* Please note that syntactically correct rules are parsed even if they are not actually
|
|
1418
|
+
* compatible with the given adblocker. This is a completely natural behavior, meaningful
|
|
1419
|
+
* checking of compatibility is not done at the parser level.
|
|
1420
|
+
*/
|
|
1421
|
+
declare class CosmeticRuleParser {
|
|
1422
|
+
/**
|
|
1423
|
+
* Determines whether a rule is a cosmetic rule. The rule is considered cosmetic if it
|
|
1424
|
+
* contains a cosmetic rule separator.
|
|
1425
|
+
*
|
|
1426
|
+
* @param raw Raw rule
|
|
1427
|
+
* @returns `true` if the rule is a cosmetic rule, `false` otherwise
|
|
1428
|
+
*/
|
|
1429
|
+
static isCosmeticRule(raw: string): boolean;
|
|
1430
|
+
/**
|
|
1431
|
+
* Parses a cosmetic rule. The structure of the cosmetic rules:
|
|
1432
|
+
* - pattern (AdGuard pattern can have modifiers, other syntaxes don't)
|
|
1433
|
+
* - separator
|
|
1434
|
+
* - body
|
|
1435
|
+
*
|
|
1436
|
+
* @param raw Raw cosmetic rule
|
|
1437
|
+
* @param loc Location of the rule
|
|
1438
|
+
* @returns
|
|
1439
|
+
* Parsed cosmetic rule AST or null if it failed to parse based on the known cosmetic rules
|
|
1440
|
+
* @throws If the input matches the cosmetic rule pattern but syntactically invalid
|
|
1441
|
+
*/
|
|
1442
|
+
static parse(raw: string, loc?: Location): AnyCosmeticRule | null;
|
|
1443
|
+
/**
|
|
1444
|
+
* Converts a cosmetic rule AST into a string.
|
|
1445
|
+
*
|
|
1446
|
+
* @param ast Cosmetic rule AST
|
|
1447
|
+
* @returns Raw string
|
|
1448
|
+
*/
|
|
1449
|
+
static generate(ast: AnyCosmeticRule): string;
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
/**
|
|
1453
|
+
* `LogicalExpressionParser` is responsible for parsing logical expressions.
|
|
1454
|
+
*
|
|
1455
|
+
* @example
|
|
1456
|
+
* From the following rule:
|
|
1457
|
+
* ```adblock
|
|
1458
|
+
* !#if (adguard_ext_android_cb || adguard_ext_safari)
|
|
1459
|
+
* ```
|
|
1460
|
+
* this parser will parse the expression `(adguard_ext_android_cb || adguard_ext_safari)`.
|
|
1461
|
+
*/
|
|
1462
|
+
declare class LogicalExpressionParser {
|
|
1463
|
+
/**
|
|
1464
|
+
* Split the expression into tokens.
|
|
1465
|
+
*
|
|
1466
|
+
* @param raw Source code of the expression
|
|
1467
|
+
* @param loc Location of the expression
|
|
1468
|
+
* @returns Token list
|
|
1469
|
+
* @throws {AdblockSyntaxError} If the expression is invalid
|
|
1470
|
+
*/
|
|
1471
|
+
private static tokenize;
|
|
1472
|
+
/**
|
|
1473
|
+
* Parses a logical expression.
|
|
1474
|
+
*
|
|
1475
|
+
* @param raw Source code of the expression
|
|
1476
|
+
* @param loc Location of the expression
|
|
1477
|
+
* @returns Parsed expression
|
|
1478
|
+
* @throws {AdblockSyntaxError} If the expression is invalid
|
|
1479
|
+
*/
|
|
1480
|
+
static parse(raw: string, loc?: Location): AnyExpressionNode;
|
|
1481
|
+
/**
|
|
1482
|
+
* Generates a string representation of the logical expression (serialization).
|
|
1483
|
+
*
|
|
1484
|
+
* @param ast Expression node
|
|
1485
|
+
* @returns String representation of the logical expression
|
|
1486
|
+
*/
|
|
1487
|
+
static generate(ast: AnyExpressionNode): string;
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
/**
|
|
1491
|
+
* `DomainListParser` is responsible for parsing a domain list.
|
|
1492
|
+
*
|
|
1493
|
+
* @example
|
|
1494
|
+
* - If the rule is `example.com,~example.net##.ads`, the domain list is `example.com,~example.net`.
|
|
1495
|
+
* - If the rule is `ads.js^$script,domains=example.com|~example.org`, the domain list is `example.com|~example.org`.
|
|
1496
|
+
* This parser is responsible for parsing these domain lists.
|
|
1497
|
+
* @see {@link https://help.eyeo.com/adblockplus/how-to-write-filters#elemhide_domains}
|
|
1498
|
+
*/
|
|
1499
|
+
declare class DomainListParser {
|
|
1500
|
+
/**
|
|
1501
|
+
* Parses a domain list, eg. `example.com,example.org,~example.org`
|
|
1502
|
+
*
|
|
1503
|
+
* @param raw Raw domain list
|
|
1504
|
+
* @param separator Separator character
|
|
1505
|
+
* @param loc Location of the domain list
|
|
1506
|
+
* @returns Domain list AST
|
|
1507
|
+
* @throws If the domain list is syntactically invalid
|
|
1508
|
+
*/
|
|
1509
|
+
static parse(raw: string, separator?: DomainListSeparator, loc?: Location): DomainList;
|
|
1510
|
+
/**
|
|
1511
|
+
* Converts a domain list AST to a string.
|
|
1512
|
+
*
|
|
1513
|
+
* @param ast Domain list AST
|
|
1514
|
+
* @returns Raw string
|
|
1515
|
+
*/
|
|
1516
|
+
static generate(ast: DomainList): string;
|
|
1517
|
+
}
|
|
1518
|
+
|
|
1519
|
+
/**
|
|
1520
|
+
* `ModifierListParser` is responsible for parsing modifier lists. Please note that the name is not
|
|
1521
|
+
* uniform, "modifiers" are also known as "options".
|
|
1522
|
+
*
|
|
1523
|
+
* @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#basic-rules-modifiers}
|
|
1524
|
+
* @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#non-basic-rules-modifiers}
|
|
1525
|
+
* @see {@link https://help.eyeo.com/adblockplus/how-to-write-filters#options}
|
|
1526
|
+
*/
|
|
1527
|
+
declare class ModifierListParser {
|
|
1528
|
+
/**
|
|
1529
|
+
* Parses the cosmetic rule modifiers, eg. `third-party,domain=example.com|~example.org`.
|
|
1530
|
+
*
|
|
1531
|
+
* _Note:_ you should remove `$` separator before passing the raw modifiers to this function,
|
|
1532
|
+
* or it will be parsed in the first modifier.
|
|
1533
|
+
*
|
|
1534
|
+
* @param raw Raw modifier list
|
|
1535
|
+
* @param loc Location of the modifier list
|
|
1536
|
+
* @returns Parsed modifiers interface
|
|
1537
|
+
*/
|
|
1538
|
+
static parse(raw: string, loc?: Location): ModifierList;
|
|
1539
|
+
/**
|
|
1540
|
+
* Converts a modifier list AST to a string.
|
|
1541
|
+
*
|
|
1542
|
+
* @param ast Modifier list AST
|
|
1543
|
+
* @returns Raw string
|
|
1544
|
+
*/
|
|
1545
|
+
static generate(ast: ModifierList): string;
|
|
1546
|
+
}
|
|
1547
|
+
|
|
1548
|
+
/**
|
|
1549
|
+
* `ModifierParser` is responsible for parsing modifiers.
|
|
1550
|
+
*
|
|
1551
|
+
* @example
|
|
1552
|
+
* `match-case`, `~third-party`, `domain=example.com|~example.org`
|
|
1553
|
+
*/
|
|
1554
|
+
declare class ModifierParser {
|
|
1555
|
+
/**
|
|
1556
|
+
* Parses a modifier.
|
|
1557
|
+
*
|
|
1558
|
+
* @param raw Raw modifier string
|
|
1559
|
+
* @param loc Location of the modifier
|
|
1560
|
+
* @returns Parsed modifier
|
|
1561
|
+
*/
|
|
1562
|
+
static parse(raw: string, loc?: Location): Modifier;
|
|
1563
|
+
/**
|
|
1564
|
+
* Generates a string from a modifier (serializes it).
|
|
1565
|
+
*
|
|
1566
|
+
* @param modifier Modifier to generate string from
|
|
1567
|
+
* @returns String representation of the modifier
|
|
1568
|
+
*/
|
|
1569
|
+
static generate(modifier: Modifier): string;
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
declare class ParameterListParser {
|
|
1573
|
+
/**
|
|
1574
|
+
* Parses a raw parameter list.
|
|
1575
|
+
*
|
|
1576
|
+
* @param raw Raw parameter list
|
|
1577
|
+
* @param separator Separator character (default: comma)
|
|
1578
|
+
* @param loc Base location
|
|
1579
|
+
* @returns Parameter list AST
|
|
1580
|
+
*/
|
|
1581
|
+
static parse(raw: string, separator?: string, loc?: Location): ParameterList;
|
|
1582
|
+
/**
|
|
1583
|
+
* Converts a parameter list AST to a string.
|
|
1584
|
+
*
|
|
1585
|
+
* @param params Parameter list AST
|
|
1586
|
+
* @param separator Separator character (default: comma)
|
|
1587
|
+
* @returns String representation of the parameter list
|
|
1588
|
+
*/
|
|
1589
|
+
static generate(params: ParameterList, separator?: string): string;
|
|
1590
|
+
}
|
|
1591
|
+
|
|
1592
|
+
/**
|
|
1593
|
+
* @file Customized syntax error class for Adblock Filter Parser.
|
|
1594
|
+
*/
|
|
1595
|
+
|
|
1596
|
+
/**
|
|
1597
|
+
* Customized syntax error class for Adblock Filter Parser,
|
|
1598
|
+
* which contains the location range of the error.
|
|
1599
|
+
*/
|
|
1600
|
+
declare class AdblockSyntaxError extends SyntaxError {
|
|
1601
|
+
/**
|
|
1602
|
+
* Location range of the error.
|
|
1603
|
+
*/
|
|
1604
|
+
loc: LocationRange;
|
|
1605
|
+
/**
|
|
1606
|
+
* Constructs a new `AdblockSyntaxError` instance.
|
|
1607
|
+
*
|
|
1608
|
+
* @param message Error message
|
|
1609
|
+
* @param loc Location range of the error
|
|
1610
|
+
*/
|
|
1611
|
+
constructor(message: string, loc: LocationRange);
|
|
1612
|
+
}
|
|
1613
|
+
|
|
1614
|
+
/**
|
|
1615
|
+
* `FilterListParser` is responsible for parsing a whole adblock filter list (list of rules).
|
|
1616
|
+
* It is a wrapper around `RuleParser` which parses each line separately.
|
|
1617
|
+
*/
|
|
1618
|
+
declare class FilterListParser {
|
|
1619
|
+
/**
|
|
1620
|
+
* Parses a whole adblock filter list (list of rules).
|
|
1621
|
+
*
|
|
1622
|
+
* @param raw Filter list source code (including new lines)
|
|
1623
|
+
* @param tolerant If `true`, then the parser will not throw if the rule is syntactically invalid,
|
|
1624
|
+
* instead it will return an `InvalidRule` object with the error attached to it. Default is `true`.
|
|
1625
|
+
* It is useful for parsing filter lists with invalid rules, because most of the rules are valid,
|
|
1626
|
+
* and some invalid rules can't break the whole filter list parsing.
|
|
1627
|
+
* @returns AST of the source code (list of rules)
|
|
1628
|
+
* @example
|
|
1629
|
+
* ```js
|
|
1630
|
+
* import { readFileSync } from 'fs';
|
|
1631
|
+
* import { FilterListParser } from '@adguard/agtree';
|
|
1632
|
+
*
|
|
1633
|
+
* // Read filter list content from file
|
|
1634
|
+
* const content = readFileSync('your-adblock-filter-list.txt', 'utf-8');
|
|
1635
|
+
*
|
|
1636
|
+
* // Parse the filter list content, then do something with the AST
|
|
1637
|
+
* const ast = FilterListParser.parse(content);
|
|
1638
|
+
* ```
|
|
1639
|
+
* @throws If one of the rules is syntactically invalid (if `tolerant` is `false`)
|
|
1640
|
+
*/
|
|
1641
|
+
static parse(raw: string, tolerant?: boolean): FilterList;
|
|
1642
|
+
/**
|
|
1643
|
+
* Serializes a whole adblock filter list (list of rules).
|
|
1644
|
+
*
|
|
1645
|
+
* @param ast AST to generate
|
|
1646
|
+
* @param preferRaw If `true`, then the parser will use `raws.text` property of each rule
|
|
1647
|
+
* if it is available. Default is `false`.
|
|
1648
|
+
* @returns Serialized filter list
|
|
1649
|
+
*/
|
|
1650
|
+
static generate(ast: FilterList, preferRaw?: boolean): string;
|
|
1651
|
+
}
|
|
1652
|
+
|
|
1653
|
+
/**
|
|
1654
|
+
* @file Utility functions for domain and hostname validation.
|
|
1655
|
+
*/
|
|
1656
|
+
declare class DomainUtils {
|
|
1657
|
+
/**
|
|
1658
|
+
* Check if the input is a valid domain or hostname.
|
|
1659
|
+
*
|
|
1660
|
+
* @param domain Domain to check
|
|
1661
|
+
* @returns `true` if the domain is valid, `false` otherwise
|
|
1662
|
+
*/
|
|
1663
|
+
static isValidDomainOrHostname(domain: string): boolean;
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1666
|
+
/**
|
|
1667
|
+
* @file Utility functions for logical expression AST.
|
|
1668
|
+
*/
|
|
1669
|
+
|
|
1670
|
+
/**
|
|
1671
|
+
* Variable table. Key is variable name, value is boolean.
|
|
1672
|
+
*/
|
|
1673
|
+
type VariableTable = {
|
|
1674
|
+
[key: string]: boolean;
|
|
1675
|
+
};
|
|
1676
|
+
/**
|
|
1677
|
+
* Utility functions for logical expression AST.
|
|
1678
|
+
*/
|
|
1679
|
+
declare class LogicalExpressionUtils {
|
|
1680
|
+
/**
|
|
1681
|
+
* Get all variables in the expression.
|
|
1682
|
+
*
|
|
1683
|
+
* @param ast Logical expression AST
|
|
1684
|
+
* @returns List of variables in the expression (nodes)
|
|
1685
|
+
* @example
|
|
1686
|
+
* If the expression is `a && b || c`, the returned list will be
|
|
1687
|
+
* nodes for `a`, `b`, and `c`.
|
|
1688
|
+
*/
|
|
1689
|
+
static getVariables(ast: AnyExpressionNode): ExpressionVariableNode[];
|
|
1690
|
+
/**
|
|
1691
|
+
* Evaluate the parsed logical expression. You'll need to provide a
|
|
1692
|
+
* variable table.
|
|
1693
|
+
*
|
|
1694
|
+
* @param ast Logical expression AST
|
|
1695
|
+
* @param table Variable table (key: variable name, value: boolean)
|
|
1696
|
+
* @returns Evaluation result
|
|
1697
|
+
* @example
|
|
1698
|
+
* If the expression is `a && b`, and the variable table is
|
|
1699
|
+
* `{ a: true, b: false }`, the result will be `false`.
|
|
1700
|
+
*
|
|
1701
|
+
* Example code:
|
|
1702
|
+
* ```js
|
|
1703
|
+
* LogicalExpressionUtils.evaluate(
|
|
1704
|
+
* LogicalExpressionParser.parse('a && b'),
|
|
1705
|
+
* { a: true, b: false }
|
|
1706
|
+
* );
|
|
1707
|
+
* ```
|
|
1708
|
+
*/
|
|
1709
|
+
static evaluate(ast: AnyExpressionNode, table: VariableTable): boolean;
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
/**
|
|
1713
|
+
* @file AGTree version
|
|
1714
|
+
*/
|
|
1715
|
+
declare const version: string;
|
|
1716
|
+
|
|
1717
|
+
export { AdblockSyntaxError, Agent, AgentCommentRule, AgentCommentRuleParser, AgentParser, AnyCommentRule, AnyCosmeticRule, AnyExpressionNode, AnyOperator, AnyRule, CommentBase, CommentMarker, CommentRule, CommentRuleParser, CommentRuleType, ConfigCommentRule, ConfigCommentRuleParser, CosmeticRule, CosmeticRuleParser, CosmeticRuleSeparator, CosmeticRuleType, CssInjectionRule, CssInjectionRuleBody, Domain, DomainList, DomainListParser, DomainListSeparator, DomainUtils, ElementHidingRule, ElementHidingRuleBody, EmptyRule, ExpressionOperatorNode, ExpressionParenthesisNode, ExpressionVariableNode, FilterList, FilterListParser, Hint, HintCommentRule, HintCommentRuleParser, HintParser, HtmlFilteringRule, HtmlFilteringRuleBody, JsInjectionRule, Location, LocationRange, LogicalExpressionParser, LogicalExpressionUtils, MetadataCommentRule, MetadataCommentRuleParser, Modifier, ModifierList, ModifierListParser, ModifierParser, NetworkRule, NetworkRuleParser, Node, Parameter, ParameterList, ParameterListParser, PreProcessorCommentRule, PreProcessorCommentRuleParser, RuleBase, RuleCategory, RuleParser, ScriptletInjectionRule, ScriptletInjectionRuleBody, Value, VariableTable, version };
|