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

Delta Between Two Patch Sets: lib/filterClasses.js

Issue 29978576: Issue 7208 - Drop superfluous wildcards before processing pattern (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Fix spelling Created Jan. 10, 2019, 10:57 p.m.
Right Patch Set: Move rewrite property to RegExpFilter Created Jan. 10, 2019, 11:03 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 | no next file » | 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 672 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 * letters 683 * letters
684 * @param {string} [domains] 684 * @param {string} [domains]
685 * Domains that the filter is restricted to, e.g. "foo.com|bar.com|~baz.com" 685 * Domains that the filter is restricted to, e.g. "foo.com|bar.com|~baz.com"
686 * @param {boolean} [thirdParty] 686 * @param {boolean} [thirdParty]
687 * Defines whether the filter should apply to third-party or first-party 687 * Defines whether the filter should apply to third-party or first-party
688 * content only 688 * content only
689 * @param {string} [sitekeys] 689 * @param {string} [sitekeys]
690 * Public keys of websites that this filter should apply to 690 * Public keys of websites that this filter should apply to
691 * @param {?string} [rewrite] 691 * @param {?string} [rewrite]
692 * The (optional) rule specifying how to rewrite the URL. See 692 * The (optional) rule specifying how to rewrite the URL. See
693 * BlockingFilter.prototype.rewrite. 693 * RegExpFilter.prototype.rewrite.
694 * @constructor 694 * @constructor
695 * @augments ActiveFilter 695 * @augments ActiveFilter
696 */ 696 */
697 function RegExpFilter(text, regexpSource, contentType, matchCase, domains, 697 function RegExpFilter(text, regexpSource, contentType, matchCase, domains,
698 thirdParty, sitekeys, rewrite) 698 thirdParty, sitekeys, rewrite)
699 { 699 {
700 ActiveFilter.call(this, text, domains); 700 ActiveFilter.call(this, text, domains);
701 701
702 if (contentType != null) 702 if (contentType != null)
703 this.contentType = contentType; 703 this.contentType = contentType;
704 if (matchCase) 704 if (matchCase)
705 this.matchCase = matchCase; 705 this.matchCase = matchCase;
706 if (thirdParty != null) 706 if (thirdParty != null)
707 this.thirdParty = thirdParty; 707 this.thirdParty = thirdParty;
708 if (sitekeys != null) 708 if (sitekeys != null)
709 this.sitekeySource = sitekeys; 709 this.sitekeySource = sitekeys;
710 if (rewrite != null)
711 this.rewrite = rewrite;
710 712
711 if (regexpSource.length >= 2 && 713 if (regexpSource.length >= 2 &&
712 regexpSource[0] == "/" && 714 regexpSource[0] == "/" &&
713 regexpSource[regexpSource.length - 1] == "/") 715 regexpSource[regexpSource.length - 1] == "/")
714 { 716 {
715 // The filter is a regular expression - convert it immediately to 717 // The filter is a regular expression - convert it immediately to
716 // catch syntax errors 718 // catch syntax errors
717 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2), 719 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2),
718 this.matchCase ? "" : "i"); 720 this.matchCase ? "" : "i");
719 Object.defineProperty(this, "regexp", {value: regexp}); 721 Object.defineProperty(this, "regexp", {value: regexp});
720 } 722 }
721 else 723 else
722 { 724 {
723 // Patterns like /foo/bar/* exist so that they are not treated as regular 725 // Patterns like /foo/bar/* exist so that they are not treated as regular
724 // expressions. We drop any superfluous wildcards here so our optimizations 726 // expressions. We drop any superfluous wildcards here so our optimizations
725 // can kick in. 727 // can kick in.
726 if (rewrite == null) 728 if (this.rewrite == null)
727 regexpSource = regexpSource.replace(/^\*+/, "").replace(/\*+$/, ""); 729 regexpSource = regexpSource.replace(/^\*+/, "").replace(/\*+$/, "");
728 730
729 if (!this.matchCase && isLiteralPattern(regexpSource)) 731 if (!this.matchCase && isLiteralPattern(regexpSource))
730 regexpSource = regexpSource.toLowerCase(); 732 regexpSource = regexpSource.toLowerCase();
731 733
732 // No need to convert this filter to regular expression yet, do it on demand 734 // No need to convert this filter to regular expression yet, do it on demand
733 this.pattern = regexpSource; 735 this.pattern = regexpSource;
734 } 736 }
735 } 737 }
736 exports.RegExpFilter = RegExpFilter; 738 exports.RegExpFilter = RegExpFilter;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 { 810 {
809 sitekeys = this.sitekeySource.split("|"); 811 sitekeys = this.sitekeySource.split("|");
810 this.sitekeySource = null; 812 this.sitekeySource = null;
811 } 813 }
812 814
813 Object.defineProperty( 815 Object.defineProperty(
814 this, "sitekeys", {value: sitekeys, enumerable: true} 816 this, "sitekeys", {value: sitekeys, enumerable: true}
815 ); 817 );
816 return this.sitekeys; 818 return this.sitekeys;
817 }, 819 },
820
821 /**
822 * The rule specifying how to rewrite the URL.
823 * The syntax is similar to the one of String.prototype.replace().
824 * @type {?string}
825 */
826 rewrite: null,
818 827
819 /** 828 /**
820 * Tests whether the URL matches this filter 829 * Tests whether the URL matches this filter
821 * @param {string} location URL to be tested 830 * @param {string} location URL to be tested
822 * @param {number} typeMask bitmask of content / request types to match 831 * @param {number} typeMask bitmask of content / request types to match
823 * @param {string} [docDomain] domain name of the document that loads the URL 832 * @param {string} [docDomain] domain name of the document that loads the URL
824 * @param {boolean} [thirdParty] should be true if the URL is a third-party 833 * @param {boolean} [thirdParty] should be true if the URL is a third-party
825 * request 834 * request
826 * @param {string} [sitekey] public key provided by the document 835 * @param {string} [sitekey] public key provided by the document
827 * @return {boolean} true in case of a match 836 * @return {boolean} true in case of a match
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
999 } 1008 }
1000 1009
1001 try 1010 try
1002 { 1011 {
1003 if (blocking) 1012 if (blocking)
1004 { 1013 {
1005 if (csp && Filter.invalidCSPRegExp.test(csp)) 1014 if (csp && Filter.invalidCSPRegExp.test(csp))
1006 return new InvalidFilter(origText, "filter_invalid_csp"); 1015 return new InvalidFilter(origText, "filter_invalid_csp");
1007 1016
1008 return new BlockingFilter(origText, text, contentType, matchCase, domains, 1017 return new BlockingFilter(origText, text, contentType, matchCase, domains,
1009 thirdParty, sitekeys, collapse, csp, rewrite); 1018 thirdParty, sitekeys, rewrite, collapse, csp);
1010 } 1019 }
1011 return new WhitelistFilter(origText, text, contentType, matchCase, domains, 1020 return new WhitelistFilter(origText, text, contentType, matchCase, domains,
1012 thirdParty, sitekeys); 1021 thirdParty, sitekeys);
1013 } 1022 }
1014 catch (e) 1023 catch (e)
1015 { 1024 {
1016 return new InvalidFilter(origText, "filter_invalid_regexp"); 1025 return new InvalidFilter(origText, "filter_invalid_regexp");
1017 } 1026 }
1018 }; 1027 };
1019 1028
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1058 1067
1059 /** 1068 /**
1060 * Class for blocking filters 1069 * Class for blocking filters
1061 * @param {string} text see {@link Filter Filter()} 1070 * @param {string} text see {@link Filter Filter()}
1062 * @param {string} regexpSource see {@link RegExpFilter RegExpFilter()} 1071 * @param {string} regexpSource see {@link RegExpFilter RegExpFilter()}
1063 * @param {number} [contentType] see {@link RegExpFilter RegExpFilter()} 1072 * @param {number} [contentType] see {@link RegExpFilter RegExpFilter()}
1064 * @param {boolean} [matchCase] see {@link RegExpFilter RegExpFilter()} 1073 * @param {boolean} [matchCase] see {@link RegExpFilter RegExpFilter()}
1065 * @param {string} [domains] see {@link RegExpFilter RegExpFilter()} 1074 * @param {string} [domains] see {@link RegExpFilter RegExpFilter()}
1066 * @param {boolean} [thirdParty] see {@link RegExpFilter RegExpFilter()} 1075 * @param {boolean} [thirdParty] see {@link RegExpFilter RegExpFilter()}
1067 * @param {string} [sitekeys] see {@link RegExpFilter RegExpFilter()} 1076 * @param {string} [sitekeys] see {@link RegExpFilter RegExpFilter()}
1077 * @param {?string} [rewrite]
1078 * The (optional) rule specifying how to rewrite the URL. See
1079 * RegExpFilter.prototype.rewrite.
1068 * @param {boolean} [collapse] 1080 * @param {boolean} [collapse]
1069 * defines whether the filter should collapse blocked content, can be null 1081 * defines whether the filter should collapse blocked content, can be null
1070 * @param {string} [csp] 1082 * @param {string} [csp]
1071 * Content Security Policy to inject when the filter matches 1083 * Content Security Policy to inject when the filter matches
1072 * @param {?string} [rewrite]
1073 * The (optional) rule specifying how to rewrite the URL. See
1074 * BlockingFilter.prototype.rewrite.
1075 * @constructor 1084 * @constructor
1076 * @augments RegExpFilter 1085 * @augments RegExpFilter
1077 */ 1086 */
1078 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, 1087 function BlockingFilter(text, regexpSource, contentType, matchCase, domains,
1079 thirdParty, sitekeys, collapse, csp, rewrite) 1088 thirdParty, sitekeys, rewrite, collapse, csp)
1080 { 1089 {
1081 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, 1090 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains,
1082 thirdParty, sitekeys, rewrite); 1091 thirdParty, sitekeys, rewrite);
1083 1092
1084 if (collapse != null) 1093 if (collapse != null)
1085 this.collapse = collapse; 1094 this.collapse = collapse;
1086 1095
1087 if (csp != null) 1096 if (csp != null)
1088 this.csp = csp; 1097 this.csp = csp;
1089
1090 if (rewrite != null)
1091 this.rewrite = rewrite;
1092 } 1098 }
1093 exports.BlockingFilter = BlockingFilter; 1099 exports.BlockingFilter = BlockingFilter;
1094 1100
1095 BlockingFilter.prototype = extend(RegExpFilter, { 1101 BlockingFilter.prototype = extend(RegExpFilter, {
1096 type: "blocking", 1102 type: "blocking",
1097 1103
1098 /** 1104 /**
1099 * Defines whether the filter should collapse blocked content. 1105 * Defines whether the filter should collapse blocked content.
1100 * Can be null (use the global preference). 1106 * Can be null (use the global preference).
1101 * @type {?boolean} 1107 * @type {?boolean}
1102 */ 1108 */
1103 collapse: null, 1109 collapse: null,
1104 1110
1105 /** 1111 /**
1106 * Content Security Policy to inject for matching requests. 1112 * Content Security Policy to inject for matching requests.
1107 * @type {?string} 1113 * @type {?string}
1108 */ 1114 */
1109 csp: null, 1115 csp: null,
1110
1111 /**
1112 * The rule specifying how to rewrite the URL.
1113 * The syntax is similar to the one of String.prototype.replace().
1114 * @type {?string}
1115 */
1116 rewrite: null,
1117 1116
1118 /** 1117 /**
1119 * Rewrites an URL. 1118 * Rewrites an URL.
1120 * @param {string} url the URL to rewrite 1119 * @param {string} url the URL to rewrite
1121 * @return {string} the rewritten URL, or the original in case of failure 1120 * @return {string} the rewritten URL, or the original in case of failure
1122 */ 1121 */
1123 rewriteUrl(url) 1122 rewriteUrl(url)
1124 { 1123 {
1125 try 1124 try
1126 { 1125 {
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
1362 1361
1363 /** 1362 /**
1364 * Script that should be executed 1363 * Script that should be executed
1365 * @type {string} 1364 * @type {string}
1366 */ 1365 */
1367 get script() 1366 get script()
1368 { 1367 {
1369 return this.body; 1368 return this.body;
1370 } 1369 }
1371 }); 1370 });
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld