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: Created Oct. 3, 2018, 11:47 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.
147 * @yields {string} 167 * @yields {string}
148 */ 168 */
149 *serialize() 169 *serialize()
150 { 170 {
171 let {text} = this;
172
151 yield "[Filter]"; 173 yield "[Filter]";
152 yield "text=" + this.text; 174 yield "text=" + text;
153 }, 175 },
154 176
155 toString() 177 toString()
156 { 178 {
157 return this.text; 179 return this.text;
158 } 180 }
159 }; 181 };
160 182
161 /** 183 /**
162 * Cache for known filters, maps string representation to filter objects. 184 * Cache for known filters, maps string representation to filter objects.
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 * Map containing domains that this filter should match on/not match 472 * Map containing domains that this filter should match on/not match
451 * on or null if the filter should match on all domains 473 * on or null if the filter should match on all domains
452 * @type {?Map.<string,boolean>} 474 * @type {?Map.<string,boolean>}
453 */ 475 */
454 get domains() 476 get domains()
455 { 477 {
456 let domains = null; 478 let domains = null;
457 479
458 if (this.domainSource) 480 if (this.domainSource)
459 { 481 {
482 // For some filter types this property is accessed only rarely,
483 // especially when the subscriptions are initially loaded. We defer any
484 // caching for such filters.
485 let {cacheDomains} = this;
486
460 let source = this.domainSource.toLowerCase(); 487 let source = this.domainSource.toLowerCase();
461 488
462 let knownMap = knownDomainMaps.get(source); 489 let knownMap = knownDomainMaps.get(source);
463 if (knownMap) 490 if (knownMap)
464 { 491 {
465 domains = knownMap; 492 domains = knownMap;
466 } 493 }
467 else 494 else
468 { 495 {
469 let list = source.split(this.domainSeparator); 496 let list = source.split(this.domainSeparator);
(...skipping 26 matching lines...) Expand all
496 if (!domains) 523 if (!domains)
497 domains = new Map(); 524 domains = new Map();
498 525
499 domains.set(domain, include); 526 domains.set(domain, include);
500 } 527 }
501 528
502 if (domains) 529 if (domains)
503 domains.set("", !hasIncludes); 530 domains.set("", !hasIncludes);
504 } 531 }
505 532
506 if (domains) 533 if (!domains || cacheDomains)
507 knownDomainMaps.set(source, domains); 534 knownDomainMaps.set(source, domains);
508 } 535 }
509 536
510 this.domainSource = null; 537 if (!domains || cacheDomains)
511 } 538 {
512 539 this.domainSource = null;
513 Object.defineProperty(this, "domains", {value: domains}); 540 Object.defineProperty(this, "domains", {value: domains});
514 return this.domains; 541 }
515 }, 542 }
543
544 return domains;
545 },
546
547 /**
548 * Whether the value of {@link ActiveFilter#domains} should be cached.
549 * Defaults to <code>true</code>, but may be overridden by subclasses that
550 * don't want the value to be cached (for better memory usage).
551 * @type {boolean}
552 * @protected
553 */
554 cacheDomains: true,
516 555
517 /** 556 /**
518 * Array containing public keys of websites that this filter should apply to 557 * Array containing public keys of websites that this filter should apply to
519 * @type {?string[]} 558 * @type {?string[]}
520 */ 559 */
521 sitekeys: null, 560 sitekeys: null,
522 561
523 /** 562 /**
524 * Checks whether this filter is active on a domain. 563 * Checks whether this filter is active on a domain.
525 * @param {string} [docDomain] domain name of the document that loads the URL 564 * @param {string} [docDomain] domain name of the document that loads the URL
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
609 648
610 return !(sitekeys && sitekeys.length) && (!domains || domains.get("")); 649 return !(sitekeys && sitekeys.length) && (!domains || domains.get(""));
611 }, 650 },
612 651
613 /** 652 /**
614 * See Filter.serialize() 653 * See Filter.serialize()
615 * @inheritdoc 654 * @inheritdoc
616 */ 655 */
617 *serialize() 656 *serialize()
618 { 657 {
619 if (this._disabled || this._hitCount || this._lastHit) 658 let {_disabled, _hitCount, _lastHit} = this;
659
660 if (_disabled || _hitCount || _lastHit)
620 { 661 {
621 yield* Filter.prototype.serialize.call(this); 662 yield* Filter.prototype.serialize.call(this);
622 if (this._disabled) 663 if (_disabled)
623 yield "disabled=true"; 664 yield "disabled=true";
624 if (this._hitCount) 665 if (_hitCount)
625 yield "hitCount=" + this._hitCount; 666 yield "hitCount=" + _hitCount;
626 if (this._lastHit) 667 if (_lastHit)
627 yield "lastHit=" + this._lastHit; 668 yield "lastHit=" + _lastHit;
628 } 669 }
629 } 670 }
630 }); 671 });
631 672
632 /** 673 /**
633 * Abstract base class for RegExp-based filters 674 * Abstract base class for RegExp-based filters
634 * @param {string} text see {@link Filter Filter()} 675 * @param {string} text see {@link Filter Filter()}
635 * @param {string} regexpSource 676 * @param {string} regexpSource
636 * filter part that the regular expression should be build from 677 * filter part that the regular expression should be build from
637 * @param {number} [contentType] 678 * @param {number} [contentType]
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
669 regexpSource[regexpSource.length - 1] == "/") 710 regexpSource[regexpSource.length - 1] == "/")
670 { 711 {
671 // The filter is a regular expression - convert it immediately to 712 // The filter is a regular expression - convert it immediately to
672 // catch syntax errors 713 // catch syntax errors
673 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2), 714 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2),
674 this.matchCase ? "" : "i"); 715 this.matchCase ? "" : "i");
675 Object.defineProperty(this, "regexp", {value: regexp}); 716 Object.defineProperty(this, "regexp", {value: regexp});
676 } 717 }
677 else 718 else
678 { 719 {
720 if (!this.matchCase && isLiteralPattern(regexpSource))
721 regexpSource = regexpSource.toLowerCase();
722
679 // 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
680 this.pattern = regexpSource; 724 this.pattern = regexpSource;
681 } 725 }
682 } 726 }
683 exports.RegExpFilter = RegExpFilter; 727 exports.RegExpFilter = RegExpFilter;
684 728
685 RegExpFilter.prototype = extend(ActiveFilter, { 729 RegExpFilter.prototype = extend(ActiveFilter, {
686 /** 730 /**
687 * Number of filters contained, will always be 1 (required to 731 * Number of filters contained, will always be 1 (required to
688 * optimize {@link Matcher}). 732 * optimize {@link Matcher}).
(...skipping 11 matching lines...) Expand all
700 * for delayed creation of the regexp property 744 * for delayed creation of the regexp property
701 * @type {?string} 745 * @type {?string}
702 */ 746 */
703 pattern: null, 747 pattern: null,
704 /** 748 /**
705 * Regular expression to be used when testing against this filter 749 * Regular expression to be used when testing against this filter
706 * @type {RegExp} 750 * @type {RegExp}
707 */ 751 */
708 get regexp() 752 get regexp()
709 { 753 {
710 let source = filterToRegExp(this.pattern, this.rewrite != null); 754 let value = null;
711 let regexp = new RegExp(source, this.matchCase ? "" : "i"); 755
712 Object.defineProperty(this, "regexp", {value: regexp}); 756 let {pattern, rewrite} = this;
713 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;
714 }, 765 },
715 /** 766 /**
716 * Content types the filter applies to, combination of values from 767 * Content types the filter applies to, combination of values from
717 * RegExpFilter.typeMap 768 * RegExpFilter.typeMap
718 * @type {number} 769 * @type {number}
719 */ 770 */
720 contentType: 0x7FFFFFFF, 771 contentType: 0x7FFFFFFF,
721 /** 772 /**
722 * Defines whether the filter should distinguish between lower and 773 * Defines whether the filter should distinguish between lower and
723 * upper case letters 774 * upper case letters
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
764 * @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
765 * request 816 * request
766 * @param {string} [sitekey] public key provided by the document 817 * @param {string} [sitekey] public key provided by the document
767 * @return {boolean} true in case of a match 818 * @return {boolean} true in case of a match
768 */ 819 */
769 matches(location, typeMask, docDomain, thirdParty, sitekey) 820 matches(location, typeMask, docDomain, thirdParty, sitekey)
770 { 821 {
771 return (this.contentType & typeMask) != 0 && 822 return (this.contentType & typeMask) != 0 &&
772 (this.thirdParty == null || this.thirdParty == thirdParty) && 823 (this.thirdParty == null || this.thirdParty == thirdParty) &&
773 this.isActiveOnDomain(docDomain, sitekey) && 824 this.isActiveOnDomain(docDomain, sitekey) &&
774 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);
775 } 856 }
776 }); 857 });
777 858
778 /** 859 /**
779 * Yields the filter itself (required to optimize {@link Matcher}). 860 * Yields the filter itself (required to optimize {@link Matcher}).
780 * @yields {RegExpFilter} 861 * @yields {RegExpFilter}
781 */ 862 */
782 RegExpFilter.prototype[Symbol.iterator] = function*() 863 RegExpFilter.prototype[Symbol.iterator] = function*()
783 { 864 {
784 yield this; 865 yield this;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
835 if (contentType == null) 916 if (contentType == null)
836 ({contentType} = RegExpFilter.prototype); 917 ({contentType} = RegExpFilter.prototype);
837 contentType &= ~type; 918 contentType &= ~type;
838 } 919 }
839 else 920 else
840 { 921 {
841 contentType |= type; 922 contentType |= type;
842 923
843 if (type == RegExpFilter.typeMap.CSP) 924 if (type == RegExpFilter.typeMap.CSP)
844 { 925 {
845 if (!value) 926 if (blocking && !value)
846 return new InvalidFilter(origText, "filter_invalid_csp"); 927 return new InvalidFilter(origText, "filter_invalid_csp");
847 csp = value; 928 csp = value;
848 } 929 }
849 } 930 }
850 } 931 }
851 else 932 else
852 { 933 {
853 switch (option.toLowerCase()) 934 switch (option.toLowerCase())
854 { 935 {
855 case "match-case": 936 case "match-case":
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
1147 * @augments ContentFilter 1228 * @augments ContentFilter
1148 */ 1229 */
1149 function ElemHideBase(text, domains, selector) 1230 function ElemHideBase(text, domains, selector)
1150 { 1231 {
1151 ContentFilter.call(this, text, domains, selector); 1232 ContentFilter.call(this, text, domains, selector);
1152 } 1233 }
1153 exports.ElemHideBase = ElemHideBase; 1234 exports.ElemHideBase = ElemHideBase;
1154 1235
1155 ElemHideBase.prototype = extend(ContentFilter, { 1236 ElemHideBase.prototype = extend(ContentFilter, {
1156 /** 1237 /**
1238 * @see ActiveFilter#domains
1239 * @type {?Map.<string,boolean>}
1240 */
1241 get domains()
1242 {
1243 let {get} = Object.getOwnPropertyDescriptor(ActiveFilter.prototype,
1244 "domains");
1245 let value = get.call(this);
1246 this.cacheDomains = true;
1247 return value;
1248 },
1249
1250 /**
1251 * Initially <code>false</code>, but set to <code>true</code> after
1252 * {@link ActiveFilter#domains} has been accessed once.
1253 * @see ActiveFilter#cacheDomains
1254 * @type {boolean}
1255 * @protected
1256 */
1257 cacheDomains: false,
1258
1259 /**
1157 * CSS selector for the HTML elements that should be hidden 1260 * CSS selector for the HTML elements that should be hidden
1158 * @type {string} 1261 * @type {string}
1159 */ 1262 */
1160 get selector() 1263 get selector()
1161 { 1264 {
1162 // Braces are being escaped to prevent CSS rule injection. 1265 // Braces are being escaped to prevent CSS rule injection.
1163 return this.body.replace("{", "\\7B ").replace("}", "\\7D "); 1266 return this.body.replace("{", "\\7B ").replace("}", "\\7D ");
1164 } 1267 }
1165 }); 1268 });
1166 1269
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1237 1340
1238 /** 1341 /**
1239 * Script that should be executed 1342 * Script that should be executed
1240 * @type {string} 1343 * @type {string}
1241 */ 1344 */
1242 get script() 1345 get script()
1243 { 1346 {
1244 return this.body; 1347 return this.body;
1245 } 1348 }
1246 }); 1349 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld