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

Side by Side Diff: lib/elemHide.js

Issue 29353428: Noissue - Simplify unconditional selector lookup (Closed)
Patch Set: Created Sept. 20, 2016, 9:22 a.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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 25 matching lines...) Expand all
36 var keyByFilter = Object.create(null); 36 var keyByFilter = Object.create(null);
37 37
38 /** 38 /**
39 * Nested lookup table, filter (or false if inactive) by filter key by domain. 39 * Nested lookup table, filter (or false if inactive) by filter key by domain.
40 * (Only contains filters that aren't unconditionally matched for all domains.) 40 * (Only contains filters that aren't unconditionally matched for all domains.)
41 * @type Object 41 * @type Object
42 */ 42 */
43 var filtersByDomain = Object.create(null); 43 var filtersByDomain = Object.create(null);
44 44
45 /** 45 /**
46 * Lookup table, filter keys by selector. (Only contains filters that have a 46 * Lookup table, filter key by selector. (Only contains filters that have a
47 * selector that is unconditionally matched for all domains.) 47 * selector that is unconditionally matched for all domains.)
48 */ 48 */
49 var filterKeysBySelector = Object.create(null); 49 var filterKeyBySelector = Object.create(null);
50 50
51 /** 51 /**
52 * This array caches the keys of filterKeysBySelector table (selectors which 52 * This array caches the keys of filterKeyBySelector table (selectors which
53 * unconditionally apply on all domains). It will be null if the cache needs to 53 * unconditionally apply on all domains). It will be null if the cache needs to
54 * be rebuilt. 54 * be rebuilt.
55 */ 55 */
56 var unconditionalSelectors = null; 56 var unconditionalSelectors = null;
57 57
58 /** 58 /**
59 * This array caches the values of filterKeysBySelector table (filterIds for 59 * This array caches the values of filterKeyBySelector table (filterIds for
60 * selectors which unconditionally apply on all domains). It will be null if the 60 * selectors which unconditionally apply on all domains). It will be null if the
61 * cache needs to be rebuilt. Note: Only the first filter key for each selector 61 * cache needs to be rebuilt.
62 * is cached.
63 */ 62 */
64 var unconditionalFilterKeys = null; 63 var unconditionalFilterKeys = null;
65 64
66 /** 65 /**
67 * Object to be used instead when a filter has a blank domains property. 66 * Object to be used instead when a filter has a blank domains property.
68 */ 67 */
69 var defaultDomains = Object.create(null); 68 var defaultDomains = Object.create(null);
70 defaultDomains[""] = true; 69 defaultDomains[""] = true;
71 70
72 /** 71 /**
(...skipping 15 matching lines...) Expand all
88 var ElemHide = exports.ElemHide = 87 var ElemHide = exports.ElemHide =
89 { 88 {
90 /** 89 /**
91 * Removes all known filters 90 * Removes all known filters
92 */ 91 */
93 clear: function() 92 clear: function()
94 { 93 {
95 filterByKey = []; 94 filterByKey = [];
96 keyByFilter = Object.create(null); 95 keyByFilter = Object.create(null);
97 filtersByDomain = Object.create(null); 96 filtersByDomain = Object.create(null);
98 filterKeysBySelector = Object.create(null); 97 filterKeyBySelector = Object.create(null);
99 unconditionalSelectors = unconditionalFilterKeys = null; 98 unconditionalSelectors = unconditionalFilterKeys = null;
100 knownExceptions = Object.create(null); 99 knownExceptions = Object.create(null);
101 exceptions = Object.create(null); 100 exceptions = Object.create(null);
102 FilterNotifier.emit("elemhideupdate"); 101 FilterNotifier.emit("elemhideupdate");
103 }, 102 },
104 103
105 _addToFiltersByDomain: function(key, filter) 104 _addToFiltersByDomain: function(key, filter)
106 { 105 {
107 let domains = filter.domains || defaultDomains; 106 let domains = filter.domains || defaultDomains;
108 for (let domain in domains) 107 for (let domain in domains)
(...skipping 21 matching lines...) Expand all
130 return; 129 return;
131 130
132 let selector = filter.selector; 131 let selector = filter.selector;
133 if (!(selector in exceptions)) 132 if (!(selector in exceptions))
134 exceptions[selector] = []; 133 exceptions[selector] = [];
135 exceptions[selector].push(filter); 134 exceptions[selector].push(filter);
136 135
137 // If this is the first exception for a previously unconditionally 136 // If this is the first exception for a previously unconditionally
138 // applied element hiding selector we need to take care to update the 137 // applied element hiding selector we need to take care to update the
139 // lookups. 138 // lookups.
140 let filterKeys = filterKeysBySelector[selector]; 139 let filterKey = filterKeyBySelector[selector];
141 if (filterKeys) 140 if (filterKey)
142 { 141 {
143 for (let filterKey of filterKeys) 142 this._addToFiltersByDomain(filterKey, filterByKey[filterKey]);
144 this._addToFiltersByDomain(filterKey, filterByKey[filterKey]); 143 delete filterKeyBySelector[selector];
145 delete filterKeysBySelector[selector];
146 unconditionalSelectors = unconditionalFilterKeys = null;
147 } 144 }
148 145
149 knownExceptions[filter.text] = true; 146 knownExceptions[filter.text] = true;
150 } 147 }
151 else 148 else
152 { 149 {
153 if (filter.text in keyByFilter) 150 if (filter.text in keyByFilter)
154 return; 151 return;
155 152
156 let key = filterByKey.push(filter) - 1; 153 let key = filterByKey.push(filter) - 1;
157 keyByFilter[filter.text] = key; 154 keyByFilter[filter.text] = key;
158 155
159 if (!(filter.domains || filter.selector in exceptions)) 156 if (!(filter.domains || filter.selector in exceptions))
160 { 157 {
161 // The new filter's selector is unconditionally applied to all domains 158 // The new filter's selector is unconditionally applied to all domains
162 let filterKeys = filterKeysBySelector[filter.selector]; 159 filterKeyBySelector[filter.selector] = key;
163 if (filterKeys) 160 unconditionalSelectors = unconditionalFilterKeys = null;
164 {
165 filterKeys.push(key);
166 }
167 else
168 {
169 filterKeysBySelector[filter.selector] = [key];
170 unconditionalSelectors = unconditionalFilterKeys = null;
171 }
172 } 161 }
173 else 162 else
174 { 163 {
175 // The new filter's selector only applies to some domains 164 // The new filter's selector only applies to some domains
176 this._addToFiltersByDomain(key, filter); 165 this._addToFiltersByDomain(key, filter);
177 } 166 }
178 } 167 }
179 168
180 FilterNotifier.emit("elemhideupdate"); 169 FilterNotifier.emit("elemhideupdate");
181 }, 170 },
(...skipping 17 matching lines...) Expand all
199 } 188 }
200 else 189 else
201 { 190 {
202 if (!(filter.text in keyByFilter)) 191 if (!(filter.text in keyByFilter))
203 return; 192 return;
204 193
205 let key = keyByFilter[filter.text]; 194 let key = keyByFilter[filter.text];
206 delete filterByKey[key]; 195 delete filterByKey[key];
207 delete keyByFilter[filter.text]; 196 delete keyByFilter[filter.text];
208 197
209 let filterKeys = filterKeysBySelector[filter.selector]; 198 if (filter.selector in filterKeyBySelector)
210 if (filterKeys)
211 { 199 {
212 if (filterKeys.length > 1) 200 delete filterKeyBySelector[filter.selector];
213 { 201 unconditionalSelectors = unconditionalFilterKeys = null;
214 let index = filterKeys.indexOf(key);
215 filterKeys.splice(index, 1);
216 }
217 else
218 {
219 delete filterKeysBySelector[filter.selector];
220 unconditionalSelectors = unconditionalFilterKeys = null;
221 }
222 } 202 }
223 else 203 else
224 { 204 {
225 let domains = filter.domains || defaultDomains; 205 let domains = filter.domains || defaultDomains;
226 for (let domain in domains) 206 for (let domain in domains)
227 { 207 {
228 let filters = filtersByDomain[domain]; 208 let filters = filtersByDomain[domain];
229 if (filters) 209 if (filters)
230 delete filters[key]; 210 delete filters[key];
231 } 211 }
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 return domains; 267 return domains;
288 }, 268 },
289 269
290 /** 270 /**
291 * Returns a list of selectors that apply on each website unconditionally. 271 * Returns a list of selectors that apply on each website unconditionally.
292 * @returns {String[]} 272 * @returns {String[]}
293 */ 273 */
294 getUnconditionalSelectors: function() 274 getUnconditionalSelectors: function()
295 { 275 {
296 if (!unconditionalSelectors) 276 if (!unconditionalSelectors)
297 unconditionalSelectors = Object.keys(filterKeysBySelector); 277 unconditionalSelectors = Object.keys(filterKeyBySelector);
298 return unconditionalSelectors.slice(); 278 return unconditionalSelectors.slice();
299 }, 279 },
300 280
301 /** 281 /**
302 * Returns a list of all selectors active on a particular domain. 282 * Returns a list of all selectors active on a particular domain.
303 * Returns a list of filterKeys for selectors that apply on each website 283 * Returns a list of filterKeys for selectors that apply on each website
304 * unconditionally. 284 * unconditionally.
305 * @returns {Number[]} 285 * @returns {Number[]}
306 */ 286 */
307 getUnconditionalFilterKeys: function() 287 getUnconditionalFilterKeys: function()
308 { 288 {
309 if (!unconditionalFilterKeys) 289 if (!unconditionalFilterKeys)
310 { 290 {
311 let selectors = this.getUnconditionalSelectors(); 291 let selectors = this.getUnconditionalSelectors();
312 unconditionalFilterKeys = []; 292 unconditionalFilterKeys = [];
313 for (let selector of selectors) 293 for (let selector of selectors)
314 unconditionalFilterKeys.push(filterKeysBySelector[selector][0]); 294 unconditionalFilterKeys.push(filterKeyBySelector[selector]);
315 } 295 }
316 return unconditionalFilterKeys.slice(); 296 return unconditionalFilterKeys.slice();
317 }, 297 },
318 298
319 /** 299 /**
320 * Returns a list of all selectors active on a particular domain. Optionally 300 * Returns a list of all selectors active on a particular domain. Optionally
321 * a list of corresponding filter keys for the selectors can be returned too. 301 * a list of corresponding filter keys for the selectors can be returned too.
322 */ 302 */
323 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly, 303 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly,
324 /**Boolean*/ noUnconditional, 304 /**Boolean*/ noUnconditional,
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 let nextDot = currentDomain.indexOf("."); 345 let nextDot = currentDomain.indexOf(".");
366 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 346 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
367 } 347 }
368 348
369 if (provideFilterKeys) 349 if (provideFilterKeys)
370 return [selectors, filterKeys]; 350 return [selectors, filterKeys];
371 else 351 else
372 return selectors; 352 return selectors;
373 } 353 }
374 }; 354 };
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