Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 } | 77 } |
78 }; | 78 }; |
79 | 79 |
80 /** | 80 /** |
81 * Cache for known filters, maps string representation to filter objects. | 81 * Cache for known filters, maps string representation to filter objects. |
82 * @type {Map.<string,Filter>} | 82 * @type {Map.<string,Filter>} |
83 */ | 83 */ |
84 Filter.knownFilters = new Map(); | 84 Filter.knownFilters = new Map(); |
85 | 85 |
86 /** | 86 /** |
87 * Regular expression that code injection filters should match | 87 * Regular expression that content filters should match |
88 * @type {RegExp} | 88 * @type {RegExp} |
89 */ | 89 */ |
90 Filter.codeInjectionRegExp = /^([^/*|@"!]*?)#([@?$])?#(.+)$/; | 90 Filter.contentRegExp = /^([^/*|@"!]*?)#([@?$])?#(.+)$/; |
91 /** | 91 /** |
92 * Regular expression that RegExp filters specified as RegExps should match | 92 * Regular expression that RegExp filters specified as RegExps should match |
93 * @type {RegExp} | 93 * @type {RegExp} |
94 */ | 94 */ |
95 Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^, \s]+)?)*)?$/; | 95 Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^, \s]+)?)*)?$/; |
96 /** | 96 /** |
97 * Regular expression that options on a RegExp filter should match | 97 * Regular expression that options on a RegExp filter should match |
98 * @type {RegExp} | 98 * @type {RegExp} |
99 */ | 99 */ |
100 Filter.optionsRegExp = /\$(~?[\w-]+(?:=[^,]+)?(?:,~?[\w-]+(?:=[^,]+)?)*)$/; | 100 Filter.optionsRegExp = /\$(~?[\w-]+(?:=[^,]+)?(?:,~?[\w-]+(?:=[^,]+)?)*)$/; |
101 /** | 101 /** |
102 * Regular expression that matches an invalid Content Security Policy | 102 * Regular expression that matches an invalid Content Security Policy |
103 * @type {RegExp} | 103 * @type {RegExp} |
104 */ | 104 */ |
105 Filter.invalidCSPRegExp = /(;|^) ?(base-uri|referrer|report-to|report-uri|upgrad e-insecure-requests)\b/i; | 105 Filter.invalidCSPRegExp = /(;|^) ?(base-uri|referrer|report-to|report-uri|upgrad e-insecure-requests)\b/i; |
106 | 106 |
107 /** | 107 /** |
108 * Creates a filter of correct type from its text representation - does the | 108 * Creates a filter of correct type from its text representation - does the |
109 * basic parsing and calls the right constructor then. | 109 * basic parsing and calls the right constructor then. |
110 * | 110 * |
111 * @param {string} text as in Filter() | 111 * @param {string} text as in Filter() |
112 * @return {Filter} | 112 * @return {Filter} |
113 */ | 113 */ |
114 Filter.fromText = function(text) | 114 Filter.fromText = function(text) |
115 { | 115 { |
116 let filter = Filter.knownFilters.get(text); | 116 let filter = Filter.knownFilters.get(text); |
117 if (filter) | 117 if (filter) |
118 return filter; | 118 return filter; |
119 | 119 |
120 let match = text.includes("#") ? Filter.codeInjectionRegExp.exec(text) : null; | 120 let match = text.includes("#") ? Filter.contentRegExp.exec(text) : null; |
121 if (match) | 121 if (match) |
122 { | 122 { |
123 let propsMatch; | 123 let propsMatch; |
124 if (!match[2] && | 124 if (!match[2] && |
125 (propsMatch = /\[-abp-properties=(["'])([^"']+)\1\]/.exec(match[3]))) | 125 (propsMatch = /\[-abp-properties=(["'])([^"']+)\1\]/.exec(match[3]))) |
126 { | 126 { |
127 // This is legacy CSS properties syntax, convert to current syntax | 127 // This is legacy CSS properties syntax, convert to current syntax |
128 let prefix = match[3].substr(0, propsMatch.index); | 128 let prefix = match[3].substr(0, propsMatch.index); |
129 let expression = propsMatch[2]; | 129 let expression = propsMatch[2]; |
130 let suffix = match[3].substr(propsMatch.index + propsMatch[0].length); | 130 let suffix = match[3].substr(propsMatch.index + propsMatch[0].length); |
131 return Filter.fromText(`${match[1]}#?#` + | 131 return Filter.fromText(`${match[1]}#?#` + |
132 `${prefix}:-abp-properties(${expression})${suffix}`); | 132 `${prefix}:-abp-properties(${expression})${suffix}`); |
133 } | 133 } |
134 | 134 |
135 if (match[2] == "$") | 135 filter = ContentFilter.fromText(text, match[1], match[2], match[3]); |
136 return new SnippetFilter(text, match[1], match[3]); | |
137 | |
138 filter = ElemHideBase.fromText( | |
139 text, match[1], match[2], match[3] | |
140 ); | |
141 } | 136 } |
142 else if (text[0] == "!") | 137 else if (text[0] == "!") |
143 filter = new CommentFilter(text); | 138 filter = new CommentFilter(text); |
144 else | 139 else |
145 filter = RegExpFilter.fromText(text); | 140 filter = RegExpFilter.fromText(text); |
146 | 141 |
147 Filter.knownFilters.set(filter.text, filter); | 142 Filter.knownFilters.set(filter.text, filter); |
148 return filter; | 143 return filter; |
149 }; | 144 }; |
150 | 145 |
(...skipping 29 matching lines...) Expand all Loading... | |
180 if (!text) | 175 if (!text) |
181 return text; | 176 return text; |
182 | 177 |
183 // Remove line breaks, tabs etc | 178 // Remove line breaks, tabs etc |
184 text = text.replace(/[^\S ]+/g, ""); | 179 text = text.replace(/[^\S ]+/g, ""); |
185 | 180 |
186 // Don't remove spaces inside comments | 181 // Don't remove spaces inside comments |
187 if (/^ *!/.test(text)) | 182 if (/^ *!/.test(text)) |
188 return text.trim(); | 183 return text.trim(); |
189 | 184 |
190 // Special treatment for code injection filters, right side is allowed to | 185 // Special treatment for content filters, right side is allowed to contain |
191 // contain spaces | 186 // spaces |
192 if (Filter.codeInjectionRegExp.test(text)) | 187 if (Filter.contentRegExp.test(text)) |
193 { | 188 { |
194 let [, domains, separator, code] = /^(.*?)(#[@?$]?#?)(.*)$/.exec(text); | 189 let [, domains, separator, body] = /^(.*?)(#[@?$]?#?)(.*)$/.exec(text); |
195 return domains.replace(/ +/g, "") + separator + code.trim(); | 190 return domains.replace(/ +/g, "") + separator + body.trim(); |
196 } | 191 } |
197 | 192 |
198 // For most regexp filters we strip all spaces, but $csp filter options | 193 // For most regexp filters we strip all spaces, but $csp filter options |
199 // are allowed to contain single (non trailing) spaces. | 194 // are allowed to contain single (non trailing) spaces. |
200 let strippedText = text.replace(/ +/g, ""); | 195 let strippedText = text.replace(/ +/g, ""); |
201 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) | 196 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) |
202 return strippedText; | 197 return strippedText; |
203 | 198 |
204 let optionsMatch = Filter.optionsRegExp.exec(strippedText); | 199 let optionsMatch = Filter.optionsRegExp.exec(strippedText); |
205 if (!optionsMatch) | 200 if (!optionsMatch) |
(...skipping 30 matching lines...) Expand all Loading... | |
236 return beforeOptions + "$" + options.join(); | 231 return beforeOptions + "$" + options.join(); |
237 }; | 232 }; |
238 | 233 |
239 /** | 234 /** |
240 * @see filterToRegExp | 235 * @see filterToRegExp |
241 */ | 236 */ |
242 Filter.toRegExp = filterToRegExp; | 237 Filter.toRegExp = filterToRegExp; |
243 | 238 |
244 /** | 239 /** |
245 * Class for invalid filters | 240 * Class for invalid filters |
246 * @param {string} text see Filter() | 241 * @param {string} text see {@link Filter Filter()} |
247 * @param {string} reason Reason why this filter is invalid | 242 * @param {string} reason Reason why this filter is invalid |
248 * @constructor | 243 * @constructor |
249 * @augments Filter | 244 * @augments Filter |
250 */ | 245 */ |
251 function InvalidFilter(text, reason) | 246 function InvalidFilter(text, reason) |
252 { | 247 { |
253 Filter.call(this, text); | 248 Filter.call(this, text); |
254 | 249 |
255 this.reason = reason; | 250 this.reason = reason; |
256 } | 251 } |
(...skipping 10 matching lines...) Expand all Loading... | |
267 | 262 |
268 /** | 263 /** |
269 * See Filter.serialize() | 264 * See Filter.serialize() |
270 * @inheritdoc | 265 * @inheritdoc |
271 */ | 266 */ |
272 serialize(buffer) {} | 267 serialize(buffer) {} |
273 }); | 268 }); |
274 | 269 |
275 /** | 270 /** |
276 * Class for comments | 271 * Class for comments |
277 * @param {string} text see Filter() | 272 * @param {string} text see {@link Filter Filter()} |
278 * @constructor | 273 * @constructor |
279 * @augments Filter | 274 * @augments Filter |
280 */ | 275 */ |
281 function CommentFilter(text) | 276 function CommentFilter(text) |
282 { | 277 { |
283 Filter.call(this, text); | 278 Filter.call(this, text); |
284 } | 279 } |
285 exports.CommentFilter = CommentFilter; | 280 exports.CommentFilter = CommentFilter; |
286 | 281 |
287 CommentFilter.prototype = extend(Filter, { | 282 CommentFilter.prototype = extend(Filter, { |
288 type: "comment", | 283 type: "comment", |
289 | 284 |
290 /** | 285 /** |
291 * See Filter.serialize() | 286 * See Filter.serialize() |
292 * @inheritdoc | 287 * @inheritdoc |
293 */ | 288 */ |
294 serialize(buffer) {} | 289 serialize(buffer) {} |
295 }); | 290 }); |
296 | 291 |
297 /** | 292 /** |
298 * Abstract base class for filters that can get hits | 293 * Abstract base class for filters that can get hits |
299 * @param {string} text | 294 * @param {string} text |
300 * see Filter() | 295 * see {@link Filter Filter()} |
301 * @param {string} [domains] | 296 * @param {string} [domains] |
302 * Domains that the filter is restricted to separated by domainSeparator | 297 * Domains that the filter is restricted to separated by domainSeparator |
303 * e.g. "foo.com|bar.com|~baz.com" | 298 * e.g. "foo.com|bar.com|~baz.com" |
304 * @constructor | 299 * @constructor |
305 * @augments Filter | 300 * @augments Filter |
306 */ | 301 */ |
307 function ActiveFilter(text, domains) | 302 function ActiveFilter(text, domains) |
308 { | 303 { |
309 Filter.call(this, text); | 304 Filter.call(this, text); |
310 | 305 |
311 this.domainSource = domains; | 306 if (domains) |
307 this.domainSource = domains; | |
312 } | 308 } |
313 exports.ActiveFilter = ActiveFilter; | 309 exports.ActiveFilter = ActiveFilter; |
314 | 310 |
315 ActiveFilter.prototype = extend(Filter, { | 311 ActiveFilter.prototype = extend(Filter, { |
316 _disabled: false, | 312 _disabled: false, |
317 _hitCount: 0, | 313 _hitCount: 0, |
318 _lastHit: 0, | 314 _lastHit: 0, |
319 | 315 |
320 /** | 316 /** |
321 * Defines whether the filter is disabled | 317 * Defines whether the filter is disabled |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
370 { | 366 { |
371 let oldValue = this._lastHit; | 367 let oldValue = this._lastHit; |
372 this._lastHit = value; | 368 this._lastHit = value; |
373 FilterNotifier.triggerListeners("filter.lastHit", this, value, oldValue); | 369 FilterNotifier.triggerListeners("filter.lastHit", this, value, oldValue); |
374 } | 370 } |
375 return this._lastHit; | 371 return this._lastHit; |
376 }, | 372 }, |
377 | 373 |
378 /** | 374 /** |
379 * String that the domains property should be generated from | 375 * String that the domains property should be generated from |
380 * @type {string} | 376 * @type {?string} |
381 */ | 377 */ |
382 domainSource: null, | 378 domainSource: null, |
383 | 379 |
384 /** | 380 /** |
385 * Separator character used in domainSource property, must be | 381 * Separator character used in domainSource property, must be |
386 * overridden by subclasses | 382 * overridden by subclasses |
387 * @type {string} | 383 * @type {string} |
388 */ | 384 */ |
389 domainSeparator: null, | 385 domainSeparator: null, |
390 | 386 |
391 /** | 387 /** |
392 * Determines whether the trailing dot in domain names isn't important and | 388 * Determines whether domainSource is already lower-case, |
393 * should be ignored, must be overridden by subclasses. | |
394 * @type {boolean} | |
395 */ | |
396 ignoreTrailingDot: true, | |
397 | |
398 /** | |
399 * Determines whether domainSource is already upper-case, | |
400 * can be overridden by subclasses. | 389 * can be overridden by subclasses. |
401 * @type {boolean} | 390 * @type {boolean} |
402 */ | 391 */ |
403 domainSourceIsUpperCase: false, | 392 domainSourceIsLowerCase: false, |
404 | 393 |
405 /** | 394 /** |
406 * Map containing domains that this filter should match on/not match | 395 * Map containing domains that this filter should match on/not match |
407 * on or null if the filter should match on all domains | 396 * on or null if the filter should match on all domains |
408 * @type {?Map.<string,boolean>} | 397 * @type {?Map.<string,boolean>} |
409 */ | 398 */ |
410 get domains() | 399 get domains() |
411 { | 400 { |
412 // Despite this property being cached, the getter is called | 401 let prop = Object.getOwnPropertyDescriptor(this, "_domains"); |
413 // several times on Safari, due to WebKit bug 132872 | |
414 let prop = Object.getOwnPropertyDescriptor(this, "domains"); | |
415 if (prop) | 402 if (prop) |
416 return prop.value; | 403 { |
404 let {value} = prop; | |
405 return typeof value == "string" ? | |
406 new Map([[value, true], ["", false]]) : value; | |
407 } | |
417 | 408 |
418 let domains = null; | 409 let domains = null; |
419 | 410 |
420 if (this.domainSource) | 411 if (this.domainSource) |
421 { | 412 { |
422 let source = this.domainSource; | 413 let source = this.domainSource; |
423 if (!this.domainSourceIsUpperCase) | 414 if (!this.domainSourceIsLowerCase) |
424 { | 415 { |
425 // RegExpFilter already have uppercase domains | 416 // RegExpFilter already have lowercase domains |
426 source = source.toUpperCase(); | 417 source = source.toLowerCase(); |
427 } | 418 } |
428 let list = source.split(this.domainSeparator); | 419 let list = source.split(this.domainSeparator); |
429 if (list.length == 1 && list[0][0] != "~") | 420 if (list.length == 1 && list[0][0] != "~") |
430 { | 421 { |
431 // Fast track for the common one-domain scenario | 422 // Fast track for the common one-domain scenario |
432 if (this.ignoreTrailingDot) | 423 domains = list[0]; |
433 list[0] = list[0].replace(/\.+$/, ""); | |
434 domains = new Map([["", false], [list[0], true]]); | |
435 } | 424 } |
436 else | 425 else |
437 { | 426 { |
438 let hasIncludes = false; | 427 let hasIncludes = false; |
439 for (let i = 0; i < list.length; i++) | 428 for (let i = 0; i < list.length; i++) |
440 { | 429 { |
441 let domain = list[i]; | 430 let domain = list[i]; |
442 if (this.ignoreTrailingDot) | |
443 domain = domain.replace(/\.+$/, ""); | |
444 if (domain == "") | 431 if (domain == "") |
445 continue; | 432 continue; |
446 | 433 |
447 let include; | 434 let include; |
448 if (domain[0] == "~") | 435 if (domain[0] == "~") |
449 { | 436 { |
450 include = false; | 437 include = false; |
451 domain = domain.substr(1); | 438 domain = domain.substr(1); |
452 } | 439 } |
453 else | 440 else |
454 { | 441 { |
455 include = true; | 442 include = true; |
456 hasIncludes = true; | 443 hasIncludes = true; |
457 } | 444 } |
458 | 445 |
459 if (!domains) | 446 if (!domains) |
460 domains = new Map(); | 447 domains = new Map(); |
461 | 448 |
462 domains.set(domain, include); | 449 domains.set(domain, include); |
463 } | 450 } |
464 if (domains) | 451 if (domains) |
465 domains.set("", !hasIncludes); | 452 domains.set("", !hasIncludes); |
466 } | 453 } |
467 | 454 |
468 this.domainSource = null; | 455 this.domainSource = null; |
469 } | 456 } |
470 | 457 |
471 Object.defineProperty(this, "domains", {value: domains, enumerable: true}); | 458 Object.defineProperty(this, "_domains", {value: domains}); |
472 return this.domains; | 459 return this.domains; |
473 }, | 460 }, |
474 | 461 |
475 /** | 462 /** |
476 * Array containing public keys of websites that this filter should apply to | 463 * Array containing public keys of websites that this filter should apply to |
477 * @type {string[]} | 464 * @type {?string[]} |
478 */ | 465 */ |
479 sitekeys: null, | 466 sitekeys: null, |
480 | 467 |
481 /** | 468 /** |
482 * Checks whether this filter is active on a domain. | 469 * Checks whether this filter is active on a domain. |
483 * @param {string} docDomain domain name of the document that loads the URL | 470 * @param {string} [docDomain] domain name of the document that loads the URL |
484 * @param {string} [sitekey] public key provided by the document | 471 * @param {string} [sitekey] public key provided by the document |
485 * @return {boolean} true in case of the filter being active | 472 * @return {boolean} true in case of the filter being active |
486 */ | 473 */ |
487 isActiveOnDomain(docDomain, sitekey) | 474 isActiveOnDomain(docDomain, sitekey) |
488 { | 475 { |
489 // Sitekeys are case-sensitive so we shouldn't convert them to | 476 // Sitekeys are case-sensitive so we shouldn't convert them to |
490 // upper-case to avoid false positives here. Instead we need to | 477 // upper-case to avoid false positives here. Instead we need to |
491 // change the way filter options are parsed. | 478 // change the way filter options are parsed. |
492 if (this.sitekeys && | 479 if (this.sitekeys && |
493 (!sitekey || this.sitekeys.indexOf(sitekey.toUpperCase()) < 0)) | 480 (!sitekey || this.sitekeys.indexOf(sitekey.toUpperCase()) < 0)) |
494 { | 481 { |
495 return false; | 482 return false; |
496 } | 483 } |
497 | 484 |
498 // If no domains are set the rule matches everywhere | 485 // If no domains are set the rule matches everywhere |
499 if (!this.domains) | 486 if (!this.domains) |
500 return true; | 487 return true; |
501 | 488 |
502 // If the document has no host name, match only if the filter | 489 // If the document has no host name, match only if the filter |
503 // isn't restricted to specific domains | 490 // isn't restricted to specific domains |
504 if (!docDomain) | 491 if (!docDomain) |
505 return this.domains.get(""); | 492 return this.domains.get(""); |
506 | 493 |
507 if (this.ignoreTrailingDot) | 494 docDomain = docDomain.replace(/\.+$/, "").toLowerCase(); |
508 docDomain = docDomain.replace(/\.+$/, ""); | |
509 docDomain = docDomain.toUpperCase(); | |
510 | 495 |
511 while (true) | 496 while (true) |
512 { | 497 { |
513 let isDomainIncluded = this.domains.get(docDomain); | 498 let isDomainIncluded = this.domains.get(docDomain); |
514 if (typeof isDomainIncluded != "undefined") | 499 if (typeof isDomainIncluded != "undefined") |
515 return isDomainIncluded; | 500 return isDomainIncluded; |
516 | 501 |
517 let nextDot = docDomain.indexOf("."); | 502 let nextDot = docDomain.indexOf("."); |
518 if (nextDot < 0) | 503 if (nextDot < 0) |
519 break; | 504 break; |
520 docDomain = docDomain.substr(nextDot + 1); | 505 docDomain = docDomain.substr(nextDot + 1); |
521 } | 506 } |
522 return this.domains.get(""); | 507 return this.domains.get(""); |
523 }, | 508 }, |
524 | 509 |
525 /** | 510 /** |
526 * Checks whether this filter is active only on a domain and its subdomains. | 511 * Checks whether this filter is active only on a domain and its subdomains. |
527 * @param {string} docDomain | 512 * @param {string} docDomain |
528 * @return {boolean} | 513 * @return {boolean} |
529 */ | 514 */ |
530 isActiveOnlyOnDomain(docDomain) | 515 isActiveOnlyOnDomain(docDomain) |
531 { | 516 { |
532 if (!docDomain || !this.domains || this.domains.get("")) | 517 if (!docDomain || !this.domains || this.domains.get("")) |
533 return false; | 518 return false; |
534 | 519 |
535 if (this.ignoreTrailingDot) | 520 docDomain = docDomain.replace(/\.+$/, "").toLowerCase(); |
536 docDomain = docDomain.replace(/\.+$/, ""); | |
537 docDomain = docDomain.toUpperCase(); | |
538 | 521 |
539 for (let [domain, isIncluded] of this.domains) | 522 for (let [domain, isIncluded] of this.domains) |
540 { | 523 { |
541 if (isIncluded && domain != docDomain) | 524 if (isIncluded && domain != docDomain) |
542 { | 525 { |
543 if (domain.length <= docDomain.length) | 526 if (domain.length <= docDomain.length) |
544 return false; | 527 return false; |
545 | 528 |
546 if (!domain.endsWith("." + docDomain)) | 529 if (!domain.endsWith("." + docDomain)) |
547 return false; | 530 return false; |
(...skipping 27 matching lines...) Expand all Loading... | |
575 if (this._hitCount) | 558 if (this._hitCount) |
576 buffer.push("hitCount=" + this._hitCount); | 559 buffer.push("hitCount=" + this._hitCount); |
577 if (this._lastHit) | 560 if (this._lastHit) |
578 buffer.push("lastHit=" + this._lastHit); | 561 buffer.push("lastHit=" + this._lastHit); |
579 } | 562 } |
580 } | 563 } |
581 }); | 564 }); |
582 | 565 |
583 /** | 566 /** |
584 * Abstract base class for RegExp-based filters | 567 * Abstract base class for RegExp-based filters |
585 * @param {string} text see Filter() | 568 * @param {string} text see {@link Filter Filter()} |
586 * @param {string} regexpSource | 569 * @param {string} regexpSource |
587 * filter part that the regular expression should be build from | 570 * filter part that the regular expression should be build from |
588 * @param {number} [contentType] | 571 * @param {number} [contentType] |
589 * Content types the filter applies to, combination of values from | 572 * Content types the filter applies to, combination of values from |
590 * RegExpFilter.typeMap | 573 * RegExpFilter.typeMap |
591 * @param {boolean} [matchCase] | 574 * @param {boolean} [matchCase] |
592 * Defines whether the filter should distinguish between lower and upper case | 575 * Defines whether the filter should distinguish between lower and upper case |
593 * letters | 576 * letters |
594 * @param {string} [domains] | 577 * @param {string} [domains] |
595 * Domains that the filter is restricted to, e.g. "foo.com|bar.com|~baz.com" | 578 * Domains that the filter is restricted to, e.g. "foo.com|bar.com|~baz.com" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
628 else | 611 else |
629 { | 612 { |
630 // No need to convert this filter to regular expression yet, do it on demand | 613 // No need to convert this filter to regular expression yet, do it on demand |
631 this.regexpSource = regexpSource; | 614 this.regexpSource = regexpSource; |
632 } | 615 } |
633 } | 616 } |
634 exports.RegExpFilter = RegExpFilter; | 617 exports.RegExpFilter = RegExpFilter; |
635 | 618 |
636 RegExpFilter.prototype = extend(ActiveFilter, { | 619 RegExpFilter.prototype = extend(ActiveFilter, { |
637 /** | 620 /** |
638 * @see ActiveFilter.domainSourceIsUpperCase | 621 * @see ActiveFilter.domainSourceIsLowerCase |
639 */ | 622 */ |
640 domainSourceIsUpperCase: true, | 623 domainSourceIsLowerCase: true, |
641 | 624 |
642 /** | 625 /** |
643 * Number of filters contained, will always be 1 (required to | 626 * Number of filters contained, will always be 1 (required to |
644 * optimize Matcher). | 627 * optimize Matcher). |
645 * @type {number} | 628 * @type {number} |
646 */ | 629 */ |
647 length: 1, | 630 length: 1, |
648 | 631 |
649 /** | 632 /** |
650 * @see ActiveFilter.domainSeparator | 633 * @see ActiveFilter.domainSeparator |
651 */ | 634 */ |
652 domainSeparator: "|", | 635 domainSeparator: "|", |
653 | 636 |
654 /** | 637 /** |
655 * Expression from which a regular expression should be generated - | 638 * Expression from which a regular expression should be generated - |
656 * for delayed creation of the regexp property | 639 * for delayed creation of the regexp property |
657 * @type {string} | 640 * @type {string} |
658 */ | 641 */ |
659 regexpSource: null, | 642 regexpSource: null, |
660 /** | 643 /** |
661 * Regular expression to be used when testing against this filter | 644 * Regular expression to be used when testing against this filter |
662 * @type {RegExp} | 645 * @type {RegExp} |
663 */ | 646 */ |
664 get regexp() | 647 get regexp() |
665 { | 648 { |
666 // Despite this property being cached, the getter is called | |
667 // several times on Safari, due to WebKit bug 132872 | |
668 let prop = Object.getOwnPropertyDescriptor(this, "regexp"); | |
669 if (prop) | |
670 return prop.value; | |
671 | |
672 let source = Filter.toRegExp(this.regexpSource); | 649 let source = Filter.toRegExp(this.regexpSource); |
673 let regexp = new RegExp(source, this.matchCase ? "" : "i"); | 650 let regexp = new RegExp(source, this.matchCase ? "" : "i"); |
674 Object.defineProperty(this, "regexp", {value: regexp}); | 651 Object.defineProperty(this, "regexp", {value: regexp}); |
652 this.regexpSource = null; | |
675 return regexp; | 653 return regexp; |
676 }, | 654 }, |
677 /** | 655 /** |
678 * Content types the filter applies to, combination of values from | 656 * Content types the filter applies to, combination of values from |
679 * RegExpFilter.typeMap | 657 * RegExpFilter.typeMap |
680 * @type {number} | 658 * @type {number} |
681 */ | 659 */ |
682 contentType: 0x7FFFFFFF, | 660 contentType: 0x7FFFFFFF, |
683 /** | 661 /** |
684 * Defines whether the filter should distinguish between lower and | 662 * Defines whether the filter should distinguish between lower and |
685 * upper case letters | 663 * upper case letters |
686 * @type {boolean} | 664 * @type {boolean} |
687 */ | 665 */ |
688 matchCase: false, | 666 matchCase: false, |
689 /** | 667 /** |
690 * Defines whether the filter should apply to third-party or | 668 * Defines whether the filter should apply to third-party or |
691 * first-party content only. Can be null (apply to all content). | 669 * first-party content only. Can be null (apply to all content). |
692 * @type {boolean} | 670 * @type {?boolean} |
693 */ | 671 */ |
694 thirdParty: null, | 672 thirdParty: null, |
695 | 673 |
696 /** | 674 /** |
697 * String that the sitekey property should be generated from | 675 * String that the sitekey property should be generated from |
698 * @type {string} | 676 * @type {?string} |
699 */ | 677 */ |
700 sitekeySource: null, | 678 sitekeySource: null, |
701 | 679 |
702 /** | 680 /** |
703 * Array containing public keys of websites that this filter should apply to | 681 * @see ActiveFilter.sitekeys |
704 * @type {string[]} | |
705 */ | 682 */ |
706 get sitekeys() | 683 get sitekeys() |
707 { | 684 { |
708 // Despite this property being cached, the getter is called | |
709 // several times on Safari, due to WebKit bug 132872 | |
710 let prop = Object.getOwnPropertyDescriptor(this, "sitekeys"); | |
711 if (prop) | |
712 return prop.value; | |
713 | |
714 let sitekeys = null; | 685 let sitekeys = null; |
715 | 686 |
716 if (this.sitekeySource) | 687 if (this.sitekeySource) |
717 { | 688 { |
718 sitekeys = this.sitekeySource.split("|"); | 689 sitekeys = this.sitekeySource.split("|"); |
719 this.sitekeySource = null; | 690 this.sitekeySource = null; |
720 } | 691 } |
721 | 692 |
722 Object.defineProperty( | 693 Object.defineProperty( |
723 this, "sitekeys", {value: sitekeys, enumerable: true} | 694 this, "sitekeys", {value: sitekeys, enumerable: true} |
724 ); | 695 ); |
725 return this.sitekeys; | 696 return this.sitekeys; |
726 }, | 697 }, |
727 | 698 |
728 /** | 699 /** |
729 * Tests whether the URL matches this filter | 700 * Tests whether the URL matches this filter |
730 * @param {string} location URL to be tested | 701 * @param {string} location URL to be tested |
731 * @param {number} typeMask bitmask of content / request types to match | 702 * @param {number} typeMask bitmask of content / request types to match |
732 * @param {string} docDomain domain name of the document that loads the URL | 703 * @param {string} [docDomain] domain name of the document that loads the URL |
733 * @param {boolean} thirdParty should be true if the URL is a third-party | 704 * @param {boolean} [thirdParty] should be true if the URL is a third-party |
734 * request | 705 * request |
735 * @param {string} sitekey public key provided by the document | 706 * @param {string} [sitekey] public key provided by the document |
736 * @return {boolean} true in case of a match | 707 * @return {boolean} true in case of a match |
737 */ | 708 */ |
738 matches(location, typeMask, docDomain, thirdParty, sitekey) | 709 matches(location, typeMask, docDomain, thirdParty, sitekey) |
739 { | 710 { |
740 if (this.contentType & typeMask && | 711 if (this.contentType & typeMask && |
741 (this.thirdParty == null || this.thirdParty == thirdParty) && | 712 (this.thirdParty == null || this.thirdParty == thirdParty) && |
742 this.isActiveOnDomain(docDomain, sitekey) && this.regexp.test(location)) | 713 this.isActiveOnDomain(docDomain, sitekey) && this.regexp.test(location)) |
743 { | 714 { |
744 return true; | 715 return true; |
745 } | 716 } |
746 return false; | 717 return false; |
747 } | 718 } |
748 }); | 719 }); |
749 | 720 |
750 // Required to optimize Matcher, see also RegExpFilter.prototype.length | 721 // Required to optimize Matcher, see also RegExpFilter.prototype.length |
751 Object.defineProperty(RegExpFilter.prototype, "0", { | 722 Object.defineProperty(RegExpFilter.prototype, "0", { |
752 get() { return this; } | 723 get() { return this; } |
753 }); | 724 }); |
754 | 725 |
755 /** | 726 /** |
756 * Creates a RegExp filter from its text representation | 727 * Creates a RegExp filter from its text representation |
757 * @param {string} text same as in Filter() | 728 * @param {string} text same as in Filter() |
758 * @return {Filter} | 729 * @return {Filter} |
759 */ | 730 */ |
760 RegExpFilter.fromText = function(text) | 731 RegExpFilter.fromText = function(text) |
761 { | 732 { |
762 let blocking = true; | 733 let blocking = true; |
763 let origText = text; | 734 let origText = text; |
764 if (text.indexOf("@@") == 0) | 735 if (text[0] == "@" && text[1] == "@") |
765 { | 736 { |
766 blocking = false; | 737 blocking = false; |
767 text = text.substr(2); | 738 text = text.substr(2); |
768 } | 739 } |
769 | 740 |
770 let contentType = null; | 741 let contentType = null; |
771 let matchCase = null; | 742 let matchCase = null; |
772 let domains = null; | 743 let domains = null; |
773 let sitekeys = null; | 744 let sitekeys = null; |
774 let thirdParty = null; | 745 let thirdParty = null; |
775 let collapse = null; | 746 let collapse = null; |
776 let csp = null; | 747 let csp = null; |
748 let rewrite = null; | |
777 let options; | 749 let options; |
778 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); | 750 let match = text.includes("$") ? Filter.optionsRegExp.exec(text) : null; |
779 if (match) | 751 if (match) |
780 { | 752 { |
781 options = match[1].split(","); | 753 options = match[1].split(","); |
782 text = match.input.substr(0, match.index); | 754 text = match.input.substr(0, match.index); |
783 for (let option of options) | 755 for (let option of options) |
784 { | 756 { |
785 let value = null; | 757 let value = null; |
786 let separatorIndex = option.indexOf("="); | 758 let separatorIndex = option.indexOf("="); |
787 if (separatorIndex >= 0) | 759 if (separatorIndex >= 0) |
788 { | 760 { |
789 value = option.substr(separatorIndex + 1); | 761 value = option.substr(separatorIndex + 1); |
790 option = option.substr(0, separatorIndex); | 762 option = option.substr(0, separatorIndex); |
791 } | 763 } |
792 option = option.replace(/-/, "_").toUpperCase(); | 764 |
793 if (option in RegExpFilter.typeMap) | 765 let inverse = option[0] == "~"; |
766 if (inverse) | |
767 option = option.substr(1); | |
768 | |
769 let type = RegExpFilter.typeMap[option.replace(/-/, "_").toUpperCase()]; | |
770 if (type) | |
794 { | 771 { |
795 if (contentType == null) | 772 if (inverse) |
796 contentType = 0; | 773 { |
797 contentType |= RegExpFilter.typeMap[option]; | 774 if (contentType == null) |
798 | 775 ({contentType} = RegExpFilter.prototype); |
799 if (option == "CSP" && value) | 776 contentType &= ~type; |
800 csp = value; | 777 } |
778 else | |
779 { | |
780 contentType |= type; | |
781 | |
782 if (type == RegExpFilter.typeMap.CSP && value) | |
783 csp = value; | |
784 } | |
801 } | 785 } |
802 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap) | 786 else |
803 { | 787 { |
804 if (contentType == null) | 788 switch (option.toLowerCase()) |
805 ({contentType} = RegExpFilter.prototype); | 789 { |
806 contentType &= ~RegExpFilter.typeMap[option.substr(1)]; | 790 case "match-case": |
791 matchCase = !inverse; | |
792 break; | |
793 case "domain": | |
794 if (!value) | |
795 return new InvalidFilter(origText, "filter_unknown_option"); | |
796 domains = value.toLowerCase(); | |
797 break; | |
798 case "third-party": | |
799 thirdParty = !inverse; | |
800 break; | |
801 case "collapse": | |
802 collapse = !inverse; | |
803 break; | |
804 case "sitekey": | |
805 if (!value) | |
806 return new InvalidFilter(origText, "filter_unknown_option"); | |
807 sitekeys = value.toUpperCase(); | |
808 break; | |
809 case "rewrite": | |
810 if (!value) | |
811 return new InvalidFilter(origText, "filter_unknown_option"); | |
812 rewrite = value; | |
813 break; | |
814 default: | |
815 return new InvalidFilter(origText, "filter_unknown_option"); | |
816 } | |
807 } | 817 } |
808 else if (option == "MATCH_CASE") | 818 } |
809 matchCase = true; | 819 } |
810 else if (option == "~MATCH_CASE") | 820 |
811 matchCase = false; | 821 // For security reasons, never match $rewrite filters |
812 else if (option == "DOMAIN" && value) | 822 // against requests that might load any code to be executed. |
813 domains = value.toUpperCase(); | 823 if (rewrite != null) |
814 else if (option == "THIRD_PARTY") | 824 { |
815 thirdParty = true; | 825 if (contentType == null) |
816 else if (option == "~THIRD_PARTY") | 826 ({contentType} = RegExpFilter.prototype); |
817 thirdParty = false; | 827 contentType &= ~(RegExpFilter.typeMap.SCRIPT | |
818 else if (option == "COLLAPSE") | 828 RegExpFilter.typeMap.SUBDOCUMENT | |
819 collapse = true; | 829 RegExpFilter.typeMap.OBJECT | |
820 else if (option == "~COLLAPSE") | 830 RegExpFilter.typeMap.OBJECT_SUBREQUEST); |
821 collapse = false; | |
822 else if (option == "SITEKEY" && value) | |
823 sitekeys = value.toUpperCase(); | |
824 else | |
825 return new InvalidFilter(origText, "filter_unknown_option"); | |
826 } | |
827 } | 831 } |
828 | 832 |
829 try | 833 try |
830 { | 834 { |
831 if (blocking) | 835 if (blocking) |
832 { | 836 { |
833 if (csp && Filter.invalidCSPRegExp.test(csp)) | 837 if (csp && Filter.invalidCSPRegExp.test(csp)) |
834 return new InvalidFilter(origText, "filter_invalid_csp"); | 838 return new InvalidFilter(origText, "filter_invalid_csp"); |
835 | 839 |
836 return new BlockingFilter(origText, text, contentType, matchCase, domains, | 840 return new BlockingFilter(origText, text, contentType, matchCase, domains, |
837 thirdParty, sitekeys, collapse, csp); | 841 thirdParty, sitekeys, collapse, csp, rewrite); |
838 } | 842 } |
839 return new WhitelistFilter(origText, text, contentType, matchCase, domains, | 843 return new WhitelistFilter(origText, text, contentType, matchCase, domains, |
840 thirdParty, sitekeys); | 844 thirdParty, sitekeys); |
841 } | 845 } |
842 catch (e) | 846 catch (e) |
843 { | 847 { |
844 return new InvalidFilter(origText, "filter_invalid_regexp"); | 848 return new InvalidFilter(origText, "filter_invalid_regexp"); |
845 } | 849 } |
846 }; | 850 }; |
847 | 851 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
879 // shouldn't be there by default | 883 // shouldn't be there by default |
880 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.CSP | | 884 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.CSP | |
881 RegExpFilter.typeMap.DOCUMENT | | 885 RegExpFilter.typeMap.DOCUMENT | |
882 RegExpFilter.typeMap.ELEMHIDE | | 886 RegExpFilter.typeMap.ELEMHIDE | |
883 RegExpFilter.typeMap.POPUP | | 887 RegExpFilter.typeMap.POPUP | |
884 RegExpFilter.typeMap.GENERICHIDE | | 888 RegExpFilter.typeMap.GENERICHIDE | |
885 RegExpFilter.typeMap.GENERICBLOCK); | 889 RegExpFilter.typeMap.GENERICBLOCK); |
886 | 890 |
887 /** | 891 /** |
888 * Class for blocking filters | 892 * Class for blocking filters |
889 * @param {string} text see Filter() | 893 * @param {string} text see {@link Filter Filter()} |
890 * @param {string} regexpSource see RegExpFilter() | 894 * @param {string} regexpSource see {@link RegExpFilter RegExpFilter()} |
891 * @param {number} contentType see RegExpFilter() | 895 * @param {number} [contentType] see {@link RegExpFilter RegExpFilter()} |
892 * @param {boolean} matchCase see RegExpFilter() | 896 * @param {boolean} [matchCase] see {@link RegExpFilter RegExpFilter()} |
893 * @param {string} domains see RegExpFilter() | 897 * @param {string} [domains] see {@link RegExpFilter RegExpFilter()} |
894 * @param {boolean} thirdParty see RegExpFilter() | 898 * @param {boolean} [thirdParty] see {@link RegExpFilter RegExpFilter()} |
895 * @param {string} sitekeys see RegExpFilter() | 899 * @param {string} [sitekeys] see {@link RegExpFilter RegExpFilter()} |
896 * @param {boolean} collapse | 900 * @param {boolean} [collapse] |
897 * defines whether the filter should collapse blocked content, can be null | 901 * defines whether the filter should collapse blocked content, can be null |
898 * @param {string} [csp] | 902 * @param {string} [csp] |
899 * Content Security Policy to inject when the filter matches | 903 * Content Security Policy to inject when the filter matches |
904 * @param {?string} [rewrite] | |
905 * The (optional) rule specifying how to rewrite the URL. See | |
906 * BlockingFilter.prototype.rewrite. | |
900 * @constructor | 907 * @constructor |
901 * @augments RegExpFilter | 908 * @augments RegExpFilter |
902 */ | 909 */ |
903 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, | 910 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, |
904 thirdParty, sitekeys, collapse, csp) | 911 thirdParty, sitekeys, collapse, csp, rewrite) |
905 { | 912 { |
906 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, | 913 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, |
907 thirdParty, sitekeys); | 914 thirdParty, sitekeys); |
908 | 915 |
909 this.collapse = collapse; | 916 if (collapse != null) |
910 this.csp = csp; | 917 this.collapse = collapse; |
918 | |
919 if (csp != null) | |
920 this.csp = csp; | |
921 | |
922 if (rewrite != null) | |
923 this.rewrite = rewrite; | |
911 } | 924 } |
912 exports.BlockingFilter = BlockingFilter; | 925 exports.BlockingFilter = BlockingFilter; |
913 | 926 |
914 BlockingFilter.prototype = extend(RegExpFilter, { | 927 BlockingFilter.prototype = extend(RegExpFilter, { |
915 type: "blocking", | 928 type: "blocking", |
916 | 929 |
917 /** | 930 /** |
918 * Defines whether the filter should collapse blocked content. | 931 * Defines whether the filter should collapse blocked content. |
919 * Can be null (use the global preference). | 932 * Can be null (use the global preference). |
920 * @type {boolean} | 933 * @type {?boolean} |
921 */ | 934 */ |
922 collapse: null, | 935 collapse: null, |
923 | 936 |
924 /** | 937 /** |
925 * Content Security Policy to inject for matching requests. | 938 * Content Security Policy to inject for matching requests. |
926 * @type {string} | 939 * @type {?string} |
927 */ | 940 */ |
928 csp: null | 941 csp: null, |
942 | |
943 /** | |
944 * The rule specifying how to rewrite the URL. | |
945 * The syntax is similar to the one of String.prototype.replace(). | |
946 * @type {?string} | |
947 */ | |
948 rewrite: null, | |
949 | |
950 /** | |
951 * Rewrites an URL. | |
952 * @param {string} url the URL to rewrite | |
953 * @return {string} the rewritten URL, or the original in case of failure | |
954 */ | |
955 rewriteUrl(url) | |
956 { | |
957 try | |
958 { | |
959 let rewrittenUrl = new URL(url.replace(this.regexp, this.rewrite), url); | |
960 if (rewrittenUrl.origin == new URL(url).origin) | |
961 return rewrittenUrl.href; | |
962 } | |
963 catch (e) | |
964 { | |
965 } | |
966 | |
967 return url; | |
968 } | |
929 }); | 969 }); |
930 | 970 |
931 /** | 971 /** |
932 * Class for whitelist filters | 972 * Class for whitelist filters |
933 * @param {string} text see Filter() | 973 * @param {string} text see {@link Filter Filter()} |
934 * @param {string} regexpSource see RegExpFilter() | 974 * @param {string} regexpSource see {@link RegExpFilter RegExpFilter()} |
935 * @param {number} contentType see RegExpFilter() | 975 * @param {number} [contentType] see {@link RegExpFilter RegExpFilter()} |
936 * @param {boolean} matchCase see RegExpFilter() | 976 * @param {boolean} [matchCase] see {@link RegExpFilter RegExpFilter()} |
937 * @param {string} domains see RegExpFilter() | 977 * @param {string} [domains] see {@link RegExpFilter RegExpFilter()} |
938 * @param {boolean} thirdParty see RegExpFilter() | 978 * @param {boolean} [thirdParty] see {@link RegExpFilter RegExpFilter()} |
939 * @param {string} sitekeys see RegExpFilter() | 979 * @param {string} [sitekeys] see {@link RegExpFilter RegExpFilter()} |
940 * @constructor | 980 * @constructor |
941 * @augments RegExpFilter | 981 * @augments RegExpFilter |
942 */ | 982 */ |
943 function WhitelistFilter(text, regexpSource, contentType, matchCase, domains, | 983 function WhitelistFilter(text, regexpSource, contentType, matchCase, domains, |
944 thirdParty, sitekeys) | 984 thirdParty, sitekeys) |
945 { | 985 { |
946 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, | 986 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, |
947 thirdParty, sitekeys); | 987 thirdParty, sitekeys); |
948 } | 988 } |
949 exports.WhitelistFilter = WhitelistFilter; | 989 exports.WhitelistFilter = WhitelistFilter; |
950 | 990 |
951 WhitelistFilter.prototype = extend(RegExpFilter, { | 991 WhitelistFilter.prototype = extend(RegExpFilter, { |
952 type: "whitelist" | 992 type: "whitelist" |
953 }); | 993 }); |
954 | 994 |
955 /** | 995 /** |
956 * Base class for code injection filters | 996 * Base class for content filters |
957 * @param {string} text see Filter() | 997 * @param {string} text see {@link Filter Filter()} |
958 * @param {string} [domains] Host names or domains the filter should be | 998 * @param {string} [domains] Host names or domains the filter should be |
959 * restricted to | 999 * restricted to |
960 * @param {string} code Code that should be injected | 1000 * @param {string} body The body of the filter |
961 * hidden | |
962 * @constructor | 1001 * @constructor |
963 * @augments ActiveFilter | 1002 * @augments ActiveFilter |
964 */ | 1003 */ |
965 function CodeInjectionFilter(text, domains, code) | 1004 function ContentFilter(text, domains, body) |
966 { | 1005 { |
967 ActiveFilter.call(this, text, domains || null); | 1006 ActiveFilter.call(this, text, domains || null); |
968 | 1007 |
969 if (domains) | 1008 this.body = body; |
970 { | 1009 } |
971 this.injectionDomains = domains.replace(/,~[^,]+/g, "") | 1010 exports.ContentFilter = ContentFilter; |
972 .replace(/^~[^,]+,?/, "").toLowerCase(); | 1011 |
973 } | 1012 ContentFilter.prototype = extend(ActiveFilter, { |
974 | |
975 this.code = code; | |
976 } | |
977 exports.CodeInjectionFilter = CodeInjectionFilter; | |
978 | |
979 CodeInjectionFilter.prototype = extend(ActiveFilter, { | |
980 /** | 1013 /** |
981 * @see ActiveFilter.domainSeparator | 1014 * @see ActiveFilter.domainSeparator |
982 */ | 1015 */ |
983 domainSeparator: ",", | 1016 domainSeparator: ",", |
984 | 1017 |
985 /** | 1018 /** |
986 * @see ActiveFilter.ignoreTrailingDot | 1019 * The body of the filter |
987 */ | |
988 ignoreTrailingDot: false, | |
989 | |
990 /** | |
991 * Host names or domains the filter should be restricted to (can be null for | |
992 * no restriction) | |
993 * @type {string} | 1020 * @type {string} |
994 */ | 1021 */ |
995 injectionDomains: null, | 1022 body: null |
996 | 1023 }); |
997 /** | 1024 |
998 * Code that should be injected | 1025 /** |
999 * @type {string} | 1026 * Creates a content filter from a pre-parsed text representation |
1000 */ | |
1001 code: null | |
1002 }); | |
1003 | |
1004 /** | |
1005 * Base class for element hiding filters | |
1006 * @param {string} text see Filter() | |
1007 * @param {string} [domains] see CodeInjectionFilter() | |
1008 * @param {string} selector CSS selector for the HTML elements that should be | |
1009 * hidden | |
1010 * @constructor | |
1011 * @augments CodeInjectionFilter | |
1012 */ | |
1013 function ElemHideBase(text, domains, selector) | |
1014 { | |
1015 CodeInjectionFilter.call(this, text, domains, selector); | |
1016 | |
1017 // Braces are being escaped to prevent CSS rule injection. | |
1018 this.code = this.code.replace("{", "\\7B ").replace("}", "\\7D "); | |
1019 } | |
1020 exports.ElemHideBase = ElemHideBase; | |
1021 | |
1022 ElemHideBase.prototype = extend(CodeInjectionFilter, {}); | |
1023 | |
1024 /** | |
1025 * Creates an element hiding filter from a pre-parsed text representation | |
1026 * | 1027 * |
1027 * @param {string} text same as in Filter() | 1028 * @param {string} text same as in Filter() |
1028 * @param {string?} domains | 1029 * @param {string} [domains] |
1029 * domains part of the text representation | 1030 * domains part of the text representation |
1030 * @param {string?} type | 1031 * @param {string} [type] |
1031 * rule type, either empty or @ (exception) or ? (emulation rule) | 1032 * rule type, either: |
1032 * @param {string} selector raw CSS selector | 1033 * <li>"" for an element hiding filter</li> |
Manish Jethani
2018/07/11 13:04:26
Using just "-" doesn't work in the HTML version, b
kzar
2018/07/11 17:17:52
Acknowledged.
| |
1034 * <li>"@" for an element hiding exception filter</li> | |
1035 * <li>"?" for an element hiding emulation filter</li> | |
1036 * <li>"$" for a snippet filter</li> | |
1037 * @param {string} body | |
1038 * body part of the text representation, either a CSS selector or a snippet | |
1039 * script | |
1033 * @return {ElemHideFilter|ElemHideException| | 1040 * @return {ElemHideFilter|ElemHideException| |
1034 * ElemHideEmulationFilter|InvalidFilter} | 1041 * ElemHideEmulationFilter|SnippetFilter|InvalidFilter} |
1035 */ | 1042 */ |
1036 ElemHideBase.fromText = function(text, domains, type, selector) | 1043 ContentFilter.fromText = function(text, domains, type, body) |
1037 { | 1044 { |
1038 // We don't allow ElemHide filters which have any empty domains. | 1045 // We don't allow content filters which have any empty domains. |
1039 // Note: The ElemHide.prototype.domainSeparator is duplicated here, if that | 1046 // Note: The ContentFilter.prototype.domainSeparator is duplicated here, if |
1040 // changes this must be changed too. | 1047 // that changes this must be changed too. |
1041 if (domains && /(^|,)~?(,|$)/.test(domains)) | 1048 if (domains && /(^|,)~?(,|$)/.test(domains)) |
1042 return new InvalidFilter(text, "filter_invalid_domain"); | 1049 return new InvalidFilter(text, "filter_invalid_domain"); |
1043 | 1050 |
1044 if (type == "@") | 1051 if (type == "@") |
1045 return new ElemHideException(text, domains, selector); | 1052 return new ElemHideException(text, domains, body); |
1053 | |
1054 if (type == "$") | |
1055 return new SnippetFilter(text, domains, body); | |
1046 | 1056 |
1047 if (type == "?") | 1057 if (type == "?") |
1048 { | 1058 { |
1049 // Element hiding emulation filters are inefficient so we need to make sure | 1059 // Element hiding emulation filters are inefficient so we need to make sure |
1050 // that they're only applied if they specify active domains | 1060 // that they're only applied if they specify active domains |
1051 if (!/,[^~][^,.]*\.[^,]/.test("," + domains)) | 1061 if (!/,[^~][^,.]*\.[^,]/.test("," + domains)) |
1052 return new InvalidFilter(text, "filter_elemhideemulation_nodomain"); | 1062 return new InvalidFilter(text, "filter_elemhideemulation_nodomain"); |
1053 | 1063 |
1054 return new ElemHideEmulationFilter(text, domains, selector); | 1064 return new ElemHideEmulationFilter(text, domains, body); |
1055 } | 1065 } |
1056 | 1066 |
1057 return new ElemHideFilter(text, domains, selector); | 1067 return new ElemHideFilter(text, domains, body); |
1058 }; | 1068 }; |
1059 | 1069 |
1060 /** | 1070 /** |
1071 * Base class for element hiding filters | |
1072 * @param {string} text see {@link Filter Filter()} | |
1073 * @param {string} [domains] see {@link ContentFilter ContentFilter()} | |
1074 * @param {string} selector CSS selector for the HTML elements that should be | |
1075 * hidden | |
1076 * @constructor | |
1077 * @augments ContentFilter | |
1078 */ | |
1079 function ElemHideBase(text, domains, selector) | |
1080 { | |
1081 ContentFilter.call(this, text, domains, selector); | |
1082 } | |
1083 exports.ElemHideBase = ElemHideBase; | |
1084 | |
1085 ElemHideBase.prototype = extend(ContentFilter, { | |
1086 /** | |
1087 * CSS selector for the HTML elements that should be hidden | |
1088 * @type {string} | |
1089 */ | |
1090 get selector() | |
1091 { | |
1092 // Braces are being escaped to prevent CSS rule injection. | |
1093 return this.body.replace("{", "\\7B ").replace("}", "\\7D "); | |
1094 } | |
1095 }); | |
1096 | |
1097 /** | |
1061 * Class for element hiding filters | 1098 * Class for element hiding filters |
1062 * @param {string} text see Filter() | 1099 * @param {string} text see {@link Filter Filter()} |
1063 * @param {string} domains see ElemHideBase() | 1100 * @param {string} [domains] see {@link ElemHideBase ElemHideBase()} |
1064 * @param {string} selector see ElemHideBase() | 1101 * @param {string} selector see {@link ElemHideBase ElemHideBase()} |
1065 * @constructor | 1102 * @constructor |
1066 * @augments ElemHideBase | 1103 * @augments ElemHideBase |
1067 */ | 1104 */ |
1068 function ElemHideFilter(text, domains, selector) | 1105 function ElemHideFilter(text, domains, selector) |
1069 { | 1106 { |
1070 ElemHideBase.call(this, text, domains, selector); | 1107 ElemHideBase.call(this, text, domains, selector); |
1071 } | 1108 } |
1072 exports.ElemHideFilter = ElemHideFilter; | 1109 exports.ElemHideFilter = ElemHideFilter; |
1073 | 1110 |
1074 ElemHideFilter.prototype = extend(ElemHideBase, { | 1111 ElemHideFilter.prototype = extend(ElemHideBase, { |
1075 type: "elemhide" | 1112 type: "elemhide" |
1076 }); | 1113 }); |
1077 | 1114 |
1078 /** | 1115 /** |
1079 * Class for element hiding exceptions | 1116 * Class for element hiding exceptions |
1080 * @param {string} text see Filter() | 1117 * @param {string} text see {@link Filter Filter()} |
1081 * @param {string} domains see ElemHideBase() | 1118 * @param {string} [domains] see {@link ElemHideBase ElemHideBase()} |
1082 * @param {string} selector see ElemHideBase() | 1119 * @param {string} selector see {@link ElemHideBase ElemHideBase()} |
1083 * @constructor | 1120 * @constructor |
1084 * @augments ElemHideBase | 1121 * @augments ElemHideBase |
1085 */ | 1122 */ |
1086 function ElemHideException(text, domains, selector) | 1123 function ElemHideException(text, domains, selector) |
1087 { | 1124 { |
1088 ElemHideBase.call(this, text, domains, selector); | 1125 ElemHideBase.call(this, text, domains, selector); |
1089 } | 1126 } |
1090 exports.ElemHideException = ElemHideException; | 1127 exports.ElemHideException = ElemHideException; |
1091 | 1128 |
1092 ElemHideException.prototype = extend(ElemHideBase, { | 1129 ElemHideException.prototype = extend(ElemHideBase, { |
1093 type: "elemhideexception" | 1130 type: "elemhideexception" |
1094 }); | 1131 }); |
1095 | 1132 |
1096 /** | 1133 /** |
1097 * Class for element hiding emulation filters | 1134 * Class for element hiding emulation filters |
1098 * @param {string} text see Filter() | 1135 * @param {string} text see {@link Filter Filter()} |
1099 * @param {string} domains see ElemHideBase() | 1136 * @param {string} domains see {@link ElemHideBase ElemHideBase()} |
1100 * @param {string} selector see ElemHideBase() | 1137 * @param {string} selector see {@link ElemHideBase ElemHideBase()} |
1101 * @constructor | 1138 * @constructor |
1102 * @augments ElemHideBase | 1139 * @augments ElemHideBase |
1103 */ | 1140 */ |
1104 function ElemHideEmulationFilter(text, domains, selector) | 1141 function ElemHideEmulationFilter(text, domains, selector) |
1105 { | 1142 { |
1106 ElemHideBase.call(this, text, domains, selector); | 1143 ElemHideBase.call(this, text, domains, selector); |
1107 } | 1144 } |
1108 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; | 1145 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; |
1109 | 1146 |
1110 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { | 1147 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { |
1111 type: "elemhideemulation" | 1148 type: "elemhideemulation" |
1112 }); | 1149 }); |
1113 | 1150 |
1114 /** | 1151 /** |
1115 * Class for snippet filters | 1152 * Class for snippet filters |
1116 * @param {string} text see Filter() | 1153 * @param {string} text see Filter() |
1117 * @param {string} [domains] see CodeInjectionFilter() | 1154 * @param {string} [domains] see ContentFilter() |
1118 * @param {string} script Script that should be executed | 1155 * @param {string} script Script that should be executed |
1119 * @constructor | 1156 * @constructor |
1120 * @augments CodeInjectionFilter | 1157 * @augments ContentFilter |
1121 */ | 1158 */ |
1122 function SnippetFilter(text, domains, script) | 1159 function SnippetFilter(text, domains, script) |
1123 { | 1160 { |
1124 CodeInjectionFilter.call(this, text, domains, script); | 1161 ContentFilter.call(this, text, domains, script); |
1125 } | 1162 } |
1126 exports.SnippetFilter = SnippetFilter; | 1163 exports.SnippetFilter = SnippetFilter; |
1127 | 1164 |
1128 SnippetFilter.prototype = extend(CodeInjectionFilter, { | 1165 SnippetFilter.prototype = extend(ContentFilter, { |
1129 type: "snippet" | 1166 type: "snippet", |
1130 }); | 1167 |
1168 /** | |
1169 * Script that should be executed | |
1170 * @type {string} | |
1171 */ | |
1172 get script() | |
1173 { | |
1174 return this.body; | |
1175 } | |
1176 }); | |
LEFT | RIGHT |