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

Side by Side Diff: lib/filterClasses.js

Issue 29791555: Issue 6727 - Use string rather than map for single-domain filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Rebase Created June 6, 2018, 11:39 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/elemHide.js ('k') | test/filterClasses.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 375 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 domainSeparator: null, 386 domainSeparator: null,
387 387
388 /** 388 /**
389 * Determines whether domainSource is already upper-case, 389 * Determines whether domainSource is already upper-case,
390 * can be overridden by subclasses. 390 * can be overridden by subclasses.
391 * @type {boolean} 391 * @type {boolean}
392 */ 392 */
393 domainSourceIsUpperCase: false, 393 domainSourceIsUpperCase: false,
394 394
395 /** 395 /**
396 * Map containing domains that this filter should match on/not match 396 * String specifying the domain that this filter should match on, or map
397 * on or null if the filter should match on all domains 397 * containing domains that this filter should match on/not match on, or null
398 * @type {?Map.<string,boolean>} 398 * if the filter should match on all domains
399 * @type {?Map.<string,boolean>|string}
399 */ 400 */
400 get domains() 401 get domains()
401 { 402 {
402 // Despite this property being cached, the getter is called 403 // Despite this property being cached, the getter is called
403 // several times on Safari, due to WebKit bug 132872 404 // several times on Safari, due to WebKit bug 132872
404 let prop = Object.getOwnPropertyDescriptor(this, "domains"); 405 let prop = Object.getOwnPropertyDescriptor(this, "domains");
405 if (prop) 406 if (prop)
406 return prop.value; 407 return prop.value;
407 408
408 let domains = null; 409 let domains = null;
409 410
410 if (this.domainSource) 411 if (this.domainSource)
411 { 412 {
412 let source = this.domainSource; 413 let source = this.domainSource;
413 if (!this.domainSourceIsUpperCase) 414 if (!this.domainSourceIsUpperCase)
414 { 415 {
415 // RegExpFilter already have uppercase domains 416 // RegExpFilter already have uppercase domains
416 source = source.toUpperCase(); 417 source = source.toUpperCase();
417 } 418 }
418 let list = source.split(this.domainSeparator); 419 let list = source.split(this.domainSeparator);
419 if (list.length == 1 && list[0][0] != "~") 420 if (list.length == 1 && list[0][0] != "~")
420 { 421 {
421 // Fast track for the common one-domain scenario 422 // Fast track for the common one-domain scenario
422 domains = new Map([["", false], [list[0], true]]); 423 domains = list[0];
423 } 424 }
424 else 425 else
425 { 426 {
426 let hasIncludes = false; 427 let hasIncludes = false;
427 for (let i = 0; i < list.length; i++) 428 for (let i = 0; i < list.length; i++)
428 { 429 {
429 let domain = list[i]; 430 let domain = list[i];
430 if (domain == "") 431 if (domain == "")
431 continue; 432 continue;
432 433
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
481 return false; 482 return false;
482 } 483 }
483 484
484 // If no domains are set the rule matches everywhere 485 // If no domains are set the rule matches everywhere
485 if (!this.domains) 486 if (!this.domains)
486 return true; 487 return true;
487 488
488 // 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
489 // isn't restricted to specific domains 490 // isn't restricted to specific domains
490 if (!docDomain) 491 if (!docDomain)
491 return this.domains.get(""); 492 return typeof this.domains != "string" && this.domains.get("");
492 493
493 docDomain = docDomain.replace(/\.+$/, "").toUpperCase(); 494 docDomain = docDomain.replace(/\.+$/, "").toUpperCase();
494 495
496 if (typeof this.domains == "string")
497 {
498 return docDomain == this.domains ||
499 docDomain.endsWith("." + this.domains);
500 }
501
495 while (true) 502 while (true)
496 { 503 {
497 let isDomainIncluded = this.domains.get(docDomain); 504 let isDomainIncluded = this.domains.get(docDomain);
498 if (typeof isDomainIncluded != "undefined") 505 if (typeof isDomainIncluded != "undefined")
499 return isDomainIncluded; 506 return isDomainIncluded;
500 507
501 let nextDot = docDomain.indexOf("."); 508 let nextDot = docDomain.indexOf(".");
502 if (nextDot < 0) 509 if (nextDot < 0)
503 break; 510 break;
504 docDomain = docDomain.substr(nextDot + 1); 511 docDomain = docDomain.substr(nextDot + 1);
505 } 512 }
506 return this.domains.get(""); 513 return this.domains.get("");
507 }, 514 },
508 515
509 /** 516 /**
510 * Checks whether this filter is active only on a domain and its subdomains. 517 * Checks whether this filter is active only on a domain and its subdomains.
511 * @param {string} docDomain 518 * @param {string} docDomain
512 * @return {boolean} 519 * @return {boolean}
513 */ 520 */
514 isActiveOnlyOnDomain(docDomain) 521 isActiveOnlyOnDomain(docDomain)
515 { 522 {
516 if (!docDomain || !this.domains || this.domains.get("")) 523 if (!docDomain || !this.domains ||
524 typeof this.domains != "string" && this.domains.get(""))
525 {
517 return false; 526 return false;
527 }
518 528
519 docDomain = docDomain.replace(/\.+$/, "").toUpperCase(); 529 docDomain = docDomain.replace(/\.+$/, "").toUpperCase();
520 530
531 if (typeof this.domains == "string")
532 {
533 return docDomain == this.domains ||
534 this.domains.endsWith("." + docDomain);
535 }
536
521 for (let [domain, isIncluded] of this.domains) 537 for (let [domain, isIncluded] of this.domains)
522 { 538 {
523 if (isIncluded && domain != docDomain) 539 if (isIncluded && domain != docDomain)
524 { 540 {
525 if (domain.length <= docDomain.length) 541 if (domain.length <= docDomain.length)
526 return false; 542 return false;
527 543
528 if (!domain.endsWith("." + docDomain)) 544 if (!domain.endsWith("." + docDomain))
529 return false; 545 return false;
530 } 546 }
531 } 547 }
532 548
533 return true; 549 return true;
534 }, 550 },
535 551
536 /** 552 /**
537 * Checks whether this filter is generic or specific 553 * Checks whether this filter is generic or specific
538 * @return {boolean} 554 * @return {boolean}
539 */ 555 */
540 isGeneric() 556 isGeneric()
541 { 557 {
542 return !(this.sitekeys && this.sitekeys.length) && 558 return !(this.sitekeys && this.sitekeys.length) &&
543 (!this.domains || this.domains.get("")); 559 (!this.domains ||
560 typeof this.domains != "string" && this.domains.get(""));
544 }, 561 },
545 562
546 /** 563 /**
547 * See Filter.serialize() 564 * See Filter.serialize()
548 * @inheritdoc 565 * @inheritdoc
549 */ 566 */
550 serialize(buffer) 567 serialize(buffer)
551 { 568 {
552 if (this._disabled || this._hitCount || this._lastHit) 569 if (this._disabled || this._hitCount || this._lastHit)
553 { 570 {
(...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after
1099 */ 1116 */
1100 function ElemHideEmulationFilter(text, domains, selector) 1117 function ElemHideEmulationFilter(text, domains, selector)
1101 { 1118 {
1102 ElemHideBase.call(this, text, domains, selector); 1119 ElemHideBase.call(this, text, domains, selector);
1103 } 1120 }
1104 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; 1121 exports.ElemHideEmulationFilter = ElemHideEmulationFilter;
1105 1122
1106 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { 1123 ElemHideEmulationFilter.prototype = extend(ElemHideBase, {
1107 type: "elemhideemulation" 1124 type: "elemhideemulation"
1108 }); 1125 });
OLDNEW
« no previous file with comments | « lib/elemHide.js ('k') | test/filterClasses.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld