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

Side by Side Diff: lib/elemHide.js

Issue 29349187: Issue 4167 - getSelectorsForDomain criteria + keys (Closed)
Patch Set: Addressed feedback Created Sept. 20, 2016, 6:32 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 | test/elemHide.js » ('j') | test/elemHide.js » ('J')
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 18 matching lines...) Expand all
29 */ 29 */
30 var filterByKey = []; 30 var filterByKey = [];
31 31
32 /** 32 /**
33 * Lookup table, keys of the filters by filter text 33 * Lookup table, keys of the filters by filter text
34 * @type Object 34 * @type Object
35 */ 35 */
36 var keyByFilter = Object.create(null); 36 var keyByFilter = Object.create(null);
37 37
38 /** 38 /**
39 * Indicates whether we are using the getSelectorsForDomain function and
40 * therefore mainting the required filtersByDomain, filtersBySelector and
41 * unconditionalSelectors lookups. (Will be false for Firefox)
42 * @type Boolean
43 */
44 var usingGetSelectorsForDomain = !("nsIStyleSheetService" in Ci);
45
46 /**
47 * 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.
48 * (Only contains filters that aren't unconditionally matched for all domains.) 40 * (Only contains filters that aren't unconditionally matched for all domains.)
49 * @type Object 41 * @type Object
50 */ 42 */
51 var filtersByDomain = Object.create(null); 43 var filtersByDomain = Object.create(null);
52 44
53 /** 45 /**
54 * Lookup table, filters by selector. (Only contains filters that have a 46 * Lookup table, filter keys by selector. (Only contains filters that have a
55 * selector that is unconditionally matched for all domains.) 47 * selector that is unconditionally matched for all domains.)
56 */ 48 */
57 var filtersBySelector = Object.create(null); 49 var filterKeysBySelector = Object.create(null);
58 50
59 /** 51 /**
60 * This array caches the keys of filtersBySelector table (selectors which 52 * This array caches the keys of filterKeysBySelector table (selectors which
61 * 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
62 * be rebuilt. 54 * be rebuilt.
63 */ 55 */
64 var unconditionalSelectors = null; 56 var unconditionalSelectors = null;
65 57
66 /** 58 /**
59 * This array caches the values of filterKeysBySelector table (filterIds for
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
62 * is cached.
63 */
64 var unconditionalFilterKeys = null;
65
66 /**
67 * Object to be used instead when a filter has a blank domains property. 67 * Object to be used instead when a filter has a blank domains property.
68 */ 68 */
69 var defaultDomains = Object.create(null); 69 var defaultDomains = Object.create(null);
70 defaultDomains[""] = true; 70 defaultDomains[""] = true;
71 71
72 /** 72 /**
73 * Lookup table, keys are known element hiding exceptions 73 * Lookup table, keys are known element hiding exceptions
74 * @type Object 74 * @type Object
75 */ 75 */
76 var knownExceptions = Object.create(null); 76 var knownExceptions = Object.create(null);
(...skipping 11 matching lines...) Expand all
88 var ElemHide = exports.ElemHide = 88 var ElemHide = exports.ElemHide =
89 { 89 {
90 /** 90 /**
91 * Removes all known filters 91 * Removes all known filters
92 */ 92 */
93 clear: function() 93 clear: function()
94 { 94 {
95 filterByKey = []; 95 filterByKey = [];
96 keyByFilter = Object.create(null); 96 keyByFilter = Object.create(null);
97 filtersByDomain = Object.create(null); 97 filtersByDomain = Object.create(null);
98 filtersBySelector = Object.create(null); 98 filterKeysBySelector = Object.create(null);
99 unconditionalSelectors = null; 99 unconditionalSelectors = unconditionalFilterKeys = null;
100 knownExceptions = Object.create(null); 100 knownExceptions = Object.create(null);
101 exceptions = Object.create(null); 101 exceptions = Object.create(null);
102 FilterNotifier.emit("elemhideupdate"); 102 FilterNotifier.emit("elemhideupdate");
103 }, 103 },
104 104
105 _addToFiltersByDomain: function(filter) 105 _addToFiltersByDomain: function(key, filter)
106 { 106 {
107 let key = keyByFilter[filter.text];
108 let domains = filter.domains || defaultDomains; 107 let domains = filter.domains || defaultDomains;
109 for (let domain in domains) 108 for (let domain in domains)
110 { 109 {
111 let filters = filtersByDomain[domain]; 110 let filters = filtersByDomain[domain];
112 if (!filters) 111 if (!filters)
113 filters = filtersByDomain[domain] = Object.create(null); 112 filters = filtersByDomain[domain] = Object.create(null);
114 113
115 if (domains[domain]) 114 if (domains[domain])
116 filters[key] = filter; 115 filters[key] = filter;
117 else 116 else
(...skipping 10 matching lines...) Expand all
128 if (filter instanceof ElemHideException) 127 if (filter instanceof ElemHideException)
129 { 128 {
130 if (filter.text in knownExceptions) 129 if (filter.text in knownExceptions)
131 return; 130 return;
132 131
133 let selector = filter.selector; 132 let selector = filter.selector;
134 if (!(selector in exceptions)) 133 if (!(selector in exceptions))
135 exceptions[selector] = []; 134 exceptions[selector] = [];
136 exceptions[selector].push(filter); 135 exceptions[selector].push(filter);
137 136
138 if (usingGetSelectorsForDomain) 137 // If this is the first exception for a previously unconditionally
138 // applied element hiding selector we need to take care to update the
139 // lookups.
140 let filterKeys = filterKeysBySelector[selector];
141 if (filterKeys)
139 { 142 {
140 // If this is the first exception for a previously unconditionally 143 for (let filterKey of filterKeys)
141 // applied element hiding selector we need to take care to update the 144 this._addToFiltersByDomain(filterKey, filterByKey[filterKey]);
142 // lookups. 145 delete filterKeysBySelector[selector];
143 let unconditionalFilters = filtersBySelector[selector]; 146 unconditionalSelectors = unconditionalFilterKeys = null;
144 if (unconditionalFilters)
145 {
146 for (let f of unconditionalFilters)
147 this._addToFiltersByDomain(f);
148 delete filtersBySelector[selector];
149 unconditionalSelectors = null;
150 }
151 } 147 }
152 148
153 knownExceptions[filter.text] = true; 149 knownExceptions[filter.text] = true;
154 } 150 }
155 else 151 else
156 { 152 {
157 if (filter.text in keyByFilter) 153 if (filter.text in keyByFilter)
158 return; 154 return;
159 155
160 let key = filterByKey.push(filter) - 1; 156 let key = filterByKey.push(filter) - 1;
161 keyByFilter[filter.text] = key; 157 keyByFilter[filter.text] = key;
162 158
163 if (usingGetSelectorsForDomain) 159 if (!(filter.domains || filter.selector in exceptions))
164 { 160 {
165 if (!(filter.domains || filter.selector in exceptions)) 161 // The new filter's selector is unconditionally applied to all domains
162 let filterKeys = filterKeysBySelector[filter.selector];
163 if (filterKeys)
166 { 164 {
167 // The new filter's selector is unconditionally applied to all domains 165 filterKeys.push(key);
168 let filters = filtersBySelector[filter.selector];
169 if (filters)
170 {
171 filters.push(filter);
172 }
173 else
174 {
175 filtersBySelector[filter.selector] = [filter];
176 unconditionalSelectors = null;
177 }
178 } 166 }
179 else 167 else
180 { 168 {
181 // The new filter's selector only applies to some domains 169 filterKeysBySelector[filter.selector] = [key];
182 this._addToFiltersByDomain(filter); 170 unconditionalSelectors = unconditionalFilterKeys = null;
183 } 171 }
184 } 172 }
173 else
174 {
175 // The new filter's selector only applies to some domains
176 this._addToFiltersByDomain(key, filter);
177 }
185 } 178 }
186 179
187 FilterNotifier.emit("elemhideupdate"); 180 FilterNotifier.emit("elemhideupdate");
188 }, 181 },
189 182
183 _removeFilterKey: function(key, filter)
184 {
185 let filterKeys = filterKeysBySelector[filter.selector];
186 if (filterKeys)
187 {
188 let index = filterKeys.indexOf(key);
189 if (index >= 0)
kzar 2016/09/20 18:37:35 It kind of bugs me how we check index twice, also
190 {
191 if (filterKeys.length > 1)
192 {
193 filterKeys.splice(index, 1);
194 if (index == 0)
195 unconditionalFilterKeys = null;
196 }
197 else
198 {
199 delete filterKeysBySelector[filter.selector];
200 unconditionalSelectors = unconditionalFilterKeys = null;
201 }
202 return;
203 }
204 }
205
206 // We haven't found this filter in unconditional filters, look in
207 // filtersByDomain.
208 let domains = filter.domains || defaultDomains;
209 for (let domain in domains)
210 {
211 let filters = filtersByDomain[domain];
212 if (filters)
213 delete filters[key];
214 }
215 },
216
190 /** 217 /**
191 * Removes an element hiding filter 218 * Removes an element hiding filter
192 * @param {ElemHideFilter} filter 219 * @param {ElemHideFilter} filter
193 */ 220 */
194 remove: function(filter) 221 remove: function(filter)
195 { 222 {
196 if (filter instanceof ElemHideException) 223 if (filter instanceof ElemHideException)
197 { 224 {
198 if (!(filter.text in knownExceptions)) 225 if (!(filter.text in knownExceptions))
199 return; 226 return;
200 227
201 let list = exceptions[filter.selector]; 228 let list = exceptions[filter.selector];
202 let index = list.indexOf(filter); 229 let index = list.indexOf(filter);
203 if (index >= 0) 230 if (index >= 0)
204 list.splice(index, 1); 231 list.splice(index, 1);
205 delete knownExceptions[filter.text]; 232 delete knownExceptions[filter.text];
206 } 233 }
207 else 234 else
208 { 235 {
209 if (!(filter.text in keyByFilter)) 236 if (!(filter.text in keyByFilter))
210 return; 237 return;
211 238
212 let key = keyByFilter[filter.text]; 239 let key = keyByFilter[filter.text];
213 delete filterByKey[key]; 240 delete filterByKey[key];
214 delete keyByFilter[filter.text]; 241 delete keyByFilter[filter.text];
215 242 this._removeFilterKey(key, filter);
216 if (usingGetSelectorsForDomain)
217 {
218 let filters = filtersBySelector[filter.selector];
219 if (filters)
220 {
221 if (filters.length > 1)
222 {
223 let index = filters.indexOf(filter);
224 filters.splice(index, 1);
225 }
226 else
227 {
228 delete filtersBySelector[filter.selector];
229 unconditionalSelectors = null;
230 }
231 }
232 else
233 {
234 let domains = filter.domains || defaultDomains;
235 for (let domain in domains)
236 {
237 let filters = filtersByDomain[domain];
238 if (filters)
239 delete filters[key];
240 }
241 }
242 }
243 } 243 }
244 244
245 FilterNotifier.emit("elemhideupdate"); 245 FilterNotifier.emit("elemhideupdate");
246 }, 246 },
247 247
248 /** 248 /**
249 * Checks whether an exception rule is registered for a filter on a particular 249 * Checks whether an exception rule is registered for a filter on a particular
250 * domain. 250 * domain.
251 */ 251 */
252 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE xception*/ 252 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE xception*/
253 { 253 {
254 if (!(filter.selector in exceptions)) 254 if (!(filter.selector in exceptions))
255 return null; 255 return null;
256 256
257 let list = exceptions[filter.selector]; 257 let list = exceptions[filter.selector];
258 for (let i = list.length - 1; i >= 0; i--) 258 for (let i = list.length - 1; i >= 0; i--)
259 if (list[i].isActiveOnDomain(docDomain)) 259 if (list[i].isActiveOnDomain(docDomain))
260 return list[i]; 260 return list[i];
261 261
262 return null; 262 return null;
263 }, 263 },
264 264
265 /** 265 /**
266 * Retrieves an element hiding filter by the corresponding protocol key 266 * Retrieves an element hiding filter by the corresponding protocol key
267 */ 267 */
268 getFilterByKey: function(/**String*/ key) /**Filter*/ 268 getFilterByKey: function(/**Number*/ key) /**Filter*/
269 { 269 {
270 return (key in filterByKey ? filterByKey[key] : null); 270 return (key in filterByKey ? filterByKey[key] : null);
271 }, 271 },
272 272
273 /** 273 /**
274 * Returns a list of all selectors as a nested map. On first level, the keys 274 * Returns a list of all selectors as a nested map. On first level, the keys
275 * are all values of `ElemHideBase.selectorDomain` (domains on which these 275 * are all values of `ElemHideBase.selectorDomain` (domains on which these
276 * selectors should apply, ignoring exceptions). The values are maps again, 276 * selectors should apply, ignoring exceptions). The values are maps again,
277 * with the keys being selectors and values the corresponding filter keys. 277 * with the keys being selectors and values the corresponding filter keys.
278 * @returns {Map.<String,Map<String,String>>} 278 * @returns {Map.<String,Map<String,String>>}
(...skipping 12 matching lines...) Expand all
291 291
292 if (!domains.has(domain)) 292 if (!domains.has(domain))
293 domains.set(domain, new Map()); 293 domains.set(domain, new Map());
294 domains.get(domain).set(selector, key); 294 domains.get(domain).set(selector, key);
295 } 295 }
296 296
297 return domains; 297 return domains;
298 }, 298 },
299 299
300 /** 300 /**
301 * Returns a list of all selectors active on a particular domain, must not be 301 * Returns a list of selectors that apply on each website unconditionally.
302 * used in Firefox (when usingGetSelectorsForDomain is false). 302 * @returns {String[]}
303 */ 303 */
304 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) 304 getUnconditionalSelectors: function()
305 { 305 {
306 if (!usingGetSelectorsForDomain) 306 if (!unconditionalSelectors)
307 throw new Error("getSelectorsForDomain can not be used in Firefox!"); 307 unconditionalSelectors = Object.keys(filterKeysBySelector);
308 return unconditionalSelectors.slice();
309 },
308 310
309 if (!unconditionalSelectors) 311 /**
310 unconditionalSelectors = Object.keys(filtersBySelector); 312 * Returns a list of all selectors active on a particular domain.
311 let selectors = specificOnly ? [] : unconditionalSelectors.slice(); 313 * Returns a list of filterKeys for selectors that apply on each website
314 * unconditionally.
315 * @returns {Number[]}
316 */
317 getUnconditionalFilterKeys: function()
318 {
319 if (!unconditionalFilterKeys)
320 {
321 let selectors = this.getUnconditionalSelectors();
322 unconditionalFilterKeys = [];
323 for (let selector of selectors)
324 unconditionalFilterKeys.push(filterKeysBySelector[selector][0]);
325 }
326 return unconditionalFilterKeys.slice();
327 },
312 328
329
330 /**
331 * Constant used by getSelectorsForDomain's when matching all selectors.
332 */
333 ALL_MATCHING: 0,
334
335 /**
336 * Constant used by getSelectorsForDomain's when not matching unconditional
337 * selectors.
338 */
339 NO_UNCONDITIONAL: 1,
340
341 /**
342 * Constant used by getSelectorsForDomain's when only matching specific
343 * selectors.
344 */
345 SPECIFIC_ONLY: 2,
346
347 /**
348 * Returns a list of all selectors active on a particular domain. Optionally
349 * a list of corresponding filter keys for the selectors can be returned too.
350 * The optional criteria parameter restricts the results, and must have the
351 * value of ALL_MATCHING, NO_UNCONDITIONAL or SPECIFIC_ONLY if provided.
Wladimir Palant 2016/09/26 14:56:54 Nit: You are documenting return value and paramete
kzar 2016/09/27 12:40:22 Done.
352 */
353 getSelectorsForDomain: function(/**String*/ domain,
354 /**Number*/ criteria,
355 /**Boolean*/ provideFilterKeys)
356 {
357 let filterKeys = [];
358 let selectors = [];
359
360 if (typeof criteria == "undefined")
361 criteria = ElemHide.ALL_MATCHING;
362 if (criteria < ElemHide.NO_UNCONDITIONAL)
363 {
364 selectors = this.getUnconditionalSelectors();
365 if (provideFilterKeys)
366 filterKeys = this.getUnconditionalFilterKeys();
367 }
368
369 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY);
313 let seenFilters = Object.create(null); 370 let seenFilters = Object.create(null);
314 let currentDomain = domain ? domain.toUpperCase() : ""; 371 let currentDomain = domain ? domain.toUpperCase() : "";
315 while (true) 372 while (true)
316 { 373 {
317 if (specificOnly && currentDomain == "") 374 if (specificOnly && currentDomain == "")
318 break; 375 break;
319 376
320 let filters = filtersByDomain[currentDomain]; 377 let filters = filtersByDomain[currentDomain];
321 if (filters) 378 if (filters)
322 { 379 {
323 for (let filterKey in filters) 380 for (let filterKey in filters)
324 { 381 {
325 if (filterKey in seenFilters) 382 if (filterKey in seenFilters)
326 continue; 383 continue;
327 seenFilters[filterKey] = true; 384 seenFilters[filterKey] = true;
328 385
329 let filter = filters[filterKey]; 386 let filter = filters[filterKey];
330 if (filter && !this.getException(filter, domain)) 387 if (filter && !this.getException(filter, domain))
388 {
331 selectors.push(filter.selector); 389 selectors.push(filter.selector);
390 // It is faster to always push the key, even if not required.
391 filterKeys.push(filterKey);
392 }
332 } 393 }
333 } 394 }
334 395
335 if (currentDomain == "") 396 if (currentDomain == "")
336 break; 397 break;
337 398
338 let nextDot = currentDomain.indexOf("."); 399 let nextDot = currentDomain.indexOf(".");
339 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 400 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
340 } 401 }
341 402
342 return selectors; 403 if (provideFilterKeys)
404 return [selectors, filterKeys];
405 else
406 return selectors;
343 } 407 }
344 }; 408 };
OLDNEW
« no previous file with comments | « no previous file | test/elemHide.js » ('j') | test/elemHide.js » ('J')

Powered by Google App Engine
This is Rietveld