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

Side by Side Diff: lib/filterClasses.js

Issue 30021563: Issue 7321 - Lower-case non-literal patterns for case-insensitive matching (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created March 1, 2019, 10:59 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 | « no previous file | no next file » | 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 706 matching lines...) Expand 10 before | Expand all | Expand 10 after
717 this.matchCase = matchCase; 717 this.matchCase = matchCase;
718 if (thirdParty != null) 718 if (thirdParty != null)
719 this.thirdParty = thirdParty; 719 this.thirdParty = thirdParty;
720 if (sitekeys != null) 720 if (sitekeys != null)
721 this.sitekeySource = sitekeys; 721 this.sitekeySource = sitekeys;
722 if (rewrite != null) 722 if (rewrite != null)
723 this.rewrite = rewrite; 723 this.rewrite = rewrite;
724 if (resourceName) 724 if (resourceName)
725 this.resourceName = resourceName; 725 this.resourceName = resourceName;
726 726
727 if (!this.matchCase)
728 regexpSource = regexpSource.toLowerCase();
729
727 if (regexpSource.length >= 2 && 730 if (regexpSource.length >= 2 &&
728 regexpSource[0] == "/" && 731 regexpSource[0] == "/" &&
729 regexpSource[regexpSource.length - 1] == "/") 732 regexpSource[regexpSource.length - 1] == "/")
730 { 733 {
731 // The filter is a regular expression - convert it immediately to 734 // The filter is a regular expression - convert it immediately to
732 // catch syntax errors 735 // catch syntax errors
733 let regexp = new RegExp(regexpSource.substring(1, regexpSource.length - 1), 736 let regexp = new RegExp(regexpSource.substring(1, regexpSource.length - 1));
734 this.matchCase ? "" : "i");
735 Object.defineProperty(this, "regexp", {value: regexp}); 737 Object.defineProperty(this, "regexp", {value: regexp});
736 } 738 }
737 else 739 else
738 { 740 {
739 // Patterns like /foo/bar/* exist so that they are not treated as regular 741 // Patterns like /foo/bar/* exist so that they are not treated as regular
740 // expressions. We drop any superfluous wildcards here so our optimizations 742 // expressions. We drop any superfluous wildcards here so our optimizations
741 // can kick in. 743 // can kick in.
742 if (this.rewrite == null || this.resourceName) 744 if (this.rewrite == null || this.resourceName)
743 regexpSource = regexpSource.replace(/^\*+/, "").replace(/\*+$/, ""); 745 regexpSource = regexpSource.replace(/^\*+/, "").replace(/\*+$/, "");
744 746
745 if (!this.matchCase && isLiteralPattern(regexpSource))
746 regexpSource = regexpSource.toLowerCase();
747
748 // No need to convert this filter to regular expression yet, do it on demand 747 // No need to convert this filter to regular expression yet, do it on demand
749 this.pattern = regexpSource; 748 this.pattern = regexpSource;
750 } 749 }
751 } 750 }
752 exports.RegExpFilter = RegExpFilter; 751 exports.RegExpFilter = RegExpFilter;
753 752
754 RegExpFilter.prototype = extend(ActiveFilter, { 753 RegExpFilter.prototype = extend(ActiveFilter, {
755 /** 754 /**
756 * Number of filters contained, will always be 1 (required to 755 * Number of filters contained, will always be 1 (required to
757 * optimize {@link Matcher}). 756 * optimize {@link Matcher}).
(...skipping 16 matching lines...) Expand all
774 /** 773 /**
775 * Regular expression to be used when testing against this filter 774 * Regular expression to be used when testing against this filter
776 * @type {RegExp} 775 * @type {RegExp}
777 */ 776 */
778 get regexp() 777 get regexp()
779 { 778 {
780 let value = null; 779 let value = null;
781 780
782 let {pattern, rewrite, resourceName} = this; 781 let {pattern, rewrite, resourceName} = this;
783 if ((rewrite != null && !resourceName) || !isLiteralPattern(pattern)) 782 if ((rewrite != null && !resourceName) || !isLiteralPattern(pattern))
784 { 783 value = new RegExp(filterToRegExp(pattern, rewrite != null));
785 value = new RegExp(filterToRegExp(pattern, rewrite != null),
786 this.matchCase ? "" : "i");
787 }
788 784
789 Object.defineProperty(this, "regexp", {value}); 785 Object.defineProperty(this, "regexp", {value});
790 return value; 786 return value;
791 }, 787 },
792 /** 788 /**
793 * Content types the filter applies to, combination of values from 789 * Content types the filter applies to, combination of values from
794 * RegExpFilter.typeMap 790 * RegExpFilter.typeMap
795 * @type {number} 791 * @type {number}
796 */ 792 */
797 contentType: 0x7FFFFFFF, 793 contentType: 0x7FFFFFFF,
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
891 /** 887 /**
892 * Checks whether the given URL matches this filter's pattern. 888 * Checks whether the given URL matches this filter's pattern.
893 * @param {string} location The URL to check. 889 * @param {string} location The URL to check.
894 * @param {?string} [lowerCaseLocation] The lower-case version of the URL to 890 * @param {?string} [lowerCaseLocation] The lower-case version of the URL to
895 * check, for case-insensitive matching. 891 * check, for case-insensitive matching.
896 * @returns {boolean} <code>true</code> if the URL matches. 892 * @returns {boolean} <code>true</code> if the URL matches.
897 * @package 893 * @package
898 */ 894 */
899 matchesLocation(location, lowerCaseLocation) 895 matchesLocation(location, lowerCaseLocation)
900 { 896 {
897 if (!this.matchCase)
898 location = lowerCaseLocation || location.toLowerCase();
899
901 let {regexp} = this; 900 let {regexp} = this;
902 901
903 if (regexp) 902 if (regexp)
904 return regexp.test(location); 903 return regexp.test(location);
905 904
906 if (!this.matchCase)
907 location = lowerCaseLocation || location.toLowerCase();
908
909 let {pattern} = this; 905 let {pattern} = this;
910 906
911 let startsWithDoubleAnchor = pattern[0] == "|" && pattern[1] == "|"; 907 let startsWithDoubleAnchor = pattern[0] == "|" && pattern[1] == "|";
912 let endsWithSeparator = pattern[pattern.length - 1] == "^"; 908 let endsWithSeparator = pattern[pattern.length - 1] == "^";
913 909
914 if (startsWithDoubleAnchor) 910 if (startsWithDoubleAnchor)
915 pattern = pattern.substr(2); 911 pattern = pattern.substr(2);
916 912
917 if (endsWithSeparator) 913 if (endsWithSeparator)
918 pattern = pattern.slice(0, -1); 914 pattern = pattern.slice(0, -1);
(...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after
1437 1433
1438 /** 1434 /**
1439 * Script that should be executed 1435 * Script that should be executed
1440 * @type {string} 1436 * @type {string}
1441 */ 1437 */
1442 get script() 1438 get script()
1443 { 1439 {
1444 return this.body; 1440 return this.body;
1445 } 1441 }
1446 }); 1442 });
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld