| Left: | ||
| Right: |
| 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 19 matching lines...) Expand all Loading... | |
| 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 | 39 * Indicates whether we are using the getSelectorsForDomain function and |
| 40 * therefore mainting the required filtersByDomain, filtersBySelector and | 40 * therefore maintaining the required filtersByDomain, filterKeysBySelector, |
| 41 * unconditionalSelectors lookups. (Will be false for Firefox) | 41 * unconditionalSelectors and unconditionalFilterKeys lookups. |
| 42 * (Will be false for Firefox) | |
| 42 * @type Boolean | 43 * @type Boolean |
| 43 */ | 44 */ |
| 44 var usingGetSelectorsForDomain = !("nsIStyleSheetService" in Ci); | 45 var usingGetSelectorsForDomain = !("nsIStyleSheetService" in Ci); |
|
Wladimir Palant
2016/09/19 08:53:59
This flag needs to go - we'll be using this functi
kzar
2016/09/19 16:54:31
Done.
| |
| 45 | 46 |
| 46 /** | 47 /** |
| 47 * Nested lookup table, filter (or false if inactive) by filter key by domain. | 48 * 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.) | 49 * (Only contains filters that aren't unconditionally matched for all domains.) |
| 49 * @type Object | 50 * @type Object |
| 50 */ | 51 */ |
| 51 var filtersByDomain = Object.create(null); | 52 var filtersByDomain = Object.create(null); |
| 52 | 53 |
| 53 /** | 54 /** |
| 54 * Lookup table, filters by selector. (Only contains filters that have a | 55 * Lookup table, filters keys by selector. (Only contains filters that have a |
| 55 * selector that is unconditionally matched for all domains.) | 56 * selector that is unconditionally matched for all domains.) |
| 56 */ | 57 */ |
| 57 var filtersBySelector = Object.create(null); | 58 var filterKeysBySelector = Object.create(null); |
| 58 | 59 |
| 59 /** | 60 /** |
| 60 * This array caches the keys of filtersBySelector table (selectors which | 61 * 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 | 62 * unconditionally apply on all domains). It will be null if the cache needs to |
| 62 * be rebuilt. | 63 * be rebuilt. |
| 63 */ | 64 */ |
| 64 var unconditionalSelectors = null; | 65 var unconditionalSelectors = null; |
| 65 | 66 |
| 66 /** | 67 /** |
| 68 * This array caches the values of filterKeysBySelector table (filterIds for | |
| 69 * selectors which unconditionally apply on all domains). It will be null if the | |
| 70 * cache needs to be rebuilt. | |
| 71 */ | |
| 72 var unconditionalFilterKeys = null; | |
| 73 | |
| 74 /** | |
| 67 * Object to be used instead when a filter has a blank domains property. | 75 * Object to be used instead when a filter has a blank domains property. |
| 68 */ | 76 */ |
| 69 var defaultDomains = Object.create(null); | 77 var defaultDomains = Object.create(null); |
| 70 defaultDomains[""] = true; | 78 defaultDomains[""] = true; |
| 71 | 79 |
| 72 /** | 80 /** |
| 73 * Lookup table, keys are known element hiding exceptions | 81 * Lookup table, keys are known element hiding exceptions |
| 74 * @type Object | 82 * @type Object |
| 75 */ | 83 */ |
| 76 var knownExceptions = Object.create(null); | 84 var knownExceptions = Object.create(null); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 88 var ElemHide = exports.ElemHide = | 96 var ElemHide = exports.ElemHide = |
| 89 { | 97 { |
| 90 /** | 98 /** |
| 91 * Removes all known filters | 99 * Removes all known filters |
| 92 */ | 100 */ |
| 93 clear: function() | 101 clear: function() |
| 94 { | 102 { |
| 95 filterByKey = []; | 103 filterByKey = []; |
| 96 keyByFilter = Object.create(null); | 104 keyByFilter = Object.create(null); |
| 97 filtersByDomain = Object.create(null); | 105 filtersByDomain = Object.create(null); |
| 98 filtersBySelector = Object.create(null); | 106 filterKeysBySelector = Object.create(null); |
| 99 unconditionalSelectors = null; | 107 unconditionalSelectors = unconditionalFilterKeys = null; |
| 100 knownExceptions = Object.create(null); | 108 knownExceptions = Object.create(null); |
| 101 exceptions = Object.create(null); | 109 exceptions = Object.create(null); |
| 102 FilterNotifier.emit("elemhideupdate"); | 110 FilterNotifier.emit("elemhideupdate"); |
| 103 }, | 111 }, |
| 104 | 112 |
| 105 _addToFiltersByDomain: function(filter) | 113 _addToFiltersByDomain: function(key, filter) |
| 106 { | 114 { |
| 107 let key = keyByFilter[filter.text]; | |
| 108 let domains = filter.domains || defaultDomains; | 115 let domains = filter.domains || defaultDomains; |
| 109 for (let domain in domains) | 116 for (let domain in domains) |
| 110 { | 117 { |
| 111 let filters = filtersByDomain[domain]; | 118 let filters = filtersByDomain[domain]; |
| 112 if (!filters) | 119 if (!filters) |
| 113 filters = filtersByDomain[domain] = Object.create(null); | 120 filters = filtersByDomain[domain] = Object.create(null); |
| 114 | 121 |
| 115 if (domains[domain]) | 122 if (domains[domain]) |
| 116 filters[key] = filter; | 123 filters[key] = filter; |
| 117 else | 124 else |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 133 let selector = filter.selector; | 140 let selector = filter.selector; |
| 134 if (!(selector in exceptions)) | 141 if (!(selector in exceptions)) |
| 135 exceptions[selector] = []; | 142 exceptions[selector] = []; |
| 136 exceptions[selector].push(filter); | 143 exceptions[selector].push(filter); |
| 137 | 144 |
| 138 if (usingGetSelectorsForDomain) | 145 if (usingGetSelectorsForDomain) |
| 139 { | 146 { |
| 140 // If this is the first exception for a previously unconditionally | 147 // If this is the first exception for a previously unconditionally |
| 141 // applied element hiding selector we need to take care to update the | 148 // applied element hiding selector we need to take care to update the |
| 142 // lookups. | 149 // lookups. |
| 143 let unconditionalFilters = filtersBySelector[selector]; | 150 let unconditionalFilterKeys = filterKeysBySelector[selector]; |
| 144 if (unconditionalFilters) | 151 if (unconditionalFilterKeys) |
| 145 { | 152 { |
| 146 for (let f of unconditionalFilters) | 153 for (let filterKey of unconditionalFilterKeys) |
| 147 this._addToFiltersByDomain(f); | 154 this._addToFiltersByDomain(filterKey, filterByKey[filterKey]); |
| 148 delete filtersBySelector[selector]; | 155 delete filterKeysBySelector[selector]; |
| 149 unconditionalSelectors = null; | 156 unconditionalSelectors = unconditionalFilterKeys = null; |
| 150 } | 157 } |
| 151 } | 158 } |
| 152 | 159 |
| 153 knownExceptions[filter.text] = true; | 160 knownExceptions[filter.text] = true; |
| 154 } | 161 } |
| 155 else | 162 else |
| 156 { | 163 { |
| 157 if (filter.text in keyByFilter) | 164 if (filter.text in keyByFilter) |
| 158 return; | 165 return; |
| 159 | 166 |
| 160 let key = filterByKey.push(filter) - 1; | 167 let key = filterByKey.push(filter) - 1; |
| 161 keyByFilter[filter.text] = key; | 168 keyByFilter[filter.text] = key; |
| 162 | 169 |
| 163 if (usingGetSelectorsForDomain) | 170 if (usingGetSelectorsForDomain) |
| 164 { | 171 { |
| 165 if (!(filter.domains || filter.selector in exceptions)) | 172 if (!(filter.domains || filter.selector in exceptions)) |
| 166 { | 173 { |
| 167 // The new filter's selector is unconditionally applied to all domains | 174 // The new filter's selector is unconditionally applied to all domains |
| 168 let filters = filtersBySelector[filter.selector]; | 175 let filterKeys = filterKeysBySelector[filter.selector]; |
| 169 if (filters) | 176 if (filterKeys) |
| 170 { | 177 { |
| 171 filters.push(filter); | 178 filterKeys.push(filter); |
|
Wladimir Palant
2016/09/19 08:53:59
Shouldn't you push `key` here?
Given that you hav
kzar
2016/09/19 16:54:30
As discussed in IRC it turns out this branch was r
| |
| 172 } | 179 } |
| 173 else | 180 else |
| 174 { | 181 { |
| 175 filtersBySelector[filter.selector] = [filter]; | 182 filterKeysBySelector[filter.selector] = [key]; |
| 176 unconditionalSelectors = null; | 183 unconditionalSelectors = unconditionalFilterKeys = null; |
| 177 } | 184 } |
| 178 } | 185 } |
| 179 else | 186 else |
| 180 { | 187 { |
| 181 // The new filter's selector only applies to some domains | 188 // The new filter's selector only applies to some domains |
| 182 this._addToFiltersByDomain(filter); | 189 this._addToFiltersByDomain(key, filter); |
| 183 } | 190 } |
| 184 } | 191 } |
| 185 } | 192 } |
| 186 | 193 |
| 187 FilterNotifier.emit("elemhideupdate"); | 194 FilterNotifier.emit("elemhideupdate"); |
| 188 }, | 195 }, |
| 189 | 196 |
| 190 /** | 197 /** |
| 191 * Removes an element hiding filter | 198 * Removes an element hiding filter |
| 192 * @param {ElemHideFilter} filter | 199 * @param {ElemHideFilter} filter |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 208 { | 215 { |
| 209 if (!(filter.text in keyByFilter)) | 216 if (!(filter.text in keyByFilter)) |
| 210 return; | 217 return; |
| 211 | 218 |
| 212 let key = keyByFilter[filter.text]; | 219 let key = keyByFilter[filter.text]; |
| 213 delete filterByKey[key]; | 220 delete filterByKey[key]; |
| 214 delete keyByFilter[filter.text]; | 221 delete keyByFilter[filter.text]; |
| 215 | 222 |
| 216 if (usingGetSelectorsForDomain) | 223 if (usingGetSelectorsForDomain) |
| 217 { | 224 { |
| 218 let filters = filtersBySelector[filter.selector]; | 225 let filterKeys = filterKeysBySelector[filter.selector]; |
| 219 if (filters) | 226 if (filterKeys) |
| 220 { | 227 { |
| 221 if (filters.length > 1) | 228 if (filterKeys.length > 1) |
| 222 { | 229 { |
| 223 let index = filters.indexOf(filter); | 230 let index = filterKeys.indexOf(key); |
| 224 filters.splice(index, 1); | 231 filterKeys.splice(index, 1); |
| 225 } | 232 } |
| 226 else | 233 else |
| 227 { | 234 { |
| 228 delete filtersBySelector[filter.selector]; | 235 delete filterKeysBySelector[filter.selector]; |
| 229 unconditionalSelectors = null; | 236 unconditionalSelectors = unconditionalFilterKeys = null; |
| 230 } | 237 } |
| 231 } | 238 } |
| 232 else | 239 else |
| 233 { | 240 { |
| 234 let domains = filter.domains || defaultDomains; | 241 let domains = filter.domains || defaultDomains; |
| 235 for (let domain in domains) | 242 for (let domain in domains) |
| 236 { | 243 { |
| 237 let filters = filtersByDomain[domain]; | 244 let filters = filtersByDomain[domain]; |
| 238 if (filters) | 245 if (filters) |
| 239 delete filters[key]; | 246 delete filters[key]; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 258 for (let i = list.length - 1; i >= 0; i--) | 265 for (let i = list.length - 1; i >= 0; i--) |
| 259 if (list[i].isActiveOnDomain(docDomain)) | 266 if (list[i].isActiveOnDomain(docDomain)) |
| 260 return list[i]; | 267 return list[i]; |
| 261 | 268 |
| 262 return null; | 269 return null; |
| 263 }, | 270 }, |
| 264 | 271 |
| 265 /** | 272 /** |
| 266 * Retrieves an element hiding filter by the corresponding protocol key | 273 * Retrieves an element hiding filter by the corresponding protocol key |
| 267 */ | 274 */ |
| 268 getFilterByKey: function(/**String*/ key) /**Filter*/ | 275 getFilterByKey: function(/**Number*/ key) /**Filter*/ |
| 269 { | 276 { |
| 270 return (key in filterByKey ? filterByKey[key] : null); | 277 return (key in filterByKey ? filterByKey[key] : null); |
| 271 }, | 278 }, |
| 272 | 279 |
| 273 /** | 280 /** |
| 274 * Returns a list of all selectors as a nested map. On first level, the keys | 281 * 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 | 282 * are all values of `ElemHideBase.selectorDomain` (domains on which these |
| 276 * selectors should apply, ignoring exceptions). The values are maps again, | 283 * selectors should apply, ignoring exceptions). The values are maps again, |
| 277 * with the keys being selectors and values the corresponding filter keys. | 284 * with the keys being selectors and values the corresponding filter keys. |
| 278 * @returns {Map.<String,Map<String,String>>} | 285 * @returns {Map.<String,Map<String,String>>} |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 291 | 298 |
| 292 if (!domains.has(domain)) | 299 if (!domains.has(domain)) |
| 293 domains.set(domain, new Map()); | 300 domains.set(domain, new Map()); |
| 294 domains.get(domain).set(selector, key); | 301 domains.get(domain).set(selector, key); |
| 295 } | 302 } |
| 296 | 303 |
| 297 return domains; | 304 return domains; |
| 298 }, | 305 }, |
| 299 | 306 |
| 300 /** | 307 /** |
| 301 * Returns a list of all selectors active on a particular domain, must not be | 308 * Returns a list of selectors that apply on each website unconditionally. |
| 302 * used in Firefox (when usingGetSelectorsForDomain is false). | 309 * @returns {String[]} |
| 303 */ | 310 */ |
| 304 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) | 311 getUnconditionalSelectors: function() |
| 312 { | |
| 313 if (!usingGetSelectorsForDomain) | |
| 314 throw new Error("getUconditionalSelectors can not be used in Firefox!"); | |
| 315 | |
| 316 if (!unconditionalSelectors) | |
| 317 unconditionalSelectors = Object.keys(filterKeysBySelector); | |
| 318 return unconditionalSelectors.slice(); | |
| 319 }, | |
| 320 | |
| 321 /** | |
| 322 * Returns a list of all selectors active on a particular domain. | |
| 323 * Returns a list of filterKeys for selectors that apply on each website | |
| 324 * unconditionally. | |
| 325 * @returns {Number[]} | |
| 326 */ | |
| 327 getUnconditionalFilterKeys: function() | |
| 328 { | |
| 329 if (!usingGetSelectorsForDomain) | |
| 330 throw new Error("getUconditionalFilterKeys can not be used in Firefox!"); | |
| 331 | |
| 332 if (!unconditionalFilterKeys) | |
| 333 { | |
| 334 let selectors = this.getUnconditionalSelectors(); | |
| 335 unconditionalFilterKeys = []; | |
| 336 for (let selector of selectors) | |
| 337 for (let key of filterKeysBySelector[selector]) | |
| 338 unconditionalFilterKeys.push(key); | |
|
Wladimir Palant
2016/09/19 08:53:59
On Firefox we actually need a mapping from selecto
kzar
2016/09/19 16:54:31
Since we only store one key per unconditional sele
| |
| 339 } | |
| 340 return unconditionalFilterKeys.slice(); | |
| 341 }, | |
| 342 | |
| 343 /** | |
| 344 * Returns a list of all selectors active on a particular domain. Optionally | |
| 345 * a list of corresponding filter keys for the selectors can be returned too. | |
| 346 * (Must not be used in Firefox - when usingGetSelectorsForDomain is false). | |
|
Wladimir Palant
2016/09/19 08:53:59
Actually, it will be used in Firefox now, that's t
kzar
2016/09/19 16:54:31
Done.
| |
| 347 */ | |
| 348 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly, | |
| 349 /**Boolean*/ noUnconditional, | |
| 350 /**Boolean*/ provideFilterKeys) | |
| 305 { | 351 { |
| 306 if (!usingGetSelectorsForDomain) | 352 if (!usingGetSelectorsForDomain) |
| 307 throw new Error("getSelectorsForDomain can not be used in Firefox!"); | 353 throw new Error("getSelectorsForDomain can not be used in Firefox!"); |
| 308 | 354 |
| 309 if (!unconditionalSelectors) | 355 let filterKeys = []; |
| 310 unconditionalSelectors = Object.keys(filtersBySelector); | 356 let selectors = []; |
| 311 let selectors = specificOnly ? [] : unconditionalSelectors.slice(); | 357 if (!(specificOnly || noUnconditional)) |
|
Wladimir Palant
2016/09/19 08:53:59
Personally, I find this easier to understand if yo
kzar
2016/09/19 16:54:30
Done.
| |
| 358 { | |
| 359 selectors = this.getUnconditionalSelectors(); | |
| 360 if (provideFilterKeys) | |
| 361 filterKeys = this.getUnconditionalFilterKeys(); | |
| 362 } | |
| 312 | 363 |
| 313 let seenFilters = Object.create(null); | 364 let seenFilters = Object.create(null); |
| 314 let currentDomain = domain ? domain.toUpperCase() : ""; | 365 let currentDomain = domain ? domain.toUpperCase() : ""; |
| 315 while (true) | 366 while (true) |
| 316 { | 367 { |
| 317 if (specificOnly && currentDomain == "") | 368 if (specificOnly && currentDomain == "") |
| 318 break; | 369 break; |
| 319 | 370 |
| 320 let filters = filtersByDomain[currentDomain]; | 371 let filters = filtersByDomain[currentDomain]; |
| 321 if (filters) | 372 if (filters) |
| 322 { | 373 { |
| 323 for (let filterKey in filters) | 374 for (let filterKey in filters) |
| 324 { | 375 { |
| 325 if (filterKey in seenFilters) | 376 if (filterKey in seenFilters) |
| 326 continue; | 377 continue; |
| 327 seenFilters[filterKey] = true; | 378 seenFilters[filterKey] = true; |
| 328 | 379 |
| 329 let filter = filters[filterKey]; | 380 let filter = filters[filterKey]; |
| 330 if (filter && !this.getException(filter, domain)) | 381 if (filter && !this.getException(filter, domain)) |
| 382 { | |
| 331 selectors.push(filter.selector); | 383 selectors.push(filter.selector); |
| 384 // It is faster to always push the key, even if not required. | |
| 385 filterKeys.push(filterKey); | |
| 386 } | |
| 332 } | 387 } |
| 333 } | 388 } |
| 334 | 389 |
| 335 if (currentDomain == "") | 390 if (currentDomain == "") |
| 336 break; | 391 break; |
| 337 | 392 |
| 338 let nextDot = currentDomain.indexOf("."); | 393 let nextDot = currentDomain.indexOf("."); |
| 339 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | 394 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); |
| 340 } | 395 } |
| 341 | 396 |
| 342 return selectors; | 397 if (provideFilterKeys) |
| 398 return [selectors, filterKeys]; | |
| 399 else | |
| 400 return selectors; | |
| 343 } | 401 } |
| 344 }; | 402 }; |
| OLD | NEW |