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: Remove unrelated changes Created Sept. 20, 2016, 7:56 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 | 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
190 /** 183 /**
191 * Removes an element hiding filter 184 * Removes an element hiding filter
192 * @param {ElemHideFilter} filter 185 * @param {ElemHideFilter} filter
193 */ 186 */
194 remove: function(filter) 187 remove: function(filter)
(...skipping 11 matching lines...) Expand all
206 } 199 }
207 else 200 else
208 { 201 {
209 if (!(filter.text in keyByFilter)) 202 if (!(filter.text in keyByFilter))
210 return; 203 return;
211 204
212 let key = keyByFilter[filter.text]; 205 let key = keyByFilter[filter.text];
213 delete filterByKey[key]; 206 delete filterByKey[key];
214 delete keyByFilter[filter.text]; 207 delete keyByFilter[filter.text];
215 208
216 if (usingGetSelectorsForDomain) 209 let filterKeys = filterKeysBySelector[filter.selector];
210 if (filterKeys)
217 { 211 {
218 let filters = filtersBySelector[filter.selector]; 212 if (filterKeys.length > 1)
219 if (filters)
220 { 213 {
221 if (filters.length > 1) 214 let index = filterKeys.indexOf(key);
222 { 215 filterKeys.splice(index, 1);
Wladimir Palant 2016/09/20 10:36:07 We need to null out unconditionalFilterKeys here i
kzar 2016/09/20 14:23:48 Good point, Done.
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 } 216 }
232 else 217 else
233 { 218 {
234 let domains = filter.domains || defaultDomains; 219 delete filterKeysBySelector[filter.selector];
235 for (let domain in domains) 220 unconditionalSelectors = unconditionalFilterKeys = null;
236 { 221 }
237 let filters = filtersByDomain[domain]; 222 }
238 if (filters) 223 else
239 delete filters[key]; 224 {
240 } 225 let domains = filter.domains || defaultDomains;
226 for (let domain in domains)
227 {
228 let filters = filtersByDomain[domain];
229 if (filters)
230 delete filters[key];
241 } 231 }
242 } 232 }
243 } 233 }
244 234
245 FilterNotifier.emit("elemhideupdate"); 235 FilterNotifier.emit("elemhideupdate");
246 }, 236 },
247 237
248 /** 238 /**
249 * Checks whether an exception rule is registered for a filter on a particular 239 * Checks whether an exception rule is registered for a filter on a particular
250 * domain. 240 * domain.
251 */ 241 */
252 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE xception*/ 242 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE xception*/
253 { 243 {
254 if (!(filter.selector in exceptions)) 244 if (!(filter.selector in exceptions))
255 return null; 245 return null;
256 246
257 let list = exceptions[filter.selector]; 247 let list = exceptions[filter.selector];
258 for (let i = list.length - 1; i >= 0; i--) 248 for (let i = list.length - 1; i >= 0; i--)
259 if (list[i].isActiveOnDomain(docDomain)) 249 if (list[i].isActiveOnDomain(docDomain))
260 return list[i]; 250 return list[i];
261 251
262 return null; 252 return null;
263 }, 253 },
264 254
265 /** 255 /**
266 * Retrieves an element hiding filter by the corresponding protocol key 256 * Retrieves an element hiding filter by the corresponding protocol key
267 */ 257 */
268 getFilterByKey: function(/**String*/ key) /**Filter*/ 258 getFilterByKey: function(/**Number*/ key) /**Filter*/
269 { 259 {
270 return (key in filterByKey ? filterByKey[key] : null); 260 return (key in filterByKey ? filterByKey[key] : null);
271 }, 261 },
272 262
273 /** 263 /**
274 * Returns a list of all selectors as a nested map. On first level, the keys 264 * 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 265 * are all values of `ElemHideBase.selectorDomain` (domains on which these
276 * selectors should apply, ignoring exceptions). The values are maps again, 266 * selectors should apply, ignoring exceptions). The values are maps again,
277 * with the keys being selectors and values the corresponding filter keys. 267 * with the keys being selectors and values the corresponding filter keys.
278 * @returns {Map.<String,Map<String,String>>} 268 * @returns {Map.<String,Map<String,String>>}
(...skipping 12 matching lines...) Expand all
291 281
292 if (!domains.has(domain)) 282 if (!domains.has(domain))
293 domains.set(domain, new Map()); 283 domains.set(domain, new Map());
294 domains.get(domain).set(selector, key); 284 domains.get(domain).set(selector, key);
295 } 285 }
296 286
297 return domains; 287 return domains;
298 }, 288 },
299 289
300 /** 290 /**
301 * Returns a list of all selectors active on a particular domain, must not be 291 * Returns a list of selectors that apply on each website unconditionally.
302 * used in Firefox (when usingGetSelectorsForDomain is false). 292 * @returns {String[]}
303 */ 293 */
304 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) 294 getUnconditionalSelectors: function()
305 { 295 {
306 if (!usingGetSelectorsForDomain) 296 if (!unconditionalSelectors)
307 throw new Error("getSelectorsForDomain can not be used in Firefox!"); 297 unconditionalSelectors = Object.keys(filterKeysBySelector);
298 return unconditionalSelectors.slice();
299 },
308 300
309 if (!unconditionalSelectors) 301 /**
310 unconditionalSelectors = Object.keys(filtersBySelector); 302 * Returns a list of all selectors active on a particular domain.
311 let selectors = specificOnly ? [] : unconditionalSelectors.slice(); 303 * Returns a list of filterKeys for selectors that apply on each website
304 * unconditionally.
305 * @returns {Number[]}
306 */
307 getUnconditionalFilterKeys: function()
308 {
309 if (!unconditionalFilterKeys)
310 {
311 let selectors = this.getUnconditionalSelectors();
312 unconditionalFilterKeys = [];
313 for (let selector of selectors)
314 unconditionalFilterKeys.push(filterKeysBySelector[selector][0]);
315 }
316 return unconditionalFilterKeys.slice();
317 },
318
319 /**
320 * 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.
322 */
323 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly,
324 /**Boolean*/ noUnconditional,
325 /**Boolean*/ provideFilterKeys)
326 {
327 let filterKeys = [];
328 let selectors = [];
329 if (!specificOnly && !noUnconditional)
330 {
331 selectors = this.getUnconditionalSelectors();
332 if (provideFilterKeys)
333 filterKeys = this.getUnconditionalFilterKeys();
334 }
312 335
313 let seenFilters = Object.create(null); 336 let seenFilters = Object.create(null);
314 let currentDomain = domain ? domain.toUpperCase() : ""; 337 let currentDomain = domain ? domain.toUpperCase() : "";
315 while (true) 338 while (true)
316 { 339 {
317 if (specificOnly && currentDomain == "") 340 if (specificOnly && currentDomain == "")
318 break; 341 break;
319 342
320 let filters = filtersByDomain[currentDomain]; 343 let filters = filtersByDomain[currentDomain];
321 if (filters) 344 if (filters)
322 { 345 {
323 for (let filterKey in filters) 346 for (let filterKey in filters)
324 { 347 {
325 if (filterKey in seenFilters) 348 if (filterKey in seenFilters)
326 continue; 349 continue;
327 seenFilters[filterKey] = true; 350 seenFilters[filterKey] = true;
328 351
329 let filter = filters[filterKey]; 352 let filter = filters[filterKey];
330 if (filter && !this.getException(filter, domain)) 353 if (filter && !this.getException(filter, domain))
354 {
331 selectors.push(filter.selector); 355 selectors.push(filter.selector);
356 // It is faster to always push the key, even if not required.
357 filterKeys.push(filterKey);
358 }
332 } 359 }
333 } 360 }
334 361
335 if (currentDomain == "") 362 if (currentDomain == "")
336 break; 363 break;
337 364
338 let nextDot = currentDomain.indexOf("."); 365 let nextDot = currentDomain.indexOf(".");
339 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 366 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
340 } 367 }
341 368
342 return selectors; 369 if (provideFilterKeys)
370 return [selectors, filterKeys];
371 else
372 return selectors;
343 } 373 }
344 }; 374 };
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