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

Delta Between Two Patch Sets: lib/filterClasses.js

Issue 29900557: Issue 7016 - Convert serialization functions into generators (Closed)
Left Patch Set: Actually Address PS2 Comments Created Oct. 21, 2018, 5:25 p.m.
Right Patch Set: Actually address nits Created Oct. 23, 2018, 3:21 a.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/filterStorage.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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 /** 20 /**
21 * @fileOverview Definition of Filter class and its subclasses. 21 * @fileOverview Definition of Filter class and its subclasses.
22 */ 22 */
23 23
24 const {filterNotifier} = require("./filterNotifier"); 24 const {filterNotifier} = require("./filterNotifier");
25 const {extend} = require("./coreUtils"); 25 const {extend} = require("./coreUtils");
26 const {filterToRegExp} = require("./common"); 26 const {filterToRegExp} = require("./common");
27 27
28 /** 28 /**
29 * Regular expression used to match the <code>||</code> prefix in an otherwise
30 * literal pattern.
31 * @type {RegExp}
32 */
33 let doubleAnchorRegExp = new RegExp(filterToRegExp("||") + "$");
34
35 /**
29 * All known unique domain sources mapped to their parsed values. 36 * All known unique domain sources mapped to their parsed values.
30 * @type {Map.<string,Map.<string,boolean>>} 37 * @type {Map.<string,Map.<string,boolean>>}
31 */ 38 */
32 let knownDomainMaps = new Map(); 39 let knownDomainMaps = new Map();
40
41 /**
42 * Checks whether the given pattern is a string of literal characters with no
43 * wildcards or any other special characters. If the pattern is prefixed with a
44 * <code>||</code> but otherwise contains no special characters, it is still
45 * considered to be a literal pattern.
46 * @param {string} pattern
47 * @returns {boolean}
48 */
49 function isLiteralPattern(pattern)
50 {
51 return !/[*^|]/.test(pattern.replace(/^\|{2}/, ""));
52 }
33 53
34 /** 54 /**
35 * Abstract base class for filters 55 * Abstract base class for filters
36 * 56 *
37 * @param {string} text string representation of the filter 57 * @param {string} text string representation of the filter
38 * @constructor 58 * @constructor
39 */ 59 */
40 function Filter(text) 60 function Filter(text)
41 { 61 {
42 this.text = text; 62 this.text = text;
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 this._subscriptions = [...this._subscriptions][0]; 156 this._subscriptions = [...this._subscriptions][0];
137 } 157 }
138 else if (subscription == this._subscriptions) 158 else if (subscription == this._subscriptions)
139 { 159 {
140 this._subscriptions = null; 160 this._subscriptions = null;
141 } 161 }
142 } 162 }
143 }, 163 },
144 164
145 /** 165 /**
146 * Generates serialized filter. 166 * Serializes the filter for writing out on disk.
Manish Jethani 2018/10/21 21:51:43 I like the previous comment, I think we could just
Jon Sonesen 2018/10/22 19:27:47 Acknowledged. Yeah I like that better
147 * @yields {string} 167 * @yields {string}
148 */ 168 */
149 *serialize() 169 *serialize()
150 { 170 {
151 let {text} = this; 171 let {text} = this;
Manish Jethani 2018/10/21 21:52:55 Nit: I would leave a line after this just to be co
Jon Sonesen 2018/10/22 19:27:47 Yeah good catch
172
152 yield "[Filter]"; 173 yield "[Filter]";
153 yield "text=" + text; 174 yield "text=" + text;
154 }, 175 },
155 176
156 toString() 177 toString()
157 { 178 {
158 return this.text; 179 return this.text;
159 } 180 }
160 }; 181 };
161 182
(...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 regexpSource[regexpSource.length - 1] == "/") 710 regexpSource[regexpSource.length - 1] == "/")
690 { 711 {
691 // The filter is a regular expression - convert it immediately to 712 // The filter is a regular expression - convert it immediately to
692 // catch syntax errors 713 // catch syntax errors
693 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2), 714 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2),
694 this.matchCase ? "" : "i"); 715 this.matchCase ? "" : "i");
695 Object.defineProperty(this, "regexp", {value: regexp}); 716 Object.defineProperty(this, "regexp", {value: regexp});
696 } 717 }
697 else 718 else
698 { 719 {
720 if (!this.matchCase && isLiteralPattern(regexpSource))
721 regexpSource = regexpSource.toLowerCase();
722
699 // No need to convert this filter to regular expression yet, do it on demand 723 // No need to convert this filter to regular expression yet, do it on demand
700 this.pattern = regexpSource; 724 this.pattern = regexpSource;
701 } 725 }
702 } 726 }
703 exports.RegExpFilter = RegExpFilter; 727 exports.RegExpFilter = RegExpFilter;
704 728
705 RegExpFilter.prototype = extend(ActiveFilter, { 729 RegExpFilter.prototype = extend(ActiveFilter, {
706 /** 730 /**
707 * Number of filters contained, will always be 1 (required to 731 * Number of filters contained, will always be 1 (required to
708 * optimize {@link Matcher}). 732 * optimize {@link Matcher}).
(...skipping 11 matching lines...) Expand all
720 * for delayed creation of the regexp property 744 * for delayed creation of the regexp property
721 * @type {?string} 745 * @type {?string}
722 */ 746 */
723 pattern: null, 747 pattern: null,
724 /** 748 /**
725 * Regular expression to be used when testing against this filter 749 * Regular expression to be used when testing against this filter
726 * @type {RegExp} 750 * @type {RegExp}
727 */ 751 */
728 get regexp() 752 get regexp()
729 { 753 {
730 let source = filterToRegExp(this.pattern, this.rewrite != null); 754 let value = null;
731 let regexp = new RegExp(source, this.matchCase ? "" : "i"); 755
732 Object.defineProperty(this, "regexp", {value: regexp}); 756 let {pattern, rewrite} = this;
733 return regexp; 757 if (rewrite != null || !isLiteralPattern(pattern))
758 {
759 value = new RegExp(filterToRegExp(pattern, rewrite != null),
760 this.matchCase ? "" : "i");
761 }
762
763 Object.defineProperty(this, "regexp", {value});
764 return value;
734 }, 765 },
735 /** 766 /**
736 * Content types the filter applies to, combination of values from 767 * Content types the filter applies to, combination of values from
737 * RegExpFilter.typeMap 768 * RegExpFilter.typeMap
738 * @type {number} 769 * @type {number}
739 */ 770 */
740 contentType: 0x7FFFFFFF, 771 contentType: 0x7FFFFFFF,
741 /** 772 /**
742 * Defines whether the filter should distinguish between lower and 773 * Defines whether the filter should distinguish between lower and
743 * upper case letters 774 * upper case letters
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
784 * @param {boolean} [thirdParty] should be true if the URL is a third-party 815 * @param {boolean} [thirdParty] should be true if the URL is a third-party
785 * request 816 * request
786 * @param {string} [sitekey] public key provided by the document 817 * @param {string} [sitekey] public key provided by the document
787 * @return {boolean} true in case of a match 818 * @return {boolean} true in case of a match
788 */ 819 */
789 matches(location, typeMask, docDomain, thirdParty, sitekey) 820 matches(location, typeMask, docDomain, thirdParty, sitekey)
790 { 821 {
791 return (this.contentType & typeMask) != 0 && 822 return (this.contentType & typeMask) != 0 &&
792 (this.thirdParty == null || this.thirdParty == thirdParty) && 823 (this.thirdParty == null || this.thirdParty == thirdParty) &&
793 this.isActiveOnDomain(docDomain, sitekey) && 824 this.isActiveOnDomain(docDomain, sitekey) &&
794 this.regexp.test(location); 825 this.matchesLocation(location);
826 },
827
828 /**
829 * Checks whether the given URL matches this filter's pattern.
830 * @param {string} location The URL to check.
831 * @returns {boolean} <code>true</code> if the URL matches.
832 */
833 matchesLocation(location)
834 {
835 let {regexp} = this;
836
837 if (regexp)
838 return regexp.test(location);
839
840 if (!this.matchCase)
841 location = location.toLowerCase();
842
843 let {pattern} = this;
844
845 if (pattern[0] == "|" && pattern[1] == "|")
846 {
847 let index = location.indexOf(pattern.substring(2));
848
849 // The "||" prefix requires that the text that follows does not start
850 // with a forward slash.
851 return index != -1 && location[index] != "/" &&
852 doubleAnchorRegExp.test(location.substring(0, index));
853 }
854
855 return location.includes(pattern);
795 } 856 }
796 }); 857 });
797 858
798 /** 859 /**
799 * Yields the filter itself (required to optimize {@link Matcher}). 860 * Yields the filter itself (required to optimize {@link Matcher}).
800 * @yields {RegExpFilter} 861 * @yields {RegExpFilter}
801 */ 862 */
802 RegExpFilter.prototype[Symbol.iterator] = function*() 863 RegExpFilter.prototype[Symbol.iterator] = function*()
803 { 864 {
804 yield this; 865 yield this;
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
1279 1340
1280 /** 1341 /**
1281 * Script that should be executed 1342 * Script that should be executed
1282 * @type {string} 1343 * @type {string}
1283 */ 1344 */
1284 get script() 1345 get script()
1285 { 1346 {
1286 return this.body; 1347 return this.body;
1287 } 1348 }
1288 }); 1349 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld