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

Side by Side Diff: lib/elemHide.js

Issue 29764555: Noissue - Use two sets for excluded and included filters by domain (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created April 28, 2018, 4:15 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
« no previous file with comments | « no previous file | 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
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 Element hiding implementation. 21 * @fileOverview Element hiding implementation.
22 */ 22 */
23 23
24 const {ElemHideException} = require("./filterClasses"); 24 const {ElemHideException} = require("./filterClasses");
25 const {FilterNotifier} = require("./filterNotifier"); 25 const {FilterNotifier} = require("./filterNotifier");
26 26
27 /** 27 /**
28 * Lookup table, active flag, by filter by domain. 28 * Lookup table, active flag, by filter by domain.
29 * (Only contains filters that aren't unconditionally matched for all domains.) 29 * (Only contains filters that aren't unconditionally matched for all domains.)
30 * @type {Map.<string,Map.<Filter,boolean>>} 30 * @type {Map.<string,Array.<Set.<Filter>>>}
31 */ 31 */
32 let filtersByDomain = new Map(); 32 let filtersByDomain = new Map();
33 33
34 /** 34 /**
35 * Lookup table, filter by selector. (Only used for selectors that are 35 * Lookup table, filter by selector. (Only used for selectors that are
36 * unconditionally matched for all domains.) 36 * unconditionally matched for all domains.)
37 * @type {Map.<string,Filter>} 37 * @type {Map.<string,Filter>}
38 */ 38 */
39 let filterBySelector = new Map(); 39 let filterBySelector = new Map();
40 40
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 FilterNotifier.emit("elemhideupdate"); 81 FilterNotifier.emit("elemhideupdate");
82 }, 82 },
83 83
84 _addToFiltersByDomain(filter) 84 _addToFiltersByDomain(filter)
85 { 85 {
86 let domains = filter.domains || defaultDomains; 86 let domains = filter.domains || defaultDomains;
87 for (let [domain, isIncluded] of domains) 87 for (let [domain, isIncluded] of domains)
88 { 88 {
89 let filters = filtersByDomain.get(domain); 89 let filters = filtersByDomain.get(domain);
90 if (!filters) 90 if (!filters)
91 filtersByDomain.set(domain, filters = new Map()); 91 filtersByDomain.set(domain, filters = []);
92 filters.set(filter, isIncluded); 92
93 let setIndex = +isIncluded;
Manish Jethani 2018/04/28 16:35:35 Conveniently boolean converted to number gives us
94 if (!filters[setIndex])
Manish Jethani 2018/04/28 16:35:35 The number of entries is the same as before. If th
95 filters[setIndex] = new Set();
96
97 filters[setIndex].add(filter);
93 } 98 }
94 }, 99 },
95 100
96 /** 101 /**
97 * Add a new element hiding filter 102 * Add a new element hiding filter
98 * @param {ElemHideFilter} filter 103 * @param {ElemHideFilter} filter
99 */ 104 */
100 add(filter) 105 add(filter)
101 { 106 {
102 if (knownFilters.has(filter)) 107 if (knownFilters.has(filter))
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 { 165 {
161 filterBySelector.delete(filter.selector); 166 filterBySelector.delete(filter.selector);
162 unconditionalSelectors = null; 167 unconditionalSelectors = null;
163 } 168 }
164 // Conditionally applied element hiding filters 169 // Conditionally applied element hiding filters
165 else 170 else
166 { 171 {
167 let domains = filter.domains || defaultDomains; 172 let domains = filter.domains || defaultDomains;
168 for (let domain of domains.keys()) 173 for (let domain of domains.keys())
169 { 174 {
170 let filters = filtersByDomain.get(domain); 175 for (let set of filtersByDomain.get(domain) || [])
171 if (filters) 176 {
172 filters.delete(filter); 177 if (set)
178 set.delete(filter);
179 }
173 } 180 }
174 } 181 }
175 182
176 knownFilters.delete(filter); 183 knownFilters.delete(filter);
177 FilterNotifier.emit("elemhideupdate"); 184 FilterNotifier.emit("elemhideupdate");
178 }, 185 },
179 186
180 /** 187 /**
181 * Checks whether an exception rule is registered for a filter on a particular 188 * Checks whether an exception rule is registered for a filter on a particular
182 * domain. 189 * domain.
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 getSelectorsForDomain(domain, criteria) 248 getSelectorsForDomain(domain, criteria)
242 { 249 {
243 let selectors = []; 250 let selectors = [];
244 251
245 if (typeof criteria == "undefined") 252 if (typeof criteria == "undefined")
246 criteria = ElemHide.ALL_MATCHING; 253 criteria = ElemHide.ALL_MATCHING;
247 if (criteria < ElemHide.NO_UNCONDITIONAL) 254 if (criteria < ElemHide.NO_UNCONDITIONAL)
248 selectors = this.getUnconditionalSelectors(); 255 selectors = this.getUnconditionalSelectors();
249 256
250 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); 257 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY);
251 let seenFilters = new Set(); 258 let seenFilters = [];
Manish Jethani 2018/04/28 16:35:35 This is now an array of sets. The maximum number o
252 let currentDomain = domain ? domain.toUpperCase() : ""; 259 let currentDomain = domain ? domain.toUpperCase() : "";
253 while (true) 260 while (true)
254 { 261 {
255 if (specificOnly && currentDomain == "") 262 if (specificOnly && currentDomain == "")
256 break; 263 break;
257 264
258 let filters = filtersByDomain.get(currentDomain); 265 let [excluded, included] = filtersByDomain.get(currentDomain) || [];
259 if (filters) 266 if (excluded)
267 seenFilters.push(excluded);
268
269 for (let filter of included || [])
260 { 270 {
261 for (let [filter, isIncluded] of filters) 271 for (let set of seenFilters)
Manish Jethani 2018/04/28 18:47:06 By the way, in case it isn't clear, the reason thi
262 { 272 {
Manish Jethani 2018/04/28 18:47:06 Note that as a side effect of this change a filter
263 if (seenFilters.has(filter)) 273 if (set.has(filter))
264 continue; 274 {
265 seenFilters.add(filter); 275 filter = null;
276 break;
277 }
278 }
266 279
267 if (isIncluded && !this.getException(filter, domain)) 280 if (filter && !this.getException(filter, domain))
268 selectors.push(filter.selector); 281 selectors.push(filter.selector);
269 }
270 } 282 }
271 283
272 if (currentDomain == "") 284 if (currentDomain == "")
273 break; 285 break;
274 286
275 let nextDot = currentDomain.indexOf("."); 287 let nextDot = currentDomain.indexOf(".");
276 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 288 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
277 } 289 }
278 290
279 return selectors; 291 return selectors;
280 } 292 }
281 }; 293 };
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld