@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/dist/agtree.d.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  /*
2
- * AGTree v1.0.1 (build date: Wed, 24 May 2023 17:09:26 GMT)
2
+ * AGTree v1.1.1 (build date: Fri, 11 Aug 2023 13:11:01 GMT)
3
3
  * (c) 2023 AdGuard Software Ltd.
4
4
  * Released under the MIT license
5
- * https://github.com/AdguardTeam/AGTree#readme
5
+ * https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree#readme
6
6
  */
7
- import { MediaQueryListPlain, SelectorListPlain, DeclarationListPlain, FunctionNodePlain } from '@adguard/ecss-tree';
7
+ import { MediaQueryListPlain, SelectorListPlain, DeclarationListPlain, FunctionNodePlain, CssNode, CssNodePlain, SelectorList, DeclarationList, MediaQueryList, MediaQuery, Selector, AttributeSelector, PseudoClassSelectorPlain, PseudoClassSelector, FunctionNode } from '@adguard/ecss-tree';
8
+ import * as ecssTree from '@adguard/ecss-tree';
9
+ export { ecssTree as ECSSTree };
8
10
 
9
11
  /**
10
12
  * @file Possible adblock syntaxes are listed here.
@@ -12,7 +14,7 @@ import { MediaQueryListPlain, SelectorListPlain, DeclarationListPlain, FunctionN
12
14
  /**
13
15
  * Possible adblock syntaxes (supported by this library)
14
16
  */
