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

Side by Side Diff: lib/filterClasses.js

Issue 29800595: Issue 6735 - Store domains in lower case (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Use Map version of RegExpFilter.typeMap Created June 8, 2018, 5:27 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 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 domainSource: null, 380 domainSource: null,
381 381
382 /** 382 /**
383 * Separator character used in domainSource property, must be 383 * Separator character used in domainSource property, must be
384 * overridden by subclasses 384 * overridden by subclasses
385 * @type {string} 385 * @type {string}
386 */ 386 */
387 domainSeparator: null, 387 domainSeparator: null,
388 388
389 /** 389 /**
390 * Determines whether domainSource is already upper-case, 390 * Determines whether domainSource is already lower-case,
391 * can be overridden by subclasses. 391 * can be overridden by subclasses.
392 * @type {boolean} 392 * @type {boolean}
393 */ 393 */
394 domainSourceIsUpperCase: false, 394 domainSourceIsLowerCase: false,
395 395
396 /** 396 /**
397 * Map containing domains that this filter should match on/not match 397 * Map containing domains that this filter should match on/not match
398 * on or null if the filter should match on all domains 398 * on or null if the filter should match on all domains
399 * @type {?Map.<string,boolean>} 399 * @type {?Map.<string,boolean>}
400 */ 400 */
401 get domains() 401 get domains()
402 { 402 {
403 let domains = null; 403 let domains = null;
404 404
405 if (this.domainSource) 405 if (this.domainSource)
406 { 406 {
407 let source = this.domainSource; 407 let source = this.domainSource;
408 if (!this.domainSourceIsUpperCase) 408 if (!this.domainSourceIsLowerCase)
409 { 409 {
410 // RegExpFilter already have uppercase domains 410 // RegExpFilter already have lowercase domains
411 source = source.toUpperCase(); 411 source = source.toLowerCase();
412 } 412 }
413 let list = source.split(this.domainSeparator); 413 let list = source.split(this.domainSeparator);
414 if (list.length == 1 && list[0][0] != "~") 414 if (list.length == 1 && list[0][0] != "~")
415 { 415 {
416 // Fast track for the common one-domain scenario 416 // Fast track for the common one-domain scenario
417 domains = new Map([["", false], [list[0], true]]); 417 domains = new Map([["", false], [list[0], true]]);
418 } 418 }
419 else 419 else
420 { 420 {
421 let hasIncludes = false; 421 let hasIncludes = false;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 478
479 // If no domains are set the rule matches everywhere 479 // If no domains are set the rule matches everywhere
480 if (!this.domains) 480 if (!this.domains)
481 return true; 481 return true;
482 482
483 // If the document has no host name, match only if the filter 483 // If the document has no host name, match only if the filter
484 // isn't restricted to specific domains 484 // isn't restricted to specific domains
485 if (!docDomain) 485 if (!docDomain)
486 return this.domains.get(""); 486 return this.domains.get("");
487 487
488 docDomain = docDomain.replace(/\.+$/, "").toUpperCase(); 488 docDomain = docDomain.replace(/\.+$/, "").toLowerCase();
489 489
490 while (true) 490 while (true)
491 { 491 {
492 let isDomainIncluded = this.domains.get(docDomain); 492 let isDomainIncluded = this.domains.get(docDomain);
493 if (typeof isDomainIncluded != "undefined") 493 if (typeof isDomainIncluded != "undefined")
494 return isDomainIncluded; 494 return isDomainIncluded;
495 495
496 let nextDot = docDomain.indexOf("."); 496 let nextDot = docDomain.indexOf(".");
497 if (nextDot < 0) 497 if (nextDot < 0)
498 break; 498 break;
499 docDomain = docDomain.substr(nextDot + 1); 499 docDomain = docDomain.substr(nextDot + 1);
500 } 500 }
501 return this.domains.get(""); 501 return this.domains.get("");
502 }, 502 },
503 503
504 /** 504 /**
505 * Checks whether this filter is active only on a domain and its subdomains. 505 * Checks whether this filter is active only on a domain and its subdomains.
506 * @param {string} docDomain 506 * @param {string} docDomain
507 * @return {boolean} 507 * @return {boolean}
508 */ 508 */
509 isActiveOnlyOnDomain(docDomain) 509 isActiveOnlyOnDomain(docDomain)
510 { 510 {
511 if (!docDomain || !this.domains || this.domains.get("")) 511 if (!docDomain || !this.domains || this.domains.get(""))
512 return false; 512 return false;
513 513
514 docDomain = docDomain.replace(/\.+$/, "").toUpperCase(); 514 docDomain = docDomain.replace(/\.+$/, "").toLowerCase();
515 515
516 for (let [domain, isIncluded] of this.domains) 516 for (let [domain, isIncluded] of this.domains)
517 { 517 {
518 if (isIncluded && domain != docDomain) 518 if (isIncluded && domain != docDomain)
519 { 519 {
520 if (domain.length <= docDomain.length) 520 if (domain.length <= docDomain.length)
521 return false; 521 return false;
522 522
523 if (!domain.endsWith("." + docDomain)) 523 if (!domain.endsWith("." + docDomain))
524 return false; 524 return false;
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
605 else 605 else
606 { 606 {
607 // No need to convert this filter to regular expression yet, do it on demand 607 // No need to convert this filter to regular expression yet, do it on demand
608 this.regexpSource = regexpSource; 608 this.regexpSource = regexpSource;
609 } 609 }
610 } 610 }
611 exports.RegExpFilter = RegExpFilter; 611 exports.RegExpFilter = RegExpFilter;
612 612
613 RegExpFilter.prototype = extend(ActiveFilter, { 613 RegExpFilter.prototype = extend(ActiveFilter, {
614 /** 614 /**
615 * @see ActiveFilter.domainSourceIsUpperCase 615 * @see ActiveFilter.domainSourceIsLowerCase
616 */ 616 */
617 domainSourceIsUpperCase: true, 617 domainSourceIsLowerCase: true,
618 618
619 /** 619 /**
620 * Number of filters contained, will always be 1 (required to 620 * Number of filters contained, will always be 1 (required to
621 * optimize Matcher). 621 * optimize Matcher).
622 * @type {number} 622 * @type {number}
623 */ 623 */
624 length: 1, 624 length: 1,
625 625
626 /** 626 /**
627 * @see ActiveFilter.domainSeparator 627 * @see ActiveFilter.domainSeparator
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
748 text = match.input.substr(0, match.index); 748 text = match.input.substr(0, match.index);
749 for (let option of options) 749 for (let option of options)
750 { 750 {
751 let value = null; 751 let value = null;
752 let separatorIndex = option.indexOf("="); 752 let separatorIndex = option.indexOf("=");
753 if (separatorIndex >= 0) 753 if (separatorIndex >= 0)
754 { 754 {
755 value = option.substr(separatorIndex + 1); 755 value = option.substr(separatorIndex + 1);
756 option = option.substr(0, separatorIndex); 756 option = option.substr(0, separatorIndex);
757 } 757 }
758 option = option.replace(/-/, "_").toUpperCase(); 758
759 if (option in RegExpFilter.typeMap) 759 let inverse = option[0] == "~";
760 if (inverse)
761 option = option.substr(1);
762
763 option = option.toLowerCase();
764
765 let type = typeMap.get(option);
766 if (type)
760 { 767 {
761 contentType |= RegExpFilter.typeMap[option]; 768 if (inverse)
769 {
770 if (contentType == null)
771 ({contentType} = RegExpFilter.prototype);
772 contentType &= ~type;
773 }
774 else
775 {
776 contentType |= type;
762 777
763 if (option == "CSP" && value) 778 if (type == RegExpFilter.typeMap.CSP && value)
764 csp = value; 779 csp = value;
780 }
765 } 781 }
766 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap) 782 else
767 { 783 {
768 if (contentType == null) 784 switch (option)
769 ({contentType} = RegExpFilter.prototype); 785 {
770 contentType &= ~RegExpFilter.typeMap[option.substr(1)]; 786 case "match-case":
787 matchCase = !inverse;
788 break;
789 case "domain":
790 if (value)
791 domains = value.toLowerCase();
792 break;
793 case "third-party":
794 thirdParty = !inverse;
795 break;
796 case "collapse":
797 collapse = !inverse;
798 break;
799 case "sitekey":
800 if (value)
801 sitekeys = value.toUpperCase();
802 break;
803 case "rewrite":
804 if (value)
805 rewrite = value;
806 break;
807 default:
808 return new InvalidFilter(origText, "filter_unknown_option");
809 }
771 } 810 }
772 else if (option == "MATCH_CASE")
773 matchCase = true;
774 else if (option == "~MATCH_CASE")
775 matchCase = false;
776 else if (option == "DOMAIN" && value)
777 domains = value.toUpperCase();
778 else if (option == "THIRD_PARTY")
779 thirdParty = true;
780 else if (option == "~THIRD_PARTY")
781 thirdParty = false;
782 else if (option == "COLLAPSE")
783 collapse = true;
784 else if (option == "~COLLAPSE")
785 collapse = false;
786 else if (option == "SITEKEY" && value)
787 sitekeys = value.toUpperCase();
788 else if (option == "REWRITE" && value)
789 rewrite = value;
790 else
791 return new InvalidFilter(origText, "filter_unknown_option");
792 } 811 }
793 } 812 }
794 813
795 // For security reasons, never match $rewrite filters 814 // For security reasons, never match $rewrite filters
796 // against requests that might load any code to be executed. 815 // against requests that might load any code to be executed.
797 if (rewrite != null) 816 if (rewrite != null)
798 { 817 {
799 if (contentType == null) 818 if (contentType == null)
800 ({contentType} = RegExpFilter.prototype); 819 ({contentType} = RegExpFilter.prototype);
801 contentType &= ~(RegExpFilter.typeMap.SCRIPT | 820 contentType &= ~(RegExpFilter.typeMap.SCRIPT |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
846 FONT: 32768, 865 FONT: 32768,
847 866
848 BACKGROUND: 4, // Backwards compat, same as IMAGE 867 BACKGROUND: 4, // Backwards compat, same as IMAGE
849 868
850 POPUP: 0x10000000, 869 POPUP: 0x10000000,
851 GENERICBLOCK: 0x20000000, 870 GENERICBLOCK: 0x20000000,
852 ELEMHIDE: 0x40000000, 871 ELEMHIDE: 0x40000000,
853 GENERICHIDE: 0x80000000 872 GENERICHIDE: 0x80000000
854 }; 873 };
855 874
875 // Create a Map version of RegExpFilter.typeMap for faster lookups and fewer
876 // string transformations in RegExpFilter.fromText
877 let typeMap = new Map(
878 Object.keys(RegExpFilter.typeMap).map(
879 key => [key.replace("_", "-").toLowerCase(), RegExpFilter.typeMap[key]]
880 )
881 );
882
856 // CSP, DOCUMENT, ELEMHIDE, POPUP, GENERICHIDE and GENERICBLOCK options 883 // CSP, DOCUMENT, ELEMHIDE, POPUP, GENERICHIDE and GENERICBLOCK options
857 // shouldn't be there by default 884 // shouldn't be there by default
858 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.CSP | 885 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.CSP |
859 RegExpFilter.typeMap.DOCUMENT | 886 RegExpFilter.typeMap.DOCUMENT |
860 RegExpFilter.typeMap.ELEMHIDE | 887 RegExpFilter.typeMap.ELEMHIDE |
861 RegExpFilter.typeMap.POPUP | 888 RegExpFilter.typeMap.POPUP |
862 RegExpFilter.typeMap.GENERICHIDE | 889 RegExpFilter.typeMap.GENERICHIDE |
863 RegExpFilter.typeMap.GENERICBLOCK); 890 RegExpFilter.typeMap.GENERICBLOCK);
864 891
865 /** 892 /**
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
1080 */ 1107 */
1081 function ElemHideEmulationFilter(text, domains, selector) 1108 function ElemHideEmulationFilter(text, domains, selector)
1082 { 1109 {
1083 ElemHideBase.call(this, text, domains, selector); 1110 ElemHideBase.call(this, text, domains, selector);
1084 } 1111 }
1085 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; 1112 exports.ElemHideEmulationFilter = ElemHideEmulationFilter;
1086 1113
1087 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { 1114 ElemHideEmulationFilter.prototype = extend(ElemHideBase, {
1088 type: "elemhideemulation" 1115 type: "elemhideemulation"
1089 }); 1116 });
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