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: Address PS2 Comments Created Oct. 12, 2018, 3:46 a.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 {
151 let text = this; 171 let {text} = this;
Manish Jethani 2018/10/14 20:05:36 This should be: let {text} = this; It's workin
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 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 * Map containing domains that this filter should match on/not match 472 * Map containing domains that this filter should match on/not match
452 * on or null if the filter should match on all domains 473 * on or null if the filter should match on all domains
453 * @type {?Map.<string,boolean>} 474 * @type {?Map.<string,boolean>}
454 */ 475 */
455 get domains() 476 get domains()
456 { 477 {
457 let domains = null; 478 let domains = null;
458 479
459 if (this.domainSource) 480 if (this.domainSource)
460 { 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
461 let source = this.domainSource.toLowerCase(); 487 let source = this.domainSource.toLowerCase();
462 488
463 let knownMap = knownDomainMaps.get(source); 489 let knownMap = knownDomainMaps.get(source);
464 if (knownMap) 490 if (knownMap)
465 { 491 {
466 domains = knownMap; 492 domains = knownMap;
467 } 493 }
468 else 494 else
469 { 495 {
470 let list = source.split(this.domainSeparator); 496 let list = source.split(this.domainSeparator);
(...skipping 26 matching lines...) Expand all
497 if (!domains) 523 if (!domains)
498 domains = new Map(); 524 domains = new Map();
499 525
500 domains.set(domain, include); 526 domains.set(domain, include);
501 } 527 }
502 528
503 if (domains) 529 if (domains)
504 domains.set("", !hasIncludes); 530 domains.set("", !hasIncludes);
505 } 531 }
506 532
507 if (domains) 533 if (!domains || cacheDomains)
508 knownDomainMaps.set(source, domains); 534 knownDomainMaps.set(source, domains);
509 } 535 }
510 536
511 this.domainSource = null; 537 if (!domains || cacheDomains)
512 } 538 {
513 539 this.domainSource = null;
514 Object.defineProperty(this, "domains", {value: domains}); 540 Object.defineProperty(this, "domains", {value: domains});
515 return this.domains; 541 }
516 }, 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,
517 555
518 /** 556 /**
519 * 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
520 * @type {?string[]} 558 * @type {?string[]}
521 */ 559 */
522 sitekeys: null, 560 sitekeys: null,
523 561
524 /** 562 /**
525 * Checks whether this filter is active on a domain. 563 * Checks whether this filter is active on a domain.
526 * @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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 return !(sitekeys && sitekeys.length) && (!domains || domains.get("")); 649 return !(sitekeys && sitekeys.length) && (!domains || domains.get(""));
612 }, 650 },
613 651
614 /** 652 /**
615 * See Filter.serialize() 653 * See Filter.serialize()
616 * @inheritdoc 654 * @inheritdoc
617 */ 655 */
618 *serialize() 656 *serialize()
619 { 657 {
620 let {_disabled, _hitCount, _lastHit} = this; 658 let {_disabled, _hitCount, _lastHit} = this;
659
621 if (_disabled || _hitCount || _lastHit) 660 if (_disabled || _hitCount || _lastHit)
622 { 661 {
623 yield* Filter.prototype.serialize.call(this); 662 yield* Filter.prototype.serialize.call(this);
624 if (this._disabled) 663 if (_disabled)
Manish Jethani 2018/10/14 20:05:36 We extracted the values first so we would use them
625 yield "disabled=true"; 664 yield "disabled=true";
626 if (this._hitCount) 665 if (_hitCount)
627 yield "hitCount=" + this._hitCount; 666 yield "hitCount=" + _hitCount;
628 if (this._lastHit) 667 if (_lastHit)
629 yield "lastHit=" + this._lastHit; 668 yield "lastHit=" + _lastHit;
630 } 669 }
631 } 670 }
632 }); 671 });
633 672
634 /** 673 /**
635 * Abstract base class for RegExp-based filters 674 * Abstract base class for RegExp-based filters
636 * @param {string} text see {@link Filter Filter()} 675 * @param {string} text see {@link Filter Filter()}
637 * @param {string} regexpSource 676 * @param {string} regexpSource
638 * filter part that the regular expression should be build from 677 * filter part that the regular expression should be build from
639 * @param {number} [contentType] 678 * @param {number} [contentType]
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 regexpSource[regexpSource.length - 1] == "/") 710 regexpSource[regexpSource.length - 1] == "/")
672 { 711 {
673 // The filter is a regular expression - convert it immediately to 712 // The filter is a regular expression - convert it immediately to
674 // catch syntax errors 713 // catch syntax errors
675 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2), 714 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2),
676 this.matchCase ? "" : "i"); 715 this.matchCase ? "" : "i");
677 Object.defineProperty(this, "regexp", {value: regexp}); 716 Object.defineProperty(this, "regexp", {value: regexp});
678 } 717 }
679 else 718 else
680 { 719 {
720 if (!this.matchCase && isLiteralPattern(regexpSource))
721 regexpSource = regexpSource.toLowerCase();
722
681 // 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
682 this.pattern = regexpSource; 724 this.pattern = regexpSource;
683 } 725 }
684 } 726 }
685 exports.RegExpFilter = RegExpFilter; 727 exports.RegExpFilter = RegExpFilter;
686 728
687 RegExpFilter.prototype = extend(ActiveFilter, { 729 RegExpFilter.prototype = extend(ActiveFilter, {
688 /** 730 /**
689 * Number of filters contained, will always be 1 (required to 731 * Number of filters contained, will always be 1 (required to
690 * optimize {@link Matcher}). 732 * optimize {@link Matcher}).
(...skipping 11 matching lines...) Expand all
702 * for delayed creation of the regexp property 744 * for delayed creation of the regexp property
703 * @type {?string} 745 * @type {?string}
704 */ 746 */
705 pattern: null, 747 pattern: null,
706 /** 748 /**
707 * Regular expression to be used when testing against this filter 749 * Regular expression to be used when testing against this filter
708 * @type {RegExp} 750 * @type {RegExp}
709 */ 751 */
710 get regexp() 752 get regexp()
711 { 753 {
712 let source = filterToRegExp(this.pattern, this.rewrite != null); 754 let value = null;
713 let regexp = new RegExp(source, this.matchCase ? "" : "i"); 755
714 Object.defineProperty(this, "regexp", {value: regexp}); 756 let {pattern, rewrite} = this;
715 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;
716 }, 765 },
717 /** 766 /**
718 * Content types the filter applies to, combination of values from 767 * Content types the filter applies to, combination of values from
719 * RegExpFilter.typeMap 768 * RegExpFilter.typeMap
720 * @type {number} 769 * @type {number}
721 */ 770 */
722 contentType: 0x7FFFFFFF, 771 contentType: 0x7FFFFFFF,
723 /** 772 /**
724 * Defines whether the filter should distinguish between lower and 773 * Defines whether the filter should distinguish between lower and
725 * upper case letters 774 * upper case letters
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
766 * @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
767 * request 816 * request
768 * @param {string} [sitekey] public key provided by the document 817 * @param {string} [sitekey] public key provided by the document
769 * @return {boolean} true in case of a match 818 * @return {boolean} true in case of a match
770 */ 819 */
771 matches(location, typeMask, docDomain, thirdParty, sitekey) 820 matches(location, typeMask, docDomain, thirdParty, sitekey)
772 { 821 {
773 return (this.contentType & typeMask) != 0 && 822 return (this.contentType & typeMask) != 0 &&
774 (this.thirdParty == null || this.thirdParty == thirdParty) && 823 (this.thirdParty == null || this.thirdParty == thirdParty) &&
775 this.isActiveOnDomain(docDomain, sitekey) && 824 this.isActiveOnDomain(docDomain, sitekey) &&
776 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);
777 } 856 }
778 }); 857 });
779 858
780 /** 859 /**
781 * Yields the filter itself (required to optimize {@link Matcher}). 860 * Yields the filter itself (required to optimize {@link Matcher}).
782 * @yields {RegExpFilter} 861 * @yields {RegExpFilter}
783 */ 862 */
784 RegExpFilter.prototype[Symbol.iterator] = function*() 863 RegExpFilter.prototype[Symbol.iterator] = function*()
785 { 864 {
786 yield this; 865 yield this;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
837 if (contentType == null) 916 if (contentType == null)
838 ({contentType} = RegExpFilter.prototype); 917 ({contentType} = RegExpFilter.prototype);
839 contentType &= ~type; 918 contentType &= ~type;
840 } 919 }
841 else 920 else
842 { 921 {
843 contentType |= type; 922 contentType |= type;
844 923
845 if (type == RegExpFilter.typeMap.CSP) 924 if (type == RegExpFilter.typeMap.CSP)
846 { 925 {
847 if (!value) 926 if (blocking && !value)
848 return new InvalidFilter(origText, "filter_invalid_csp"); 927 return new InvalidFilter(origText, "filter_invalid_csp");
849 csp = value; 928 csp = value;
850 } 929 }
851 } 930 }
852 } 931 }
853 else 932 else
854 { 933 {
855 switch (option.toLowerCase()) 934 switch (option.toLowerCase())
856 { 935 {
857 case "match-case": 936 case "match-case":
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
1149 * @augments ContentFilter 1228 * @augments ContentFilter
1150 */ 1229 */
1151 function ElemHideBase(text, domains, selector) 1230 function ElemHideBase(text, domains, selector)
1152 { 1231 {
1153 ContentFilter.call(this, text, domains, selector); 1232 ContentFilter.call(this, text, domains, selector);
1154 } 1233 }
1155 exports.ElemHideBase = ElemHideBase; 1234 exports.ElemHideBase = ElemHideBase;
1156 1235
1157 ElemHideBase.prototype = extend(ContentFilter, { 1236 ElemHideBase.prototype = extend(ContentFilter, {
1158 /** 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 /**
1159 * CSS selector for the HTML elements that should be hidden 1260 * CSS selector for the HTML elements that should be hidden
1160 * @type {string} 1261 * @type {string}
1161 */ 1262 */
1162 get selector() 1263 get selector()
1163 { 1264 {
1164 // Braces are being escaped to prevent CSS rule injection. 1265 // Braces are being escaped to prevent CSS rule injection.
1165 return this.body.replace("{", "\\7B ").replace("}", "\\7D "); 1266 return this.body.replace("{", "\\7B ").replace("}", "\\7D ");
1166 } 1267 }
1167 }); 1268 });
1168 1269
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1239 1340
1240 /** 1341 /**
1241 * Script that should be executed 1342 * Script that should be executed
1242 * @type {string} 1343 * @type {string}
1243 */ 1344 */
1244 get script() 1345 get script()
1245 { 1346 {
1246 return this.body; 1347 return this.body;
1247 } 1348 }
1248 }); 1349 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld