Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: lib/filterClasses.js

Issue 29737558: Issue 6538, 6781 - Implement support for snippet filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Rebase Created June 11, 2018, 10 a.m.
Right Patch Set: Improve comment formatting Created July 11, 2018, 1:02 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | lib/filterListener.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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 script filters should match 87 * Regular expression that content filters should match
88 * @type {RegExp} 88 * @type {RegExp}
89 */ 89 */
90 Filter.scriptRegExp = /^([^/*|@"!]*?)#([@?$])?#(.+)$/; 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.scriptRegExp.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 filter = new SnippetFilter(text, match[1], match[3]);
137 else
138 filter = ElemHideBase.fromText(text, match[1], match[2], match[3]);
139 } 136 }
140 else if (text[0] == "!") 137 else if (text[0] == "!")
141 filter = new CommentFilter(text); 138 filter = new CommentFilter(text);
142 else 139 else
143 filter = RegExpFilter.fromText(text); 140 filter = RegExpFilter.fromText(text);
144 141
145 Filter.knownFilters.set(filter.text, filter); 142 Filter.knownFilters.set(filter.text, filter);
146 return filter; 143 return filter;
147 }; 144 };
148 145
(...skipping 29 matching lines...) Expand all
178 if (!text) 175 if (!text)
179 return text; 176 return text;
180 177
181 // Remove line breaks, tabs etc 178 // Remove line breaks, tabs etc
182 text = text.replace(/[^\S ]+/g, ""); 179 text = text.replace(/[^\S ]+/g, "");
183 180
184 // Don't remove spaces inside comments 181 // Don't remove spaces inside comments
185 if (/^ *!/.test(text)) 182 if (/^ *!/.test(text))
186 return text.trim(); 183 return text.trim();
187 184
188 // Special treatment for script filters, right side is allowed to contain 185 // Special treatment for content filters, right side is allowed to contain
189 // spaces 186 // spaces
190 if (Filter.scriptRegExp.test(text)) 187 if (Filter.contentRegExp.test(text))
191 { 188 {
192 let [, domains, separator, script] = /^(.*?)(#[@?$]?#?)(.*)$/.exec(text); 189 let [, domains, separator, body] = /^(.*?)(#[@?$]?#?)(.*)$/.exec(text);
193 return domains.replace(/ +/g, "") + separator + script.trim(); 190 return domains.replace(/ +/g, "") + separator + body.trim();
194 } 191 }
195 192
196 // 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
197 // are allowed to contain single (non trailing) spaces. 194 // are allowed to contain single (non trailing) spaces.
198 let strippedText = text.replace(/ +/g, ""); 195 let strippedText = text.replace(/ +/g, "");
199 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) 196 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText))
200 return strippedText; 197 return strippedText;
201 198
202 let optionsMatch = Filter.optionsRegExp.exec(strippedText); 199 let optionsMatch = Filter.optionsRegExp.exec(strippedText);
203 if (!optionsMatch) 200 if (!optionsMatch)
(...skipping 30 matching lines...) Expand all
234 return beforeOptions + "$" + options.join(); 231 return beforeOptions + "$" + options.join();
235 }; 232 };
236 233
237 /** 234 /**
238 * @see filterToRegExp 235 * @see filterToRegExp
239 */ 236 */
240 Filter.toRegExp = filterToRegExp; 237 Filter.toRegExp = filterToRegExp;
241 238
242 /** 239 /**
243 * Class for invalid filters 240 * Class for invalid filters
244 * @param {string} text see Filter() 241 * @param {string} text see {@link Filter Filter()}
245 * @param {string} reason Reason why this filter is invalid 242 * @param {string} reason Reason why this filter is invalid
246 * @constructor 243 * @constructor
247 * @augments Filter 244 * @augments Filter
248 */ 245 */
249 function InvalidFilter(text, reason) 246 function InvalidFilter(text, reason)
250 { 247 {
251 Filter.call(this, text); 248 Filter.call(this, text);
252 249
253 this.reason = reason; 250 this.reason = reason;
254 } 251 }
(...skipping 10 matching lines...) Expand all
265 262
266 /** 263 /**
267 * See Filter.serialize() 264 * See Filter.serialize()
268 * @inheritdoc 265 * @inheritdoc
269 */ 266 */
270 serialize(buffer) {} 267 serialize(buffer) {}
271 }); 268 });
272 269
273 /** 270 /**
274 * Class for comments 271 * Class for comments
275 * @param {string} text see Filter() 272 * @param {string} text see {@link Filter Filter()}
276 * @constructor 273 * @constructor
277 * @augments Filter 274 * @augments Filter
278 */ 275 */
279 function CommentFilter(text) 276 function CommentFilter(text)
280 { 277 {
281 Filter.call(this, text); 278 Filter.call(this, text);
282 } 279 }
283 exports.CommentFilter = CommentFilter; 280 exports.CommentFilter = CommentFilter;
284 281
285 CommentFilter.prototype = extend(Filter, { 282 CommentFilter.prototype = extend(Filter, {
286 type: "comment", 283 type: "comment",
287 284
288 /** 285 /**
289 * See Filter.serialize() 286 * See Filter.serialize()
290 * @inheritdoc 287 * @inheritdoc
291 */ 288 */
292 serialize(buffer) {} 289 serialize(buffer) {}
293 }); 290 });
294 291
295 /** 292 /**
296 * Abstract base class for filters that can get hits 293 * Abstract base class for filters that can get hits
297 * @param {string} text 294 * @param {string} text
298 * see Filter() 295 * see {@link Filter Filter()}
299 * @param {string} [domains] 296 * @param {string} [domains]
300 * Domains that the filter is restricted to separated by domainSeparator 297 * Domains that the filter is restricted to separated by domainSeparator
301 * e.g. "foo.com|bar.com|~baz.com" 298 * e.g. "foo.com|bar.com|~baz.com"
302 * @constructor 299 * @constructor
303 * @augments Filter 300 * @augments Filter
304 */ 301 */
305 function ActiveFilter(text, domains) 302 function ActiveFilter(text, domains)
306 { 303 {
307 Filter.call(this, text); 304 Filter.call(this, text);
308 305
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 domainSource: null, 378 domainSource: null,
382 379
383 /** 380 /**
384 * Separator character used in domainSource property, must be 381 * Separator character used in domainSource property, must be
385 * overridden by subclasses 382 * overridden by subclasses
386 * @type {string} 383 * @type {string}
387 */ 384 */
388 domainSeparator: null, 385 domainSeparator: null,
389 386
390 /** 387 /**
391 * Determines whether domainSource is already upper-case, 388 * Determines whether domainSource is already lower-case,
392 * can be overridden by subclasses. 389 * can be overridden by subclasses.
393 * @type {boolean} 390 * @type {boolean}
394 */ 391 */
395 domainSourceIsUpperCase: false, 392 domainSourceIsLowerCase: false,
396 393
397 /** 394 /**
398 * Map containing domains that this filter should match on/not match 395 * Map containing domains that this filter should match on/not match
399 * on or null if the filter should match on all domains 396 * on or null if the filter should match on all domains
400 * @type {?Map.<string,boolean>} 397 * @type {?Map.<string,boolean>}
401 */ 398 */
402 get domains() 399 get domains()
403 { 400 {
401 let prop = Object.getOwnPropertyDescriptor(this, "_domains");
402 if (prop)
403 {
404 let {value} = prop;
405 return typeof value == "string" ?
406 new Map([[value, true], ["", false]]) : value;
407 }
408
404 let domains = null; 409 let domains = null;
405 410
406 if (this.domainSource) 411 if (this.domainSource)
407 { 412 {
408 let source = this.domainSource; 413 let source = this.domainSource;
409 if (!this.domainSourceIsUpperCase) 414 if (!this.domainSourceIsLowerCase)
410 { 415 {
411 // RegExpFilter already have uppercase domains 416 // RegExpFilter already have lowercase domains
412 source = source.toUpperCase(); 417 source = source.toLowerCase();
413 } 418 }
414 let list = source.split(this.domainSeparator); 419 let list = source.split(this.domainSeparator);
415 if (list.length == 1 && list[0][0] != "~") 420 if (list.length == 1 && list[0][0] != "~")
416 { 421 {
417 // Fast track for the common one-domain scenario 422 // Fast track for the common one-domain scenario
418 domains = new Map([["", false], [list[0], true]]); 423 domains = list[0];
419 } 424 }
420 else 425 else
421 { 426 {
422 let hasIncludes = false; 427 let hasIncludes = false;
423 for (let i = 0; i < list.length; i++) 428 for (let i = 0; i < list.length; i++)
424 { 429 {
425 let domain = list[i]; 430 let domain = list[i];
426 if (domain == "") 431 if (domain == "")
427 continue; 432 continue;
428 433
(...skipping 14 matching lines...) Expand all
443 448
444 domains.set(domain, include); 449 domains.set(domain, include);
445 } 450 }
446 if (domains) 451 if (domains)
447 domains.set("", !hasIncludes); 452 domains.set("", !hasIncludes);
448 } 453 }
449 454
450 this.domainSource = null; 455 this.domainSource = null;
451 } 456 }
452 457
453 Object.defineProperty(this, "domains", {value: domains, enumerable: true}); 458 Object.defineProperty(this, "_domains", {value: domains});
454 return this.domains; 459 return this.domains;
455 }, 460 },
456 461
457 /** 462 /**
458 * 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
459 * @type {?string[]} 464 * @type {?string[]}
460 */ 465 */
461 sitekeys: null, 466 sitekeys: null,
462 467
463 /** 468 /**
(...skipping 15 matching lines...) Expand all
479 484
480 // If no domains are set the rule matches everywhere 485 // If no domains are set the rule matches everywhere
481 if (!this.domains) 486 if (!this.domains)
482 return true; 487 return true;
483 488
484 // 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
485 // isn't restricted to specific domains 490 // isn't restricted to specific domains
486 if (!docDomain) 491 if (!docDomain)
487 return this.domains.get(""); 492 return this.domains.get("");
488 493
489 docDomain = docDomain.replace(/\.+$/, "").toUpperCase(); 494 docDomain = docDomain.replace(/\.+$/, "").toLowerCase();
490 495
491 while (true) 496 while (true)
492 { 497 {
493 let isDomainIncluded = this.domains.get(docDomain); 498 let isDomainIncluded = this.domains.get(docDomain);
494 if (typeof isDomainIncluded != "undefined") 499 if (typeof isDomainIncluded != "undefined")
495 return isDomainIncluded; 500 return isDomainIncluded;
496 501
497 let nextDot = docDomain.indexOf("."); 502 let nextDot = docDomain.indexOf(".");
498 if (nextDot < 0) 503 if (nextDot < 0)
499 break; 504 break;
500 docDomain = docDomain.substr(nextDot + 1); 505 docDomain = docDomain.substr(nextDot + 1);
501 } 506 }
502 return this.domains.get(""); 507 return this.domains.get("");
503 }, 508 },
504 509
505 /** 510 /**
506 * 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.
507 * @param {string} docDomain 512 * @param {string} docDomain
508 * @return {boolean} 513 * @return {boolean}
509 */ 514 */
510 isActiveOnlyOnDomain(docDomain) 515 isActiveOnlyOnDomain(docDomain)
511 { 516 {
512 if (!docDomain || !this.domains || this.domains.get("")) 517 if (!docDomain || !this.domains || this.domains.get(""))
513 return false; 518 return false;
514 519
515 docDomain = docDomain.replace(/\.+$/, "").toUpperCase(); 520 docDomain = docDomain.replace(/\.+$/, "").toLowerCase();
516 521
517 for (let [domain, isIncluded] of this.domains) 522 for (let [domain, isIncluded] of this.domains)
518 { 523 {
519 if (isIncluded && domain != docDomain) 524 if (isIncluded && domain != docDomain)
520 { 525 {
521 if (domain.length <= docDomain.length) 526 if (domain.length <= docDomain.length)
522 return false; 527 return false;
523 528
524 if (!domain.endsWith("." + docDomain)) 529 if (!domain.endsWith("." + docDomain))
525 return false; 530 return false;
(...skipping 27 matching lines...) Expand all
553 if (this._hitCount) 558 if (this._hitCount)
554 buffer.push("hitCount=" + this._hitCount); 559 buffer.push("hitCount=" + this._hitCount);
555 if (this._lastHit) 560 if (this._lastHit)
556 buffer.push("lastHit=" + this._lastHit); 561 buffer.push("lastHit=" + this._lastHit);
557 } 562 }
558 } 563 }
559 }); 564 });
560 565
561 /** 566 /**
562 * Abstract base class for RegExp-based filters 567 * Abstract base class for RegExp-based filters
563 * @param {string} text see Filter() 568 * @param {string} text see {@link Filter Filter()}
564 * @param {string} regexpSource 569 * @param {string} regexpSource
565 * filter part that the regular expression should be build from 570 * filter part that the regular expression should be build from
566 * @param {number} [contentType] 571 * @param {number} [contentType]
567 * Content types the filter applies to, combination of values from 572 * Content types the filter applies to, combination of values from
568 * RegExpFilter.typeMap 573 * RegExpFilter.typeMap
569 * @param {boolean} [matchCase] 574 * @param {boolean} [matchCase]
570 * Defines whether the filter should distinguish between lower and upper case 575 * Defines whether the filter should distinguish between lower and upper case
571 * letters 576 * letters
572 * @param {string} [domains] 577 * @param {string} [domains]
573 * 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
606 else 611 else
607 { 612 {
608 // 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
609 this.regexpSource = regexpSource; 614 this.regexpSource = regexpSource;
610 } 615 }
611 } 616 }
612 exports.RegExpFilter = RegExpFilter; 617 exports.RegExpFilter = RegExpFilter;
613 618
614 RegExpFilter.prototype = extend(ActiveFilter, { 619 RegExpFilter.prototype = extend(ActiveFilter, {
615 /** 620 /**
616 * @see ActiveFilter.domainSourceIsUpperCase 621 * @see ActiveFilter.domainSourceIsLowerCase
617 */ 622 */
618 domainSourceIsUpperCase: true, 623 domainSourceIsLowerCase: true,
619 624
620 /** 625 /**
621 * Number of filters contained, will always be 1 (required to 626 * Number of filters contained, will always be 1 (required to
622 * optimize Matcher). 627 * optimize Matcher).
623 * @type {number} 628 * @type {number}
624 */ 629 */
625 length: 1, 630 length: 1,
626 631
627 /** 632 /**
628 * @see ActiveFilter.domainSeparator 633 * @see ActiveFilter.domainSeparator
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
749 text = match.input.substr(0, match.index); 754 text = match.input.substr(0, match.index);
750 for (let option of options) 755 for (let option of options)
751 { 756 {
752 let value = null; 757 let value = null;
753 let separatorIndex = option.indexOf("="); 758 let separatorIndex = option.indexOf("=");
754 if (separatorIndex >= 0) 759 if (separatorIndex >= 0)
755 { 760 {
756 value = option.substr(separatorIndex + 1); 761 value = option.substr(separatorIndex + 1);
757 option = option.substr(0, separatorIndex); 762 option = option.substr(0, separatorIndex);
758 } 763 }
759 option = option.replace(/-/, "_").toUpperCase(); 764
760 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)
761 { 771 {
762 contentType |= RegExpFilter.typeMap[option]; 772 if (inverse)
763 773 {
764 if (option == "CSP" && value) 774 if (contentType == null)
765 csp = value; 775 ({contentType} = RegExpFilter.prototype);
776 contentType &= ~type;
777 }
778 else
779 {
780 contentType |= type;
781
782 if (type == RegExpFilter.typeMap.CSP && value)
783 csp = value;
784 }
766 } 785 }
767 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap) 786 else
768 { 787 {
769 if (contentType == null) 788 switch (option.toLowerCase())
770 ({contentType} = RegExpFilter.prototype); 789 {
771 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 }
772 } 817 }
773 else if (option == "MATCH_CASE")
774 matchCase = true;
775 else if (option == "~MATCH_CASE")
776 matchCase = false;
777 else if (option == "DOMAIN" && value)
778 domains = value.toUpperCase();
779 else if (option == "THIRD_PARTY")
780 thirdParty = true;
781 else if (option == "~THIRD_PARTY")
782 thirdParty = false;
783 else if (option == "COLLAPSE")
784 collapse = true;
785 else if (option == "~COLLAPSE")
786 collapse = false;
787 else if (option == "SITEKEY" && value)
788 sitekeys = value.toUpperCase();
789 else if (option == "REWRITE" && value)
790 rewrite = value;
791 else
792 return new InvalidFilter(origText, "filter_unknown_option");
793 } 818 }
794 } 819 }
795 820
796 // For security reasons, never match $rewrite filters 821 // For security reasons, never match $rewrite filters
797 // against requests that might load any code to be executed. 822 // against requests that might load any code to be executed.
798 if (rewrite != null) 823 if (rewrite != null)
799 { 824 {
800 if (contentType == null) 825 if (contentType == null)
801 ({contentType} = RegExpFilter.prototype); 826 ({contentType} = RegExpFilter.prototype);
802 contentType &= ~(RegExpFilter.typeMap.SCRIPT | 827 contentType &= ~(RegExpFilter.typeMap.SCRIPT |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
858 // shouldn't be there by default 883 // shouldn't be there by default
859 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.CSP | 884 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.CSP |
860 RegExpFilter.typeMap.DOCUMENT | 885 RegExpFilter.typeMap.DOCUMENT |
861 RegExpFilter.typeMap.ELEMHIDE | 886 RegExpFilter.typeMap.ELEMHIDE |
862 RegExpFilter.typeMap.POPUP | 887 RegExpFilter.typeMap.POPUP |
863 RegExpFilter.typeMap.GENERICHIDE | 888 RegExpFilter.typeMap.GENERICHIDE |
864 RegExpFilter.typeMap.GENERICBLOCK); 889 RegExpFilter.typeMap.GENERICBLOCK);
865 890
866 /** 891 /**
867 * Class for blocking filters 892 * Class for blocking filters
868 * @param {string} text see Filter() 893 * @param {string} text see {@link Filter Filter()}
869 * @param {string} regexpSource see RegExpFilter() 894 * @param {string} regexpSource see {@link RegExpFilter RegExpFilter()}
870 * @param {number} [contentType] see RegExpFilter() 895 * @param {number} [contentType] see {@link RegExpFilter RegExpFilter()}
871 * @param {boolean} [matchCase] see RegExpFilter() 896 * @param {boolean} [matchCase] see {@link RegExpFilter RegExpFilter()}
872 * @param {string} [domains] see RegExpFilter() 897 * @param {string} [domains] see {@link RegExpFilter RegExpFilter()}
873 * @param {boolean} [thirdParty] see RegExpFilter() 898 * @param {boolean} [thirdParty] see {@link RegExpFilter RegExpFilter()}
874 * @param {string} [sitekeys] see RegExpFilter() 899 * @param {string} [sitekeys] see {@link RegExpFilter RegExpFilter()}
875 * @param {boolean} [collapse] 900 * @param {boolean} [collapse]
876 * defines whether the filter should collapse blocked content, can be null 901 * defines whether the filter should collapse blocked content, can be null
877 * @param {string} [csp] 902 * @param {string} [csp]
878 * Content Security Policy to inject when the filter matches 903 * Content Security Policy to inject when the filter matches
879 * @param {?string} [rewrite] 904 * @param {?string} [rewrite]
880 * The (optional) rule specifying how to rewrite the URL. See 905 * The (optional) rule specifying how to rewrite the URL. See
881 * BlockingFilter.prototype.rewrite. 906 * BlockingFilter.prototype.rewrite.
882 * @constructor 907 * @constructor
883 * @augments RegExpFilter 908 * @augments RegExpFilter
884 */ 909 */
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
938 catch (e) 963 catch (e)
939 { 964 {
940 } 965 }
941 966
942 return url; 967 return url;
943 } 968 }
944 }); 969 });
945 970
946 /** 971 /**
947 * Class for whitelist filters 972 * Class for whitelist filters
948 * @param {string} text see Filter() 973 * @param {string} text see {@link Filter Filter()}
949 * @param {string} regexpSource see RegExpFilter() 974 * @param {string} regexpSource see {@link RegExpFilter RegExpFilter()}
950 * @param {number} [contentType] see RegExpFilter() 975 * @param {number} [contentType] see {@link RegExpFilter RegExpFilter()}
951 * @param {boolean} [matchCase] see RegExpFilter() 976 * @param {boolean} [matchCase] see {@link RegExpFilter RegExpFilter()}
952 * @param {string} [domains] see RegExpFilter() 977 * @param {string} [domains] see {@link RegExpFilter RegExpFilter()}
953 * @param {boolean} [thirdParty] see RegExpFilter() 978 * @param {boolean} [thirdParty] see {@link RegExpFilter RegExpFilter()}
954 * @param {string} [sitekeys] see RegExpFilter() 979 * @param {string} [sitekeys] see {@link RegExpFilter RegExpFilter()}
955 * @constructor 980 * @constructor
956 * @augments RegExpFilter 981 * @augments RegExpFilter
957 */ 982 */
958 function WhitelistFilter(text, regexpSource, contentType, matchCase, domains, 983 function WhitelistFilter(text, regexpSource, contentType, matchCase, domains,
959 thirdParty, sitekeys) 984 thirdParty, sitekeys)
960 { 985 {
961 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, 986 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains,
962 thirdParty, sitekeys); 987 thirdParty, sitekeys);
963 } 988 }
964 exports.WhitelistFilter = WhitelistFilter; 989 exports.WhitelistFilter = WhitelistFilter;
965 990
966 WhitelistFilter.prototype = extend(RegExpFilter, { 991 WhitelistFilter.prototype = extend(RegExpFilter, {
967 type: "whitelist" 992 type: "whitelist"
968 }); 993 });
969 994
970 /** 995 /**
971 * Base class for script filters 996 * Base class for content filters
972 * @param {string} text see Filter() 997 * @param {string} text see {@link Filter Filter()}
973 * @param {string} [domains] Host names or domains the filter should be 998 * @param {string} [domains] Host names or domains the filter should be
974 * restricted to 999 * restricted to
975 * @param {string} script Script that should be executed 1000 * @param {string} body The body of the filter
976 * @constructor 1001 * @constructor
977 * @augments ActiveFilter 1002 * @augments ActiveFilter
978 */ 1003 */
979 function ScriptFilter(text, domains, script) 1004 function ContentFilter(text, domains, body)
980 { 1005 {
981 ActiveFilter.call(this, text, domains || null); 1006 ActiveFilter.call(this, text, domains || null);
982 1007
983 this.script = script; 1008 this.body = body;
984 } 1009 }
985 exports.ScriptFilter = ScriptFilter; 1010 exports.ContentFilter = ContentFilter;
986 1011
987 ScriptFilter.prototype = extend(ActiveFilter, { 1012 ContentFilter.prototype = extend(ActiveFilter, {
988 /** 1013 /**
989 * @see ActiveFilter.domainSeparator 1014 * @see ActiveFilter.domainSeparator
990 */ 1015 */
991 domainSeparator: ",", 1016 domainSeparator: ",",
992 1017
993 /** 1018 /**
994 * Script that should be executed 1019 * The body of the filter
995 * @type {string} 1020 * @type {string}
996 */ 1021 */
997 script: null 1022 body: null
998 }); 1023 });
999 1024
1000 /* 1025 /**
1001 * Base class for element hiding filters 1026 * Creates a content filter from a pre-parsed text representation
1002 * @param {string} text see Filter()
1003 * @param {string} [domains] see ScriptFilter()
1004 * @param {string} selector CSS selector for the HTML elements that should be
1005 * hidden
1006 * @constructor
1007 * @augments ScriptFilter
1008 */
1009 function ElemHideBase(text, domains, selector)
1010 {
1011 ScriptFilter.call(this, text, domains, selector);
1012
1013 // Braces are being escaped to prevent CSS rule injection.
1014 this.selector = this.script.replace("{", "\\7B ").replace("}", "\\7D ");
1015 }
1016 exports.ElemHideBase = ElemHideBase;
1017
1018 ElemHideBase.prototype = extend(ScriptFilter, {
1019 /**
1020 * CSS selector for the HTML elements that should be hidden
1021 * @type {string}
1022 */
1023 selector: null
1024 });
1025
1026 /**
1027 * Creates an element hiding filter from a pre-parsed text representation
1028 * 1027 *
1029 * @param {string} text same as in Filter() 1028 * @param {string} text same as in Filter()
1030 * @param {string} [domains] 1029 * @param {string} [domains]
1031 * domains part of the text representation 1030 * domains part of the text representation
1032 * @param {string} [type] 1031 * @param {string} [type]
1033 * rule type, either empty or @ (exception) or ? (emulation rule) 1032 * rule type, either:
1034 * @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
1035 * @return {ElemHideFilter|ElemHideException| 1040 * @return {ElemHideFilter|ElemHideException|
1036 * ElemHideEmulationFilter|InvalidFilter} 1041 * ElemHideEmulationFilter|SnippetFilter|InvalidFilter}
1037 */ 1042 */
1038 ElemHideBase.fromText = function(text, domains, type, selector) 1043 ContentFilter.fromText = function(text, domains, type, body)
1039 { 1044 {
1040 // We don't allow ElemHide filters which have any empty domains. 1045 // We don't allow content filters which have any empty domains.
1041 // Note: The ElemHide.prototype.domainSeparator is duplicated here, if that 1046 // Note: The ContentFilter.prototype.domainSeparator is duplicated here, if
1042 // changes this must be changed too. 1047 // that changes this must be changed too.
1043 if (domains && /(^|,)~?(,|$)/.test(domains)) 1048 if (domains && /(^|,)~?(,|$)/.test(domains))
1044 return new InvalidFilter(text, "filter_invalid_domain"); 1049 return new InvalidFilter(text, "filter_invalid_domain");
1045 1050
1046 if (type == "@") 1051 if (type == "@")
1047 return new ElemHideException(text, domains, selector); 1052 return new ElemHideException(text, domains, body);
1053
1054 if (type == "$")
1055 return new SnippetFilter(text, domains, body);
1048 1056
1049 if (type == "?") 1057 if (type == "?")
1050 { 1058 {
1051 // 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
1052 // that they're only applied if they specify active domains 1060 // that they're only applied if they specify active domains
1053 if (!/,[^~][^,.]*\.[^,]/.test("," + domains)) 1061 if (!/,[^~][^,.]*\.[^,]/.test("," + domains))
1054 return new InvalidFilter(text, "filter_elemhideemulation_nodomain"); 1062 return new InvalidFilter(text, "filter_elemhideemulation_nodomain");
1055 1063
1056 return new ElemHideEmulationFilter(text, domains, selector); 1064 return new ElemHideEmulationFilter(text, domains, body);
1057 } 1065 }
1058 1066
1059 return new ElemHideFilter(text, domains, selector); 1067 return new ElemHideFilter(text, domains, body);
1060 }; 1068 };
1061 1069
1062 /** 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 /**
1063 * Class for element hiding filters 1098 * Class for element hiding filters
1064 * @param {string} text see Filter() 1099 * @param {string} text see {@link Filter Filter()}
1065 * @param {string} [domains] see ElemHideBase() 1100 * @param {string} [domains] see {@link ElemHideBase ElemHideBase()}
1066 * @param {string} selector see ElemHideBase() 1101 * @param {string} selector see {@link ElemHideBase ElemHideBase()}
1067 * @constructor 1102 * @constructor
1068 * @augments ElemHideBase 1103 * @augments ElemHideBase
1069 */ 1104 */
1070 function ElemHideFilter(text, domains, selector) 1105 function ElemHideFilter(text, domains, selector)
1071 { 1106 {
1072 ElemHideBase.call(this, text, domains, selector); 1107 ElemHideBase.call(this, text, domains, selector);
1073 } 1108 }
1074 exports.ElemHideFilter = ElemHideFilter; 1109 exports.ElemHideFilter = ElemHideFilter;
1075 1110
1076 ElemHideFilter.prototype = extend(ElemHideBase, { 1111 ElemHideFilter.prototype = extend(ElemHideBase, {
1077 type: "elemhide" 1112 type: "elemhide"
1078 }); 1113 });
1079 1114
1080 /** 1115 /**
1081 * Class for element hiding exceptions 1116 * Class for element hiding exceptions
1082 * @param {string} text see Filter() 1117 * @param {string} text see {@link Filter Filter()}
1083 * @param {string} [domains] see ElemHideBase() 1118 * @param {string} [domains] see {@link ElemHideBase ElemHideBase()}
1084 * @param {string} selector see ElemHideBase() 1119 * @param {string} selector see {@link ElemHideBase ElemHideBase()}
1085 * @constructor 1120 * @constructor
1086 * @augments ElemHideBase 1121 * @augments ElemHideBase
1087 */ 1122 */
1088 function ElemHideException(text, domains, selector) 1123 function ElemHideException(text, domains, selector)
1089 { 1124 {
1090 ElemHideBase.call(this, text, domains, selector); 1125 ElemHideBase.call(this, text, domains, selector);
1091 } 1126 }
1092 exports.ElemHideException = ElemHideException; 1127 exports.ElemHideException = ElemHideException;
1093 1128
1094 ElemHideException.prototype = extend(ElemHideBase, { 1129 ElemHideException.prototype = extend(ElemHideBase, {
1095 type: "elemhideexception" 1130 type: "elemhideexception"
1096 }); 1131 });
1097 1132
1098 /** 1133 /**
1099 * Class for element hiding emulation filters 1134 * Class for element hiding emulation filters
1100 * @param {string} text see Filter() 1135 * @param {string} text see {@link Filter Filter()}
1101 * @param {string} domains see ElemHideBase() 1136 * @param {string} domains see {@link ElemHideBase ElemHideBase()}
1102 * @param {string} selector see ElemHideBase() 1137 * @param {string} selector see {@link ElemHideBase ElemHideBase()}
1103 * @constructor 1138 * @constructor
1104 * @augments ElemHideBase 1139 * @augments ElemHideBase
1105 */ 1140 */
1106 function ElemHideEmulationFilter(text, domains, selector) 1141 function ElemHideEmulationFilter(text, domains, selector)
1107 { 1142 {
1108 ElemHideBase.call(this, text, domains, selector); 1143 ElemHideBase.call(this, text, domains, selector);
1109 } 1144 }
1110 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; 1145 exports.ElemHideEmulationFilter = ElemHideEmulationFilter;
1111 1146
1112 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { 1147 ElemHideEmulationFilter.prototype = extend(ElemHideBase, {
1113 type: "elemhideemulation" 1148 type: "elemhideemulation"
1114 }); 1149 });
1115 1150
1116 /** 1151 /**
1117 * Class for snippet filters 1152 * Class for snippet filters
1118 * @param {string} text see Filter() 1153 * @param {string} text see Filter()
1119 * @param {string} [domains] see ScriptFilter() 1154 * @param {string} [domains] see ContentFilter()
1120 * @param {string} script Script that should be executed 1155 * @param {string} script Script that should be executed
1121 * @constructor 1156 * @constructor
1122 * @augments ScriptFilter 1157 * @augments ContentFilter
1123 */ 1158 */
1124 function SnippetFilter(text, domains, script) 1159 function SnippetFilter(text, domains, script)
1125 { 1160 {
1126 ScriptFilter.call(this, text, domains, script); 1161 ContentFilter.call(this, text, domains, script);
1127 } 1162 }
1128 exports.SnippetFilter = SnippetFilter; 1163 exports.SnippetFilter = SnippetFilter;
1129 1164
1130 SnippetFilter.prototype = extend(ScriptFilter, { 1165 SnippetFilter.prototype = extend(ContentFilter, {
1131 type: "snippet" 1166 type: "snippet",
1132 }); 1167
1168 /**
1169 * Script that should be executed
1170 * @type {string}
1171 */
1172 get script()
1173 {
1174 return this.body;
1175 }
1176 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld