@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,522 @@
1
+ import { tryRegex } from "../util.js";
2
+ const ALIASES = {
3
+ "-abp-has": "has",
4
+ if: "has",
5
+ "if-not": "not",
6
+ "-abp-contains": "has-text",
7
+ contains: "has-text",
8
+ "nth-ancestor": "upward",
9
+ };
10
+ // Operators that always need JavaScript. `has` and `not` do when their
11
+ // argument does, or when the filter says so with `#?#`.
12
+ const PROCEDURAL = new Set([
13
+ "has-text",
14
+ "matches-attr",
15
+ "matches-css",
16
+ "matches-css-before",
17
+ "matches-css-after",
18
+ "matches-media",
19
+ "matches-path",
20
+ "matches-prop",
21
+ "min-text-length",
22
+ "others",
23
+ "upward",
24
+ "watch-attr",
25
+ "xpath",
26
+ "style",
27
+ "remove",
28
+ "remove-attr",
29
+ "remove-class",
30
+ ]);
31
+ const ACTIONS = new Set(["style", "remove", "remove-attr", "remove-class"]);
32
+ // Their argument is free text, where a quote is only a quote.
33
+ const TEXT_ARGUMENT = new Set([
34
+ "has-text",
35
+ "matches-attr",
36
+ "matches-css",
37
+ "matches-css-before",
38
+ "matches-css-after",
39
+ "matches-path",
40
+ "matches-prop",
41
+ "min-text-length",
42
+ "others",
43
+ "watch-attr",
44
+ "remove",
45
+ "remove-attr",
46
+ "remove-class",
47
+ ]);
48
+ function skipString(text, start) {
49
+ const quote = text[start];
50
+ for (let i = start + 1; i < text.length; i++) {
51
+ if (text[i] === "\\")
52
+ i++;
53
+ else if (text[i] === quote)
54
+ return i + 1;
55
+ }
56
+ return -1;
57
+ }
58
+ function skipBrackets(text, start) {
59
+ for (let i = start + 1; i < text.length; i++) {
60
+ const c = text[i];
61
+ if (c === "\\")
62
+ i++;
63
+ else if (c === '"' || c === "'") {
64
+ i = skipString(text, i) - 1;
65
+ if (i < 0)
66
+ return -1;
67
+ }
68
+ else if (c === "]")
69
+ return i + 1;
70
+ }
71
+ return -1;
72
+ }
73
+ /** The index of the `)` that closes an operator's argument, or -1. */
74
+ function findArgumentEnd(text, start, name) {
75
+ let first = start;
76
+ while (text[first] === " ")
77
+ first++;
78
+ const quote = text[first];
79
+ if (quote === '"' || quote === "'") {
80
+ let after = skipString(text, first);
81
+ if (after !== -1) {
82
+ while (text[after] === " ")
83
+ after++;
84
+ if (text[after] === ")")
85
+ return after;
86
+ }
87
+ }
88
+ else if (quote === "/" && TEXT_ARGUMENT.has(name)) {
89
+ // A regular expression may hold a lone parenthesis.
90
+ for (let i = first + 1; i < text.length; i++) {
91
+ if (text[i] === "\\")
92
+ i++;
93
+ else if (text[i] === "/") {
94
+ let after = i + 1;
95
+ while (/[a-z]/.test(text[after] ?? ""))
96
+ after++;
97
+ if (text[after] === ")")
98
+ return after;
99
+ }
100
+ }
101
+ }
102
+ const strings = !TEXT_ARGUMENT.has(name);
103
+ let depth = 1;
104
+ for (let i = start; i < text.length; i++) {
105
+ const c = text[i];
106
+ if (c === "\\")
107
+ i++;
108
+ else if (strings && (c === '"' || c === "'")) {
109
+ i = skipString(text, i) - 1;
110
+ if (i < 0)
111
+ return -1;
112
+ }
113
+ else if (c === "(")
114
+ depth++;
115
+ else if (c === ")" && --depth === 0)
116
+ return i;
117
+ }
118
+ return -1;
119
+ }
120
+ const PSEUDO = /::?(-?[a-z][\w-]*)\(/iy;
121
+ /**
122
+ * Splits a selector at its top-level procedural operators. The CSS between
123
+ * them, native pseudo-classes included, stays as text.
124
+ */
125
+ function splitOperators(text, force) {
126
+ const parts = [];
127
+ // Where the top-level commas of a selector list are.
128
+ const commas = [];
129
+ let cssStart = 0;
130
+ let i = 0;
131
+ while (i < text.length) {
132
+ const c = text[i];
133
+ if (c === "\\") {
134
+ i += 2;
135
+ }
136
+ else if (c === '"' || c === "'") {
137
+ i = skipString(text, i);
138
+ if (i === -1)
139
+ return "unbalanced quote";
140
+ }
141
+ else if (c === "[") {
142
+ i = skipBrackets(text, i);
143
+ if (i === -1)
144
+ return "unbalanced bracket";
145
+ }
146
+ else if (c === ":") {
147
+ PSEUDO.lastIndex = i;
148
+ const match = PSEUDO.exec(text);
149
+ if (match === null) {
150
+ i++;
151
+ continue;
152
+ }
153
+ const raw = match[1].toLowerCase();
154
+ const name = ALIASES[raw] ?? raw;
155
+ const argStart = i + match[0].length;
156
+ const argEnd = findArgumentEnd(text, argStart, name);
157
+ if (argEnd === -1)
158
+ return "unbalanced parenthesis";
159
+ const arg = text.slice(argStart, argEnd);
160
+ let procedural = PROCEDURAL.has(name);
161
+ if (!procedural) {
162
+ // A native pseudo-class: procedural when its argument is.
163
+ const inner = splitOperators(arg, false);
164
+ if (typeof inner === "string")
165
+ return inner;
166
+ const nested = inner.parts.some((part) => part.type === "op");
167
+ if (name === "has" || name === "not")
168
+ procedural = force || nested;
169
+ else if (nested)
170
+ return `procedural operator inside :${name}()`;
171
+ }
172
+ if (procedural) {
173
+ if (i > cssStart) {
174
+ parts.push({ type: "css", text: text.slice(cssStart, i) });
175
+ }
176
+ parts.push({ type: "op", name, arg });
177
+ cssStart = argEnd + 1;
178
+ }
179
+ i = argEnd + 1;
180
+ }
181
+ else if (c === "(" || c === ")") {
182
+ return "unbalanced parenthesis";
183
+ }
184
+ else if (c === "{" || c === "}") {
185
+ return "braces in a selector";
186
+ }
187
+ else {
188
+ if (c === ",")
189
+ commas.push(i);
190
+ i++;
191
+ }
192
+ }
193
+ if (cssStart < text.length) {
194
+ parts.push({ type: "css", text: text.slice(cssStart) });
195
+ }
196
+ return { parts, commas };
197
+ }
198
+ /** Why a plain CSS selector can't be used, or undefined when it can. */
199
+ function plainSelectorError(selector) {
200
+ if (selector.trim() === "")
201
+ return "empty selector";
202
+ // eslint-disable-next-line no-control-regex
203
+ if (/[\x00-\x1f]/.test(selector))
204
+ return "control character in a selector";
205
+ if (selector.includes("/*"))
206
+ return "comment in a selector";
207
+ return undefined;
208
+ }
209
+ function unquote(text) {
210
+ const quote = text[0];
211
+ if (text.length < 2 ||
212
+ (quote !== '"' && quote !== "'") ||
213
+ !text.endsWith(quote)) {
214
+ return undefined;
215
+ }
216
+ // Only the quote and the backslash are escaped: `\w` in a quoted regular
217
+ // expression stays as it is.
218
+ return text.slice(1, -1).replace(/\\(["'\\])/g, "$1");
219
+ }
220
+ /** A literal, quoted or bare, or `/regex/flags`, quoted or bare. */
221
+ function compileTextMatcher(text) {
222
+ text = unquote(text.trim()) ?? text.trim();
223
+ // `/shop/item` is a path, not a regular expression with the flags `item`.
224
+ const regex = /^\/(.+)\/([dgimsuvy]*)$/.exec(text);
225
+ if (regex === null)
226
+ return text;
227
+ if (tryRegex(regex[1], regex[2]) === undefined)
228
+ return undefined;
229
+ return [regex[1], regex[2]];
230
+ }
231
+ /** Whether `:style()` declarations are free of what uBlock Origin forbids. */
232
+ function styleError(style) {
233
+ if (style.trim() === "")
234
+ return "empty :style()";
235
+ if (/url\s*\(|image-set\s*\(|\/\*|\*\/|\\|\/\s*\/|[{}]|@import/i.test(style)) {
236
+ return "forbidden value in :style()";
237
+ }
238
+ return undefined;
239
+ }
240
+ function compileOperator(name, arg, force) {
241
+ switch (name) {
242
+ case "has":
243
+ case "not": {
244
+ const inner = splitOperators(arg, force);
245
+ if (typeof inner === "string")
246
+ return inner;
247
+ const tasks = [];
248
+ for (const part of inner.parts) {
249
+ if (part.type === "op") {
250
+ if (ACTIONS.has(part.name))
251
+ return `:${part.name}() inside :${name}()`;
252
+ const task = compileOperator(part.name, part.arg, force);
253
+ if (typeof task === "string")
254
+ return task;
255
+ tasks.push(task);
256
+ continue;
257
+ }
258
+ let css = part.text;
259
+ const first = tasks.length === 0;
260
+ if (first)
261
+ css = css.trimStart();
262
+ if (css.trim() === "")
263
+ continue;
264
+ if (first && inner.commas.length > 0) {
265
+ if (inner.parts.length > 1) {
266
+ return "selector list with procedural operators";
267
+ }
268
+ css = `:is(${css})`;
269
+ }
270
+ // `:has()` looks below its subject, `:not()` at the subject itself.
271
+ if (first &&
272
+ name === "has" &&
273
+ !/^[>+~]/.test(css) &&
274
+ !css.includes(":scope")) {
275
+ css = ` ${css}`;
276
+ }
277
+ tasks.push(["spath", css]);
278
+ }
279
+ if (tasks.length === 0)
280
+ return `empty :${name}()`;
281
+ return [name, { selector: "", tasks }];
282
+ }
283
+ case "has-text":
284
+ case "matches-path": {
285
+ const matcher = compileTextMatcher(arg);
286
+ return matcher === undefined
287
+ ? `bad :${name}() argument`
288
+ : [name, matcher];
289
+ }
290
+ case "matches-attr":
291
+ case "matches-prop": {
292
+ // name="value", "name"="value" or name alone.
293
+ const match = /^\s*("(?:\\.|[^"])*"|'(?:\\.|[^'])*'|\/.+\/[a-z]*|[^=]+?)\s*(?:=\s*(.+))?$/s.exec(arg);
294
+ if (match === null)
295
+ return `bad :${name}() argument`;
296
+ const key = compileTextMatcher(match[1]);
297
+ const value = match[2] === undefined ? undefined : compileTextMatcher(match[2]);
298
+ if (key === undefined ||
299
+ (match[2] !== undefined && value === undefined)) {
300
+ return `bad :${name}() argument`;
301
+ }
302
+ return [name, value === undefined ? { name: key } : { name: key, value }];
303
+ }
304
+ case "matches-css":
305
+ case "matches-css-before":
306
+ case "matches-css-after": {
307
+ const colon = arg.indexOf(":");
308
+ const property = arg.slice(0, colon).trim();
309
+ const value = compileTextMatcher(arg.slice(colon + 1));
310
+ if (colon === -1 ||
311
+ !/^-?[a-z-]+$/i.test(property) ||
312
+ value === undefined) {
313
+ return `bad :${name}() argument`;
314
+ }
315
+ const pseudo = name === "matches-css" ? "" : `::${name.slice("matches-css-".length)}`;
316
+ return ["matches-css", { name: property, pseudo, value }];
317
+ }
318
+ case "matches-media":
319
+ if (arg.trim() === "" || /[{};]/.test(arg)) {
320
+ return "bad :matches-media() argument";
321
+ }
322
+ return [name, arg.trim()];
323
+ case "min-text-length": {
324
+ const length = Number(arg.trim());
325
+ if (!Number.isInteger(length) || length < 0) {
326
+ return "bad :min-text-length() argument";
327
+ }
328
+ return [name, length];
329
+ }
330
+ case "others":
331
+ return [name, ""];
332
+ case "upward": {
333
+ const text = arg.trim();
334
+ if (/^\d+$/.test(text)) {
335
+ const distance = Number(text);
336
+ if (distance < 1 || distance > 255)
337
+ return "bad :upward() argument";
338
+ return [name, distance];
339
+ }
340
+ const error = plainSelectorError(text);
341
+ return error === undefined ? [name, text] : error;
342
+ }
343
+ case "watch-attr":
344
+ return [
345
+ name,
346
+ arg
347
+ .split(",")
348
+ .map((attribute) => attribute.trim())
349
+ .filter((attribute) => attribute !== ""),
350
+ ];
351
+ case "xpath":
352
+ return arg.trim() === "" ? "empty :xpath()" : [name, arg.trim()];
353
+ default:
354
+ return `unknown operator :${name}()`;
355
+ }
356
+ }
357
+ const LIST = "selector list with procedural operators";
358
+ /**
359
+ * Compiles a cosmetic filter's selector. `force` (`#?#`) keeps `:has()` and
360
+ * `:not()` procedural even when the browser could match them itself.
361
+ * A selector list with procedural parts gives one result per procedural part
362
+ * and one for the plain parts together. Returns the reason when the selector
363
+ * can't be used.
364
+ */
365
+ export function compileSelector(text, force = false) {
366
+ text = text.trim();
367
+ const split = splitOperators(text, force);
368
+ if (typeof split === "string")
369
+ return split;
370
+ const whole = compileOne(text, split, force);
371
+ if (whole !== LIST)
372
+ return typeof whole === "string" ? whole : [whole];
373
+ const compiled = [];
374
+ const plain = [];
375
+ let start = 0;
376
+ for (const end of [...split.commas, text.length]) {
377
+ const part = text.slice(start, end);
378
+ start = end + 1;
379
+ const partSplit = splitOperators(part, force);
380
+ if (typeof partSplit === "string")
381
+ return partSplit;
382
+ const one = compileOne(part.trim(), partSplit, force);
383
+ if (typeof one === "string")
384
+ return one;
385
+ if (one.procedural === undefined &&
386
+ one.style === undefined &&
387
+ one.media === undefined) {
388
+ plain.push(one.selector);
389
+ }
390
+ else
391
+ compiled.push(one);
392
+ }
393
+ if (plain.length > 0) {
394
+ compiled.unshift({
395
+ selector: plain.join(", "),
396
+ style: undefined,
397
+ media: undefined,
398
+ procedural: undefined,
399
+ });
400
+ }
401
+ return compiled;
402
+ }
403
+ function compileOne(text, split, force) {
404
+ const { parts } = split;
405
+ const ops = parts.filter((part) => part.type === "op");
406
+ if (ops.length === 0) {
407
+ const error = plainSelectorError(text);
408
+ if (error !== undefined)
409
+ return error;
410
+ return {
411
+ selector: text,
412
+ style: undefined,
413
+ media: undefined,
414
+ procedural: undefined,
415
+ };
416
+ }
417
+ // `a, b:style()` restyles both; any other operator belongs to its part.
418
+ if (split.commas.length > 0 && ops.some((op) => !ACTIONS.has(op.name))) {
419
+ return LIST;
420
+ }
421
+ const plan = { selector: "", tasks: [] };
422
+ const first = parts[0];
423
+ let rest = parts;
424
+ if (first.type === "css") {
425
+ const error = plainSelectorError(first.text);
426
+ if (error !== undefined)
427
+ return error;
428
+ plan.selector = first.text.trim();
429
+ rest = parts.slice(1);
430
+ }
431
+ for (const [index, part] of rest.entries()) {
432
+ if (plan.action !== undefined)
433
+ return "operator after an action operator";
434
+ if (part.type === "css") {
435
+ if (part.text.trim() === "")
436
+ continue;
437
+ // What follows an operator continues from the elements it kept.
438
+ plan.tasks.push(["spath", part.text.trimEnd()]);
439
+ continue;
440
+ }
441
+ if (!ACTIONS.has(part.name)) {
442
+ const task = compileOperator(part.name, part.arg, force);
443
+ if (typeof task === "string")
444
+ return task;
445
+ plan.tasks.push(task);
446
+ continue;
447
+ }
448
+ if (index !== rest.length - 1)
449
+ return "operator after an action operator";
450
+ if (part.name === "style") {
451
+ const error = styleError(part.arg);
452
+ if (error !== undefined)
453
+ return error;
454
+ plan.action = ["style", part.arg.trim()];
455
+ }
456
+ else if (part.name === "remove") {
457
+ if (part.arg.trim() !== "")
458
+ return "bad :remove() argument";
459
+ plan.action = ["remove", ""];
460
+ }
461
+ else {
462
+ const matcher = compileTextMatcher(part.arg);
463
+ if (matcher === undefined || matcher === "") {
464
+ return `bad :${part.name}() argument`;
465
+ }
466
+ plan.action = [
467
+ part.name === "remove-attr" ? "remove-attr" : "remove-class",
468
+ typeof matcher === "string" ? matcher : `/${matcher[0]}/${matcher[1]}`,
469
+ ];
470
+ }
471
+ }
472
+ // A plain selector that only restyles, or only applies to some media, needs
473
+ // no JavaScript: a stylesheet does it.
474
+ const style = plan.action?.[0] === "style" ? plan.action[1] : undefined;
475
+ const declarative = plan.action === undefined || style !== undefined;
476
+ const only = plan.tasks.length === 1 ? plan.tasks[0] : undefined;
477
+ if (plan.selector !== "" && declarative) {
478
+ if (plan.tasks.length === 0) {
479
+ return {
480
+ selector: plan.selector,
481
+ style,
482
+ media: undefined,
483
+ procedural: undefined,
484
+ };
485
+ }
486
+ if (only?.[0] === "matches-media" && typeof only[1] === "string") {
487
+ return {
488
+ selector: plan.selector,
489
+ style,
490
+ media: only[1],
491
+ procedural: undefined,
492
+ };
493
+ }
494
+ }
495
+ if (plan.selector === "" && plan.tasks.length === 0)
496
+ return "empty selector";
497
+ return { selector: "", style: undefined, media: undefined, procedural: plan };
498
+ }
499
+ /**
500
+ * A class or id a generic selector needs in the page, so the selector can be
501
+ * looked up by what the page uses: `.ad-box` and `div > #ad-box a` have one,
502
+ * `[href^="ad"]` doesn't. A selector list, or one with `:not()` and the like,
503
+ * may not need its first class or id, and an escaped name isn't the page's
504
+ * text, so those have none either.
505
+ */
506
+ export function selectorKey(selector) {
507
+ if (/[,(\\]/.test(selector))
508
+ return undefined;
509
+ for (let i = 0; i < selector.length; i++) {
510
+ const c = selector[i];
511
+ if (c === "[") {
512
+ i = skipBrackets(selector, i) - 1;
513
+ if (i < 0)
514
+ return undefined;
515
+ }
516
+ else if (c === "." || c === "#") {
517
+ const name = /^[\w-]+/.exec(selector.slice(i + 1));
518
+ return name === null ? undefined : c + name[0];
519
+ }
520
+ }
521
+ return undefined;
522
+ }
@@ -0,0 +1,175 @@
1
+ export declare const T_DOCUMENT: number;
2
+ export declare const T_SUBDOCUMENT: number;
3
+ export declare const T_STYLESHEET: number;
4
+ export declare const T_SCRIPT: number;
5
+ export declare const T_IMAGE: number;
6
+ export declare const T_FONT: number;
7
+ export declare const T_OBJECT: number;
8
+ export declare const T_XHR: number;
9
+ export declare const T_PING: number;
10
+ export declare const T_MEDIA: number;
11
+ export declare const T_WEBSOCKET: number;
12
+ export declare const T_OTHER: number;
13
+ export declare const T_POPUP: number;
14
+ export declare const T_INLINE_SCRIPT: number;
15
+ export declare const T_INLINE_FONT: number;
16
+ export declare const T_GENERICHIDE: number;
17
+ export declare const T_SPECIFICHIDE: number;
18
+ export declare const T_ELEMHIDE: number;
19
+ /** What a filter without a type option applies to. */
20
+ export declare const T_NETWORK: number;
21
+ /** The `all` option. */
22
+ export declare const T_ALL: number;
23
+ export declare const T_COSMETIC: number;
24
+ export declare const F_EXCEPTION: number;
25
+ export declare const F_IMPORTANT: number;
26
+ export declare const F_BADFILTER: number;
27
+ export declare const F_MATCH_CASE: number;
28
+ /** `||`: the pattern starts at a hostname label. */
29
+ export declare const F_HOST_ANCHOR: number;
30
+ /** `|` at the start: the pattern starts where the URL does. */
31
+ export declare const F_LEFT_ANCHOR: number;
32
+ /** `|` at the end: the pattern ends where the URL does. */
33
+ export declare const F_RIGHT_ANCHOR: number;
34
+ export declare const F_FIRST_PARTY: number;
35
+ export declare const F_THIRD_PARTY: number;
36
+ export declare const F_STRICT_FIRST_PARTY: number;
37
+ export declare const F_STRICT_THIRD_PARTY: number;
38
+ export declare const METHODS: Readonly<Record<string, number>>;
39
+ /**
40
+ * - `P_ALL`: every URL (`*` or no pattern at all).
41
+ * - `P_HOST`: `||hostname^`: the request's hostname is that one or one below
42
+ * it. Hosts file lines and bare hostnames become this too.
43
+ * - `P_PATTERN`: an Adblock Plus pattern (`*`, `^`), lowercased, without its
44
+ * anchors (they are flags).
45
+ * - `P_REGEX`: the source of a regular expression.
46
+ */
47
+ export declare const P_ALL = 0;
48
+ export declare const P_HOST = 1;
49
+ export declare const P_PATTERN = 2;
50
+ export declare const P_REGEX = 3;
51
+ export type PatternKind = typeof P_ALL | typeof P_HOST | typeof P_PATTERN | typeof P_REGEX;
52
+ /**
53
+ * A list of hostnames a filter is limited to. Entries are lowercase:
54
+ * `example.com` (it and its subdomains), `example.*` (an entity: any public
55
+ * suffix), `/regex/` (tested against the hostname) or `*` (everywhere, only in
56
+ * cosmetic filters). A hostname matches when no `exclude` entry matches and,
57
+ * if there are `include` entries, one of them does.
58
+ */
59
+ export interface HostList {
60
+ include: string[];
61
+ exclude: string[];
62
+ }
63
+ export type ModifierType = "redirect" | "redirect-rule" | "removeparam" | "csp" | "permissions" | "urlskip" | "uritransform";
64
+ /** What a modifier filter does to a request instead of blocking it. */
65
+ export interface Modifier {
66
+ type: ModifierType;
67
+ /** Empty when the option had no value (`removeparam`, `@@…$csp`). */
68
+ value: string;
69
+ /** `redirect=name:priority`. */
70
+ priority: number;
71
+ }
72
+ export interface HeaderMatch {
73
+ /** Lowercase header name; empty in a bare `header` exception. */
74
+ name: string;
75
+ /** Undefined: the header only has to be present. */
76
+ value: string | undefined;
77
+ /** The value is the source of a regular expression. */
78
+ regex: boolean;
79
+ regexFlags: string;
80
+ /** `~value`: matches when the value doesn't. */
81
+ negated: boolean;
82
+ }
83
+ /** The options few filters carry, kept off the rule to keep rules small. */
84
+ export interface ExtraOptions {
85
+ to?: HostList;
86
+ top?: HostList;
87
+ denyallow?: string[];
88
+ /** `method=`: bits of `METHODS`. Zero means no constraint. */
89
+ methods?: number;
90
+ notMethods?: number;
91
+ header?: HeaderMatch;
92
+ ipaddress?: string;
93
+ }
94
+ export interface NetworkRule {
95
+ kind: "network";
96
+ /** The filter as written, for reports. */
97
+ raw: string;
98
+ patternKind: PatternKind;
99
+ pattern: string;
100
+ flags: number;
101
+ types: number;
102
+ /** `domain=` / `from=`. */
103
+ from: HostList | undefined;
104
+ modifier: Modifier | undefined;
105
+ extra: ExtraOptions | undefined;
106
+ }
107
+ /** One step of a procedural selector: an operator and its argument. */
108
+ export type ProceduralTask = [op: string, arg: unknown];
109
+ /**
110
+ * A procedural cosmetic filter, compiled to plain data the page-side engine
111
+ * runs: start from `selector` (empty: the document), apply each task to the
112
+ * set of elements, then `action` to what is left (no action: hide).
113
+ */
114
+ export interface ProceduralPlan {
115
+ selector: string;
116
+ tasks: ProceduralTask[];
117
+ action?: [
118
+ op: "style" | "remove" | "remove-attr" | "remove-class",
119
+ arg: string
120
+ ];
121
+ }
122
+ export interface CosmeticRule {
123
+ kind: "cosmetic";
124
+ raw: string;
125
+ exception: boolean;
126
+ hosts: HostList;
127
+ /** The filter's body as written: an exception cancels the same body. */
128
+ key: string;
129
+ /** A plain CSS selector, when the filter needs no JavaScript. */
130
+ selector: string;
131
+ /** `:style()` declarations for a plain selector. Undefined: hide. */
132
+ style: string | undefined;
133
+ /** `:matches-media()` on a plain selector, as a media query. */
134
+ media: string | undefined;
135
+ /** Set when the filter is procedural. */
136
+ procedural: ProceduralPlan | undefined;
137
+ }
138
+ export interface ScriptletRule {
139
+ kind: "scriptlet";
140
+ raw: string;
141
+ exception: boolean;
142
+ hosts: HostList;
143
+ /** The token as written, without `.js`. Empty in `#@#+js()`. */
144
+ name: string;
145
+ args: string[];
146
+ /** The rule comes from a list allowed to use trusted scriptlets. */
147
+ trusted: boolean;
148
+ }
149
+ /** `##^responseheader(name)`: remove a response header from documents. */
150
+ export interface ResponseHeaderRule {
151
+ kind: "responseheader";
152
+ raw: string;
153
+ exception: boolean;
154
+ hosts: HostList;
155
+ header: string;
156
+ }
157
+ export type Rule = NetworkRule | CosmeticRule | ScriptletRule | ResponseHeaderRule;
158
+ /** What a line is when it isn't a rule. */
159
+ export interface NotARule {
160
+ /**
161
+ * - `comment`: a comment, a header or an empty line.
162
+ * - `unsupported`: valid syntax the library can't apply in Electron.
163
+ * - `invalid`: malformed, or discarded as uBlock Origin documents.
164
+ */
165
+ kind: "comment" | "unsupported" | "invalid";
166
+ reason: string;
167
+ }
168
+ export type ParsedLine = Rule | NotARule;
169
+ export interface ParseContext {
170
+ /**
171
+ * The list may use what uBlock Origin keeps for its own lists: trusted
172
+ * scriptlets and the `urlskip`, `uritransform` and `replace` options.
173
+ */
174
+ trusted: boolean;
175
+ }