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

Delta Between Two Patch Sets: lib/elemHide.js

Issue 29757591: Issue 6562 - Remove filter keys from the element hiding code (Closed)
Left Patch Set: Avoid adding duplicate hiding filters Created April 26, 2018, noon
Right Patch Set: == instead of === Created May 4, 2018, 12:05 p.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 | test/filterListener.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
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 */ 45 */
46 let unconditionalSelectors = null; 46 let unconditionalSelectors = null;
47 47
48 /** 48 /**
49 * Map to be used instead when a filter has a blank domains property. 49 * Map to be used instead when a filter has a blank domains property.
50 */ 50 */
51 let defaultDomains = new Map([["", true]]); 51 let defaultDomains = new Map([["", true]]);
52 52
53 /** 53 /**
54 * Set containing known element hiding and exception filters 54 * Set containing known element hiding and exception filters
55 * @type {Set.<string>} 55 * @type {Set.<ElemHideBase>}
Manish Jethani 2018/04/27 12:39:36 This is really {Set.<ElemHideBase>}
kzar 2018/04/30 10:11:46 Done.
56 */ 56 */
57 let knownFilters = new Set(); 57 let knownFilters = new Set();
Manish Jethani 2018/04/27 12:39:36 I wonder if we should make this a WeakSet since we
kzar 2018/04/30 10:11:46 I considered that as well but we keep a reference
58 58
59 /** 59 /**
60 * Lookup table, lists of element hiding exceptions by selector 60 * Lookup table, lists of element hiding exceptions by selector
61 * @type {Map.<string,Filter>} 61 * @type {Map.<string,Filter>}
62 */ 62 */
63 let exceptions = new Map(); 63 let exceptions = new Map();
64 64
65 /** 65 /**
66 * Container for element hiding filters 66 * Container for element hiding filters
67 * @class 67 * @class
(...skipping 11 matching lines...) Expand all
79 } 79 }
80 unconditionalSelectors = null; 80 unconditionalSelectors = null;
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 // There's no need to note that a filter is generically disabled.
90 if (!isIncluded && domain == "")
91 continue;
92
89 let filters = filtersByDomain.get(domain); 93 let filters = filtersByDomain.get(domain);
90 if (!filters) 94 if (!filters)
91 filtersByDomain.set(domain, filters = new Map()); 95 filtersByDomain.set(domain, filters = new Map());
92 filters.set(filter, isIncluded); 96 filters.set(filter, isIncluded);
93 } 97 }
94 }, 98 },
95 99
96 /** 100 /**
97 * Add a new element hiding filter 101 * Add a new element hiding filter
98 * @param {ElemHideFilter} filter 102 * @param {ElemHideBase} filter
Manish Jethani 2018/04/27 12:39:36 This is really {ElemHideBase}
kzar 2018/04/30 10:11:46 Well spotted, Done.
99 */ 103 */
100 add(filter) 104 add(filter)
101 { 105 {
102 if (knownFilters.has(filter)) 106 if (knownFilters.has(filter))
103 return; 107 return;
104 108
105 if (filter instanceof ElemHideException) 109 if (filter instanceof ElemHideException)
106 { 110 {
107 let {selector} = filter; 111 let {selector} = filter;
108 let list = exceptions.get(selector); 112 let list = exceptions.get(selector);
(...skipping 24 matching lines...) Expand all
133 // The new filter's selector only applies to some domains 137 // The new filter's selector only applies to some domains
134 this._addToFiltersByDomain(filter); 138 this._addToFiltersByDomain(filter);
135 } 139 }
136 140
137 knownFilters.add(filter); 141 knownFilters.add(filter);
138 FilterNotifier.emit("elemhideupdate"); 142 FilterNotifier.emit("elemhideupdate");
139 }, 143 },
140 144
141 /** 145 /**
142 * Removes an element hiding filter 146 * Removes an element hiding filter
143 * @param {ElemHideFilter} filter 147 * @param {ElemHideBase} filter
Manish Jethani 2018/04/27 12:39:36 This is really {ElemHideBase}
144 */ 148 */
145 remove(filter) 149 remove(filter)
146 { 150 {
147 if (!knownFilters.has(filter)) 151 if (!knownFilters.has(filter))
148 return; 152 return;
149 153
150 // Whitelisting filters 154 // Whitelisting filters
151 if (filter instanceof ElemHideException) 155 if (filter instanceof ElemHideException)
152 { 156 {
153 let list = exceptions.get(filter.selector); 157 let list = exceptions.get(filter.selector);
154 let index = list.indexOf(filter); 158 let index = list.indexOf(filter);
155 if (index >= 0) 159 if (index >= 0)
156 list.splice(index, 1); 160 list.splice(index, 1);
157 } 161 }
158 // Unconditially applied element hiding filters 162 // Unconditially applied element hiding filters
159 else if (filterBySelector.get(filter.selector) === filter) 163 else if (filterBySelector.get(filter.selector) == filter)
160 { 164 {
161 filterBySelector.delete(filter.selector); 165 filterBySelector.delete(filter.selector);
162 unconditionalSelectors = null; 166 unconditionalSelectors = null;
163 } 167 }
164 // Conditionally applied element hiding filters 168 // Conditionally applied element hiding filters
165 else 169 else
166 { 170 {
167 let domains = filter.domains || defaultDomains; 171 let domains = filter.domains || defaultDomains;
168 for (let domain of domains.keys()) 172 for (let domain of domains.keys())
169 { 173 {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 getSelectorsForDomain(domain, criteria) 245 getSelectorsForDomain(domain, criteria)
242 { 246 {
243 let selectors = []; 247 let selectors = [];
244 248
245 if (typeof criteria == "undefined") 249 if (typeof criteria == "undefined")
246 criteria = ElemHide.ALL_MATCHING; 250 criteria = ElemHide.ALL_MATCHING;
247 if (criteria < ElemHide.NO_UNCONDITIONAL) 251 if (criteria < ElemHide.NO_UNCONDITIONAL)
248 selectors = this.getUnconditionalSelectors(); 252 selectors = this.getUnconditionalSelectors();
249 253
250 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); 254 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY);
251 let seenFilters = new Set(); 255 let excluded = new Set();
252 let currentDomain = domain ? domain.toUpperCase() : ""; 256 let currentDomain = domain ? domain.toUpperCase() : "";
257
258 // This code is a performance hot-spot, which is why we've made certain
259 // micro-optimisations. Please be careful before making changes.
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 filters = filtersByDomain.get(currentDomain);
259 if (filters) 266 if (filters)
260 { 267 {
261 for (let [filter, isIncluded] of filters) 268 for (let [filter, isIncluded] of filters)
Manish Jethani 2018/04/27 12:39:36 Just a thought, but I wonder if we could do this (
Manish Jethani 2018/04/27 12:41:58 Anyway, I'm all for not changing things here as pa
kzar 2018/04/27 17:01:50 On 2018/04/27 12:39:36, Manish Jethani wrote:
Manish Jethani 2018/04/28 16:22:17 That's what excludedFilterSets in the above code s
kzar 2018/04/30 10:11:46 Well no, excludedFilterSets contained the excluded
Manish Jethani 2018/04/30 17:55:21 Let's do that as part of the other patch.
262 { 269 {
263 if (seenFilters.has(filter)) 270 if (!isIncluded)
264 continue; 271 {
265 seenFilters.add(filter); 272 excluded.add(filter);
266 273 }
267 if (isIncluded && !this.getException(filter, domain)) 274 else if ((excluded.size == 0 || !excluded.has(filter)) &&
275 !this.getException(filter, domain))
276 {
268 selectors.push(filter.selector); 277 selectors.push(filter.selector);
278 }
269 } 279 }
270 } 280 }
271 281
272 if (currentDomain == "") 282 if (currentDomain == "")
273 break; 283 break;
274 284
275 let nextDot = currentDomain.indexOf("."); 285 let nextDot = currentDomain.indexOf(".");
276 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 286 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
277 } 287 }
278 288
279 return selectors; 289 return selectors;
280 } 290 }
281 }; 291 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld