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: Improved comments Created Sept. 27, 2016, 1:52 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') | 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 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)
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 to return all selectors applying to
332 * a particular hostname.
333 */
334 ALL_MATCHING: 0,
335
336 /**
337 * Constant used by getSelectorsForDomain to exclude selectors which apply to
338 * all websites without exception.
339 */
340 NO_UNCONDITIONAL: 1,
341
342 /**
343 * Constant used by getSelectorsForDomain to return only selectors for filters
344 * which specifically match the given host name.
345 */
346 SPECIFIC_ONLY: 2,
347
348 /**
349 * Determines from the current filter list which selectors should be applied
350 * on a particular host name. Optionally returns the corresponding filter
351 * keys.
352 * @param {String} domain
353 * @param {Number} [criteria]
354 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or
355 * ElemHide.SPECIFIC_ONLY.
356 * @param {Boolean} [provideFilterKeys]
357 * If true, the function will return a list of corresponding filter keys in
358 * addition to selectors.
359 * @returns {string[]|Array.<string[]>}
360 * List of selectors or an array with two elements (list of selectors and
361 * list of corresponding keys) if provideFilterKeys is true.
362 */
363 getSelectorsForDomain: function(domain, criteria, provideFilterKeys)
364 {
365 let filterKeys = [];
366 let selectors = [];
367
368 if (typeof criteria == "undefined")
369 criteria = ElemHide.ALL_MATCHING;
370 if (criteria < ElemHide.NO_UNCONDITIONAL)
371 {
372 selectors = this.getUnconditionalSelectors();
373 if (provideFilterKeys)
374 filterKeys = this.getUnconditionalFilterKeys();
375 }
376
377 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY);
313 let seenFilters = Object.create(null); 378 let seenFilters = Object.create(null);
314 let currentDomain = domain ? domain.toUpperCase() : ""; 379 let currentDomain = domain ? domain.toUpperCase() : "";
315 while (true) 380 while (true)
316 { 381 {
317 if (specificOnly && currentDomain == "") 382 if (specificOnly && currentDomain == "")
318 break; 383 break;
319 384
320 let filters = filtersByDomain[currentDomain]; 385 let filters = filtersByDomain[currentDomain];
321 if (filters) 386 if (filters)
322 { 387 {
323 for (let filterKey in filters) 388 for (let filterKey in filters)
324 { 389 {
325 if (filterKey in seenFilters) 390 if (filterKey in seenFilters)
326 continue; 391 continue;
327 seenFilters[filterKey] = true; 392 seenFilters[filterKey] = true;
328 393
329 let filter = filters[filterKey]; 394 let filter = filters[filterKey];
330 if (filter && !this.getException(filter, domain)) 395 if (filter && !this.getException(filter, domain))
396 {
331 selectors.push(filter.selector); 397 selectors.push(filter.selector);
398 // It is faster to always push the key, even if not required.
399 filterKeys.push(filterKey);
400 }
332 } 401 }
333 } 402 }
334 403
335 if (currentDomain == "") 404 if (currentDomain == "")
336 break; 405 break;
337 406
338 let nextDot = currentDomain.indexOf("."); 407 let nextDot = currentDomain.indexOf(".");
339 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 408 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
340 } 409 }
341 410
342 return selectors; 411 if (provideFilterKeys)
412 return [selectors, filterKeys];
413 else
414 return selectors;
343 } 415 }
344 }; 416 };
OLDNEW
« no previous file with comments | « no previous file | test/elemHide.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld