OLD | NEW |
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 Loading... |
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, filters key 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 filterKeyBySelector = 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 filterKeyBySelector 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 filterKeyBySelector table (filterIds for |
| 60 * selectors which unconditionally apply on all domains). It will be null if the |
| 61 * cache needs to be rebuilt. |
| 62 */ |
| 63 var unconditionalFilterKeys = null; |
| 64 |
| 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 /** |
73 * Lookup table, keys are known element hiding exceptions | 72 * Lookup table, keys are known element hiding exceptions |
74 * @type Object | 73 * @type Object |
75 */ | 74 */ |
76 var knownExceptions = Object.create(null); | 75 var knownExceptions = Object.create(null); |
(...skipping 11 matching lines...) Expand all Loading... |
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 filtersBySelector = Object.create(null); | 97 filterKeyBySelector = Object.create(null); |
99 unconditionalSelectors = 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(filter) | 104 _addToFiltersByDomain: function(key, filter) |
106 { | 105 { |
107 let key = keyByFilter[filter.text]; | |
108 let domains = filter.domains || defaultDomains; | 106 let domains = filter.domains || defaultDomains; |
109 for (let domain in domains) | 107 for (let domain in domains) |
110 { | 108 { |
111 let filters = filtersByDomain[domain]; | 109 let filters = filtersByDomain[domain]; |
112 if (!filters) | 110 if (!filters) |
113 filters = filtersByDomain[domain] = Object.create(null); | 111 filters = filtersByDomain[domain] = Object.create(null); |
114 | 112 |
115 if (domains[domain]) | 113 if (domains[domain]) |
116 filters[key] = filter; | 114 filters[key] = filter; |
117 else | 115 else |
(...skipping 10 matching lines...) Expand all Loading... |
128 if (filter instanceof ElemHideException) | 126 if (filter instanceof ElemHideException) |
129 { | 127 { |
130 if (filter.text in knownExceptions) | 128 if (filter.text in knownExceptions) |
131 return; | 129 return; |
132 | 130 |
133 let selector = filter.selector; | 131 let selector = filter.selector; |
134 if (!(selector in exceptions)) | 132 if (!(selector in exceptions)) |
135 exceptions[selector] = []; | 133 exceptions[selector] = []; |
136 exceptions[selector].push(filter); | 134 exceptions[selector].push(filter); |
137 | 135 |
138 if (usingGetSelectorsForDomain) | 136 // If this is the first exception for a previously unconditionally |
| 137 // applied element hiding selector we need to take care to update the |
| 138 // lookups. |
| 139 let filterKey = filterKeyBySelector[selector]; |
| 140 if (filterKey) |
139 { | 141 { |
140 // If this is the first exception for a previously unconditionally | 142 this._addToFiltersByDomain(filterKey, filterByKey[filterKey]); |
141 // applied element hiding selector we need to take care to update the | 143 delete filterKeyBySelector[selector]; |
142 // lookups. | 144 unconditionalSelectors = unconditionalFilterKeys = null; |
143 let unconditionalFilters = filtersBySelector[selector]; | |
144 if (unconditionalFilters) | |
145 { | |
146 for (let f of unconditionalFilters) | |
147 this._addToFiltersByDomain(f); | |
148 delete filtersBySelector[selector]; | |
149 unconditionalSelectors = null; | |
150 } | |
151 } | 145 } |
152 | 146 |
153 knownExceptions[filter.text] = true; | 147 knownExceptions[filter.text] = true; |
154 } | 148 } |
155 else | 149 else |
156 { | 150 { |
157 if (filter.text in keyByFilter) | 151 if (filter.text in keyByFilter) |
158 return; | 152 return; |
159 | 153 |
160 let key = filterByKey.push(filter) - 1; | 154 let key = filterByKey.push(filter) - 1; |
161 keyByFilter[filter.text] = key; | 155 keyByFilter[filter.text] = key; |
162 | 156 |
163 if (usingGetSelectorsForDomain) | 157 if (!(filter.domains || filter.selector in exceptions)) |
164 { | 158 { |
165 if (!(filter.domains || filter.selector in exceptions)) | 159 // The new filter's selector is unconditionally applied to all domains |
166 { | 160 filterKeyBySelector[filter.selector] = key; |
167 // The new filter's selector is unconditionally applied to all domains | 161 unconditionalSelectors = unconditionalFilterKeys = null; |
168 let filters = filtersBySelector[filter.selector]; | 162 } |
169 if (filters) | 163 else |
170 { | 164 { |
171 filters.push(filter); | 165 // The new filter's selector only applies to some domains |
172 } | 166 this._addToFiltersByDomain(key, filter); |
173 else | |
174 { | |
175 filtersBySelector[filter.selector] = [filter]; | |
176 unconditionalSelectors = null; | |
177 } | |
178 } | |
179 else | |
180 { | |
181 // The new filter's selector only applies to some domains | |
182 this._addToFiltersByDomain(filter); | |
183 } | |
184 } | 167 } |
185 } | 168 } |
186 | 169 |
187 FilterNotifier.emit("elemhideupdate"); | 170 FilterNotifier.emit("elemhideupdate"); |
188 }, | 171 }, |
189 | 172 |
190 /** | 173 /** |
191 * Removes an element hiding filter | 174 * Removes an element hiding filter |
192 * @param {ElemHideFilter} filter | 175 * @param {ElemHideFilter} filter |
193 */ | 176 */ |
(...skipping 12 matching lines...) Expand all Loading... |
206 } | 189 } |
207 else | 190 else |
208 { | 191 { |
209 if (!(filter.text in keyByFilter)) | 192 if (!(filter.text in keyByFilter)) |
210 return; | 193 return; |
211 | 194 |
212 let key = keyByFilter[filter.text]; | 195 let key = keyByFilter[filter.text]; |
213 delete filterByKey[key]; | 196 delete filterByKey[key]; |
214 delete keyByFilter[filter.text]; | 197 delete keyByFilter[filter.text]; |
215 | 198 |
216 if (usingGetSelectorsForDomain) | 199 if (filter.selector in filterKeyBySelector) |
217 { | 200 { |
218 let filters = filtersBySelector[filter.selector]; | 201 delete filterKeyBySelector[filter.selector]; |
219 if (filters) | 202 unconditionalSelectors = unconditionalFilterKeys = null; |
| 203 } |
| 204 else |
| 205 { |
| 206 let domains = filter.domains || defaultDomains; |
| 207 for (let domain in domains) |
220 { | 208 { |
221 if (filters.length > 1) | 209 let filters = filtersByDomain[domain]; |
222 { | 210 if (filters) |
223 let index = filters.indexOf(filter); | 211 delete filters[key]; |
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 } | 212 } |
242 } | 213 } |
243 } | 214 } |
244 | 215 |
245 FilterNotifier.emit("elemhideupdate"); | 216 FilterNotifier.emit("elemhideupdate"); |
246 }, | 217 }, |
247 | 218 |
248 /** | 219 /** |
249 * Checks whether an exception rule is registered for a filter on a particular | 220 * Checks whether an exception rule is registered for a filter on a particular |
250 * domain. | 221 * domain. |
251 */ | 222 */ |
252 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE
xception*/ | 223 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE
xception*/ |
253 { | 224 { |
254 if (!(filter.selector in exceptions)) | 225 if (!(filter.selector in exceptions)) |
255 return null; | 226 return null; |
256 | 227 |
257 let list = exceptions[filter.selector]; | 228 let list = exceptions[filter.selector]; |
258 for (let i = list.length - 1; i >= 0; i--) | 229 for (let i = list.length - 1; i >= 0; i--) |
259 if (list[i].isActiveOnDomain(docDomain)) | 230 if (list[i].isActiveOnDomain(docDomain)) |
260 return list[i]; | 231 return list[i]; |
261 | 232 |
262 return null; | 233 return null; |
263 }, | 234 }, |
264 | 235 |
265 /** | 236 /** |
266 * Retrieves an element hiding filter by the corresponding protocol key | 237 * Retrieves an element hiding filter by the corresponding protocol key |
267 */ | 238 */ |
268 getFilterByKey: function(/**String*/ key) /**Filter*/ | 239 getFilterByKey: function(/**Number*/ key) /**Filter*/ |
269 { | 240 { |
270 return (key in filterByKey ? filterByKey[key] : null); | 241 return (key in filterByKey ? filterByKey[key] : null); |
271 }, | 242 }, |
272 | 243 |
273 /** | 244 /** |
274 * Returns a list of all selectors as a nested map. On first level, the keys | 245 * 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 | 246 * are all values of `ElemHideBase.selectorDomain` (domains on which these |
276 * selectors should apply, ignoring exceptions). The values are maps again, | 247 * selectors should apply, ignoring exceptions). The values are maps again, |
277 * with the keys being selectors and values the corresponding filter keys. | 248 * with the keys being selectors and values the corresponding filter keys. |
278 * @returns {Map.<String,Map<String,String>>} | 249 * @returns {Map.<String,Map<String,String>>} |
(...skipping 12 matching lines...) Expand all Loading... |
291 | 262 |
292 if (!domains.has(domain)) | 263 if (!domains.has(domain)) |
293 domains.set(domain, new Map()); | 264 domains.set(domain, new Map()); |
294 domains.get(domain).set(selector, key); | 265 domains.get(domain).set(selector, key); |
295 } | 266 } |
296 | 267 |
297 return domains; | 268 return domains; |
298 }, | 269 }, |
299 | 270 |
300 /** | 271 /** |
301 * Returns a list of all selectors active on a particular domain, must not be | 272 * Returns a list of selectors that apply on each website unconditionally. |
302 * used in Firefox (when usingGetSelectorsForDomain is false). | 273 * @returns {String[]} |
303 */ | 274 */ |
304 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) | 275 getUnconditionalSelectors: function() |
305 { | 276 { |
306 if (!usingGetSelectorsForDomain) | 277 if (!unconditionalSelectors) |
307 throw new Error("getSelectorsForDomain can not be used in Firefox!"); | 278 unconditionalSelectors = Object.keys(filterKeyBySelector); |
| 279 return unconditionalSelectors.slice(); |
| 280 }, |
308 | 281 |
309 if (!unconditionalSelectors) | 282 /** |
310 unconditionalSelectors = Object.keys(filtersBySelector); | 283 * Returns a list of all selectors active on a particular domain. |
311 let selectors = specificOnly ? [] : unconditionalSelectors.slice(); | 284 * Returns a list of filterKeys for selectors that apply on each website |
| 285 * unconditionally. |
| 286 * @returns {Number[]} |
| 287 */ |
| 288 getUnconditionalFilterKeys: function() |
| 289 { |
| 290 if (!unconditionalFilterKeys) |
| 291 { |
| 292 let selectors = this.getUnconditionalSelectors(); |
| 293 unconditionalFilterKeys = []; |
| 294 for (let selector of selectors) |
| 295 unconditionalFilterKeys.push(filterKeyBySelector[selector]); |
| 296 } |
| 297 return unconditionalFilterKeys.slice(); |
| 298 }, |
| 299 |
| 300 /** |
| 301 * Returns a list of all selectors active on a particular domain. Optionally |
| 302 * a list of corresponding filter keys for the selectors can be returned too. |
| 303 */ |
| 304 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly, |
| 305 /**Boolean*/ noUnconditional, |
| 306 /**Boolean*/ provideFilterKeys) |
| 307 { |
| 308 let filterKeys = []; |
| 309 let selectors = []; |
| 310 if (!specificOnly && !noUnconditional) |
| 311 { |
| 312 selectors = this.getUnconditionalSelectors(); |
| 313 if (provideFilterKeys) |
| 314 filterKeys = this.getUnconditionalFilterKeys(); |
| 315 } |
312 | 316 |
313 let seenFilters = Object.create(null); | 317 let seenFilters = Object.create(null); |
314 let currentDomain = domain ? domain.toUpperCase() : ""; | 318 let currentDomain = domain ? domain.toUpperCase() : ""; |
315 while (true) | 319 while (true) |
316 { | 320 { |
317 if (specificOnly && currentDomain == "") | 321 if (specificOnly && currentDomain == "") |
318 break; | 322 break; |
319 | 323 |
320 let filters = filtersByDomain[currentDomain]; | 324 let filters = filtersByDomain[currentDomain]; |
321 if (filters) | 325 if (filters) |
322 { | 326 { |
323 for (let filterKey in filters) | 327 for (let filterKey in filters) |
324 { | 328 { |
325 if (filterKey in seenFilters) | 329 if (filterKey in seenFilters) |
326 continue; | 330 continue; |
327 seenFilters[filterKey] = true; | 331 seenFilters[filterKey] = true; |
328 | 332 |
329 let filter = filters[filterKey]; | 333 let filter = filters[filterKey]; |
330 if (filter && !this.getException(filter, domain)) | 334 if (filter && !this.getException(filter, domain)) |
| 335 { |
331 selectors.push(filter.selector); | 336 selectors.push(filter.selector); |
| 337 // It is faster to always push the key, even if not required. |
| 338 filterKeys.push(filterKey); |
| 339 } |
332 } | 340 } |
333 } | 341 } |
334 | 342 |
335 if (currentDomain == "") | 343 if (currentDomain == "") |
336 break; | 344 break; |
337 | 345 |
338 let nextDot = currentDomain.indexOf("."); | 346 let nextDot = currentDomain.indexOf("."); |
339 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | 347 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); |
340 } | 348 } |
341 | 349 |
342 return selectors; | 350 if (provideFilterKeys) |
| 351 return [selectors, filterKeys]; |
| 352 else |
| 353 return selectors; |
343 } | 354 } |
344 }; | 355 }; |
OLD | NEW |