@tokioapp/adblock 0.0.0-stage → 0.1.0

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.
Files changed (143) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +72 -2
  3. package/THIRD_PARTY_NOTICES.md +50 -0
  4. package/dist/content/cosmetics.d.ts +22 -0
  5. package/dist/content/cosmetics.js +173 -0
  6. package/dist/content/mainWorld.d.ts +8 -0
  7. package/dist/content/mainWorld.js +10 -0
  8. package/dist/content/preload.d.ts +1 -0
  9. package/dist/content/preload.js +34 -0
  10. package/dist/content/procedural.d.ts +19 -0
  11. package/dist/content/procedural.js +212 -0
  12. package/dist/cosmetic/engine.d.ts +95 -0
  13. package/dist/cosmetic/engine.js +268 -0
  14. package/dist/electron/blocker.d.ts +84 -0
  15. package/dist/electron/blocker.js +320 -0
  16. package/dist/electron/channels.d.ts +10 -0
  17. package/dist/electron/channels.js +6 -0
  18. package/dist/electron/scheme.d.ts +21 -0
  19. package/dist/electron/scheme.js +21 -0
  20. package/dist/engine.d.ts +86 -0
  21. package/dist/engine.js +163 -0
  22. package/dist/index.d.ts +13 -0
  23. package/dist/index.js +12 -0
  24. package/dist/lists/defaults.d.ts +6 -0
  25. package/dist/lists/defaults.js +59 -0
  26. package/dist/lists/manager.d.ts +86 -0
  27. package/dist/lists/manager.js +154 -0
  28. package/dist/lists/preprocessor.d.ts +21 -0
  29. package/dist/lists/preprocessor.js +152 -0
  30. package/dist/lists/storage.d.ts +37 -0
  31. package/dist/lists/storage.js +66 -0
  32. package/dist/network/engine.d.ts +105 -0
  33. package/dist/network/engine.js +431 -0
  34. package/dist/network/filterIndex.d.ts +40 -0
  35. package/dist/network/filterIndex.js +343 -0
  36. package/dist/network/modifiers.d.ts +23 -0
  37. package/dist/network/modifiers.js +192 -0
  38. package/dist/network/pattern.d.ts +7 -0
  39. package/dist/network/pattern.js +145 -0
  40. package/dist/network/request.d.ts +50 -0
  41. package/dist/network/request.js +136 -0
  42. package/dist/network/tokens.d.ts +5 -0
  43. package/dist/network/tokens.js +150 -0
  44. package/dist/parser/cosmetic.d.ts +21 -0
  45. package/dist/parser/cosmetic.js +171 -0
  46. package/dist/parser/hosts.d.ts +20 -0
  47. package/dist/parser/hosts.js +143 -0
  48. package/dist/parser/index.d.ts +40 -0
  49. package/dist/parser/index.js +158 -0
  50. package/dist/parser/network.d.ts +6 -0
  51. package/dist/parser/network.js +474 -0
  52. package/dist/parser/selector.d.ts +25 -0
  53. package/dist/parser/selector.js +522 -0
  54. package/dist/parser/types.d.ts +175 -0
  55. package/dist/parser/types.js +75 -0
  56. package/dist/preload.cjs +1988 -0
  57. package/dist/psl/psl.d.ts +32 -0
  58. package/dist/psl/psl.js +119 -0
  59. package/dist/resources/binary.d.ts +9 -0
  60. package/dist/resources/binary.js +54 -0
  61. package/dist/resources/defusers.d.ts +29 -0
  62. package/dist/resources/defusers.js +180 -0
  63. package/dist/resources/documents.d.ts +12 -0
  64. package/dist/resources/documents.js +56 -0
  65. package/dist/resources/ima.d.ts +6 -0
  66. package/dist/resources/ima.js +313 -0
  67. package/dist/resources/index.d.ts +14 -0
  68. package/dist/resources/index.js +85 -0
  69. package/dist/resources/surrogates.d.ts +30 -0
  70. package/dist/resources/surrogates.js +486 -0
  71. package/dist/scriptlets/abort.d.ts +5 -0
  72. package/dist/scriptlets/abort.js +97 -0
  73. package/dist/scriptlets/dom/attributes.d.ts +5 -0
  74. package/dist/scriptlets/dom/attributes.js +92 -0
  75. package/dist/scriptlets/dom/click.d.ts +2 -0
  76. package/dist/scriptlets/dom/click.js +107 -0
  77. package/dist/scriptlets/dom/cookies.d.ts +16 -0
  78. package/dist/scriptlets/dom/cookies.js +194 -0
  79. package/dist/scriptlets/dom/createHtml.d.ts +2 -0
  80. package/dist/scriptlets/dom/createHtml.js +27 -0
  81. package/dist/scriptlets/dom/dialogs.d.ts +3 -0
  82. package/dist/scriptlets/dom/dialogs.js +21 -0
  83. package/dist/scriptlets/dom/hrefSanitizer.d.ts +2 -0
  84. package/dist/scriptlets/dom/hrefSanitizer.js +91 -0
  85. package/dist/scriptlets/dom/index.d.ts +3 -0
  86. package/dist/scriptlets/dom/index.js +38 -0
  87. package/dist/scriptlets/dom/navigation.d.ts +5 -0
  88. package/dist/scriptlets/dom/navigation.js +60 -0
  89. package/dist/scriptlets/dom/nodeText.d.ts +3 -0
  90. package/dist/scriptlets/dom/nodeText.js +98 -0
  91. package/dist/scriptlets/dom/spoofCss.d.ts +2 -0
  92. package/dist/scriptlets/dom/spoofCss.js +58 -0
  93. package/dist/scriptlets/dom/storage.d.ts +5 -0
  94. package/dist/scriptlets/dom/storage.js +50 -0
  95. package/dist/scriptlets/dom/watch.d.ts +8 -0
  96. package/dist/scriptlets/dom/watch.js +41 -0
  97. package/dist/scriptlets/events.d.ts +2 -0
  98. package/dist/scriptlets/events.js +42 -0
  99. package/dist/scriptlets/helpers.d.ts +58 -0
  100. package/dist/scriptlets/helpers.js +206 -0
  101. package/dist/scriptlets/index.d.ts +6 -0
  102. package/dist/scriptlets/index.js +47 -0
  103. package/dist/scriptlets/network/edit.d.ts +9 -0
  104. package/dist/scriptlets/network/edit.js +104 -0
  105. package/dist/scriptlets/network/eval.d.ts +3 -0
  106. package/dist/scriptlets/network/eval.js +22 -0
  107. package/dist/scriptlets/network/fetch.d.ts +10 -0
  108. package/dist/scriptlets/network/fetch.js +145 -0
  109. package/dist/scriptlets/network/index.d.ts +3 -0
  110. package/dist/scriptlets/network/index.js +43 -0
  111. package/dist/scriptlets/network/jsonPath.d.ts +13 -0
  112. package/dist/scriptlets/network/jsonPath.js +482 -0
  113. package/dist/scriptlets/network/media.d.ts +3 -0
  114. package/dist/scriptlets/network/media.js +97 -0
  115. package/dist/scriptlets/network/methods.d.ts +5 -0
  116. package/dist/scriptlets/network/methods.js +139 -0
  117. package/dist/scriptlets/network/prune.d.ts +13 -0
  118. package/dist/scriptlets/network/prune.js +194 -0
  119. package/dist/scriptlets/network/rendering.d.ts +4 -0
  120. package/dist/scriptlets/network/rendering.js +46 -0
  121. package/dist/scriptlets/network/replace.d.ts +5 -0
  122. package/dist/scriptlets/network/replace.js +168 -0
  123. package/dist/scriptlets/network/request.d.ts +19 -0
  124. package/dist/scriptlets/network/request.js +124 -0
  125. package/dist/scriptlets/network/util.d.ts +19 -0
  126. package/dist/scriptlets/network/util.js +80 -0
  127. package/dist/scriptlets/network/webrtc.d.ts +3 -0
  128. package/dist/scriptlets/network/webrtc.js +62 -0
  129. package/dist/scriptlets/network/windowOpen.d.ts +2 -0
  130. package/dist/scriptlets/network/windowOpen.js +28 -0
  131. package/dist/scriptlets/network/xhr.d.ts +9 -0
  132. package/dist/scriptlets/network/xhr.js +177 -0
  133. package/dist/scriptlets/runner.d.ts +8 -0
  134. package/dist/scriptlets/runner.js +19 -0
  135. package/dist/scriptlets/setConstant.d.ts +5 -0
  136. package/dist/scriptlets/setConstant.js +117 -0
  137. package/dist/scriptlets/timers.d.ts +5 -0
  138. package/dist/scriptlets/timers.js +66 -0
  139. package/dist/scriptlets/types.d.ts +32 -0
  140. package/dist/scriptlets/types.js +1 -0
  141. package/dist/util.d.ts +5 -0
  142. package/dist/util.js +16 -0
  143. package/package.json +83 -4
