| 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, 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 Loading... |
| 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 Loading... |
| 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 Loading... |
| 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 unconditional = !(filter.domains || filter.selector in exceptions); |
| 210 if (unconditional) |
| 217 { | 211 { |
| 218 let filters = filtersBySelector[filter.selector]; | 212 let filterKeys = filterKeysBySelector[filter.selector]; |
| 219 if (filters) | 213 if (filterKeys) |
| 220 { | 214 { |
| 221 if (filters.length > 1) | 215 if (filterKeys.length > 1) |
| 222 { | 216 { |
| 223 let index = filters.indexOf(filter); | 217 let index = filterKeys.indexOf(key); |
| 224 filters.splice(index, 1); | 218 filterKeys.splice(index, 1); |
| 219 if (index == 0) |
| 220 unconditionalFilterKeys = null; |
| 225 } | 221 } |
| 226 else | 222 else |
| 227 { | 223 { |
| 228 delete filtersBySelector[filter.selector]; | 224 delete filterKeysBySelector[filter.selector]; |
| 229 unconditionalSelectors = null; | 225 unconditionalSelectors = unconditionalFilterKeys = null; |
| 230 } | 226 } |
| 231 } | 227 } |
| 232 else | 228 else |
| 233 { | 229 { |
| 234 let domains = filter.domains || defaultDomains; | 230 // Even if this filter appears unconditional its selector might not |
| 235 for (let domain in domains) | 231 // be. In that case we need to take care to remove it from |
| 236 { | 232 // filtersByDomain instead. |
| 237 let filters = filtersByDomain[domain]; | 233 unconditional = false; |
| 238 if (filters) | 234 } |
| 239 delete filters[key]; | 235 } |
| 240 } | 236 |
| 237 if (!unconditional) |
| 238 { |
| 239 let domains = filter.domains || defaultDomains; |
| 240 for (let domain in domains) |
| 241 { |
| 242 let filters = filtersByDomain[domain]; |
| 243 if (filters) |
| 244 delete filters[key]; |
| 241 } | 245 } |
| 242 } | 246 } |
| 243 } | 247 } |
| 244 | 248 |
| 245 FilterNotifier.emit("elemhideupdate"); | 249 FilterNotifier.emit("elemhideupdate"); |
| 246 }, | 250 }, |
| 247 | 251 |
| 248 /** | 252 /** |
| 249 * Checks whether an exception rule is registered for a filter on a particular | 253 * Checks whether an exception rule is registered for a filter on a particular |
| 250 * domain. | 254 * domain. |
| 251 */ | 255 */ |
| 252 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE
xception*/ | 256 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE
xception*/ |
| 253 { | 257 { |
| 254 if (!(filter.selector in exceptions)) | 258 if (!(filter.selector in exceptions)) |
| 255 return null; | 259 return null; |
| 256 | 260 |
| 257 let list = exceptions[filter.selector]; | 261 let list = exceptions[filter.selector]; |
| 258 for (let i = list.length - 1; i >= 0; i--) | 262 for (let i = list.length - 1; i >= 0; i--) |
| 259 if (list[i].isActiveOnDomain(docDomain)) | 263 if (list[i].isActiveOnDomain(docDomain)) |
| 260 return list[i]; | 264 return list[i]; |
| 261 | 265 |
| 262 return null; | 266 return null; |
| 263 }, | 267 }, |
| 264 | 268 |
| 265 /** | 269 /** |
| 266 * Retrieves an element hiding filter by the corresponding protocol key | 270 * Retrieves an element hiding filter by the corresponding protocol key |
| 267 */ | 271 */ |
| 268 getFilterByKey: function(/**String*/ key) /**Filter*/ | 272 getFilterByKey: function(/**Number*/ key) /**Filter*/ |
| 269 { | 273 { |
| 270 return (key in filterByKey ? filterByKey[key] : null); | 274 return (key in filterByKey ? filterByKey[key] : null); |
| 271 }, | 275 }, |
| 272 | 276 |
| 273 /** | 277 /** |
| 274 * Returns a list of all selectors as a nested map. On first level, the keys | 278 * 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 | 279 * are all values of `ElemHideBase.selectorDomain` (domains on which these |
| 276 * selectors should apply, ignoring exceptions). The values are maps again, | 280 * selectors should apply, ignoring exceptions). The values are maps again, |
| 277 * with the keys being selectors and values the corresponding filter keys. | 281 * with the keys being selectors and values the corresponding filter keys. |
| 278 * @returns {Map.<String,Map<String,String>>} | 282 * @returns {Map.<String,Map<String,String>>} |
| (...skipping 12 matching lines...) Expand all Loading... |
| 291 | 295 |
| 292 if (!domains.has(domain)) | 296 if (!domains.has(domain)) |
| 293 domains.set(domain, new Map()); | 297 domains.set(domain, new Map()); |
| 294 domains.get(domain).set(selector, key); | 298 domains.get(domain).set(selector, key); |
| 295 } | 299 } |
| 296 | 300 |
| 297 return domains; | 301 return domains; |
| 298 }, | 302 }, |
| 299 | 303 |
| 300 /** | 304 /** |
| 301 * Returns a list of all selectors active on a particular domain, must not be | 305 * Returns a list of selectors that apply on each website unconditionally. |
| 302 * used in Firefox (when usingGetSelectorsForDomain is false). | 306 * @returns {String[]} |
| 303 */ | 307 */ |
| 304 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) | 308 getUnconditionalSelectors: function() |
| 305 { | 309 { |
| 306 if (!usingGetSelectorsForDomain) | 310 if (!unconditionalSelectors) |
| 307 throw new Error("getSelectorsForDomain can not be used in Firefox!"); | 311 unconditionalSelectors = Object.keys(filterKeysBySelector); |
| 312 return unconditionalSelectors.slice(); |
| 313 }, |
| 308 | 314 |
| 309 if (!unconditionalSelectors) | 315 /** |
| 310 unconditionalSelectors = Object.keys(filtersBySelector); | 316 * Returns a list of all selectors active on a particular domain. |
| 311 let selectors = specificOnly ? [] : unconditionalSelectors.slice(); | 317 * Returns a list of filterKeys for selectors that apply on each website |
| 318 * unconditionally. |
| 319 * @returns {Number[]} |
| 320 */ |
| 321 getUnconditionalFilterKeys: function() |
| 322 { |
| 323 if (!unconditionalFilterKeys) |
| 324 { |
| 325 let selectors = this.getUnconditionalSelectors(); |
| 326 unconditionalFilterKeys = []; |
| 327 for (let selector of selectors) |
| 328 unconditionalFilterKeys.push(filterKeysBySelector[selector][0]); |
| 329 } |
| 330 return unconditionalFilterKeys.slice(); |
| 331 }, |
| 312 | 332 |
| 333 /** |
| 334 * Returns a list of all selectors active on a particular domain. Optionally |
| 335 * a list of corresponding filter keys for the selectors can be returned too. |
| 336 * The optional criteria parameter restricts the results as follows: |
| 337 * 0 - All selectors allowed (default) |
| 338 * 1 - No unconditionally matching selectors allowed |
| 339 * 2 - Only specifically matching selectors allowed |
| 340 */ |
| 341 getSelectorsForDomain: function(/**String*/ domain, |
| 342 /**Number*/ criteria, |
| 343 /**Boolean*/ provideFilterKeys) |
| 344 { |
| 345 let filterKeys = []; |
| 346 let selectors = []; |
| 347 if (!criteria) |
| 348 { |
| 349 selectors = this.getUnconditionalSelectors(); |
| 350 if (provideFilterKeys) |
| 351 filterKeys = this.getUnconditionalFilterKeys(); |
| 352 } |
| 353 |
| 354 let specificOnly = criteria == 2; |
| 313 let seenFilters = Object.create(null); | 355 let seenFilters = Object.create(null); |
| 314 let currentDomain = domain ? domain.toUpperCase() : ""; | 356 let currentDomain = domain ? domain.toUpperCase() : ""; |
| 315 while (true) | 357 while (true) |
| 316 { | 358 { |
| 317 if (specificOnly && currentDomain == "") | 359 if (specificOnly && currentDomain == "") |
| 318 break; | 360 break; |
| 319 | 361 |
| 320 let filters = filtersByDomain[currentDomain]; | 362 let filters = filtersByDomain[currentDomain]; |
| 321 if (filters) | 363 if (filters) |
| 322 { | 364 { |
| 323 for (let filterKey in filters) | 365 for (let filterKey in filters) |
| 324 { | 366 { |
| 325 if (filterKey in seenFilters) | 367 if (filterKey in seenFilters) |
| 326 continue; | 368 continue; |
| 327 seenFilters[filterKey] = true; | 369 seenFilters[filterKey] = true; |
| 328 | 370 |
| 329 let filter = filters[filterKey]; | 371 let filter = filters[filterKey]; |
| 330 if (filter && !this.getException(filter, domain)) | 372 if (filter && !this.getException(filter, domain)) |
| 373 { |
| 331 selectors.push(filter.selector); | 374 selectors.push(filter.selector); |
| 375 // It is faster to always push the key, even if not required. |
| 376 filterKeys.push(filterKey); |
| 377 } |
| 332 } | 378 } |
| 333 } | 379 } |
| 334 | 380 |
| 335 if (currentDomain == "") | 381 if (currentDomain == "") |
| 336 break; | 382 break; |
| 337 | 383 |
| 338 let nextDot = currentDomain.indexOf("."); | 384 let nextDot = currentDomain.indexOf("."); |
| 339 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | 385 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); |
| 340 } | 386 } |
| 341 | 387 |
| 342 return selectors; | 388 if (provideFilterKeys) |
| 389 return [selectors, filterKeys.map(key => parseInt(key, 10))]; |
| 390 else |
| 391 return selectors; |
| 343 } | 392 } |
| 344 }; | 393 }; |
| OLD | NEW |