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 April 8, 2018, 9:16 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 element hiding filters should match 87 * Regular expression that content filters should match
88 * @type {RegExp} 88 * @type {RegExp}
89 */ 89 */
90 Filter.elemhideRegExp = /^([^/*|@"!]*?)#([@?])?#(.+)$/; 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.elemhideRegExp.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 filter = ElemHideBase.fromText( 135 filter = ContentFilter.fromText(text, match[1], match[2], match[3]);
136 text, match[1], match[2], match[3]
137 );
138 } 136 }
139 else if (text[0] == "!") 137 else if (text[0] == "!")
140 filter = new CommentFilter(text); 138 filter = new CommentFilter(text);
141 else 139 else
142 filter = RegExpFilter.fromText(text); 140 filter = RegExpFilter.fromText(text);
143 141
144 Filter.knownFilters.set(filter.text, filter); 142 Filter.knownFilters.set(filter.text, filter);
145 return filter; 143 return filter;
146 }; 144 };
147 145
(...skipping 29 matching lines...) Expand all
177 if (!text) 175 if (!text)
178 return text; 176 return text;
179 177
180 // Remove line breaks, tabs etc 178 // Remove line breaks, tabs etc
181 text = text.replace(/[^\S ]+/g, ""); 179 text = text.replace(/[^\S ]+/g, "");
182 180
183 // Don't remove spaces inside comments 181 // Don't remove spaces inside comments
184 if (/^ *!/.test(text)) 182 if (/^ *!/.test(text))
185 return text.trim(); 183 return text.trim();
186 184
187 // Special treatment for element hiding filters, right side is allowed to 185 // Special treatment for content filters, right side is allowed to contain
188 // contain spaces 186 // spaces
189 if (Filter.elemhideRegExp.test(text)) 187 if (Filter.contentRegExp.test(text))
190 { 188 {
191 let [, domain, separator, selector] = /^(.*?)(#@?#?)(.*)$/.exec(text); 189 let [, domains, separator, body] = /^(.*?)(#[@?$]?#?)(.*)$/.exec(text);
192 return domain.replace(/ +/g, "") + separator + selector.trim(); 190 return domains.replace(/ +/g, "") + separator + body.trim();
193 } 191 }
194 192
195 // 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
196 // are allowed to contain single (non trailing) spaces. 194 // are allowed to contain single (non trailing) spaces.
197 let strippedText = text.replace(/ +/g, ""); 195 let strippedText = text.replace(/ +/g, "");
198 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) 196 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText))
199 return strippedText; 197 return strippedText;
200 198
201 let optionsMatch = Filter.optionsRegExp.exec(strippedText); 199 let optionsMatch = Filter.optionsRegExp.exec(strippedText);
202 if (!optionsMatch) 200 if (!optionsMatch)
(...skipping 30 matching lines...) Expand all
233 return beforeOptions + "$" + options.join(); 231 return beforeOptions + "$" + options.join();
234 }; 232 };
235 233
236 /** 234 /**
237 * @see filterToRegExp 235 * @see filterToRegExp
238 */ 236 */
239 Filter.toRegExp = filterToRegExp; 237 Filter.toRegExp = filterToRegExp;
240 238
241 /** 239 /**
242 * Class for invalid filters 240 * Class for invalid filters
243 * @param {string} text see Filter() 241 * @param {string} text see {@link Filter Filter()}
244 * @param {string} reason Reason why this filter is invalid 242 * @param {string} reason Reason why this filter is invalid
245 * @constructor 243 * @constructor
246 * @augments Filter 244 * @augments Filter
247 */ 245 */
248 function InvalidFilter(text, reason) 246 function InvalidFilter(text, reason)
249 { 247 {
250 Filter.call(this, text); 248 Filter.call(this, text);
251 249
252 this.reason = reason; 250 this.reason = reason;
253 } 251 }
(...skipping 10 matching lines...) Expand all
264 262
265 /** 263 /**
266 * See Filter.serialize() 264 * See Filter.serialize()
267 * @inheritdoc 265 * @inheritdoc
268 */ 266 */
269 serialize(buffer) {} 267 serialize(buffer) {}
270 }); 268 });
271 269
272 /** 270 /**
273 * Class for comments 271 * Class for comments
274 * @param {string} text see Filter() 272 * @param {string} text see {@link Filter Filter()}
275 * @constructor 273 * @constructor
276 * @augments Filter 274 * @augments Filter
277 */ 275 */
278 function CommentFilter(text) 276 function CommentFilter(text)
279 { 277 {
280 Filter.call(this, text); 278 Filter.call(this, text);
281 } 279 }
282 exports.CommentFilter = CommentFilter; 280 exports.CommentFilter = CommentFilter;
283 281
284 CommentFilter.prototype = extend(Filter, { 282 CommentFilter.prototype = extend(Filter, {
285 type: "comment", 283 type: "comment",
286 284
287 /** 285 /**
288 * See Filter.serialize() 286 * See Filter.serialize()
289 * @inheritdoc 287 * @inheritdoc
290 */ 288 */
291 serialize(buffer) {} 289 serialize(buffer) {}
292 }); 290 });
293 291
294 /** 292 /**
295 * Abstract base class for filters that can get hits 293 * Abstract base class for filters that can get hits
296 * @param {string} text 294 * @param {string} text
297 * see Filter() 295 * see {@link Filter Filter()}
298 * @param {string} [domains] 296 * @param {string} [domains]
299 * Domains that the filter is restricted to separated by domainSeparator 297 * Domains that the filter is restricted to separated by domainSeparator
300 * e.g. "foo.com|bar.com|~baz.com" 298 * e.g. "foo.com|bar.com|~baz.com"
301 * @constructor 299 * @constructor
302 * @augments Filter 300 * @augments Filter
303 */ 301 */
304 function ActiveFilter(text, domains) 302 function ActiveFilter(text, domains)
305 { 303 {
306 Filter.call(this, text); 304 Filter.call(this, text);
307 305
308 this.domainSource = domains; 306 if (domains)
307 this.domainSource = domains;
309 } 308 }
310 exports.ActiveFilter = ActiveFilter; 309 exports.ActiveFilter = ActiveFilter;
311 310
312 ActiveFilter.prototype = extend(Filter, { 311 ActiveFilter.prototype = extend(Filter, {
313 _disabled: false, 312 _disabled: false,
314 _hitCount: 0, 313 _hitCount: 0,
315 _lastHit: 0, 314 _lastHit: 0,
316 315
317 /** 316 /**
318 * 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
367 { 366 {
368 let oldValue = this._lastHit; 367 let oldValue = this._lastHit;
369 this._lastHit = value; 368 this._lastHit = value;
370 FilterNotifier.triggerListeners("filter.lastHit", this, value, oldValue); 369 FilterNotifier.triggerListeners("filter.lastHit", this, value, oldValue);
371 } 370 }
372 return this._lastHit; 371 return this._lastHit;
373 }, 372 },
374 373
375 /** 374 /**
376 * String that the domains property should be generated from 375 * String that the domains property should be generated from
377 * @type {string} 376 * @type {?string}
378 */ 377 */
379 domainSource: null, 378 domainSource: null,
380 379
381 /** 380 /**
382 * Separator character used in domainSource property, must be 381 * Separator character used in domainSource property, must be
383 * overridden by subclasses 382 * overridden by subclasses
384 * @type {string} 383 * @type {string}
385 */ 384 */
386 domainSeparator: null, 385 domainSeparator: null,
387 386
388 /** 387 /**
389 * Determines whether the trailing dot in domain names isn't important and 388 * Determines whether domainSource is already lower-case,
390 * should be ignored, must be overridden by subclasses.
391 * @type {boolean}
392 */
393 ignoreTrailingDot: true,
394
395 /**
396 * Determines whether domainSource is already upper-case,
397 * can be overridden by subclasses. 389 * can be overridden by subclasses.
398 * @type {boolean} 390 * @type {boolean}
399 */ 391 */
400 domainSourceIsUpperCase: false, 392 domainSourceIsLowerCase: false,
401 393
402 /** 394 /**
403 * Map containing domains that this filter should match on/not match 395 * Map containing domains that this filter should match on/not match
404 * on or null if the filter should match on all domains 396 * on or null if the filter should match on all domains
405 * @type {?Map.<string,boolean>} 397 * @type {?Map.<string,boolean>}
406 */ 398 */
407 get domains() 399 get domains()
408 { 400 {
409 // Despite this property being cached, the getter is called 401 let prop = Object.getOwnPropertyDescriptor(this, "_domains");
410 // several times on Safari, due to WebKit bug 132872
411 let prop = Object.getOwnPropertyDescriptor(this, "domains");
412 if (prop) 402 if (prop)
413 return prop.value; 403 {
404 let {value} = prop;
405 return typeof value == "string" ?
406 new Map([[value, true], ["", false]]) : value;
407 }
414 408
415 let domains = null; 409 let domains = null;
416 410
417 if (this.domainSource) 411 if (this.domainSource)
418 { 412 {
419 let source = this.domainSource; 413 let source = this.domainSource;
420 if (!this.domainSourceIsUpperCase) 414 if (!this.domainSourceIsLowerCase)
421 { 415 {
422 // RegExpFilter already have uppercase domains 416 // RegExpFilter already have lowercase domains
423 source = source.toUpperCase(); 417 source = source.toLowerCase();
424 } 418 }
425 let list = source.split(this.domainSeparator); 419 let list = source.split(this.domainSeparator);
426 if (list.length == 1 && list[0][0] != "~") 420 if (list.length == 1 && list[0][0] != "~")
427 { 421 {
428 // Fast track for the common one-domain scenario 422 // Fast track for the common one-domain scenario
429 if (this.ignoreTrailingDot) 423 domains = list[0];
430 list[0] = list[0].replace(/\.+$/, "");
431 domains = new Map([["", false], [list[0], true]]);
432 } 424 }
433 else 425 else
434 { 426 {
435 let hasIncludes = false; 427 let hasIncludes = false;
436 for (let i = 0; i < list.length; i++) 428 for (let i = 0; i < list.length; i++)
437 { 429 {
438 let domain = list[i]; 430 let domain = list[i];
439 if (this.ignoreTrailingDot)
440 domain = domain.replace(/\.+$/, "");
441 if (domain == "") 431 if (domain == "")
442 continue; 432 continue;
443 433
444 let include; 434 let include;
445 if (domain[0] == "~") 435 if (domain[0] == "~")
446 { 436 {
447 include = false; 437 include = false;
448 domain = domain.substr(1); 438 domain = domain.substr(1);
449 } 439 }
450 else 440 else
451 { 441 {
452 include = true; 442 include = true;
453 hasIncludes = true; 443 hasIncludes = true;
454 } 444 }
455 445
456 if (!domains) 446 if (!domains)
457 domains = new Map(); 447 domains = new Map();
458 448
459 domains.set(domain, include); 449 domains.set(domain, include);
460 } 450 }
461 if (domains) 451 if (domains)
462 domains.set("", !hasIncludes); 452 domains.set("", !hasIncludes);
463 } 453 }
464 454
465 this.domainSource = null; 455 this.domainSource = null;
466 } 456 }
467 457
468 Object.defineProperty(this, "domains", {value: domains, enumerable: true}); 458 Object.defineProperty(this, "_domains", {value: domains});
469 return this.domains; 459 return this.domains;
470 }, 460 },
471 461
472 /** 462 /**
473 * 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
474 * @type {string[]} 464 * @type {?string[]}
475 */ 465 */
476 sitekeys: null, 466 sitekeys: null,
477 467
478 /** 468 /**
479 * Checks whether this filter is active on a domain. 469 * Checks whether this filter is active on a domain.
480 * @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
481 * @param {string} [sitekey] public key provided by the document 471 * @param {string} [sitekey] public key provided by the document
482 * @return {boolean} true in case of the filter being active 472 * @return {boolean} true in case of the filter being active
483 */ 473 */
484 isActiveOnDomain(docDomain, sitekey) 474 isActiveOnDomain(docDomain, sitekey)
485 { 475 {
486 // Sitekeys are case-sensitive so we shouldn't convert them to 476 // Sitekeys are case-sensitive so we shouldn't convert them to
487 // upper-case to avoid false positives here. Instead we need to 477 // upper-case to avoid false positives here. Instead we need to
488 // change the way filter options are parsed. 478 // change the way filter options are parsed.
489 if (this.sitekeys && 479 if (this.sitekeys &&
490 (!sitekey || this.sitekeys.indexOf(sitekey.toUpperCase()) < 0)) 480 (!sitekey || this.sitekeys.indexOf(sitekey.toUpperCase()) < 0))
491 { 481 {
492 return false; 482 return false;
493 } 483 }
494 484
495 // If no domains are set the rule matches everywhere 485 // If no domains are set the rule matches everywhere
496 if (!this.domains) 486 if (!this.domains)
497 return true; 487 return true;
498 488
499 // 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
500 // isn't restricted to specific domains 490 // isn't restricted to specific domains
501 if (!docDomain) 491 if (!docDomain)
502 return this.domains.get(""); 492 return this.domains.get("");
503 493
504 if (this.ignoreTrailingDot) 494 docDomain = docDomain.replace(/\.+$/, "").toLowerCase();
505 docDomain = docDomain.replace(/\.+$/, "");
506 docDomain = docDomain.toUpperCase();
507 495
508 while (true) 496 while (true)
509 { 497 {
510 let isDomainIncluded = this.domains.get(docDomain); 498 let isDomainIncluded = this.domains.get(docDomain);
511 if (typeof isDomainIncluded != "undefined") 499 if (typeof isDomainIncluded != "undefined")
512 return isDomainIncluded; 500 return isDomainIncluded;
513 501
514 let nextDot = docDomain.indexOf("."); 502 let nextDot = docDomain.indexOf(".");
515 if (nextDot < 0) 503 if (nextDot < 0)
516 break; 504 break;
517 docDomain = docDomain.substr(nextDot + 1); 505 docDomain = docDomain.substr(nextDot + 1);
518 } 506 }
519 return this.domains.get(""); 507 return this.domains.get("");
520 }, 508 },
521 509
522 /** 510 /**
523 * 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.
524 * @param {string} docDomain 512 * @param {string} docDomain
525 * @return {boolean} 513 * @return {boolean}
526 */ 514 */
527 isActiveOnlyOnDomain(docDomain) 515 isActiveOnlyOnDomain(docDomain)
528 { 516 {
529 if (!docDomain || !this.domains || this.domains.get("")) 517 if (!docDomain || !this.domains || this.domains.get(""))
530 return false; 518 return false;
531 519
532 if (this.ignoreTrailingDot) 520 docDomain = docDomain.replace(/\.+$/, "").toLowerCase();
533 docDomain = docDomain.replace(/\.+$/, "");
534 docDomain = docDomain.toUpperCase();
535 521
536 for (let [domain, isIncluded] of this.domains) 522 for (let [domain, isIncluded] of this.domains)
537 { 523 {
538 if (isIncluded && domain != docDomain) 524 if (isIncluded && domain != docDomain)
539 { 525 {
540 if (domain.length <= docDomain.length) 526 if (domain.length <= docDomain.length)
541 return false; 527 return false;
542 528
543 if (!domain.endsWith("." + docDomain)) 529 if (!domain.endsWith("." + docDomain))
544 return false; 530 return false;
(...skipping 27 matching lines...) Expand all
572 if (this._hitCount) 558 if (this._hitCount)
573 buffer.push("hitCount=" + this._hitCount); 559 buffer.push("hitCount=" + this._hitCount);
574 if (this._lastHit) 560 if (this._lastHit)
575 buffer.push("lastHit=" + this._lastHit); 561 buffer.push("lastHit=" + this._lastHit);
576 } 562 }
577 } 563 }
578 }); 564 });
579 565
580 /** 566 /**
581 * Abstract base class for RegExp-based filters 567 * Abstract base class for RegExp-based filters
582 * @param {string} text see Filter() 568 * @param {string} text see {@link Filter Filter()}
583 * @param {string} regexpSource 569 * @param {string} regexpSource
584 * filter part that the regular expression should be build from 570 * filter part that the regular expression should be build from
585 * @param {number} [contentType] 571 * @param {number} [contentType]
586 * Content types the filter applies to, combination of values from 572 * Content types the filter applies to, combination of values from
587 * RegExpFilter.typeMap 573 * RegExpFilter.typeMap
588 * @param {boolean} [matchCase] 574 * @param {boolean} [matchCase]
589 * Defines whether the filter should distinguish between lower and upper case 575 * Defines whether the filter should distinguish between lower and upper case
590 * letters 576 * letters
591 * @param {string} [domains] 577 * @param {string} [domains]
592 * 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
625 else 611 else
626 { 612 {
627 // 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
628 this.regexpSource = regexpSource; 614 this.regexpSource = regexpSource;
629 } 615 }
630 } 616 }
631 exports.RegExpFilter = RegExpFilter; 617 exports.RegExpFilter = RegExpFilter;
632 618
633 RegExpFilter.prototype = extend(ActiveFilter, { 619 RegExpFilter.prototype = extend(ActiveFilter, {
634 /** 620 /**
635 * @see ActiveFilter.domainSourceIsUpperCase 621 * @see ActiveFilter.domainSourceIsLowerCase
636 */ 622 */
637 domainSourceIsUpperCase: true, 623 domainSourceIsLowerCase: true,
638 624
639 /** 625 /**
640 * Number of filters contained, will always be 1 (required to 626 * Number of filters contained, will always be 1 (required to
641 * optimize Matcher). 627 * optimize Matcher).
642 * @type {number} 628 * @type {number}
643 */ 629 */
644 length: 1, 630 length: 1,
645 631
646 /** 632 /**
647 * @see ActiveFilter.domainSeparator 633 * @see ActiveFilter.domainSeparator
648 */ 634 */
649 domainSeparator: "|", 635 domainSeparator: "|",
650 636
651 /** 637 /**
652 * Expression from which a regular expression should be generated - 638 * Expression from which a regular expression should be generated -
653 * for delayed creation of the regexp property 639 * for delayed creation of the regexp property
654 * @type {string} 640 * @type {string}
655 */ 641 */
656 regexpSource: null, 642 regexpSource: null,
657 /** 643 /**
658 * Regular expression to be used when testing against this filter 644 * Regular expression to be used when testing against this filter
659 * @type {RegExp} 645 * @type {RegExp}
660 */ 646 */
661 get regexp() 647 get regexp()
662 { 648 {
663 // Despite this property being cached, the getter is called
664 // several times on Safari, due to WebKit bug 132872
665 let prop = Object.getOwnPropertyDescriptor(this, "regexp");
666 if (prop)
667 return prop.value;
668
669 let source = Filter.toRegExp(this.regexpSource); 649 let source = Filter.toRegExp(this.regexpSource);
670 let regexp = new RegExp(source, this.matchCase ? "" : "i"); 650 let regexp = new RegExp(source, this.matchCase ? "" : "i");
671 Object.defineProperty(this, "regexp", {value: regexp}); 651 Object.defineProperty(this, "regexp", {value: regexp});
652 this.regexpSource = null;
672 return regexp; 653 return regexp;
673 }, 654 },
674 /** 655 /**
675 * Content types the filter applies to, combination of values from 656 * Content types the filter applies to, combination of values from
676 * RegExpFilter.typeMap 657 * RegExpFilter.typeMap
677 * @type {number} 658 * @type {number}
678 */ 659 */
679 contentType: 0x7FFFFFFF, 660 contentType: 0x7FFFFFFF,
680 /** 661 /**
681 * Defines whether the filter should distinguish between lower and 662 * Defines whether the filter should distinguish between lower and
682 * upper case letters 663 * upper case letters
683 * @type {boolean} 664 * @type {boolean}
684 */ 665 */
685 matchCase: false, 666 matchCase: false,
686 /** 667 /**
687 * Defines whether the filter should apply to third-party or 668 * Defines whether the filter should apply to third-party or
688 * first-party content only. Can be null (apply to all content). 669 * first-party content only. Can be null (apply to all content).
689 * @type {boolean} 670 * @type {?boolean}
690 */ 671 */
691 thirdParty: null, 672 thirdParty: null,
692 673
693 /** 674 /**
694 * String that the sitekey property should be generated from 675 * String that the sitekey property should be generated from
695 * @type {string} 676 * @type {?string}
696 */ 677 */
697 sitekeySource: null, 678 sitekeySource: null,
698 679
699 /** 680 /**
700 * Array containing public keys of websites that this filter should apply to 681 * @see ActiveFilter.sitekeys
701 * @type {string[]}
702 */ 682 */
703 get sitekeys() 683 get sitekeys()
704 { 684 {
705 // Despite this property being cached, the getter is called
706 // several times on Safari, due to WebKit bug 132872
707 let prop = Object.getOwnPropertyDescriptor(this, "sitekeys");
708 if (prop)
709 return prop.value;
710
711 let sitekeys = null; 685 let sitekeys = null;
712 686
713 if (this.sitekeySource) 687 if (this.sitekeySource)
714 { 688 {
715 sitekeys = this.sitekeySource.split("|"); 689 sitekeys = this.sitekeySource.split("|");
716 this.sitekeySource = null; 690 this.sitekeySource = null;
717 } 691 }
718 692
719 Object.defineProperty( 693 Object.defineProperty(
720 this, "sitekeys", {value: sitekeys, enumerable: true} 694 this, "sitekeys", {value: sitekeys, enumerable: true}
721 ); 695 );
722 return this.sitekeys; 696 return this.sitekeys;
723 }, 697 },
724 698
725 /** 699 /**
726 * Tests whether the URL matches this filter 700 * Tests whether the URL matches this filter
727 * @param {string} location URL to be tested 701 * @param {string} location URL to be tested
728 * @param {number} typeMask bitmask of content / request types to match 702 * @param {number} typeMask bitmask of content / request types to match
729 * @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
730 * @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
731 * request 705 * request
732 * @param {string} sitekey public key provided by the document 706 * @param {string} [sitekey] public key provided by the document
733 * @return {boolean} true in case of a match 707 * @return {boolean} true in case of a match
734 */ 708 */
735 matches(location, typeMask, docDomain, thirdParty, sitekey) 709 matches(location, typeMask, docDomain, thirdParty, sitekey)
736 { 710 {
737 if (this.contentType & typeMask && 711 if (this.contentType & typeMask &&
738 (this.thirdParty == null || this.thirdParty == thirdParty) && 712 (this.thirdParty == null || this.thirdParty == thirdParty) &&
739 this.isActiveOnDomain(docDomain, sitekey) && this.regexp.test(location)) 713 this.isActiveOnDomain(docDomain, sitekey) && this.regexp.test(location))
740 { 714 {
741 return true; 715 return true;
742 } 716 }
743 return false; 717 return false;
744 } 718 }
745 }); 719 });
746 720
747 // Required to optimize Matcher, see also RegExpFilter.prototype.length 721 // Required to optimize Matcher, see also RegExpFilter.prototype.length
748 Object.defineProperty(RegExpFilter.prototype, "0", { 722 Object.defineProperty(RegExpFilter.prototype, "0", {
749 get() { return this; } 723 get() { return this; }
750 }); 724 });
751 725
752 /** 726 /**
753 * Creates a RegExp filter from its text representation 727 * Creates a RegExp filter from its text representation
754 * @param {string} text same as in Filter() 728 * @param {string} text same as in Filter()
755 * @return {Filter} 729 * @return {Filter}
756 */ 730 */
757 RegExpFilter.fromText = function(text) 731 RegExpFilter.fromText = function(text)
758 { 732 {
759 let blocking = true; 733 let blocking = true;
760 let origText = text; 734 let origText = text;
761 if (text.indexOf("@@") == 0) 735 if (text[0] == "@" && text[1] == "@")
762 { 736 {
763 blocking = false; 737 blocking = false;
764 text = text.substr(2); 738 text = text.substr(2);
765 } 739 }
766 740
767 let contentType = null; 741 let contentType = null;
768 let matchCase = null; 742 let matchCase = null;
769 let domains = null; 743 let domains = null;
770 let sitekeys = null; 744 let sitekeys = null;
771 let thirdParty = null; 745 let thirdParty = null;
772 let collapse = null; 746 let collapse = null;
773 let csp = null; 747 let csp = null;
774 let snippets = null; 748 let rewrite = null;
775 let options; 749 let options;
776 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); 750 let match = text.includes("$") ? Filter.optionsRegExp.exec(text) : null;
777 if (match) 751 if (match)
778 { 752 {
779 options = match[1].split(","); 753 options = match[1].split(",");
780 text = match.input.substr(0, match.index); 754 text = match.input.substr(0, match.index);
781 for (let option of options) 755 for (let option of options)
782 { 756 {
783 let value = null; 757 let value = null;
784 let separatorIndex = option.indexOf("="); 758 let separatorIndex = option.indexOf("=");
785 if (separatorIndex >= 0) 759 if (separatorIndex >= 0)
786 { 760 {
787 value = option.substr(separatorIndex + 1); 761 value = option.substr(separatorIndex + 1);
788 option = option.substr(0, separatorIndex); 762 option = option.substr(0, separatorIndex);
789 } 763 }
790 option = option.replace(/-/, "_").toUpperCase(); 764
791 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)
792 { 771 {
793 if (contentType == null) 772 if (inverse)
794 contentType = 0; 773 {
795 contentType |= RegExpFilter.typeMap[option]; 774 if (contentType == null)
796 775 ({contentType} = RegExpFilter.prototype);
797 if (option == "CSP" && value) 776 contentType &= ~type;
798 csp = value; 777 }
799 else if (option == "SNIPPET" && value) 778 else
800 snippets = value.split("|"); 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, snippets); 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
848 /** 852 /**
849 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks 853 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks
850 */ 854 */
851 RegExpFilter.typeMap = { 855 RegExpFilter.typeMap = {
852 OTHER: 1, 856 OTHER: 1,
853 SCRIPT: 2, 857 SCRIPT: 2,
854 IMAGE: 4, 858 IMAGE: 4,
855 STYLESHEET: 8, 859 STYLESHEET: 8,
856 OBJECT: 16, 860 OBJECT: 16,
857 SUBDOCUMENT: 32, 861 SUBDOCUMENT: 32,
858 DOCUMENT: 64, 862 DOCUMENT: 64,
859 WEBSOCKET: 128, 863 WEBSOCKET: 128,
860 WEBRTC: 256, 864 WEBRTC: 256,
861 CSP: 512, 865 CSP: 512,
862 XBL: 1, 866 XBL: 1,
863 PING: 1024, 867 PING: 1024,
864 XMLHTTPREQUEST: 2048, 868 XMLHTTPREQUEST: 2048,
865 OBJECT_SUBREQUEST: 4096, 869 OBJECT_SUBREQUEST: 4096,
866 SNIPPET: 8192,
867 DTD: 1, 870 DTD: 1,
868 MEDIA: 16384, 871 MEDIA: 16384,
869 FONT: 32768, 872 FONT: 32768,
870 873
871 BACKGROUND: 4, // Backwards compat, same as IMAGE 874 BACKGROUND: 4, // Backwards compat, same as IMAGE
872 875
873 POPUP: 0x10000000, 876 POPUP: 0x10000000,
874 GENERICBLOCK: 0x20000000, 877 GENERICBLOCK: 0x20000000,
875 ELEMHIDE: 0x40000000, 878 ELEMHIDE: 0x40000000,
876 GENERICHIDE: 0x80000000 879 GENERICHIDE: 0x80000000
877 }; 880 };
878 881
879 // CSP, SNIPPET, DOCUMENT, ELEMHIDE, POPUP, GENERICHIDE and GENERICBLOCK options 882 // CSP, DOCUMENT, ELEMHIDE, POPUP, GENERICHIDE and GENERICBLOCK options
880 // shouldn't be there by default 883 // shouldn't be there by default
881 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.CSP | 884 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.CSP |
882 RegExpFilter.typeMap.SNIPPET |
883 RegExpFilter.typeMap.DOCUMENT | 885 RegExpFilter.typeMap.DOCUMENT |
884 RegExpFilter.typeMap.ELEMHIDE | 886 RegExpFilter.typeMap.ELEMHIDE |
885 RegExpFilter.typeMap.POPUP | 887 RegExpFilter.typeMap.POPUP |
886 RegExpFilter.typeMap.GENERICHIDE | 888 RegExpFilter.typeMap.GENERICHIDE |
887 RegExpFilter.typeMap.GENERICBLOCK); 889 RegExpFilter.typeMap.GENERICBLOCK);
888 890
889 /** 891 /**
890 * Class for blocking filters 892 * Class for blocking filters
891 * @param {string} text see Filter() 893 * @param {string} text see {@link Filter Filter()}
892 * @param {string} regexpSource see RegExpFilter() 894 * @param {string} regexpSource see {@link RegExpFilter RegExpFilter()}
893 * @param {number} contentType see RegExpFilter() 895 * @param {number} [contentType] see {@link RegExpFilter RegExpFilter()}
894 * @param {boolean} matchCase see RegExpFilter() 896 * @param {boolean} [matchCase] see {@link RegExpFilter RegExpFilter()}
895 * @param {string} domains see RegExpFilter() 897 * @param {string} [domains] see {@link RegExpFilter RegExpFilter()}
896 * @param {boolean} thirdParty see RegExpFilter() 898 * @param {boolean} [thirdParty] see {@link RegExpFilter RegExpFilter()}
897 * @param {string} sitekeys see RegExpFilter() 899 * @param {string} [sitekeys] see {@link RegExpFilter RegExpFilter()}
898 * @param {boolean} collapse 900 * @param {boolean} [collapse]
899 * defines whether the filter should collapse blocked content, can be null 901 * defines whether the filter should collapse blocked content, can be null
900 * @param {string} [csp] 902 * @param {string} [csp]
901 * Content Security Policy to inject when the filter matches 903 * Content Security Policy to inject when the filter matches
902 * @param {string} [snippets] 904 * @param {?string} [rewrite]
903 * the code snippets to inject when the filter matches 905 * The (optional) rule specifying how to rewrite the URL. See
906 * BlockingFilter.prototype.rewrite.
904 * @constructor 907 * @constructor
905 * @augments RegExpFilter 908 * @augments RegExpFilter
906 */ 909 */
907 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, 910 function BlockingFilter(text, regexpSource, contentType, matchCase, domains,
908 thirdParty, sitekeys, collapse, csp, snippets) 911 thirdParty, sitekeys, collapse, csp, rewrite)
909 { 912 {
910 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, 913 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains,
911 thirdParty, sitekeys); 914 thirdParty, sitekeys);
912 915
913 this.collapse = collapse; 916 if (collapse != null)
914 this.csp = csp; 917 this.collapse = collapse;
915 this.snippets = snippets; 918
919 if (csp != null)
920 this.csp = csp;
921
922 if (rewrite != null)
923 this.rewrite = rewrite;
916 } 924 }
917 exports.BlockingFilter = BlockingFilter; 925 exports.BlockingFilter = BlockingFilter;
918 926
919 BlockingFilter.prototype = extend(RegExpFilter, { 927 BlockingFilter.prototype = extend(RegExpFilter, {
920 type: "blocking", 928 type: "blocking",
921 929
922 /** 930 /**
923 * Defines whether the filter should collapse blocked content. 931 * Defines whether the filter should collapse blocked content.
924 * Can be null (use the global preference). 932 * Can be null (use the global preference).
925 * @type {boolean} 933 * @type {?boolean}
926 */ 934 */
927 collapse: null, 935 collapse: null,
928 936
929 /** 937 /**
930 * Content Security Policy to inject for matching requests. 938 * Content Security Policy to inject for matching requests.
931 * @type {string} 939 * @type {?string}
932 */ 940 */
933 csp: null, 941 csp: null,
934 942
935 /** 943 /**
936 * The code snippets to inject for matching requests. 944 * The rule specifying how to rewrite the URL.
937 * @type {string[]} 945 * The syntax is similar to the one of String.prototype.replace().
938 */ 946 * @type {?string}
939 snippets: null 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 }
940 }); 969 });
941 970
942 /** 971 /**
943 * Class for whitelist filters 972 * Class for whitelist filters
944 * @param {string} text see Filter() 973 * @param {string} text see {@link Filter Filter()}
945 * @param {string} regexpSource see RegExpFilter() 974 * @param {string} regexpSource see {@link RegExpFilter RegExpFilter()}
946 * @param {number} contentType see RegExpFilter() 975 * @param {number} [contentType] see {@link RegExpFilter RegExpFilter()}
947 * @param {boolean} matchCase see RegExpFilter() 976 * @param {boolean} [matchCase] see {@link RegExpFilter RegExpFilter()}
948 * @param {string} domains see RegExpFilter() 977 * @param {string} [domains] see {@link RegExpFilter RegExpFilter()}
949 * @param {boolean} thirdParty see RegExpFilter() 978 * @param {boolean} [thirdParty] see {@link RegExpFilter RegExpFilter()}
950 * @param {string} sitekeys see RegExpFilter() 979 * @param {string} [sitekeys] see {@link RegExpFilter RegExpFilter()}
951 * @constructor 980 * @constructor
952 * @augments RegExpFilter 981 * @augments RegExpFilter
953 */ 982 */
954 function WhitelistFilter(text, regexpSource, contentType, matchCase, domains, 983 function WhitelistFilter(text, regexpSource, contentType, matchCase, domains,
955 thirdParty, sitekeys) 984 thirdParty, sitekeys)
956 { 985 {
957 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, 986 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains,
958 thirdParty, sitekeys); 987 thirdParty, sitekeys);
959 } 988 }
960 exports.WhitelistFilter = WhitelistFilter; 989 exports.WhitelistFilter = WhitelistFilter;
961 990
962 WhitelistFilter.prototype = extend(RegExpFilter, { 991 WhitelistFilter.prototype = extend(RegExpFilter, {
963 type: "whitelist" 992 type: "whitelist"
964 }); 993 });
965 994
966 /** 995 /**
967 * Base class for element hiding filters 996 * Base class for content filters
968 * @param {string} text see Filter() 997 * @param {string} text see {@link Filter Filter()}
969 * @param {string} [domains] Host names or domains the filter should be 998 * @param {string} [domains] Host names or domains the filter should be
970 * restricted to 999 * restricted to
971 * @param {string} selector CSS selector for the HTML elements that should be 1000 * @param {string} body The body of the filter
972 * hidden
973 * @constructor 1001 * @constructor
974 * @augments ActiveFilter 1002 * @augments ActiveFilter
975 */ 1003 */
1004 function ContentFilter(text, domains, body)
1005 {
1006 ActiveFilter.call(this, text, domains || null);
1007
1008 this.body = body;
1009 }
1010 exports.ContentFilter = ContentFilter;
1011
1012 ContentFilter.prototype = extend(ActiveFilter, {
1013 /**
1014 * @see ActiveFilter.domainSeparator
1015 */
1016 domainSeparator: ",",
1017
1018 /**
1019 * The body of the filter
1020 * @type {string}
1021 */
1022 body: null
1023 });
1024
1025 /**
1026 * Creates a content filter from a pre-parsed text representation
1027 *
1028 * @param {string} text same as in Filter()
1029 * @param {string} [domains]
1030 * domains part of the text representation
1031 * @param {string} [type]
1032 * rule type, either:
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
1040 * @return {ElemHideFilter|ElemHideException|
1041 * ElemHideEmulationFilter|SnippetFilter|InvalidFilter}
1042 */
1043 ContentFilter.fromText = function(text, domains, type, body)
1044 {
1045 // We don't allow content filters which have any empty domains.
1046 // Note: The ContentFilter.prototype.domainSeparator is duplicated here, if
1047 // that changes this must be changed too.
1048 if (domains && /(^|,)~?(,|$)/.test(domains))
1049 return new InvalidFilter(text, "filter_invalid_domain");
1050
1051 if (type == "@")
1052 return new ElemHideException(text, domains, body);
1053
1054 if (type == "$")
1055 return new SnippetFilter(text, domains, body);
1056
1057 if (type == "?")
1058 {
1059 // Element hiding emulation filters are inefficient so we need to make sure
1060 // that they're only applied if they specify active domains
1061 if (!/,[^~][^,.]*\.[^,]/.test("," + domains))
1062 return new InvalidFilter(text, "filter_elemhideemulation_nodomain");
1063
1064 return new ElemHideEmulationFilter(text, domains, body);
1065 }
1066
1067 return new ElemHideFilter(text, domains, body);
1068 };
1069
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 */
976 function ElemHideBase(text, domains, selector) 1079 function ElemHideBase(text, domains, selector)
977 { 1080 {
978 ActiveFilter.call(this, text, domains || null); 1081 ContentFilter.call(this, text, domains, selector);
979
980 if (domains)
981 {
982 this.selectorDomain = domains.replace(/,~[^,]+/g, "")
983 .replace(/^~[^,]+,?/, "").toLowerCase();
984 }
985
986 // Braces are being escaped to prevent CSS rule injection.
987 this.selector = selector.replace("{", "\\7B ").replace("}", "\\7D ");
988 } 1082 }
989 exports.ElemHideBase = ElemHideBase; 1083 exports.ElemHideBase = ElemHideBase;
990 1084
991 ElemHideBase.prototype = extend(ActiveFilter, { 1085 ElemHideBase.prototype = extend(ContentFilter, {
992 /**
993 * @see ActiveFilter.domainSeparator
994 */
995 domainSeparator: ",",
996
997 /**
998 * @see ActiveFilter.ignoreTrailingDot
999 */
1000 ignoreTrailingDot: false,
1001
1002 /**
1003 * Host name or domain the filter should be restricted to (can be null for
1004 * no restriction)
1005 * @type {string}
1006 */
1007 selectorDomain: null,
1008 /** 1086 /**
1009 * CSS selector for the HTML elements that should be hidden 1087 * CSS selector for the HTML elements that should be hidden
1010 * @type {string} 1088 * @type {string}
1011 */ 1089 */
1012 selector: null 1090 get selector()
1013 }); 1091 {
1014 1092 // Braces are being escaped to prevent CSS rule injection.
1015 /** 1093 return this.body.replace("{", "\\7B ").replace("}", "\\7D ");
1016 * Creates an element hiding filter from a pre-parsed text representation 1094 }
1017 * 1095 });
1018 * @param {string} text same as in Filter()
1019 * @param {string?} domain
1020 * domain part of the text representation
1021 * @param {string?} type
1022 * rule type, either empty or @ (exception) or ? (emulation rule)
1023 * @param {string} selector raw CSS selector
1024 * @return {ElemHideFilter|ElemHideException|
1025 * ElemHideEmulationFilter|InvalidFilter}
1026 */
1027 ElemHideBase.fromText = function(text, domain, type, selector)
1028 {
1029 // We don't allow ElemHide filters which have any empty domains.
1030 // Note: The ElemHide.prototype.domainSeparator is duplicated here, if that
1031 // changes this must be changed too.
1032 if (domain && /(^|,)~?(,|$)/.test(domain))
1033 return new InvalidFilter(text, "filter_invalid_domain");
1034
1035 if (type == "@")
1036 return new ElemHideException(text, domain, selector);
1037
1038 if (type == "?")
1039 {
1040 // Element hiding emulation filters are inefficient so we need to make sure
1041 // that they're only applied if they specify active domains
1042 if (!/,[^~][^,.]*\.[^,]/.test("," + domain))
1043 return new InvalidFilter(text, "filter_elemhideemulation_nodomain");
1044
1045 return new ElemHideEmulationFilter(text, domain, selector);
1046 }
1047
1048 return new ElemHideFilter(text, domain, selector);
1049 };
1050 1096
1051 /** 1097 /**
1052 * Class for element hiding filters 1098 * Class for element hiding filters
1053 * @param {string} text see Filter() 1099 * @param {string} text see {@link Filter Filter()}
1054 * @param {string} domains see ElemHideBase() 1100 * @param {string} [domains] see {@link ElemHideBase ElemHideBase()}
1055 * @param {string} selector see ElemHideBase() 1101 * @param {string} selector see {@link ElemHideBase ElemHideBase()}
1056 * @constructor 1102 * @constructor
1057 * @augments ElemHideBase 1103 * @augments ElemHideBase
1058 */ 1104 */
1059 function ElemHideFilter(text, domains, selector) 1105 function ElemHideFilter(text, domains, selector)
1060 { 1106 {
1061 ElemHideBase.call(this, text, domains, selector); 1107 ElemHideBase.call(this, text, domains, selector);
1062 } 1108 }
1063 exports.ElemHideFilter = ElemHideFilter; 1109 exports.ElemHideFilter = ElemHideFilter;
1064 1110
1065 ElemHideFilter.prototype = extend(ElemHideBase, { 1111 ElemHideFilter.prototype = extend(ElemHideBase, {
1066 type: "elemhide" 1112 type: "elemhide"
1067 }); 1113 });
1068 1114
1069 /** 1115 /**
1070 * Class for element hiding exceptions 1116 * Class for element hiding exceptions
1071 * @param {string} text see Filter() 1117 * @param {string} text see {@link Filter Filter()}
1072 * @param {string} domains see ElemHideBase() 1118 * @param {string} [domains] see {@link ElemHideBase ElemHideBase()}
1073 * @param {string} selector see ElemHideBase() 1119 * @param {string} selector see {@link ElemHideBase ElemHideBase()}
1074 * @constructor 1120 * @constructor
1075 * @augments ElemHideBase 1121 * @augments ElemHideBase
1076 */ 1122 */
1077 function ElemHideException(text, domains, selector) 1123 function ElemHideException(text, domains, selector)
1078 { 1124 {
1079 ElemHideBase.call(this, text, domains, selector); 1125 ElemHideBase.call(this, text, domains, selector);
1080 } 1126 }
1081 exports.ElemHideException = ElemHideException; 1127 exports.ElemHideException = ElemHideException;
1082 1128
1083 ElemHideException.prototype = extend(ElemHideBase, { 1129 ElemHideException.prototype = extend(ElemHideBase, {
1084 type: "elemhideexception" 1130 type: "elemhideexception"
1085 }); 1131 });
1086 1132
1087 /** 1133 /**
1088 * Class for element hiding emulation filters 1134 * Class for element hiding emulation filters
1089 * @param {string} text see Filter() 1135 * @param {string} text see {@link Filter Filter()}
1090 * @param {string} domains see ElemHideBase() 1136 * @param {string} domains see {@link ElemHideBase ElemHideBase()}
1091 * @param {string} selector see ElemHideBase() 1137 * @param {string} selector see {@link ElemHideBase ElemHideBase()}
1092 * @constructor 1138 * @constructor
1093 * @augments ElemHideBase 1139 * @augments ElemHideBase
1094 */ 1140 */
1095 function ElemHideEmulationFilter(text, domains, selector) 1141 function ElemHideEmulationFilter(text, domains, selector)
1096 { 1142 {
1097 ElemHideBase.call(this, text, domains, selector); 1143 ElemHideBase.call(this, text, domains, selector);
1098 } 1144 }
1099 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; 1145 exports.ElemHideEmulationFilter = ElemHideEmulationFilter;
1100 1146
1101 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { 1147 ElemHideEmulationFilter.prototype = extend(ElemHideBase, {
1102 type: "elemhideemulation" 1148 type: "elemhideemulation"
1103 }); 1149 });
1150
1151 /**
1152 * Class for snippet filters
1153 * @param {string} text see Filter()
1154 * @param {string} [domains] see ContentFilter()
1155 * @param {string} script Script that should be executed
1156 * @constructor
1157 * @augments ContentFilter
1158 */
1159 function SnippetFilter(text, domains, script)
1160 {
1161 ContentFilter.call(this, text, domains, script);
1162 }
1163 exports.SnippetFilter = SnippetFilter;
1164
1165 SnippetFilter.prototype = extend(ContentFilter, {
1166 type: "snippet",
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