@@ -0,0 +1,474 @@
1
+ import { domainToASCII } from "node:url";
2
+ import { tryRegex } from "../util.js";
3
+ import { isHostname, normalizeHostname, parseHostList } from "./hosts.js";
4
+ import { F_BADFILTER, F_EXCEPTION, F_FIRST_PARTY, F_HOST_ANCHOR, F_IMPORTANT, F_LEFT_ANCHOR, F_MATCH_CASE, F_RIGHT_ANCHOR, F_STRICT_FIRST_PARTY, F_STRICT_THIRD_PARTY, F_THIRD_PARTY, METHODS, P_ALL, P_HOST, P_PATTERN, P_REGEX, T_ALL, T_COSMETIC, T_DOCUMENT, T_ELEMHIDE, T_FONT, T_GENERICHIDE, T_IMAGE, T_INLINE_FONT, T_INLINE_SCRIPT, T_MEDIA, T_NETWORK, T_OBJECT, T_OTHER, T_PING, T_POPUP, T_SCRIPT, T_SPECIFICHIDE, T_STYLESHEET, T_SUBDOCUMENT, T_WEBSOCKET, T_XHR, } from "./types.js";
5
+ const TYPE_OPTIONS = {
6
+ script: T_SCRIPT,
7
+ image: T_IMAGE,
8
+ stylesheet: T_STYLESHEET,
9
+ css: T_STYLESHEET,
10
+ object: T_OBJECT,
11
+ xmlhttprequest: T_XHR,
12
+ xhr: T_XHR,
13
+ subdocument: T_SUBDOCUMENT,
14
+ frame: T_SUBDOCUMENT,
15
+ ping: T_PING,
16
+ websocket: T_WEBSOCKET,
17
+ media: T_MEDIA,
18
+ font: T_FONT,
19
+ other: T_OTHER,
20
+ document: T_DOCUMENT,
21
+ doc: T_DOCUMENT,
22
+ popup: T_POPUP,
23
+ "inline-script": T_INLINE_SCRIPT,
24
+ "inline-font": T_INLINE_FONT,
25
+ all: T_ALL,
26
+ generichide: T_GENERICHIDE,
27
+ ghide: T_GENERICHIDE,
28
+ elemhide: T_ELEMHIDE,
29
+ ehide: T_ELEMHIDE,
30
+ specifichide: T_SPECIFICHIDE,
31
+ shide: T_SPECIFICHIDE,
32
+ };
33
+ // Every other option name. A `$` only starts the options when each name after
34
+ // it is a known one, so this list also decides where a pattern ends.
35
+ const OTHER_OPTIONS = new Set([
36
+ "popunder",
37
+ "genericblock",
38
+ "third-party",
39
+ "3p",
40
+ "first-party",
41
+ "1p",
42
+ "strict1p",
43
+ "strict3p",
44
+ "important",
45
+ "badfilter",
46
+ "match-case",
47
+ "cname",
48
+ "domain",
49
+ "from",
50
+ "to",
51
+ "top",
52
+ "denyallow",
53
+ "method",
54
+ "header",
55
+ "ipaddress",
56
+ "csp",
57
+ "permissions",
58
+ "redirect",
59
+ "redirect-rule",
60
+ "removeparam",
61
+ "queryprune",
62
+ "empty",
63
+ "mp4",
64
+ "rewrite",
65
+ "replace",
66
+ "uritransform",
67
+ "urltransform",
68
+ "urlskip",
69
+ "reason",
70
+ ]);
71
+ function isOptionName(name) {
72
+ return (Object.hasOwn(TYPE_OPTIONS, name) ||
73
+ OTHER_OPTIONS.has(name) ||
74
+ /^_+$/.test(name));
75
+ }
76
+ const NEXT_OPTION = /~?([a-z0-9_-]+)(?=[=,]|$)/iy;
77
+ /**
78
+ * Splits `a,b=c,d` at the commas that an option name follows, so a comma
79
+ * inside a value (a regex, a CSP) or an escaped one stays in the value.
80
+ */
81
+ function splitOptions(text) {
82
+ const parts = [];
83
+ let start = 0;
84
+ for (let i = 0; i < text.length; i++) {
85
+ const c = text.charCodeAt(i);
86
+ if (c === 0x5c /* \ */)
87
+ i++;
88
+ else if (c === 0x2c /* , */) {
89
+ NEXT_OPTION.lastIndex = i + 1;
90
+ const match = NEXT_OPTION.exec(text);
91
+ if (match === null || !isOptionName(match[1].toLowerCase()))
92
+ continue;
93
+ parts.push(text.slice(start, i));
94
+ start = i + 1;
95
+ }
96
+ }
97
+ parts.push(text.slice(start));
98
+ return parts;
99
+ }
100
+ function optionName(part) {
101
+ const equals = part.indexOf("=");
102
+ const name = equals === -1 ? part : part.slice(0, equals);
103
+ return (name.startsWith("~") ? name.slice(1) : name).trim().toLowerCase();
104
+ }
105
+ /**
106
+ * Finds where the options start: the first `$` after which every option name
107
+ * is known. A `$` can also be part of the pattern or of a value.
108
+ */
109
+ function splitFilter(text) {
110
+ let last = -1;
111
+ for (let i = text.indexOf("$"); i !== -1 && i < text.length - 1; i = text.indexOf("$", i + 1)) {
112
+ last = i;
113
+ const options = splitOptions(text.slice(i + 1));
114
+ if (options.every((part) => isOptionName(optionName(part)))) {
115
+ return { pattern: text.slice(0, i), options };
116
+ }
117
+ }
118
+ if (last !== -1) {
119
+ const tail = text.slice(last + 1);
120
+ if (/^~?[a-z0-9_-]+(?:[=,]|$)/i.test(tail)) {
121
+ const unknown = tail
122
+ .split(",")
123
+ .map(optionName)
124
+ .find((name) => !isOptionName(name));
125
+ return `unknown option: ${unknown ?? tail}`;
126
+ }
127
+ }
128
+ return { pattern: text, options: [] };
129
+ }
130
+ const invalid = (reason) => ({ kind: "invalid", reason });
131
+ const unsupported = (reason) => ({
132
+ kind: "unsupported",
133
+ reason,
134
+ });
135
+ function parseHeader(value) {
136
+ const colon = value.indexOf(":");
137
+ const name = (colon === -1 ? value : value.slice(0, colon))
138
+ .trim()
139
+ .toLowerCase();
140
+ if (!/^[a-z0-9!#$%&'*+.^_`|~-]+$/.test(name))
141
+ return undefined;
142
+ const header = {
143
+ name,
144
+ value: undefined,
145
+ regex: false,
146
+ regexFlags: "",
147
+ negated: false,
148
+ };
149
+ if (colon === -1)
150
+ return header;
151
+ let text = value.slice(colon + 1);
152
+ if (text.startsWith("~")) {
153
+ header.negated = true;
154
+ text = text.slice(1);
155
+ }
156
+ const regex = /^\/(.+)\/([a-z]*)$/.exec(text);
157
+ if (regex !== null) {
158
+ if (tryRegex(regex[1], regex[2]) === undefined)
159
+ return undefined;
160
+ header.value = regex[1];
161
+ header.regex = true;
162
+ header.regexFlags = regex[2];
163
+ }
164
+ else {
165
+ header.value = text.replaceAll("\\,", ",");
166
+ }
167
+ return header;
168
+ }
169
+ function parsePattern(text) {
170
+ if (text.length > 1 && text.startsWith("/") && text.endsWith("/")) {
171
+ const source = text.slice(1, -1);
172
+ if (tryRegex(source) === undefined)
173
+ return "bad regular expression";
174
+ return { kind: P_REGEX, text: source, flags: 0 };
175
+ }
176
+ let flags = 0;
177
+ if (text.startsWith("||")) {
178
+ flags |= F_HOST_ANCHOR;
179
+ text = text.slice(2);
180
+ }
181
+ else if (text.startsWith("|")) {
182
+ flags |= F_LEFT_ANCHOR;
183
+ text = text.slice(1);
184
+ }
185
+ if (text.endsWith("|")) {
186
+ flags |= F_RIGHT_ANCHOR;
187
+ text = text.slice(0, -1);
188
+ }
189
+ text = text.toLowerCase();
190
+ if (flags & F_HOST_ANCHOR) {
191
+ // Request URLs carry punycode hostnames.
192
+ const end = text.search(/[/^:?*]/);
193
+ const host = end === -1 ? text : text.slice(0, end);
194
+ // eslint-disable-next-line no-control-regex
195
+ if (/[^\x00-\x7f]/.test(host)) {
196
+ text = domainToASCII(host) + text.slice(host.length);
197
+ }
198
+ const hostname = text.endsWith("^") ? text.slice(0, -1) : text;
199
+ if (isHostname(hostname))
200
+ return { kind: P_HOST, text: hostname, flags: 0 };
201
+ }
202
+ else if (flags === 0 && isHostname(text)) {
203
+ // A pattern that reads as a hostname is one, as in a hosts file.
204
+ return { kind: P_HOST, text, flags: 0 };
205
+ }
206
+ text = text.replace(/\*{2,}/g, "*");
207
+ // A wildcard next to an anchor undoes it.
208
+ while (text.startsWith("*")) {
209
+ text = text.slice(1);
210
+ flags &= ~(F_HOST_ANCHOR | F_LEFT_ANCHOR);
211
+ }
212
+ while (text.endsWith("*")) {
213
+ text = text.slice(0, -1);
214
+ flags &= ~F_RIGHT_ANCHOR;
215
+ }
216
+ if (text === "")
217
+ return { kind: P_ALL, text: "", flags: 0 };
218
+ return { kind: P_PATTERN, text, flags };
219
+ }
220
+ /**
221
+ * Parses a network filter: `[@@]pattern[$options]`. `line` is trimmed and
222
+ * isn't a comment or a cosmetic filter.
223
+ */
224
+ export function parseNetworkFilter(line, context) {
225
+ let flags = 0;
226
+ let text = line;
227
+ if (text.startsWith("@@")) {
228
+ flags |= F_EXCEPTION;
229
+ text = text.slice(2);
230
+ }
231
+ const split = splitFilter(text);
232
+ if (typeof split === "string")
233
+ return invalid(split);
234
+ let types = 0;
235
+ let notTypes = 0;
236
+ let popunder = false;
237
+ let from;
238
+ let extra;
239
+ // A filter has one modifier: a second one discards it.
240
+ const modifiers = [];
241
+ const modify = (type, value, priority = 0) => modifiers.push({ type, value, priority });
242
+ for (const part of split.options) {
243
+ const equals = part.indexOf("=");
244
+ let name = (equals === -1 ? part : part.slice(0, equals))
245
+ .trim()
246
+ .toLowerCase();
247
+ const value = equals === -1 ? "" : part.slice(equals + 1).trim();
248
+ const negated = name.startsWith("~");
249
+ if (negated)
250
+ name = name.slice(1);
251
+ const type = TYPE_OPTIONS[name];
252
+ if (type !== undefined) {
253
+ if (negated)
254
+ notTypes |= type;
255
+ else
256
+ types |= type;
257
+ continue;
258
+ }
259
+ if (negated && !/^(?:third-party|first-party|3p|1p)$/.test(name)) {
260
+ return invalid(`option can't be negated: ${name}`);
261
+ }
262
+ switch (name) {
263
+ case "third-party":
264
+ case "3p":
265
+ flags |= negated ? F_FIRST_PARTY : F_THIRD_PARTY;
266
+ break;
267
+ case "first-party":
268
+ case "1p":
269
+ flags |= negated ? F_THIRD_PARTY : F_FIRST_PARTY;
270
+ break;
271
+ case "strict1p":
272
+ flags |= F_STRICT_FIRST_PARTY;
273
+ break;
274
+ case "strict3p":
275
+ flags |= F_STRICT_THIRD_PARTY;
276
+ break;
277
+ case "important":
278
+ flags |= F_IMPORTANT;
279
+ break;
280
+ case "badfilter":
281
+ flags |= F_BADFILTER;
282
+ break;
283
+ case "match-case":
284
+ flags |= F_MATCH_CASE;
285
+ break;
286
+ case "popunder":
287
+ popunder = true;
288
+ break;
289
+ case "domain":
290
+ case "from":
291
+ from = parseHostList(value, "|");
292
+ if (from === undefined)
293
+ return invalid(`bad ${name}= value`);
294
+ break;
295
+ case "to":
296
+ case "top": {
297
+ const list = parseHostList(value, "|");
298
+ if (list === undefined)
299
+ return invalid(`bad ${name}= value`);
300
+ (extra ??= {})[name] = list;
301
+ break;
302
+ }
303
+ case "denyallow": {
304
+ const hosts = value
305
+ .split("|")
306
+ .map((host) => normalizeHostname(host, true));
307
+ const valid = hosts.filter((host) => host !== undefined);
308
+ if (value === "" || valid.length !== hosts.length) {
309
+ return invalid("bad denyallow= value");
310
+ }
311
+ (extra ??= {}).denyallow = valid;
312
+ break;
313
+ }
314
+ case "method": {
315
+ extra ??= {};
316
+ for (let method of value.toLowerCase().split("|")) {
317
+ const not = method.startsWith("~");
318
+ if (not)
319
+ method = method.slice(1);
320
+ const bit = METHODS[method];
321
+ if (bit === undefined)
322
+ return invalid("bad method= value");
323
+ if (not)
324
+ extra.notMethods = (extra.notMethods ?? 0) | bit;
325
+ else
326
+ extra.methods = (extra.methods ?? 0) | bit;
327
+ }
328
+ break;
329
+ }
330
+ case "header": {
331
+ // A bare `header` in an exception lifts every header= block.
332
+ const header = value === "" && flags & F_EXCEPTION
333
+ ? {
334
+ name: "",
335
+ value: undefined,
336
+ regex: false,
337
+ regexFlags: "",
338
+ negated: false,
339
+ }
340
+ : parseHeader(value);
341
+ if (header === undefined)
342
+ return invalid("bad header= value");
343
+ (extra ??= {}).header = header;
344
+ break;
345
+ }
346
+ case "ipaddress":
347
+ if (value === "")
348
+ return invalid("bad ipaddress= value");
349
+ (extra ??= {}).ipaddress = value.startsWith("/")
350
+ ? value
351
+ : value.toLowerCase();
352
+ break;
353
+ case "csp":
354
+ if (/\breport-(?:uri|to)\b/i.test(value)) {
355
+ return invalid("csp= with a report directive");
356
+ }
357
+ modify("csp", value);
358
+ break;
359
+ case "permissions":
360
+ modify("permissions", value.replaceAll("|", ", "));
361
+ break;
362
+ case "redirect":
363
+ case "redirect-rule": {
364
+ const priority = /^(.+):(-?\d+)$/.exec(value);
365
+ if (priority === null)
366
+ modify(name, value);
367
+ else
368
+ modify(name, priority[1], Number(priority[2]));
369
+ break;
370
+ }
371
+ case "empty":
372
+ modify("redirect", "empty");
373
+ break;
374
+ case "mp4":
375
+ modify("redirect", "noopmp4-1s");
376
+ if (types === 0)
377
+ types = T_MEDIA;
378
+ break;
379
+ case "rewrite":
380
+ // Adblock Plus's name for redirect=.
381
+ if (!value.startsWith("abp-resource:")) {
382
+ return invalid("bad rewrite= value");
383
+ }
384
+ modify("redirect", value.slice("abp-resource:".length));
385
+ break;
386
+ case "removeparam":
387
+ case "queryprune":
388
+ modify("removeparam", value);
389
+ break;
390
+ case "uritransform":
391
+ case "urltransform":
392
+ case "urlskip":
393
+ if (!context.trusted)
394
+ return invalid(`${name}= in an untrusted list`);
395
+ modify(name === "urlskip" ? "urlskip" : "uritransform", value);
396
+ break;
397
+ case "replace":
398
+ return unsupported("replace= (Electron can't rewrite response bodies)");
399
+ case "cname":
400
+ return unsupported("cname (Chromium has no CNAME uncloaking)");
401
+ case "genericblock":
402
+ return invalid("genericblock (not supported, as in uBlock Origin)");
403
+ case "reason":
404
+ break;
405
+ default:
406
+ // `_`, a placeholder.
407
+ break;
408
+ }
409
+ }
410
+ const pattern = parsePattern(split.pattern);
411
+ if (typeof pattern === "string")
412
+ return invalid(pattern);
413
+ flags |= pattern.flags;
414
+ const exception = (flags & F_EXCEPTION) !== 0;
415
+ if (flags & F_MATCH_CASE && pattern.kind !== P_REGEX) {
416
+ return invalid("match-case on a filter that isn't a regular expression");
417
+ }
418
+ if (exception)
419
+ flags &= ~F_IMPORTANT;
420
+ if (extra?.denyallow !== undefined && from === undefined) {
421
+ return invalid("denyallow= without domain=");
422
+ }
423
+ if (popunder && types === 0) {
424
+ return unsupported("popunder (needs a browser's tabs)");
425
+ }
426
+ const explicitTypes = types !== 0 || notTypes !== 0;
427
+ if (types === 0) {
428
+ types = T_NETWORK;
429
+ // A filter that is only a hostname blocks pages on it too.
430
+ if (pattern.kind === P_HOST && notTypes === 0 && modifiers.length === 0) {
431
+ types |= T_DOCUMENT;
432
+ }
433
+ }
434
+ types &= ~notTypes;
435
+ if (types & T_COSMETIC && !exception) {
436
+ return invalid("generichide, specifichide and elemhide need an exception");
437
+ }
438
+ if (modifiers.length > 1)
439
+ return invalid("two modifiers");
440
+ const mod = modifiers[0];
441
+ if (mod !== undefined) {
442
+ if (mod.type === "redirect" && exception)
443
+ mod.type = "redirect-rule";
444
+ const needsValue = mod.type === "urlskip" ||
445
+ mod.type === "uritransform" ||
446
+ (!exception && mod.type !== "removeparam");
447
+ if (needsValue && mod.value === "")
448
+ return invalid(`${mod.type}= value`);
449
+ if (mod.type === "csp" || mod.type === "permissions") {
450
+ types = explicitTypes
451
+ ? types & (T_DOCUMENT | T_SUBDOCUMENT)
452
+ : T_DOCUMENT | T_SUBDOCUMENT;
453
+ }
454
+ else if (!explicitTypes &&
455
+ mod.type !== "redirect" &&
456
+ mod.type !== "redirect-rule") {
457
+ // removeparam, urlskip and uritransform clean navigations too.
458
+ types |= T_DOCUMENT;
459
+ }
460
+ }
461
+ if (types === 0)
462
+ return invalid("no request type left");
463
+ return {
464
+ kind: "network",
465
+ raw: line,
466
+ patternKind: pattern.kind,
467
+ pattern: pattern.text,
468
+ flags,
469
+ types,
470
+ from,
471
+ modifier: mod,
472
+ extra,
473
+ };
474
+ }
@@ -0,0 +1,25 @@
1
+ import type { ProceduralPlan } from "./types.js";
2
+ /** What a cosmetic filter's selector compiles to. */
3
+ export interface CompiledSelector {
4
+ /** A plain CSS selector, when `procedural` is undefined. */
5
+ selector: string;
6
+ style: string | undefined;
7
+ media: string | undefined;
8
+ procedural: ProceduralPlan | undefined;
9
+ }
10
+ /**
11
+ * Compiles a cosmetic filter's selector. `force` (`#?#`) keeps `:has()` and
12
+ * `:not()` procedural even when the browser could match them itself.
13
+ * A selector list with procedural parts gives one result per procedural part
14
+ * and one for the plain parts together. Returns the reason when the selector
15
+ * can't be used.
16
+ */
17
+ export declare function compileSelector(text: string, force?: boolean): CompiledSelector[] | string;
18
+ /**
19
+ * A class or id a generic selector needs in the page, so the selector can be
20
+ * looked up by what the page uses: `.ad-box` and `div > #ad-box a` have one,
21
+ * `[href^="ad"]` doesn't. A selector list, or one with `:not()` and the like,
22
+ * may not need its first class or id, and an escaped name isn't the page's
23
+ * text, so those have none either.
24
+ */
25
+ export declare function selectorKey(selector: string): string | undefined;