| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 */ | 52 */ |
| 53 let exceptions = Object.create(null); | 53 let exceptions = Object.create(null); |
| 54 | 54 |
| 55 /** | 55 /** |
| 56 * Currently applied stylesheet URL | 56 * Currently applied stylesheet URL |
| 57 * @type nsIURI | 57 * @type nsIURI |
| 58 */ | 58 */ |
| 59 let styleURL = null; | 59 let styleURL = null; |
| 60 | 60 |
| 61 /** | 61 /** |
| 62 * Lookup table, blocking filters by domain which they are applied to | 62 * Lookup table, filter selectors by domain which they are applied to |
| 63 * @type Object | 63 * @type Object |
| 64 */ | 64 */ |
| 65 let filtersByDomain = Object.create(null); | 65 let selectorsByDomain = Object.create(null); |
| 66 | |
| 67 /** | |
| 68 * Indicates whether stylesheets can be used for element hiding | |
| 69 * @type Boolean | |
| 70 */ | |
| 71 let canUseStylesheets = ("nsIStyleSheetService" in Ci); | |
| 66 | 72 |
| 67 /** | 73 /** |
| 68 * Element hiding component | 74 * Element hiding component |
| 69 * @class | 75 * @class |
| 70 */ | 76 */ |
| 71 let ElemHide = exports.ElemHide = | 77 let ElemHide = exports.ElemHide = |
| 72 { | 78 { |
| 73 /** | 79 /** |
| 74 * Indicates whether filters have been added or removed since the last apply() call. | 80 * Indicates whether filters have been added or removed since the last apply() call. |
| 75 * @type Boolean | 81 * @type Boolean |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 104 | 110 |
| 105 /** | 111 /** |
| 106 * Removes all known filters | 112 * Removes all known filters |
| 107 */ | 113 */ |
| 108 clear: function() | 114 clear: function() |
| 109 { | 115 { |
| 110 filterByKey = Object.create(null); | 116 filterByKey = Object.create(null); |
| 111 keyByFilter = Object.create(null); | 117 keyByFilter = Object.create(null); |
| 112 knownExceptions = Object.create(null); | 118 knownExceptions = Object.create(null); |
| 113 exceptions = Object.create(null); | 119 exceptions = Object.create(null); |
| 114 filtersByDomain = Object.create(null); | 120 selectorsByDomain = Object.create(null); |
| 115 ElemHide.isDirty = false; | 121 ElemHide.isDirty = false; |
| 116 ElemHide.unapply(); | 122 ElemHide.unapply(); |
| 117 }, | 123 }, |
| 118 | 124 |
| 119 /** | 125 /** |
| 120 * Add a new element hiding filter | 126 * Add a new element hiding filter |
| 121 * @param {ElemHideFilter} filter | 127 * @param {ElemHideFilter} filter |
| 122 */ | 128 */ |
| 123 add: function(filter) | 129 add: function(filter) |
| 124 { | 130 { |
| 125 if (filter instanceof ElemHideException) | 131 if (filter instanceof ElemHideException) |
| 126 { | 132 { |
| 127 if (filter.text in knownExceptions) | 133 if (filter.text in knownExceptions) |
| 128 return; | 134 return; |
| 129 | 135 |
| 130 let selector = filter.selector; | 136 let selector = filter.selector; |
| 131 if (!(selector in exceptions)) | 137 if (!(selector in exceptions)) |
| 132 exceptions[selector] = []; | 138 exceptions[selector] = []; |
| 133 exceptions[selector].push(filter); | 139 exceptions[selector].push(filter); |
| 134 knownExceptions[filter.text] = true; | 140 knownExceptions[filter.text] = true; |
| 135 } | 141 } |
| 136 else | 142 else |
| 137 { | 143 { |
| 138 if (filter.text in keyByFilter) | 144 if (filter.text in keyByFilter) |
| 139 return; | 145 return; |
| 140 | 146 |
| 141 if (!("nsIStyleSheetService" in Ci)) | 147 if (!canUseStylesheets) |
|
Wladimir Palant
2015/02/23 19:20:16
Looking up things in Components.interfaces isn't s
Thomas Greiner
2015/03/12 16:00:06
Done.
| |
| 142 { | 148 { |
| 143 let domains = filter.domains; | 149 let domains = filter.domains; |
| 144 if (domains === null) | 150 if (domains === null) |
| 145 { | 151 { |
| 146 domains = Object.create(null); | 152 domains = Object.create(null); |
| 147 domains[""] = undefined; | 153 domains[""] = true; |
|
Wladimir Palant
2015/02/23 19:20:16
This should be true, not undefined.
Thomas Greiner
2015/03/12 16:00:06
Done.
| |
| 148 } | 154 } |
| 149 | 155 |
| 150 for (let domain in domains) | 156 for (let domain in domains) |
| 151 { | 157 { |
| 152 if (!(domain in filtersByDomain)) | 158 if (!(domain in selectorsByDomain)) |
| 153 { | 159 { |
| 154 filtersByDomain[domain] = Object.create(null); | 160 selectorsByDomain[domain] = Object.create(null); |
| 155 filtersByDomain[domain]._length = 0; | 161 selectorsByDomain[domain].$length = 0; |
| 156 } | 162 } |
| 157 | 163 |
| 158 filtersByDomain[domain][filter.text] = filter.isActiveOnDomain(domain) ; | 164 let selectors = selectorsByDomain[domain]; |
|
Wladimir Palant
2015/02/23 19:20:16
The very point of this functionality is to avoid c
Thomas Greiner
2015/03/12 16:00:06
Done, you're right that it appears to be redundant
| |
| 159 filtersByDomain[domain]._length++; | 165 if (!(filter.selector in selectors)) |
|
Wladimir Palant
2015/02/23 19:20:16
Strictly speaking, _length isn't an invalid domain
Thomas Greiner
2015/03/12 16:00:06
That's true but that might make it difficult to sp
| |
| 166 { | |
| 167 selectors[filter.selector] = { | |
| 168 isActive: false, | |
| 169 count: 0 | |
| 170 }; | |
| 171 } | |
| 172 selectors[filter.selector].isActive |= domains[domain]; | |
| 173 selectors[filter.selector].count++; | |
| 174 | |
| 175 selectors.$length++; | |
| 160 } | 176 } |
| 161 } | 177 } |
| 162 | 178 |
| 163 let key; | 179 let key; |
| 164 do { | 180 do { |
| 165 key = Math.random().toFixed(15).substr(5); | 181 key = Math.random().toFixed(15).substr(5); |
| 166 } while (key in filterByKey); | 182 } while (key in filterByKey); |
| 167 | 183 |
| 168 filterByKey[key] = filter; | 184 filterByKey[key] = filter; |
| 169 keyByFilter[filter.text] = key; | 185 keyByFilter[filter.text] = key; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 186 let index = list.indexOf(filter); | 202 let index = list.indexOf(filter); |
| 187 if (index >= 0) | 203 if (index >= 0) |
| 188 list.splice(index, 1); | 204 list.splice(index, 1); |
| 189 delete knownExceptions[filter.text]; | 205 delete knownExceptions[filter.text]; |
| 190 } | 206 } |
| 191 else | 207 else |
| 192 { | 208 { |
| 193 if (!(filter.text in keyByFilter)) | 209 if (!(filter.text in keyByFilter)) |
| 194 return; | 210 return; |
| 195 | 211 |
| 196 if (!("nsIStyleSheetService" in Ci)) | 212 if (!canUseStylesheets) |
| 197 { | 213 { |
| 198 let domains = filter.domains; | 214 let domains = filter.domains; |
| 199 if (!domains) | 215 if (!domains) |
| 200 { | 216 { |
| 201 domains = Object.create(null); | 217 domains = Object.create(null); |
| 202 domains[""] = undefined; | 218 domains[""] = true; |
|
Wladimir Palant
2015/02/23 19:20:16
This should be true, not undefined.
Thomas Greiner
2015/03/12 16:00:06
Done.
| |
| 203 } | 219 } |
| 204 | 220 |
| 205 for (let domain in domains) | 221 for (let domain in domains) |
| 206 { | 222 { |
| 207 if (domain in filtersByDomain && filter.text in filtersByDomain[domain ]) | 223 if (domain in selectorsByDomain |
| 224 && filter.selector in selectorsByDomain[domain] | |
| 225 && !--selectorsByDomain[domain][filter.selector].$length) | |
| 208 { | 226 { |
| 209 delete filtersByDomain[domain][filter.text]; | 227 delete selectorsByDomain[domain][filter.selector]; |
| 210 if (!--filtersByDomain[domain]._length) | 228 if (!--selectorsByDomain[domain].$length) |
| 211 delete filtersByDomain[domain]; | 229 delete selectorsByDomain[domain]; |
| 212 } | 230 } |
| 213 } | 231 } |
| 214 } | 232 } |
| 215 | 233 |
| 216 let key = keyByFilter[filter.text]; | 234 let key = keyByFilter[filter.text]; |
| 217 delete filterByKey[key]; | 235 delete filterByKey[key]; |
| 218 delete keyByFilter[filter.text]; | 236 delete keyByFilter[filter.text]; |
| 219 ElemHide.isDirty = true; | 237 ElemHide.isDirty = true; |
| 220 } | 238 } |
| 221 }, | 239 }, |
| 222 | 240 |
| 223 /** | 241 /** |
| 224 * Checks whether an exception rule is registered for a filter on a particular | 242 * Checks whether an exception rule is registered for a filter on a particular |
| 225 * domain. | 243 * domain. |
| 226 */ | 244 */ |
| 227 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE xception*/ | 245 getException: function(/**String*/ selector, /**String*/ docDomain) /**ElemHid eException*/ |
| 228 { | 246 { |
| 229 if (!(filter.selector in exceptions)) | 247 if (!(selector in exceptions)) |
| 230 return null; | 248 return null; |
| 231 | 249 |
| 232 let list = exceptions[filter.selector]; | 250 let list = exceptions[selector]; |
| 233 for (let i = list.length - 1; i >= 0; i--) | 251 for (let i = list.length - 1; i >= 0; i--) |
| 234 if (list[i].isActiveOnDomain(docDomain)) | 252 if (list[i].isActiveOnDomain(docDomain)) |
| 235 return list[i]; | 253 return list[i]; |
| 236 | 254 |
| 237 return null; | 255 return null; |
| 238 }, | 256 }, |
| 239 | 257 |
| 240 /** | 258 /** |
| 241 * Will be set to true if apply() is running (reentrance protection). | 259 * Will be set to true if apply() is running (reentrance protection). |
| 242 * @type Boolean | 260 * @type Boolean |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 324 } | 342 } |
| 325 } | 343 } |
| 326 | 344 |
| 327 FilterNotifier.triggerListeners("elemhideupdate"); | 345 FilterNotifier.triggerListeners("elemhideupdate"); |
| 328 } | 346 } |
| 329 }.bind(this)); | 347 }.bind(this)); |
| 330 | 348 |
| 331 this._applying = true; | 349 this._applying = true; |
| 332 }, | 350 }, |
| 333 | 351 |
| 334 _generateCSSContent: function() | 352 _generateCSSContent: function*() |
| 335 { | 353 { |
| 336 // Grouping selectors by domains | 354 // Grouping selectors by domains |
| 337 let domains = Object.create(null); | 355 let domains = Object.create(null); |
| 338 let hasFilters = false; | 356 let hasFilters = false; |
| 339 for (let key in filterByKey) | 357 for (let key in filterByKey) |
| 340 { | 358 { |
| 341 let filter = filterByKey[key]; | 359 let filter = filterByKey[key]; |
| 342 let domain = filter.selectorDomain || ""; | 360 let domain = filter.selectorDomain || ""; |
| 343 | 361 |
| 344 let list; | 362 let list; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 414 | 432 |
| 415 /** | 433 /** |
| 416 * Retrieves an element hiding filter by the corresponding protocol key | 434 * Retrieves an element hiding filter by the corresponding protocol key |
| 417 */ | 435 */ |
| 418 getFilterByKey: function(/**String*/ key) /**Filter*/ | 436 getFilterByKey: function(/**String*/ key) /**Filter*/ |
| 419 { | 437 { |
| 420 return (key in filterByKey ? filterByKey[key] : null); | 438 return (key in filterByKey ? filterByKey[key] : null); |
| 421 }, | 439 }, |
| 422 | 440 |
| 423 /** | 441 /** |
| 424 * Returns a list of all selectors active on a particular domain (currently | 442 * Returns a list of all selectors active on a particular domain (it cannot |
| 425 * used only in Chrome, Opera and Safari). | 443 * be used in Firefox). |
|
Wladimir Palant
2015/02/23 19:20:16
This comment is no longer correct. Either way, it
Thomas Greiner
2015/03/12 16:00:06
Done.
| |
| 426 */ | 444 */ |
| 427 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) | 445 getSelectorsForDomain: function(/**String*/ docDomain, /**Boolean*/ specificOn ly) |
| 428 { | 446 { |
|
Wladimir Palant
2015/02/23 19:20:16
How about an exception here on Firefox, to explain
Thomas Greiner
2015/03/12 16:00:06
Done.
| |
| 429 let selectors = []; | 447 if (canUseStylesheets) |
| 430 domain = domain.toUpperCase(); | 448 throw new Error("Use of getSelectorsForDomain is limited to platforms whic h cannot inject stylesheets"); |
| 431 | 449 |
| 432 if (!specificOnly) | 450 let selectors = Object.create(null); |
| 433 { | 451 let domain = docDomain.toUpperCase(); |
| 434 let filterTexts = filtersByDomain[""]; | |
| 435 if (filterTexts) | |
| 436 { | |
| 437 for (let filterText in filterTexts) | |
| 438 { | |
| 439 if (filterTexts[filterText]) | |
| 440 { | |
| 441 let filter = filterByKey[keyByFilter[filterText]]; | |
| 442 if (!filter) | |
| 443 continue; | |
| 444 | |
| 445 let selector = filter.selector; | |
| 446 if (filter.isActiveOnDomain(docDomain) && !this.getException(selecto r, docDomain)) | |
|
Thomas Greiner
2015/03/12 16:00:06
Done.
| |
| 447 selectors[selectors.length] = selector; | |
| 448 } | |
| 449 } | |
| 450 } | |
| 451 } | |
| 452 | |
| 453 while (true) | 452 while (true) |
| 454 { | 453 { |
| 455 if (domain && domain in filtersByDomain) | 454 if (domain in selectorsByDomain && (domain != "" || !specificOnly)) |
| 456 { | 455 { |
| 457 let filterTexts = filtersByDomain[domain]; | 456 let domainSelectors = selectorsByDomain[domain]; |
| 458 if (filterTexts) | 457 let filterSelectors = Object.getOwnPropertyNames(domainSelectors); |
|
Wladimir Palant
2015/02/23 19:20:16
This check seems pointless - if a property exists
Thomas Greiner
2015/03/12 16:00:06
Done.
| |
| 459 { | 458 for (let filterSelector of filterSelectors) |
| 460 for (let filterText in filterTexts) | 459 { |
| 461 { | 460 if (filterSelector == "$length" |
| 462 if (filterTexts[filterText]) | 461 || filterSelector in selectors |
| 463 { | 462 || !domainSelectors[filterSelector].isActive |
| 464 let filter = filterByKey[keyByFilter[filterText]]; | 463 || this.getException(filterSelector, docDomain)) |
|
Wladimir Palant
2015/02/23 19:20:16
let filter = Filter.fromText(filterText);
Thomas Greiner
2015/03/12 16:00:06
Done.
| |
| 465 if (!filter) | 464 continue; |
|
Wladimir Palant
2015/07/30 11:26:53
Even with the inconsistent logic changes you are i
| |
| 466 continue; | 465 |
| 467 | 466 selectors[filterSelector] = null; |
| 468 let selector = filter.selector; | 467 } |
| 469 if (!this.getException(selector, docDomain)) | 468 } |
| 470 selectors[selectors.length] = selector; | 469 |
|
Wladimir Palant
2015/02/23 19:20:16
You meant using selectors.push(), right?
Thomas Greiner
2015/03/12 16:00:06
This turned out to be about ~3ms faster for each c
| |
| 471 } | 470 if (domain == "") |
| 472 } | 471 break; |
| 473 } | |
| 474 } | |
| 475 | 472 |
| 476 let nextDot = domain.indexOf("."); | 473 let nextDot = domain.indexOf("."); |
| 477 if (nextDot < 0) | 474 domain = (nextDot < 0) ? "" : domain.substr(nextDot + 1); |
| 478 break; | 475 } |
| 479 domain = domain.substr(nextDot + 1); | 476 |
| 480 } | 477 return Object.getOwnPropertyNames(selectors); |
|
Wladimir Palant
2015/02/23 19:20:16
This logic is wrong, we can have a filter ~subdoma
Thomas Greiner
2015/03/12 16:00:06
Indeed, that's true. I added the `ignorableFilters
| |
| 481 | |
| 482 return selectors; | |
| 483 } | 478 } |
| 484 }; | 479 }; |
| LEFT | RIGHT |