15
- declare enum AdblockSyntax {
17
+ declare const enum AdblockSyntax {
16
18
  /**
17
19
  * Common syntax, which is supported by more than one adblocker (or by all adblockers).
18
20
  *
@@ -55,6 +57,12 @@ declare enum AdblockSyntax {
55
57
  Adg = "AdGuard"
56
58
  }
57
59
 
60
+ declare const ADG_SCRIPTLET_MASK = "//scriptlet";
61
+ declare const UBO_SCRIPTLET_MASK = "js";
62
+ declare const MODIFIERS_SEPARATOR = ",";
63
+ declare const MODIFIER_EXCEPTION_MARKER = "~";
64
+ declare const MODIFIER_ASSIGN_OPERATOR = "=";
65
+ declare const DOMAIN_EXCEPTION_MARKER = "~";
58
66
  /**
59
67
  * Classic domain separator.
60
68
  *
@@ -75,6 +83,14 @@ declare const CLASSIC_DOMAIN_SEPARATOR = ",";
75
83
  * ```
76
84
  */
77
85
  declare const MODIFIER_DOMAIN_SEPARATOR = "|";
86
+ declare const HINT_MARKER = "!+";
87
+ declare const NETWORK_RULE_EXCEPTION_MARKER = "@@";
88
+ declare const NETWORK_RULE_SEPARATOR = "$";
89
+ declare const AGLINT_COMMAND_PREFIX = "aglint";
90
+ declare const PREPROCESSOR_MARKER = "!#";
91
+ declare const SAFARI_CB_AFFINITY = "safari_cb_affinity";
92
+ declare const IF = "if";
93
+ declare const INCLUDE = "include";
78
94
 
79
95
  /**
80
96
  * Represents possible logical expression operators.
@@ -107,7 +123,7 @@ type AnyCosmeticRule = CssInjectionRule | ElementHidingRule | ScriptletInjection
107
123
  * - If the rule is `! This is just a comment`, then the marker will be `!`.
108
124
  * - If the rule is `# This is just a comment`, then the marker will be `#`.
109
125
  */
110
- declare enum CommentMarker {
126
+ declare const enum CommentMarker {
111
127
  /**
112
128
  * Regular comment marker. It is supported by all ad blockers.
113
129
  */
@@ -122,7 +138,7 @@ declare enum CommentMarker {
122
138
  * Represents the main categories that an adblock rule can belong to.
123
139
  * Of course, these include additional subcategories.
124
140
  */
125
- declare enum RuleCategory {
141
+ declare const enum RuleCategory {
126
142
  /**
127
143
  * Empty "rules" that are only containing whitespaces. These rules are handled just for convenience.
128
144
  */
@@ -148,7 +164,7 @@ declare enum RuleCategory {
148
164
  /**
149
165
  * Represents possible comment types.
150
166
  */
151
- declare enum CommentRuleType {
167
+ declare const enum CommentRuleType {
152
168
  AgentCommentRule = "AgentCommentRule",
153
169
  CommentRule = "CommentRule",
154
170
  ConfigCommentRule = "ConfigCommentRule",
@@ -159,7 +175,7 @@ declare enum CommentRuleType {
159
175
  /**
160
176
  * Represents possible cosmetic rule types.
161
177
  */
162
- declare enum CosmeticRuleType {
178
+ declare const enum CosmeticRuleType {
163
179
  ElementHidingRule = "ElementHidingRule",
164
180
  CssInjectionRule = "CssInjectionRule",
165
181
  ScriptletInjectionRule = "ScriptletInjectionRule",
@@ -169,7 +185,80 @@ declare enum CosmeticRuleType {
169
185
  /**
170
186
  * Represents possible cosmetic rule separators.
171
187
  */
172
- type CosmeticRuleSeparator = '##' | '#@#' | '#?#' | '#@?#' | '#$#' | '#@$#' | '#$?#' | '#@$?#' | '##+' | '#@#+' | '#%#' | '#@%#' | '##^' | '#@#^' | '$$' | '$@$';
188
+ declare const enum CosmeticRuleSeparator {
189
+ /**
190
+ * @see {@link https://help.eyeo.com/adblockplus/how-to-write-filters#elemhide_basic}
191
+ */
192
+ ElementHiding = "##",
193
+ /**
194
+ * @see {@link https://help.eyeo.com/adblockplus/how-to-write-filters#elemhide_basic}
195
+ */
196
+ ElementHidingException = "#@#",
197
+ /**
198
+ * @see {@link https://help.eyeo.com/adblockplus/how-to-write-filters#elemhide_basic}
199
+ */
200
+ ExtendedElementHiding = "#?#",
201
+ /**
202
+ * @see {@link https://help.eyeo.com/adblockplus/how-to-write-filters#elemhide_basic}
203
+ */
204
+ ExtendedElementHidingException = "#@?#",
205
+ /**
206
+ * @see {@link https://help.eyeo.com/adblockplus/how-to-write-filters#elemhide_basic}
207
+ */
208
+ AbpSnippet = "#$#",
209
+ /**
210
+ * @see {@link https://help.eyeo.com/adblockplus/how-to-write-filters#elemhide_basic}
211
+ */
212
+ AbpSnippetException = "#@$#",
213
+ /**
214
+ * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#cosmetic-css-rules}
215
+ */
216
+ AdgCssInjection = "#$#",
217
+ /**
218
+ * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#cosmetic-css-rules}
219
+ */
220
+ AdgCssInjectionException = "#@$#",
221
+ /**
222
+ * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#cosmetic-css-rules}
223
+ */
224
+ AdgExtendedCssInjection = "#$?#",
225
+ /**
226
+ * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#cosmetic-css-rules}
227
+ */
228
+ AdgExtendedCssInjectionException = "#@$?#",
229
+ /**
230
+ * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#scriptlets}
231
+ */
232
+ AdgJsInjection = "#%#",
233
+ /**
234
+ * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#scriptlets}
235
+ */
236
+ AdgJsInjectionException = "#@%#",
237
+ /**
238
+ * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#html-filtering-rules}
239
+ */
240
+ AdgHtmlFiltering = "$$",
241
+ /**
242
+ * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#html-filtering-rules}
243
+ */
244
+ AdgHtmlFilteringException = "$@$",
245
+ /**
246
+ * @see {@link https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#scriptlet-injection}
247
+ */
248
+ UboScriptletInjection = "##+",
249
+ /**
250
+ * @see {@link https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#scriptlet-injection}
251
+ */
252
+ UboScriptletInjectionException = "#@#+",
253
+ /**
254
+ * @see {@link https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#html-filters}
255
+ */
256
+ UboHtmlFiltering = "##^",
257
+ /**
258
+ * @see {@link https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#html-filters}
259
+ */
260
+ UboHtmlFilteringException = "#@#^"
261
+ }
173
262
  /**
174
263
  * Represents a basic node in the AST.
175
264
  */
@@ -182,6 +271,10 @@ interface Node {
182
271
  * Every node should support a loc property, which refers to the location of the node in the source code.
183
272
  */
184
273
  loc?: LocationRange;
274
+ /**
275
+ * Optionally the raw representation of the node in the source code.
276
+ */
277
+ raw?: string;
185
278
  }
186
279
  /**
187
280
  * Represents a location range in the source code.
@@ -495,6 +588,10 @@ interface Agent extends Node {
495
588
  * Adblock version (if specified).
496
589
  */
497
590
  version: Value | null;
591
+ /**
592
+ * Needed for network rules modifier validation.
593
+ */
594
+ syntax: AdblockSyntax;
498
595
  }
499
596
  /**
500
597
  * Represents an agent comment rule.
@@ -1001,6 +1098,28 @@ declare class RuleParser {
1001
1098
  static generate(ast: AnyRule): string;
1002
1099
  }
1003
1100
 
1101
+ /**
1102
+ * @file Customized syntax error class for Adblock Filter Parser.
1103
+ */
1104
+
1105
+ /**
1106
+ * Customized syntax error class for Adblock Filter Parser,
1107
+ * which contains the location range of the error.
1108
+ */
1109
+ declare class AdblockSyntaxError extends SyntaxError {
1110
+ /**
1111
+ * Location range of the error.
1112
+ */
1113
+ loc: LocationRange;
1114
+ /**
1115
+ * Constructs a new `AdblockSyntaxError` instance.
1116
+ *
1117
+ * @param message Error message
1118
+ * @param loc Location range of the error
1119
+ */
1120
+ constructor(message: string, loc: LocationRange);
1121
+ }
1122
+
1004
1123
  /**
1005
1124
  * `AgentParser` is responsible for parsing an Adblock agent rules.
1006
1125
  * Adblock agent comment marks that the filter list is supposed to
@@ -1087,145 +1206,568 @@ declare class AgentParser {
1087
1206
  }
1088
1207
 
1089
1208
  /**
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.
1209
+ * `CommentParser` is responsible for parsing any comment-like adblock rules.
1096
1210
  *
1097
1211
  * @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.
1212
+ * Example rules:
1213
+ * - Adblock agent rules:
1214
+ * - ```adblock
1215
+ * [AdGuard]
1216
+ * ```
1217
+ * - ```adblock
1218
+ * [Adblock Plus 2.0]
1219
+ * ```
1220
+ * - etc.
1221
+ * - AdGuard hint rules:
1222
+ * - ```adblock
1223
+ * !+ NOT_OPTIMIZED
1224
+ * ```
1225
+ * - ```adblock
1226
+ * !+ NOT_OPTIMIZED PLATFORM(windows)
1227
+ * ```
1228
+ * - etc.
1229
+ * - Pre-processor rules:
1230
+ * - ```adblock
1231
+ * !#if (adguard)
1232
+ * ```
1233
+ * - ```adblock
1234
+ * !#endif
1235
+ * ```
1236
+ * - etc.
1237
+ * - Metadata rules:
1238
+ * - ```adblock
1239
+ * ! Title: My List
1240
+ * ```
1241
+ * - ```adblock
1242
+ * ! Version: 2.0.150
1243
+ * ```
1244
+ * - etc.
1245
+ * - AGLint inline config rules:
1246
+ * - ```adblock
1247
+ * ! aglint-enable some-rule
1248
+ * ```
1249
+ * - ```adblock
1250
+ * ! aglint-disable some-rule
1251
+ * ```
1252
+ * - etc.
1253
+ * - Simple comments:
1254
+ * - Regular version:
1255
+ * ```adblock
1256
+ * ! This is just a comment
1257
+ * ```
1258
+ * - uBlock Origin / "hostlist" version:
1259
+ * ```adblock
1260
+ * # This is just a comment
1261
+ * ```
1262
+ * - etc.
1105
1263
  */
1106
- declare class HintParser {
1264
+ declare class CommentRuleParser {
1107
1265
  /**
1108
- * Parses a raw rule as a hint.
1266
+ * Checks whether a rule is a regular comment. Regular comments are the ones that start with
1267
+ * an exclamation mark (`!`).
1109
1268
  *
1110
1269
  * @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
1270
+ * @returns `true` if the rule is a regular comment, `false` otherwise
1121
1271
  */
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 {
1272
+ static isRegularComment(raw: string): boolean;
1137
1273
  /**
1138
- * Checks if the raw rule is a hint rule.
1274
+ * Checks whether a rule is a comment.
1139
1275
  *
1140
1276
  * @param raw Raw rule
1141
- * @returns `true` if the rule is a hint rule, `false` otherwise
1277
+ * @returns `true` if the rule is a comment, `false` otherwise
1142
1278
  */
1143
- static isHintRule(raw: string): boolean;
1279
+ static isCommentRule(raw: string): boolean;
1144
1280
  /**
1145
- * Parses a raw rule as a hint comment.
1281
+ * Parses a raw rule as comment.
1146
1282
  *
1147
1283
  * @param raw Raw rule
1148
1284
  * @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}
1285
+ * @returns Comment AST or null (if the raw rule cannot be parsed as comment)
1152
1286
  */
1153
- static parse(raw: string, loc?: Location): HintCommentRule | null;
1287
+ static parse(raw: string, loc?: Location): AnyCommentRule | null;
1154
1288
  /**
1155
- * Converts a hint rule AST to a raw string.
1289
+ * Converts a comment AST to a string.
1156
1290
  *
1157
- * @param ast Hint rule AST
1291
+ * @param ast Comment AST
1158
1292
  * @returns Raw string
1159
1293
  */
1160
- static generate(ast: HintCommentRule): string;
1294
+ static generate(ast: AnyCommentRule): string;
1161
1295
  }
1162
1296
 
1163
1297
  /**
1164
- * @file Metadata comments
1298
+ * @file AGLint configuration comments. Inspired by ESLint inline configuration comments.
1299
+ * @see {@link https://eslint.org/docs/latest/user-guide/configuring/rules#using-configuration-comments}
1165
1300
  */
1166
1301
 
1167
1302
  /**
1168
- * `MetadataParser` is responsible for parsing metadata comments.
1169
- * Metadata comments are special comments that specify some properties of the list.
1303
+ * `ConfigCommentParser` is responsible for parsing inline AGLint configuration rules.
1304
+ * Generally, the idea is inspired by ESLint inline configuration comments.
1170
1305
  *
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}
1306
+ * @see {@link https://eslint.org/docs/latest/user-guide/configuring/rules#using-configuration-comments}
1179
1307
  */
1180
- declare class MetadataCommentRuleParser {
1308
+ declare class ConfigCommentRuleParser {
1181
1309
  /**
1182
- * Parses a raw rule as a metadata comment.
1310
+ * Checks if the raw rule is an inline configuration comment rule.
1311
+ *
1312
+ * @param raw Raw rule
1313
+ * @returns `true` if the rule is an inline configuration comment rule, otherwise `false`.
1314
+ */
1315
+ static isConfigComment(raw: string): boolean;
1316
+ /**
1317
+ * Parses a raw rule as an inline configuration comment.
1183
1318
  *
1184
1319
  * @param raw Raw rule
1185
1320
  * @param loc Base location
1186
- * @returns Metadata comment AST or null (if the raw rule cannot be parsed as a metadata comment)
1321
+ * @returns
1322
+ * Inline configuration comment AST or null (if the raw rule cannot be parsed as configuration comment)
1187
1323
  */
1188
- static parse(raw: string, loc?: Location): MetadataCommentRule | null;
1324
+ static parse(raw: string, loc?: Location): ConfigCommentRule | null;
1189
1325
  /**
1190
- * Converts a metadata comment AST to a string.
1326
+ * Converts an inline configuration comment AST to a string.
1191
1327
  *
1192
- * @param ast - Metadata comment AST
1328
+ * @param ast Inline configuration comment AST
1193
1329
  * @returns Raw string
1194
1330
  */
1195
- static generate(ast: MetadataCommentRule): string;
1331
+ static generate(ast: ConfigCommentRule): string;
1196
1332
  }
1197
1333
 
1198
1334
  /**
1199
- * Pre-processor directives
1335
+ * `CosmeticRuleParser` is responsible for parsing cosmetic rules.
1200
1336
  *
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.
1337
+ * Where possible, it automatically detects the difference between supported syntaxes:
1338
+ * - AdGuard
1339
+ * - uBlock Origin
1340
+ * - Adblock Plus
1210
1341
  *
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}
1342
+ * If the syntax is common / cannot be determined, the parser gives `Common` syntax.
1343
+ *
1344
+ * Please note that syntactically correct rules are parsed even if they are not actually
1345
+ * compatible with the given adblocker. This is a completely natural behavior, meaningful
1346
+ * checking of compatibility is not done at the parser level.
1220
1347
  */
1221
- declare class PreProcessorCommentRuleParser {
1348
+ declare class CosmeticRuleParser {
1222
1349
  /**
1223
- * Determines whether the rule is a pre-processor rule.
1350
+ * Determines whether a rule is a cosmetic rule. The rule is considered cosmetic if it
1351
+ * contains a cosmetic rule separator.
1224
1352
  *
1225
1353
  * @param raw Raw rule
1226
- * @returns `true` if the rule is a pre-processor rule, `false` otherwise
1354
+ * @returns `true` if the rule is a cosmetic rule, `false` otherwise
1227
1355
  */
1228
- static isPreProcessorRule(raw: string): boolean;
1356
+ static isCosmeticRule(raw: string): boolean;
1357
+ /**
1358
+ * Parses a cosmetic rule. The structure of the cosmetic rules:
1359
+ * - pattern (AdGuard pattern can have modifiers, other syntaxes don't)
1360
+ * - separator
1361
+ * - body
1362
+ *
1363
+ * @param raw Raw cosmetic rule
1364
+ * @param loc Location of the rule
1365
+ * @returns
1366
+ * Parsed cosmetic rule AST or null if it failed to parse based on the known cosmetic rules
1367
+ * @throws If the input matches the cosmetic rule pattern but syntactically invalid
1368
+ */
1369
+ static parse(raw: string, loc?: Location): AnyCosmeticRule | null;
1370
+ /**
1371
+ * Generates the rule pattern from the AST.
1372
+ *
1373
+ * @param ast Cosmetic rule AST
1374
+ * @returns Raw rule pattern
1375
+ * @example
1376
+ * - '##.foo' → ''
1377
+ * - 'example.com,example.org##.foo' → 'example.com,example.org'
1378
+ * - '[$path=/foo/bar]example.com##.foo' → '[$path=/foo/bar]example.com'
1379
+ */
1380
+ static generatePattern(ast: AnyCosmeticRule): string;
1381
+ /**
1382
+ * Generates the rule body from the AST.
1383
+ *
1384
+ * @param ast Cosmetic rule AST
1385
+ * @returns Raw rule body
1386
+ * @example
1387
+ * - '##.foo' → '.foo'
1388
+ * - 'example.com,example.org##.foo' → '.foo'
1389
+ * - 'example.com#%#//scriptlet('foo')' → '//scriptlet('foo')'
1390
+ */
1391
+ static generateBody(ast: AnyCosmeticRule): string;
1392
+ /**
1393
+ * Converts a cosmetic rule AST into a string.
1394
+ *
1395
+ * @param ast Cosmetic rule AST
1396
+ * @returns Raw string
1397
+ */
1398
+ static generate(ast: AnyCosmeticRule): string;
1399
+ }
1400
+
1401
+ /**
1402
+ * `DomainListParser` is responsible for parsing a domain list.
1403
+ *
1404
+ * @example
1405
+ * - If the rule is `example.com,~example.net##.ads`, the domain list is `example.com,~example.net`.
1406
+ * - If the rule is `ads.js^$script,domains=example.com|~example.org`, the domain list is `example.com|~example.org`.
1407
+ * This parser is responsible for parsing these domain lists.
1408
+ * @see {@link https://help.eyeo.com/adblockplus/how-to-write-filters#elemhide_domains}
1409
+ */
1410
+ declare class DomainListParser {
1411
+ /**
1412
+ * Parses a domain list, eg. `example.com,example.org,~example.org`
1413
+ *
1414
+ * @param raw Raw domain list
1415
+ * @param separator Separator character
1416
+ * @param loc Location of the domain list
1417
+ * @returns Domain list AST
1418
+ * @throws If the domain list is syntactically invalid
1419
+ */
1420
+ static parse(raw: string, separator?: DomainListSeparator, loc?: Location): DomainList;
1421
+ /**
1422
+ * Converts a domain list AST to a string.
1423
+ *
1424
+ * @param ast Domain list AST
1425
+ * @returns Raw string
1426
+ */
1427
+ static generate(ast: DomainList): string;
1428
+ }
1429
+
1430
+ /**
1431
+ * `FilterListParser` is responsible for parsing a whole adblock filter list (list of rules).
1432
+ * It is a wrapper around `RuleParser` which parses each line separately.
1433
+ */
1434
+ declare class FilterListParser {
1435
+ /**
1436
+ * Parses a whole adblock filter list (list of rules).
1437
+ *
1438
+ * @param raw Filter list source code (including new lines)
1439
+ * @param tolerant If `true`, then the parser will not throw if the rule is syntactically invalid,
1440
+ * instead it will return an `InvalidRule` object with the error attached to it. Default is `true`.
1441
+ * It is useful for parsing filter lists with invalid rules, because most of the rules are valid,
1442
+ * and some invalid rules can't break the whole filter list parsing.
1443
+ * @returns AST of the source code (list of rules)
1444
+ * @example
1445
+ * ```js
1446
+ * import { readFileSync } from 'fs';
1447
+ * import { FilterListParser } from '@adguard/agtree';
1448
+ *
1449
+ * // Read filter list content from file
1450
+ * const content = readFileSync('your-adblock-filter-list.txt', 'utf-8');
1451
+ *
1452
+ * // Parse the filter list content, then do something with the AST
1453
+ * const ast = FilterListParser.parse(content);
1454
+ * ```
1455
+ * @throws If one of the rules is syntactically invalid (if `tolerant` is `false`)
1456
+ */
1457
+ static parse(raw: string, tolerant?: boolean): FilterList;
1458
+ /**
1459
+ * Serializes a whole adblock filter list (list of rules).
1460
+ *
1461
+ * @param ast AST to generate
1462
+ * @param preferRaw If `true`, then the parser will use `raws.text` property of each rule
1463
+ * if it is available. Default is `false`.
1464
+ * @returns Serialized filter list
1465
+ */
1466
+ static generate(ast: FilterList, preferRaw?: boolean): string;
1467
+ }
1468
+
1469
+ /**
1470
+ * `HintRuleParser` is responsible for parsing AdGuard hint rules.
1471
+ *
1472
+ * @example
1473
+ * The following hint rule
1474
+ * ```adblock
1475
+ * !+ NOT_OPTIMIZED PLATFORM(windows)
1476
+ * ```
1477
+ * contains two hints: `NOT_OPTIMIZED` and `PLATFORM`.
1478
+ * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#hints}
1479
+ */
1480
+ declare class HintCommentRuleParser {
1481
+ /**
1482
+ * Checks if the raw rule is a hint rule.
1483
+ *
1484
+ * @param raw Raw rule
1485
+ * @returns `true` if the rule is a hint rule, `false` otherwise
1486
+ */
1487
+ static isHintRule(raw: string): boolean;
1488
+ /**
1489
+ * Parses a raw rule as a hint comment.
1490
+ *
1491
+ * @param raw Raw rule
1492
+ * @param loc Base location
1493
+ * @returns Hint AST or null (if the raw rule cannot be parsed as a hint comment)
1494
+ * @throws If the input matches the HINT pattern but syntactically invalid
1495
+ * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#hints-1}
1496
+ */
1497
+ static parse(raw: string, loc?: Location): HintCommentRule | null;
1498
+ /**
1499
+ * Converts a hint rule AST to a raw string.
1500
+ *
1501
+ * @param ast Hint rule AST
1502
+ * @returns Raw string
1503
+ */
1504
+ static generate(ast: HintCommentRule): string;
1505
+ }
1506
+
1507
+ /**
1508
+ * @file AdGuard Hints
1509
+ * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#hints}
1510
+ */
1511
+
1512
+ /**
1513
+ * `HintParser` is responsible for parsing AdGuard hints.
1514
+ *
1515
+ * @example
1516
+ * If the hint rule is
1517
+ * ```adblock
1518
+ * !+ NOT_OPTIMIZED PLATFORM(windows)
1519
+ * ```
1520
+ * then the hints are `NOT_OPTIMIZED` and `PLATFORM(windows)`, and this
1521
+ * class is responsible for parsing them. The rule itself is parsed by
1522
+ * the `HintRuleParser`, which uses this class to parse single hints.
1523
+ */
1524
+ declare class HintParser {
1525
+ /**
1526
+ * Parses a raw rule as a hint.
1527
+ *
1528
+ * @param raw Raw rule
1529
+ * @param loc Base location
1530
+ * @returns Hint rule AST or null
1531
+ * @throws If the syntax is invalid
1532
+ */
1533
+ static parse(raw: string, loc?: Location): Hint;
1534
+ /**
1535
+ * Converts a single hint AST to a string.
1536
+ *
1537
+ * @param hint Hint AST
1538
+ * @returns Hint string
1539
+ */
1540
+ static generate(hint: Hint): string;
1541
+ }
1542
+
1543
+ /**
1544
+ * `LogicalExpressionParser` is responsible for parsing logical expressions.
1545
+ *
1546
+ * @example
1547
+ * From the following rule:
1548
+ * ```adblock
1549
+ * !#if (adguard_ext_android_cb || adguard_ext_safari)
1550
+ * ```
1551
+ * this parser will parse the expression `(adguard_ext_android_cb || adguard_ext_safari)`.
1552
+ */
1553
+ declare class LogicalExpressionParser {
1554
+ /**
1555
+ * Split the expression into tokens.
1556
+ *
1557
+ * @param raw Source code of the expression
1558
+ * @param loc Location of the expression
1559
+ * @returns Token list
1560
+ * @throws {AdblockSyntaxError} If the expression is invalid
1561
+ */
1562
+ private static tokenize;
1563
+ /**
1564
+ * Parses a logical expression.
1565
+ *
1566
+ * @param raw Source code of the expression
1567
+ * @param loc Location of the expression
1568
+ * @returns Parsed expression
1569
+ * @throws {AdblockSyntaxError} If the expression is invalid
1570
+ */
1571
+ static parse(raw: string, loc?: Location): AnyExpressionNode;
1572
+ /**
1573
+ * Generates a string representation of the logical expression (serialization).
1574
+ *
1575
+ * @param ast Expression node
1576
+ * @returns String representation of the logical expression
1577
+ */
1578
+ static generate(ast: AnyExpressionNode): string;
1579
+ }
1580
+
1581
+ /**
1582
+ * @file Metadata comments
1583
+ */
1584
+
1585
+ /**
1586
+ * `MetadataParser` is responsible for parsing metadata comments.
1587
+ * Metadata comments are special comments that specify some properties of the list.
1588
+ *
1589
+ * @example
1590
+ * For example, in the case of
1591
+ * ```adblock
1592
+ * ! Title: My List
1593
+ * ```
1594
+ * the name of the header is `Title`, and the value is `My List`, which means that
1595
+ * the list title is `My List`, and it can be used in the adblocker UI.
1596
+ * @see {@link https://help.eyeo.com/adblockplus/how-to-write-filters#special-comments}
1597
+ */
1598
+ declare class MetadataCommentRuleParser {
1599
+ /**
1600
+ * Parses a raw rule as a metadata comment.
1601
+ *
1602
+ * @param raw Raw rule
1603
+ * @param loc Base location
1604
+ * @returns Metadata comment AST or null (if the raw rule cannot be parsed as a metadata comment)
1605
+ */
1606
+ static parse(raw: string, loc?: Location): MetadataCommentRule | null;
1607
+ /**
1608
+ * Converts a metadata comment AST to a string.
1609
+ *
1610
+ * @param ast - Metadata comment AST
1611
+ * @returns Raw string
1612
+ */
1613
+ static generate(ast: MetadataCommentRule): string;
1614
+ }
1615
+
1616
+ /**
1617
+ * `ModifierListParser` is responsible for parsing modifier lists. Please note that the name is not
1618
+ * uniform, "modifiers" are also known as "options".
1619
+ *
1620
+ * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#basic-rules-modifiers}
1621
+ * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#non-basic-rules-modifiers}
1622
+ * @see {@link https://help.eyeo.com/adblockplus/how-to-write-filters#options}
1623
+ */
1624
+ declare class ModifierListParser {
1625
+ /**
1626
+ * Parses the cosmetic rule modifiers, eg. `third-party,domain=example.com|~example.org`.
1627
+ *
1628
+ * _Note:_ you should remove `$` separator before passing the raw modifiers to this function,
1629
+ * or it will be parsed in the first modifier.
1630
+ *
1631
+ * @param raw Raw modifier list
1632
+ * @param loc Location of the modifier list
1633
+ * @returns Parsed modifiers interface
1634
+ */
1635
+ static parse(raw: string, loc?: Location): ModifierList;
1636
+ /**
1637
+ * Converts a modifier list AST to a string.
1638
+ *
1639
+ * @param ast Modifier list AST
1640
+ * @returns Raw string
1641
+ */
1642
+ static generate(ast: ModifierList): string;
1643
+ }
1644
+
1645
+ /**
1646
+ * `ModifierParser` is responsible for parsing modifiers.
1647
+ *
1648
+ * @example
1649
+ * `match-case`, `~third-party`, `domain=example.com|~example.org`
1650
+ */
1651
+ declare class ModifierParser {
1652
+ /**
1653
+ * Parses a modifier.
1654
+ *
1655
+ * @param raw Raw modifier string
1656
+ * @param loc Location of the modifier
1657
+ *
1658
+ * @returns Parsed modifier
1659
+ * @throws An error if modifier name or value is empty.
1660
+ */
1661
+ static parse(raw: string, loc?: Location): Modifier;
1662
+ /**
1663
+ * Generates a string from a modifier (serializes it).
1664
+ *
1665
+ * @param modifier Modifier to generate string from
1666
+ * @returns String representation of the modifier
1667
+ */
1668
+ static generate(modifier: Modifier): string;
1669
+ }
1670
+
1671
+ /**
1672
+ * `NetworkRuleParser` is responsible for parsing network rules.
1673
+ *
1674
+ * Please note that this will parse all syntactically correct network rules.
1675
+ * Modifier compatibility is not checked at the parser level.
1676
+ *
1677
+ * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#basic-rules}
1678
+ * @see {@link https://help.eyeo.com/adblockplus/how-to-write-filters#basic}
1679
+ */
1680
+ declare class NetworkRuleParser {
1681
+ /**
1682
+ * Parses a network rule (also known as basic rule).
1683
+ *
1684
+ * @param raw Raw rule
1685
+ * @param loc Location of the rule
1686
+ * @returns Network rule AST
1687
+ */
1688
+ static parse(raw: string, loc?: Location): NetworkRule;
1689
+ /**
1690
+ * Finds the index of the separator character in a network rule.
1691
+ *
1692
+ * @param rule Network rule to check
1693
+ * @returns The index of the separator character, or -1 if there is no separator
1694
+ */
1695
+ private static findNetworkRuleSeparatorIndex;
1696
+ /**
1697
+ * Converts a network rule (basic rule) AST to a string.
1698
+ *
1699
+ * @param ast - Network rule AST
1700
+ * @returns Raw string
1701
+ */
1702
+ static generate(ast: NetworkRule): string;
1703
+ }
1704
+
1705
+ /**
1706
+ * @file Customized error class for not implemented features.
1707
+ */
1708
+ /**
1709
+ * Customized error class for not implemented features.
1710
+ */
1711
+ declare class NotImplementedError extends Error {
1712
+ /**
1713
+ * Constructs a new `NotImplementedError` instance.
1714
+ *
1715
+ * @param message Additional error message (optional)
1716
+ */
1717
+ constructor(message?: string | undefined);
1718
+ }
1719
+
1720
+ declare class ParameterListParser {
1721
+ /**
1722
+ * Parses a raw parameter list.
1723
+ *
1724
+ * @param raw Raw parameter list
1725
+ * @param separator Separator character (default: comma)
1726
+ * @param loc Base location
1727
+ * @returns Parameter list AST
1728
+ */
1729
+ static parse(raw: string, separator?: string, loc?: Location): ParameterList;
1730
+ /**
1731
+ * Converts a parameter list AST to a string.
1732
+ *
1733
+ * @param params Parameter list AST
1734
+ * @param separator Separator character (default: comma)
1735
+ * @returns String representation of the parameter list
1736
+ */
1737
+ static generate(params: ParameterList, separator?: string): string;
1738
+ }
1739
+
1740
+ /**
1741
+ * Pre-processor directives
1742
+ *
1743
+ * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#pre-processor-directives}
1744
+ * @see {@link https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#pre-parsing-directives}
1745
+ */
1746
+
1747
+ /**
1748
+ * `PreProcessorParser` is responsible for parsing preprocessor rules.
1749
+ * Pre-processor comments are special comments that are used to control the behavior of the filter list processor.
1750
+ * Please note that this parser only handles general syntax for now, and does not validate the parameters at
1751
+ * the parsing stage.
1752
+ *
1753
+ * @example
1754
+ * If your rule is
1755
+ * ```adblock
1756
+ * !#if (adguard)
1757
+ * ```
1758
+ * then the directive's name is `if` and its value is `(adguard)`, but the parameter list
1759
+ * is not parsed / validated further.
1760
+ * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#pre-processor-directives}
1761
+ * @see {@link https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#pre-parsing-directives}
1762
+ */
1763
+ declare class PreProcessorCommentRuleParser {
1764
+ /**
1765
+ * Determines whether the rule is a pre-processor rule.
1766
+ *
1767
+ * @param raw Raw rule
1768
+ * @returns `true` if the rule is a pre-processor rule, `false` otherwise
1769
+ */
1770
+ static isPreProcessorRule(raw: string): boolean;
1229
1771
  /**
1230
1772
  * Parses a raw rule as a pre-processor comment.
1231
1773
  *
@@ -1245,409 +1787,587 @@ declare class PreProcessorCommentRuleParser {
1245
1787
  }
1246
1788
 
1247
1789
  /**
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}
1790
+ * @file Customized error class for conversion errors.
1791
+ */
1792
+ /**
1793
+ * Customized error class for conversion errors.
1794
+ */
1795
+ declare class RuleConversionError extends Error {
1796
+ /**
1797
+ * Constructs a new `RuleConversionError` instance.
1798
+ *
1799
+ * @param message Error message
1800
+ */
1801
+ constructor(message: string);
1802
+ }
1803
+
1804
+ /**
1805
+ * @file Validator for modifiers.
1250
1806
  */
1251
1807
 
1252
1808
  /**
1253
- * `ConfigCommentParser` is responsible for parsing inline AGLint configuration rules.
1254
- * Generally, the idea is inspired by ESLint inline configuration comments.
1809
+ * Result of modifier validation:
1810
+ * - `{ ok: true }` for valid and _fully supported_ modifier;
1811
+ * - `{ ok: true, warn: <deprecation notice> }` for valid
1812
+ * and _still supported but deprecated_ modifier;
1813
+ * - otherwise `{ ok: true, error: <invalidity reason> }`
1814
+ */
1815
+ type ValidationResult = {
1816
+ ok: boolean;
1817
+ error?: string;
1818
+ warn?: string;
1819
+ };
1820
+ /**
1821
+ * Modifier validator class.
1822
+ */
1823
+ declare class ModifierValidator {
1824
+ /**
1825
+ * Map of all modifiers data parsed from yaml files.
1826
+ */
1827
+ private modifiersData;
1828
+ /**
1829
+ * List of all modifier names for any adblocker.
1830
+ *
1831
+ * Please note that **deprecated** modifiers are **included** as well.
1832
+ */
1833
+ private allModifierNames;
1834
+ constructor();
1835
+ /**
1836
+ * Simply checks whether the modifier exists in any adblocker.
1837
+ *
1838
+ * **Deprecated** and **removed** modifiers are considered as **existent**.
1839
+ *
1840
+ * @param modifier Already parsed modifier AST node.
1841
+ *
1842
+ * @returns True if modifier exists, false otherwise.
1843
+ */
1844
+ exists: (modifier: Modifier) => boolean;
1845
+ /**
1846
+ * Checks whether the given `modifier` is valid for specified `syntax`.
1847
+ *
1848
+ * For `Common` syntax it simply checks whether the modifier exists.
1849
+ * For specific syntax the validation is more complex —
1850
+ * deprecated, assignable, negatable and other requirements are checked.
1851
+ *
1852
+ * @param syntax Adblock syntax to check the modifier for.
1853
+ * @param rawModifier Modifier AST node.
1854
+ * @param isException Whether the modifier is used in exception rule, default to false.
1855
+ * Needed to check whether the modifier is allowed only in blocking or exception rules.
1856
+ *
1857
+ * @returns Result of modifier validation.
1858
+ */
1859
+ validate: (syntax: AdblockSyntax, rawModifier: Modifier, isException?: boolean) => ValidationResult;
1860
+ /**
1861
+ * Returns AdGuard documentation URL for given modifier.
1862
+ *
1863
+ * @param modifier Parsed modifier AST node.
1864
+ *
1865
+ * @returns AdGuard documentation URL or `null` if not found.
1866
+ */
1867
+ getAdgDocumentationLink: (modifier: Modifier) => string | null;
1868
+ /**
1869
+ * Returns Ublock Origin documentation URL for given modifier.
1870
+ *
1871
+ * @param modifier Parsed modifier AST node.
1872
+ *
1873
+ * @returns Ublock Origin documentation URL or `null` if not found.
1874
+ */
1875
+ getUboDocumentationLink: (modifier: Modifier) => string | null;
1876
+ /**
1877
+ * Returns AdBlock Plus documentation URL for given modifier.
1878
+ *
1879
+ * @param modifier Parsed modifier AST node.
1880
+ *
1881
+ * @returns AdBlock Plus documentation URL or `null` if not found.
1882
+ */
1883
+ getAbpDocumentationLink: (modifier: Modifier) => string | null;
1884
+ }
1885
+ declare const modifierValidator: ModifierValidator;
1886
+
1887
+ /**
1888
+ * @file Base class for converters
1255
1889
  *
1256
- * @see {@link https://eslint.org/docs/latest/user-guide/configuring/rules#using-configuration-comments}
1890
+ * TS doesn't support abstract static methods, so we should use
1891
+ * a workaround and extend this class instead of implementing it
1257
1892
  */
1258
- declare class ConfigCommentRuleParser {
1893
+ /**
1894
+ * Basic class for rule converters
1895
+ */
1896
+ declare class ConverterBase {
1259
1897
  /**
1260
- * Checks if the raw rule is an inline configuration comment rule.
1898
+ * Converts some data to AdGuard format
1261
1899
  *
1262
- * @param raw Raw rule
1263
- * @returns `true` if the rule is an inline configuration comment rule, otherwise `false`.
1900
+ * @param data Data to convert
1901
+ * @returns Converted data
1902
+ * @throws If the data is invalid or incompatible
1264
1903
  */
1265
- static isConfigComment(raw: string): boolean;
1904
+ static convertToAdg(data: unknown): unknown;
1266
1905
  /**
1267
- * Parses a raw rule as an inline configuration comment.
1906
+ * Converts some data to Adblock Plus format
1268
1907
  *
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)
1908
+ * @param data Data to convert
1909
+ * @returns Converted data
1910
+ * @throws If the data is invalid or incompatible
1273
1911
  */
1274
- static parse(raw: string, loc?: Location): ConfigCommentRule | null;
1912
+ static convertToAbp(data: unknown): unknown;
1275
1913
  /**
1276
- * Converts an inline configuration comment AST to a string.
1914
+ * Converts some data to uBlock Origin format
1277
1915
  *
1278
- * @param ast Inline configuration comment AST
1279
- * @returns Raw string
1916
+ * @param data Data to convert
1917
+ * @returns Converted data
1918
+ * @throws If the data is invalid or incompatible
1280
1919
  */
1281
- static generate(ast: ConfigCommentRule): string;
1920
+ static convertToUbo(data: unknown): unknown;
1282
1921
  }
1283
1922
 
1284
1923
  /**
1285
- * `CommentParser` is responsible for parsing any comment-like adblock rules.
1924
+ * @file Base class for rule converters
1286
1925
  *
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.
1926
+ * TS doesn't support abstract static methods, so we should use
1927
+ * a workaround and extend this class instead of implementing it
1339
1928
  */
1340
- declare class CommentRuleParser {
1929
+
1930
+ /**
1931
+ * Basic class for rule converters
1932
+ */
1933
+ declare class RuleConverterBase extends ConverterBase {
1341
1934
  /**
1342
- * Checks whether a rule is a regular comment. Regular comments are the ones that start with
1343
- * an exclamation mark (`!`).
1935
+ * Converts an adblock filtering rule to AdGuard format, if possible.
1344
1936
  *
1345
- * @param raw Raw rule
1346
- * @returns `true` if the rule is a regular comment, `false` otherwise
1937
+ * @param rule Rule node to convert
1938
+ * @returns Array of converted rule nodes
1939
+ * @throws If the rule is invalid or cannot be converted
1347
1940
  */
1348
- static isRegularComment(raw: string): boolean;
1941
+ static convertToAdg(rule: Node): Node[];
1349
1942
  /**
1350
- * Checks whether a rule is a comment.
1943
+ * Converts an adblock filtering rule to Adblock Plus format, if possible.
1351
1944
  *
1352
- * @param raw Raw rule
1353
- * @returns `true` if the rule is a comment, `false` otherwise
1945
+ * @param rule Rule node to convert
1946
+ * @returns Array of converted rule nodes
1947
+ * @throws If the rule is invalid or cannot be converted
1354
1948
  */
1355
- static isCommentRule(raw: string): boolean;
1949
+ static convertToAbp(rule: Node): Node[];
1356
1950
  /**
1357
- * Parses a raw rule as comment.
1951
+ * Converts an adblock filtering rule to uBlock Origin format, if possible.
1358
1952
  *
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)
1953
+ * @param rule Rule node to convert
1954
+ * @returns Array of converted rule nodes
1955
+ * @throws If the rule is invalid or cannot be converted
1362
1956
  */
1363
- static parse(raw: string, loc?: Location): AnyCommentRule | null;
1957
+ static convertToUbo(rule: Node): Node[];
1958
+ }
1959
+
1960
+ /**
1961
+ * @file Adblock rule converter
1962
+ *
1963
+ * This file is the entry point for all rule converters
1964
+ * which automatically detects the rule type and calls
1965
+ * the corresponding "sub-converter".
1966
+ */
1967
+
1968
+ /**
1969
+ * Adblock filtering rule converter class
1970
+ *
1971
+ * @todo Implement `convertToUbo` and `convertToAbp`
1972
+ */
1973
+ declare class RuleConverter extends RuleConverterBase {
1364
1974
  /**
1365
- * Converts a comment AST to a string.
1975
+ * Converts an adblock filtering rule to AdGuard format, if possible.
1366
1976
  *
1367
- * @param ast Comment AST
1368
- * @returns Raw string
1977
+ * @param rule Rule node to convert
1978
+ * @returns Array of converted rule nodes
1979
+ * @throws If the rule is invalid or cannot be converted
1369
1980
  */
1370
- static generate(ast: AnyCommentRule): string;
1981
+ static convertToAdg(rule: AnyRule): AnyRule[];
1371
1982
  }
1372
1983
 
1373
1984
  /**
1374
- * `NetworkRuleParser` is responsible for parsing network rules.
1985
+ * @file Adblock filter list converter
1986
+ */
1987
+
1988
+ /**
1989
+ * Adblock filter list converter class
1375
1990
  *
1376
- * Please note that this will parse all syntactically correct network rules.
1377
- * Modifier compatibility is not checked at the parser level.
1991
+ * This class just provides an extra layer on top of the {@link RuleConverter}
1992
+ * and can be used to convert entire filter lists.
1378
1993
  *
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}
1994
+ * @todo Implement `convertToUbo` and `convertToAbp`
1995
+ * @todo Implement tolerant mode, which will allow to convert a filter list
1996
+ * even if some of its rules are invalid
1381
1997
  */
1382
- declare class NetworkRuleParser {
1998
+ declare class FilterListConverter extends ConverterBase {
1383
1999
  /**
1384
- * Parses a network rule (also known as basic rule).
2000
+ * Converts an adblock filter list to AdGuard format, if possible.
1385
2001
  *
1386
- * @param raw Raw rule
1387
- * @param loc Location of the rule
1388
- * @returns Network rule AST
2002
+ * @param filterListNode Filter list node to convert
2003
+ * @returns Converted filter list node
2004
+ * @throws If the filter list is invalid or cannot be converted
1389
2005
  */
1390
- static parse(raw: string, loc?: Location): NetworkRule;
2006
+ static convertToAdg(filterListNode: FilterList): FilterList;
2007
+ }
2008
+
2009
+ /**
2010
+ * @file Cosmetic rule separator finder and categorizer
2011
+ */
2012
+
2013
+ interface CosmeticRuleSeparatorFinderResult {
1391
2014
  /**
1392
- * Finds the index of the separator character in a network rule.
2015
+ * Separator type
2016
+ */
2017
+ separator: CosmeticRuleSeparator;
2018
+ /**
2019
+ * Separator start position
2020
+ */
2021
+ start: number;
2022
+ /**
2023
+ * Separator end position
2024
+ */
2025
+ end: number;
2026
+ }
2027
+ /**
2028
+ * Utility class for cosmetic rule separators.
2029
+ */
2030
+ declare class CosmeticRuleSeparatorUtils {
2031
+ /**
2032
+ * Checks whether the specified separator is an exception.
1393
2033
  *
1394
- * @param rule Network rule to check
1395
- * @returns The index of the separator character, or -1 if there is no separator
2034
+ * @param separator Separator to check
2035
+ * @returns `true` if the separator is an exception, `false` otherwise
1396
2036
  */
1397
- private static findNetworkRuleSeparatorIndex;
2037
+ static isException(separator: CosmeticRuleSeparator): boolean;
1398
2038
  /**
1399
- * Converts a network rule (basic rule) AST to a string.
2039
+ * Checks whether the specified separator is marks an Extended CSS cosmetic rule.
1400
2040
  *
1401
- * @param ast - Network rule AST
1402
- * @returns Raw string
2041
+ * @param separator Separator to check
2042
+ * @returns `true` if the separator is marks an Extended CSS cosmetic rule, `false` otherwise
1403
2043
  */
1404
- static generate(ast: NetworkRule): string;
2044
+ static isExtendedCssMarker(separator: CosmeticRuleSeparator): boolean;
2045
+ /**
2046
+ * Looks for the cosmetic rule separator in the rule. This is a simplified version that
2047
+ * masks the recursive function.
2048
+ *
2049
+ * @param rule Raw rule
2050
+ * @returns Separator result or null if no separator was found
2051
+ */
2052
+ static find(rule: string): CosmeticRuleSeparatorFinderResult | null;
1405
2053
  }
1406
2054
 
1407
2055
  /**
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
2056
+ * @file Helper file for CSSTree to provide better compatibility with TypeScript.
2057
+ * @see {@link https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/62536}
2058
+ */
2059
+ /**
2060
+ * CSSTree node types.
1414
2061
  *
1415
- * If the syntax is common / cannot be determined, the parser gives `Common` syntax.
2062
+ * @see {@link https://github.com/csstree/csstree/blob/master/docs/ast.md#node-types}
2063
+ */
2064
+ declare const enum CssTreeNodeType {
2065
+ AnPlusB = "AnPlusB",
2066
+ Atrule = "Atrule",
2067
+ AtrulePrelude = "AtrulePrelude",
2068
+ AttributeSelector = "AttributeSelector",
2069
+ Block = "Block",
2070
+ Brackets = "Brackets",
2071
+ CDC = "CDC",
2072
+ CDO = "CDO",
2073
+ ClassSelector = "ClassSelector",
2074
+ Combinator = "Combinator",
2075
+ Comment = "Comment",
2076
+ Declaration = "Declaration",
2077
+ DeclarationList = "DeclarationList",
2078
+ Dimension = "Dimension",
2079
+ Function = "Function",
2080
+ Hash = "Hash",
2081
+ Identifier = "Identifier",
2082
+ IdSelector = "IdSelector",
2083
+ MediaFeature = "MediaFeature",
2084
+ MediaQuery = "MediaQuery",
2085
+ MediaQueryList = "MediaQueryList",
2086
+ NestingSelector = "NestingSelector",
2087
+ Nth = "Nth",
2088
+ Number = "Number",
2089
+ Operator = "Operator",
2090
+ Parentheses = "Parentheses",
2091
+ Percentage = "Percentage",
2092
+ PseudoClassSelector = "PseudoClassSelector",
2093
+ PseudoElementSelector = "PseudoElementSelector",
2094
+ Ratio = "Ratio",
2095
+ Raw = "Raw",
2096
+ Rule = "Rule",
2097
+ Selector = "Selector",
2098
+ SelectorList = "SelectorList",
2099
+ String = "String",
2100
+ StyleSheet = "StyleSheet",
2101
+ TypeSelector = "TypeSelector",
2102
+ UnicodeRange = "UnicodeRange",
2103
+ Url = "Url",
2104
+ Value = "Value",
2105
+ WhiteSpace = "WhiteSpace"
2106
+ }
2107
+ /**
2108
+ * Parser context for CSSTree.
1416
2109
  *
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.
2110
+ * @see {@link https://github.com/csstree/csstree/blob/master/docs/parsing.md#context}
2111
+ */
2112
+ declare const enum CssTreeParserContext {
2113
+ /**
2114
+ * Regular stylesheet, should be suitable in most cases (default)
2115
+ */
2116
+ stylesheet = "stylesheet",
2117
+ /**
2118
+ * at-rule (e.g. `@media screen`, `print { ... }`)
2119
+ */
2120
+ atrule = "atrule",
2121
+ /**
2122
+ * at-rule prelude (screen, print for example above)
2123
+ */
2124
+ atrulePrelude = "atrulePrelude",
2125
+ /**
2126
+ * used to parse comma separated media query list
2127
+ */
2128
+ mediaQueryList = "mediaQueryList",
2129
+ /**
2130
+ * used to parse media query
2131
+ */
2132
+ mediaQuery = "mediaQuery",
2133
+ /**
2134
+ * rule (e.g. `.foo`, `.bar:hover { color: red; border: 1px solid black; }`)
2135
+ */
2136
+ rule = "rule",
2137
+ /**
2138
+ * selector group (`.foo`, `.bar:hover` for rule example)
2139
+ */
2140
+ selectorList = "selectorList",
2141
+ /**
2142
+ * selector (`.foo` or `.bar:hover` for rule example)
2143
+ */
2144
+ selector = "selector",
2145
+ /**
2146
+ * block with curly braces ({ color: red; border: 1px solid black; } for rule example)
2147
+ */
2148
+ block = "block",
2149
+ /**
2150
+ * block content w/o curly braces (`color: red; border: 1px solid black;` for rule example),
2151
+ * useful for parsing HTML style attribute value
2152
+ */
2153
+ declarationList = "declarationList",
2154
+ /**
2155
+ * declaration (`color: red` or `border: 1px solid black` for rule example)
2156
+ */
2157
+ declaration = "declaration",
2158
+ /**
2159
+ * declaration value (`red` or `1px solid black` for rule example)
2160
+ */
2161
+ value = "value"
2162
+ }
2163
+
2164
+ /**
2165
+ * Additional / helper functions for CSSTree.
1420
2166
  */
1421
- declare class CosmeticRuleParser {
2167
+ declare class CssTree {
1422
2168
  /**
1423
- * Determines whether a rule is a cosmetic rule. The rule is considered cosmetic if it
1424
- * contains a cosmetic rule separator.
2169
+ * Shifts location of the CSSTree node. Temporary workaround for CSSTree issue:
2170
+ * https://github.com/csstree/csstree/issues/251
1425
2171
  *
1426
- * @param raw Raw rule
1427
- * @returns `true` if the rule is a cosmetic rule, `false` otherwise
2172
+ * @param root Root CSSTree node
2173
+ * @param loc Location to shift
2174
+ * @returns Root CSSTree node with shifted location
1428
2175
  */
1429
- static isCosmeticRule(raw: string): boolean;
2176
+ static shiftNodePosition(root: CssNode, loc?: Location): CssNode;
1430
2177
  /**
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
2178
+ * Helper function for parsing CSS parts.
1435
2179
  *
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
2180
+ * @param raw Raw CSS input
2181
+ * @param context CSSTree context for parsing
2182
+ * @param tolerant If `true`, then the parser will not throw an error on parsing fallbacks. Default is `false`
2183
+ * @param loc Base location for the parsed node
2184
+ * @returns CSSTree node (AST)
1441
2185
  */
1442
- static parse(raw: string, loc?: Location): AnyCosmeticRule | null;
2186
+ static parse(raw: string, context: CssTreeParserContext, tolerant?: boolean, loc?: Location): CssNode;
1443
2187
  /**
1444
- * Converts a cosmetic rule AST into a string.
2188
+ * Helper function for parsing CSS parts.
1445
2189
  *
1446
- * @param ast Cosmetic rule AST
1447
- * @returns Raw string
2190
+ * @param raw Raw CSS input
2191
+ * @param context CSSTree context
2192
+ * @param tolerant If `true`, then the parser will not throw an error on parsing fallbacks. Default is `false`
2193
+ * @param loc Base location for the parsed node
2194
+ * @returns CSSTree node (AST)
1448
2195
  */
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 {
2196
+ static parsePlain(raw: string, context: CssTreeParserContext, tolerant?: boolean, loc?: Location): CssNodePlain;
1463
2197
  /**
1464
- * Split the expression into tokens.
2198
+ * Checks if the CSSTree node is an ExtendedCSS node.
1465
2199
  *
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
2200
+ * @param node Node to check
2201
+ * @param pseudoClasses List of the names of the pseudo classes to check
2202
+ * @param attributeSelectors List of the names of the attribute selectors to check
2203
+ * @returns `true` if the node is an ExtendedCSS node, otherwise `false`
1470
2204
  */
1471
- private static tokenize;
2205
+ static isExtendedCssNode(node: CssNode | CssNodePlain, pseudoClasses: Set<string>, attributeSelectors: Set<string>): boolean;
1472
2206
  /**
1473
- * Parses a logical expression.
2207
+ * Walks through the CSSTree node and returns all ExtendedCSS nodes.
1474
2208
  *
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
2209
+ * @param selectorList Selector list (can be a string or a CSSTree node)
2210
+ * @param pseudoClasses List of the names of the pseudo classes to check
2211
+ * @param attributeSelectors List of the names of the attribute selectors to check
2212
+ * @returns Extended CSS nodes (pseudos and attributes)
2213
+ * @see {@link https://github.com/csstree/csstree/blob/master/docs/ast.md#selectorlist}
1479
2214
  */
1480
- static parse(raw: string, loc?: Location): AnyExpressionNode;
2215
+ static getSelectorExtendedCssNodes(selectorList: string | SelectorList | SelectorListPlain, pseudoClasses?: Set<string>, attributeSelectors?: Set<string>): CssNode[];
1481
2216
  /**
1482
- * Generates a string representation of the logical expression (serialization).
2217
+ * Checks if the selector contains any ExtendedCSS nodes. It is a faster alternative to
2218
+ * `getSelectorExtendedCssNodes` if you only need to know if the selector contains any ExtendedCSS nodes,
2219
+ * because it stops the search on the first ExtendedCSS node instead of going through the whole selector
2220
+ * and collecting all ExtendedCSS nodes.
1483
2221
  *
1484
- * @param ast Expression node
1485
- * @returns String representation of the logical expression
2222
+ * @param selectorList Selector list (can be a string or a CSSTree node)
2223
+ * @param pseudoClasses List of the names of the pseudo classes to check
2224
+ * @param attributeSelectors List of the names of the attribute selectors to check
2225
+ * @returns `true` if the selector contains any ExtendedCSS nodes
2226
+ * @see {@link https://github.com/csstree/csstree/blob/master/docs/ast.md#selectorlist}
2227
+ * @see {@link https://github.com/csstree/csstree/blob/master/docs/traversal.md#findast-fn}
1486
2228
  */
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 {
2229
+ static hasAnySelectorExtendedCssNode(selectorList: string | SelectorList | SelectorListPlain, pseudoClasses?: Set<string>, attributeSelectors?: Set<string>): boolean;
1500
2230
  /**
1501
- * Parses a domain list, eg. `example.com,example.org,~example.org`
2231
+ * Checks if the node is a forbidden function (unsafe resource loading). Typically it is used to check
2232
+ * if the node is a `url()` function, which is a security risk when using filter lists from untrusted
2233
+ * sources.
1502
2234
  *
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
2235
+ * @param node Node to check
2236
+ * @param forbiddenFunctions Set of the names of the functions to check
2237
+ * @returns `true` if the node is a forbidden function
1508
2238
  */
1509
- static parse(raw: string, separator?: DomainListSeparator, loc?: Location): DomainList;
2239
+ static isForbiddenFunction(node: CssNode | CssNodePlain, forbiddenFunctions?: Set<string>): boolean;
1510
2240
  /**
1511
- * Converts a domain list AST to a string.
2241
+ * Gets the list of the forbidden function nodes in the declaration block. Typically it is used to get
2242
+ * the list of the functions that can be used to load external resources, which is a security risk
2243
+ * when using filter lists from untrusted sources.
1512
2244
  *
1513
- * @param ast Domain list AST
1514
- * @returns Raw string
2245
+ * @param declarationList Declaration list to check (can be a string or a CSSTree node)
2246
+ * @param forbiddenFunctions Set of the names of the functions to check
2247
+ * @returns List of the forbidden function nodes in the declaration block (can be empty)
1515
2248
  */
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 {
2249
+ static getForbiddenFunctionNodes(declarationList: string | DeclarationList | DeclarationListPlain, forbiddenFunctions?: Set<string>): CssNode[];
1528
2250
  /**
1529
- * Parses the cosmetic rule modifiers, eg. `third-party,domain=example.com|~example.org`.
2251
+ * Checks if the declaration block contains any forbidden functions. Typically it is used to check
2252
+ * if the declaration block contains any functions that can be used to load external resources,
2253
+ * which is a security risk when using filter lists from untrusted sources.
1530
2254
  *
1531
- * _Note:_ you should remove `$` separator before passing the raw modifiers to this function,
1532
- * or it will be parsed in the first modifier.
2255
+ * @param declarationList Declaration list to check (can be a string or a CSSTree node)
2256
+ * @param forbiddenFunctions Set of the names of the functions to check
2257
+ * @returns `true` if the declaration block contains any forbidden functions
2258
+ * @throws If you pass a string, but it is not a valid CSS
2259
+ * @throws If you pass an invalid CSSTree node / AST
2260
+ * @see {@link https://github.com/csstree/csstree/blob/master/docs/ast.md#declarationlist}
2261
+ * @see {@link https://github.com/AdguardTeam/AdguardBrowserExtension/issues/1196}
2262
+ * @see {@link https://github.com/AdguardTeam/AdguardBrowserExtension/issues/1920}
2263
+ */
2264
+ static hasAnyForbiddenFunction(declarationList: string | DeclarationList | DeclarationListPlain, forbiddenFunctions?: Set<string>): boolean;
2265
+ /**
2266
+ * Generates string representation of the media query list.
1533
2267
  *
1534
- * @param raw Raw modifier list
1535
- * @param loc Location of the modifier list
1536
- * @returns Parsed modifiers interface
2268
+ * @param ast Media query list AST
2269
+ * @returns String representation of the media query list
1537
2270
  */
1538
- static parse(raw: string, loc?: Location): ModifierList;
2271
+ static generateMediaQueryList(ast: MediaQueryList): string;
1539
2272
  /**
1540
- * Converts a modifier list AST to a string.
2273
+ * Generates string representation of the media query.
1541
2274
  *
1542
- * @param ast Modifier list AST
1543
- * @returns Raw string
2275
+ * @param ast Media query AST
2276
+ * @returns String representation of the media query
1544
2277
  */
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 {
2278
+ static generateMediaQuery(ast: MediaQuery): string;
1555
2279
  /**
1556
- * Parses a modifier.
2280
+ * Generates string representation of the selector list.
1557
2281
  *
1558
- * @param raw Raw modifier string
1559
- * @param loc Location of the modifier
1560
- * @returns Parsed modifier
2282
+ * @param ast SelectorList AST
2283
+ * @returns String representation of the selector list
1561
2284
  */
1562
- static parse(raw: string, loc?: Location): Modifier;
2285
+ static generateSelectorList(ast: SelectorList): string;
1563
2286
  /**
1564
- * Generates a string from a modifier (serializes it).
2287
+ * Selector generation based on CSSTree's AST. This is necessary because CSSTree
2288
+ * only adds spaces in some edge cases.
1565
2289
  *
1566
- * @param modifier Modifier to generate string from
1567
- * @returns String representation of the modifier
2290
+ * @param ast CSS Tree AST
2291
+ * @returns CSS selector as string
1568
2292
  */
1569
- static generate(modifier: Modifier): string;
1570
- }
1571
-
1572
- declare class ParameterListParser {
2293
+ static generateSelector(ast: Selector): string;
1573
2294
  /**
1574
- * Parses a raw parameter list.
2295
+ * Block generation based on CSSTree's AST. This is necessary because CSSTree only adds spaces in some edge cases.
1575
2296
  *
1576
- * @param raw Raw parameter list
1577
- * @param separator Separator character (default: comma)
1578
- * @param loc Base location
1579
- * @returns Parameter list AST
2297
+ * @param ast CSS Tree AST
2298
+ * @returns CSS selector as string
1580
2299
  */
1581
- static parse(raw: string, separator?: string, loc?: Location): ParameterList;
2300
+ static generateDeclarationList(ast: DeclarationList): string;
1582
2301
  /**
1583
- * Converts a parameter list AST to a string.
2302
+ * Helper method to assert that the attribute selector has a value
1584
2303
  *
1585
- * @param params Parameter list AST
1586
- * @param separator Separator character (default: comma)
1587
- * @returns String representation of the parameter list
2304
+ * @param node Attribute selector node
1588
2305
  */
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 {
2306
+ static assertAttributeSelectorHasStringValue(node: AttributeSelector): asserts node is AttributeSelector & {
2307
+ value: {
2308
+ type: 'String';
2309
+ };
2310
+ };
1601
2311
  /**
1602
- * Location range of the error.
2312
+ * Helper method to assert that the pseudo-class selector has at least one argument
2313
+ *
2314
+ * @param node Pseudo-class selector node
1603
2315
  */
1604
- loc: LocationRange;
2316
+ static assertPseudoClassHasAnyArgument(node: PseudoClassSelectorPlain): asserts node is PseudoClassSelectorPlain & {
2317
+ children: CssNodePlain[];
2318
+ };
1605
2319
  /**
1606
- * Constructs a new `AdblockSyntaxError` instance.
2320
+ * Helper method to parse an attribute selector value as a number
1607
2321
  *
1608
- * @param message Error message
1609
- * @param loc Location range of the error
2322
+ * @param node Attribute selector node
2323
+ * @returns Parsed attribute selector value as a number
2324
+ * @throws If the attribute selector hasn't a string value or the string value is can't be parsed as a number
1610
2325
  */
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 {
2326
+ static parseAttributeSelectorValueAsNumber(node: AttributeSelector): number;
1619
2327
  /**
1620
- * Parses a whole adblock filter list (list of rules).
2328
+ * Helper method to parse a pseudo-class argument as a number
1621
2329
  *
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';
2330
+ * @param node Pseudo-class selector node to parse
2331
+ * @returns Parsed pseudo-class argument as a number
2332
+ */
2333
+ static parsePseudoClassArgumentAsNumber(node: PseudoClassSelectorPlain): number;
2334
+ /**
2335
+ * Helper method to create an attribute selector node
1632
2336
  *
1633
- * // Read filter list content from file
1634
- * const content = readFileSync('your-adblock-filter-list.txt', 'utf-8');
2337
+ * @param name Name of the attribute
2338
+ * @param value Value of the attribute
2339
+ * @param matcher Matcher of the attribute
2340
+ * @param flags Flags of the attribute
2341
+ * @returns Attribute selector node
2342
+ * @see {@link https://github.com/csstree/csstree/blob/master/docs/ast.md#attributeselector}
2343
+ */
2344
+ static createAttributeSelectorNode(name: string, value: string, matcher?: string, flags?: string | null): AttributeSelector;
2345
+ /**
2346
+ * Helper function to rename a CSSTree pseudo-class node
1635
2347
  *
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`)
2348
+ * @param node Node to rename
2349
+ * @param name New name
1640
2350
  */
1641
- static parse(raw: string, tolerant?: boolean): FilterList;
2351
+ static renamePseudoClass(node: PseudoClassSelector, name: string): void;
1642
2352
  /**
1643
- * Serializes a whole adblock filter list (list of rules).
2353
+ * Helper function to generate a raw string from a pseudo-class
2354
+ * selector's children
1644
2355
  *
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
2356
+ * @param node Pseudo-class selector node
2357
+ * @returns Generated pseudo-class value
2358
+ * @example
2359
+ * - `:nth-child(2n+1)` -> `2n+1`
2360
+ * - `:matches-path(/foo/bar)` -> `/foo/bar`
1649
2361
  */
1650
- static generate(ast: FilterList, preferRaw?: boolean): string;
2362
+ static generatePseudoClassValue(node: PseudoClassSelector): string;
2363
+ /**
2364
+ * Helper function to generate a raw string from a function selector's children
2365
+ *
2366
+ * @param node Function node
2367
+ * @returns Generated function value
2368
+ * @example `responseheader(name)` -> `name`
2369
+ */
2370
+ static generateFunctionValue(node: FunctionNode): string;
1651
2371
  }
1652
2372
 
1653
2373
  /**
@@ -1709,9 +2429,165 @@ declare class LogicalExpressionUtils {
1709
2429
  static evaluate(ast: AnyExpressionNode, table: VariableTable): boolean;
1710
2430
  }
1711
2431
 
2432
+ /**
2433
+ * @file Utility functions for location and location range management.
2434
+ */
2435
+
2436
+ /**
2437
+ * Shifts the specified location by the specified offset.
2438
+ *
2439
+ * @param loc Location to shift
2440
+ * @param offset Offset to shift by
2441
+ * @returns Location shifted by the specified offset
2442
+ */
2443
+ declare function shiftLoc(loc: Location, offset: number): Location;
2444
+ /**
2445
+ * Calculates a location range from the specified base location and offsets.
2446
+ *
2447
+ * Since every adblock rule is a single line, the start and end locations
2448
+ * of the range will have the same line, no need to calculate it here.
2449
+ *
2450
+ * @param loc Base location
2451
+ * @param startOffset Start offset
2452
+ * @param endOffset End offset
2453
+ * @returns Calculated location range
2454
+ */
2455
+ declare function locRange(loc: Location, startOffset: number, endOffset: number): LocationRange;
2456
+
2457
+ declare const ADBLOCK_URL_START: string;
2458
+ declare const ADBLOCK_URL_START_REGEX = "^(http|https|ws|wss)://([a-z0-9-_.]+\\.)?";
2459
+ declare const ADBLOCK_URL_SEPARATOR = "^";
2460
+ declare const ADBLOCK_URL_SEPARATOR_REGEX = "([^ a-zA-Z0-9.%_-]|$)";
2461
+ declare const ADBLOCK_WILDCARD = "*";
2462
+ declare const ADBLOCK_WILDCARD_REGEX: string;
2463
+ /**
2464
+ * Special RegExp symbols
2465
+ *
2466
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-escape
2467
+ */
2468
+ declare const SPECIAL_REGEX_SYMBOLS: Set<string>;
2469
+ /**
2470
+ * Utility functions for working with RegExp patterns
2471
+ */
2472
+ declare class RegExpUtils {
2473
+ /**
2474
+ * Checks whether a string is a RegExp pattern.
2475
+ * Flags are not supported.
2476
+ *
2477
+ * @param pattern - Pattern to check
2478
+ * @returns `true` if the string is a RegExp pattern, `false` otherwise
2479
+ */
2480
+ static isRegexPattern(pattern: string): boolean;
2481
+ /**
2482
+ * Negates a RegExp pattern. Technically, this method wraps the pattern in `^((?!` and `).)*$`.
2483
+ *
2484
+ * RegExp modifiers are not supported.
2485
+ *
2486
+ * @param pattern Pattern to negate (can be wrapped in slashes or not)
2487
+ * @returns Negated RegExp pattern
2488
+ */
2489
+ static negateRegexPattern(pattern: string): string;
2490
+ /**
2491
+ * Converts a basic adblock rule pattern to a RegExp pattern. Based on
2492
+ * https://github.com/AdguardTeam/tsurlfilter/blob/9b26e0b4a0e30b87690bc60f7cf377d112c3085c/packages/tsurlfilter/src/rules/simple-regex.ts#L219
2493
+ *
2494
+ * @param pattern Pattern to convert
2495
+ * @returns RegExp equivalent of the pattern
2496
+ * @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#basic-rules}
2497
+ */
2498
+ static patternToRegexp(pattern: string): string;
2499
+ }
2500
+
2501
+ /**
2502
+ * @file Utility functions for working with quotes
2503
+ */
2504
+ /**
2505
+ * Possible quote types for scriptlet parameters
2506
+ */
2507
+ declare enum QuoteType {
2508
+ /**
2509
+ * No quotes at all
2510
+ */
2511
+ None = "none",
2512
+ /**
2513
+ * Single quotes (`'`)
2514
+ */
2515
+ Single = "single",
2516
+ /**
2517
+ * Double quotes (`"`)
2518
+ */
2519
+ Double = "double"
2520
+ }
2521
+ /**
2522
+ * Utility functions for working with quotes
2523
+ */
2524
+ declare class QuoteUtils {
2525
+ /**
2526
+ * Escape all unescaped occurrences of the character
2527
+ *
2528
+ * @param string String to escape
2529
+ * @param char Character to escape
2530
+ * @returns Escaped string
2531
+ */
2532
+ static escapeUnescapedOccurrences(string: string, char: string): string;
2533
+ /**
2534
+ * Unescape all single escaped occurrences of the character
2535
+ *
2536
+ * @param string String to unescape
2537
+ * @param char Character to unescape
2538
+ * @returns Unescaped string
2539
+ */
2540
+ static unescapeSingleEscapedOccurrences(string: string, char: string): string;
2541
+ /**
2542
+ * Get quote type of the string
2543
+ *
2544
+ * @param string String to check
2545
+ * @returns Quote type of the string
2546
+ */
2547
+ static getStringQuoteType(string: string): QuoteType;
2548
+ /**
2549
+ * Set quote type of the string
2550
+ *
2551
+ * @param string String to set quote type of
2552
+ * @param quoteType Quote type to set
2553
+ * @returns String with the specified quote type
2554
+ */
2555
+ static setStringQuoteType(string: string, quoteType: QuoteType): string;
2556
+ /**
2557
+ * Removes bounding quotes from a string, if any
2558
+ *
2559
+ * @param string Input string
2560
+ * @returns String without quotes
2561
+ */
2562
+ static removeQuotes(string: string): string;
2563
+ }
2564
+
2565
+ /**
2566
+ * Known metadata headers
2567
+ */
2568
+ declare const METADATA_HEADERS: string[];
2569
+
2570
+ /**
2571
+ * Known Extended CSS pseudo-classes. Please, keep this list sorted.
2572
+ */
2573
+ declare const EXT_CSS_PSEUDO_CLASSES: Set<string>;
2574
+ /**
2575
+ * Known legacy Extended CSS attributes. These attributes are deprecated and
2576
+ * should be replaced with the corresponding pseudo-classes. In a long term,
2577
+ * these attributes will be COMPLETELY removed from the Extended CSS syntax.
2578
+ *
2579
+ * Please, keep this list sorted.
2580
+ */
2581
+ declare const EXT_CSS_LEGACY_ATTRIBUTES: Set<string>;
2582
+ /**
2583
+ * Known CSS functions that aren't allowed in CSS injection rules, because they
2584
+ * able to load external resources. Please, keep this list sorted.
2585
+ */
2586
+ declare const FORBIDDEN_CSS_FUNCTIONS: Set<string>;
2587
+
1712
2588
  /**
1713
2589
  * @file AGTree version
1714
2590
  */
1715
2591
  declare const version: string;
1716
2592
 
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 };
2593
+ export { ADBLOCK_URL_SEPARATOR, ADBLOCK_URL_SEPARATOR_REGEX, ADBLOCK_URL_START, ADBLOCK_URL_START_REGEX, ADBLOCK_WILDCARD, ADBLOCK_WILDCARD_REGEX, ADG_SCRIPTLET_MASK, AGLINT_COMMAND_PREFIX, AdblockSyntax, AdblockSyntaxError, Agent, AgentCommentRule, AgentCommentRuleParser, AgentParser, AnyCommentRule, AnyCosmeticRule, AnyExpressionNode, AnyOperator, AnyRule, CLASSIC_DOMAIN_SEPARATOR, CommentBase, CommentMarker, CommentRule, CommentRuleParser, CommentRuleType, ConfigCommentRule, ConfigCommentRuleParser, CosmeticRule, CosmeticRuleParser, CosmeticRuleSeparator, CosmeticRuleSeparatorFinderResult, CosmeticRuleSeparatorUtils, CosmeticRuleType, CssInjectionRule, CssInjectionRuleBody, CssTree, CssTreeNodeType, CssTreeParserContext, DOMAIN_EXCEPTION_MARKER, Domain, DomainList, DomainListParser, DomainListSeparator, DomainUtils, EXT_CSS_LEGACY_ATTRIBUTES, EXT_CSS_PSEUDO_CLASSES, ElementHidingRule, ElementHidingRuleBody, EmptyRule, ExpressionOperatorNode, ExpressionParenthesisNode, ExpressionVariableNode, FORBIDDEN_CSS_FUNCTIONS, FilterList, FilterListConverter, FilterListParser, HINT_MARKER, Hint, HintCommentRule, HintCommentRuleParser, HintParser, HtmlFilteringRule, HtmlFilteringRuleBody, IF, INCLUDE, JsInjectionRule, Location, LocationRange, LogicalExpressionParser, LogicalExpressionUtils, METADATA_HEADERS, MODIFIERS_SEPARATOR, MODIFIER_ASSIGN_OPERATOR, MODIFIER_DOMAIN_SEPARATOR, MODIFIER_EXCEPTION_MARKER, MetadataCommentRule, MetadataCommentRuleParser, Modifier, ModifierList, ModifierListParser, ModifierParser, NETWORK_RULE_EXCEPTION_MARKER, NETWORK_RULE_SEPARATOR, NetworkRule, NetworkRuleParser, Node, NotImplementedError, PREPROCESSOR_MARKER, Parameter, ParameterList, ParameterListParser, PreProcessorCommentRule, PreProcessorCommentRuleParser, QuoteType, QuoteUtils, RegExpUtils, RuleBase, RuleCategory, RuleConversionError, RuleConverter, RuleParser, SAFARI_CB_AFFINITY, SPECIAL_REGEX_SYMBOLS, ScriptletInjectionRule, ScriptletInjectionRuleBody, UBO_SCRIPTLET_MASK, Value, VariableTable, locRange, modifierValidator, shiftLoc, version };