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

Side by Side Diff: lib/matcher.js

Issue 29907586: Issue 6994 - Use shortcut matching for location only filters (Closed)
Patch Set: Address PS1 Comments Created Oct. 20, 2018, 11:07 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« lib/filterClasses.js ('K') | « lib/filterClasses.js ('k') | 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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 */ 53 */
54 class Matcher 54 class Matcher
55 { 55 {
56 constructor() 56 constructor()
57 { 57 {
58 /** 58 /**
59 * Lookup table for filters by their associated keyword 59 * Lookup table for filters by their associated keyword
60 * @type {Map.<string,(Filter|Set.<Filter>)>} 60 * @type {Map.<string,(Filter|Set.<Filter>)>}
61 */ 61 */
62 this.filterByKeyword = new Map(); 62 this.filterByKeyword = new Map();
63
64 /**
65 * Lookup table for location only filters by their associated keyword
66 * for shortcut matching.
67 * @type {Map.<string,(Filter|Set.<Filter>)>}
68 */
69 this.locationFiltersByKeyword = new Map();
63 } 70 }
64 71
65 /** 72 /**
66 * Removes all known filters 73 * Removes all known filters
67 */ 74 */
68 clear() 75 clear()
69 { 76 {
70 this.filterByKeyword.clear(); 77 this.filterByKeyword.clear();
78 this.locationFiltersByKeyword.clear();
71 } 79 }
72 80
73 /** 81 /**
74 * Adds a filter to the matcher 82 * Adds a filter to the matcher
75 * @param {RegExpFilter} filter 83 * @param {RegExpFilter} filter
76 */ 84 */
77 add(filter) 85 add(filter)
78 { 86 {
79 // Look for a suitable keyword 87 // Look for a suitable keyword
80 let keyword = this.findKeyword(filter); 88 let keyword = this.findKeyword(filter);
81 let set = this.filterByKeyword.get(keyword); 89 let set = this.filterByKeyword.get(keyword);
90
91 if (filter.isLocationOnly)
92 {
93 let locationSet = this.locationFiltersByKeyword.get(keyword);
94 if (typeof locationSet == "undefined")
95 this.locationFiltersByKeyword.set(keyword, new Set([filter]));
96 else
97 {
98 locationSet.add(filter);
99 }
100 }
101
82 if (typeof set == "undefined") 102 if (typeof set == "undefined")
83 { 103 {
84 this.filterByKeyword.set(keyword, filter); 104 this.filterByKeyword.set(keyword, filter);
85 } 105 }
86 else if (set.size == 1) 106 else if (set.size == 1)
87 { 107 {
88 if (filter != set) 108 if (filter != set)
89 this.filterByKeyword.set(keyword, new Set([set, filter])); 109 this.filterByKeyword.set(keyword, new Set([set, filter]));
90 } 110 }
91 else 111 else
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 * @param {number} typeMask 181 * @param {number} typeMask
162 * @param {string} [docDomain] 182 * @param {string} [docDomain]
163 * @param {boolean} [thirdParty] 183 * @param {boolean} [thirdParty]
164 * @param {string} [sitekey] 184 * @param {string} [sitekey]
165 * @param {boolean} [specificOnly] 185 * @param {boolean} [specificOnly]
166 * @returns {?Filter} 186 * @returns {?Filter}
167 */ 187 */
168 _checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey, 188 _checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey,
169 specificOnly) 189 specificOnly)
170 { 190 {
191 let locationFilters = this.locationFiltersByKeyword.get(keyword);
192 if (typeof locationFilters != "undefined")
193 {
194 for (let filter of locationFilters)
195 {
196 if (filter.contentType == 0 && typeMask == 0)
197 continue;
Jon Sonesen 2018/10/20 23:09:48 I think this should be in both iterations?
198 if (filter.matchesLocation(location))
199 return filter;
200 }
201 }
171 let set = this.filterByKeyword.get(keyword); 202 let set = this.filterByKeyword.get(keyword);
172 if (typeof set == "undefined") 203 if (typeof set == "undefined")
173 return null; 204 return null;
174 205
175 for (let filter of set) 206 for (let filter of set)
176 { 207 {
177 if (specificOnly && filter.isGeneric() && 208 if (specificOnly && filter.isGeneric() &&
178 !(filter instanceof WhitelistFilter)) 209 !(filter instanceof WhitelistFilter))
179 continue; 210 continue;
180 211
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 398
368 exports.CombinedMatcher = CombinedMatcher; 399 exports.CombinedMatcher = CombinedMatcher;
369 400
370 /** 401 /**
371 * Shared {@link CombinedMatcher} instance that should usually be used. 402 * Shared {@link CombinedMatcher} instance that should usually be used.
372 * @type {CombinedMatcher} 403 * @type {CombinedMatcher}
373 */ 404 */
374 let defaultMatcher = new CombinedMatcher(); 405 let defaultMatcher = new CombinedMatcher();
375 406
376 exports.defaultMatcher = defaultMatcher; 407 exports.defaultMatcher = defaultMatcher;
OLDNEW
« lib/filterClasses.js ('K') | « lib/filterClasses.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld