| 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-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 13 matching lines...) Expand all Loading... |
| 24 var {Utils} = require("utils"); | 24 var {Utils} = require("utils"); |
| 25 var {IO} = require("io"); | 25 var {IO} = require("io"); |
| 26 var {Prefs} = require("prefs"); | 26 var {Prefs} = require("prefs"); |
| 27 var {ElemHideException} = require("filterClasses"); | 27 var {ElemHideException} = require("filterClasses"); |
| 28 var {FilterNotifier} = require("filterNotifier"); | 28 var {FilterNotifier} = require("filterNotifier"); |
| 29 | 29 |
| 30 /** | 30 /** |
| 31 * Lookup table, filters by their associated key | 31 * Lookup table, filters by their associated key |
| 32 * @type Object | 32 * @type Object |
| 33 */ | 33 */ |
| 34 var filterByKey = Object.create(null); | 34 var filterByKey = []; |
| 35 | 35 |
| 36 /** | 36 /** |
| 37 * Lookup table, keys of the filters by filter text | 37 * Lookup table, keys of the filters by filter text |
| 38 * @type Object | 38 * @type Object |
| 39 */ | 39 */ |
| 40 var keyByFilter = Object.create(null); | 40 var keyByFilter = Object.create(null); |
| 41 | 41 |
| 42 /** | 42 /** |
| 43 * Indicates whether we are using getSelectorsByDomain and maintaining the | 43 * Indicates whether we are using the getSelectorsForDomain function and |
| 44 * required lookup tables. (Will be false for Firefox) | 44 * therefore mainting the required filtersByDomain, filtersBySelector and |
| 45 * unconditionalSelectors lookups. (Will be false for Firefox) |
| 45 * @type Boolean | 46 * @type Boolean |
| 46 */ | 47 */ |
| 47 var usingGetSelectorsByDomain = !("nsIStyleSheetService" in Ci); | 48 var usingGetSelectorsForDomain = !("nsIStyleSheetService" in Ci); |
| 48 | 49 |
| 49 /** | 50 /** |
| 50 * Lookup table, filter selector by filter ID. | 51 * Nested lookup table, filter (or false if inactive) by filter key by domain. |
| 51 */ | 52 * (Only contains filters that aren't unconditionally matched for all domains.) |
| 52 var selectorByFilterId = []; | 53 * @type Object |
| 53 | 54 */ |
| 54 /** | 55 var filtersByDomain = Object.create(null); |
| 55 * Lookup table, active filter IDs by domain. | 56 |
| 56 */ | 57 /** |
| 57 var activeFilterIdsByDomain = Object.create(null); | 58 * Lookup table, filters by selector. (Only contains filters that have a |
| 58 | 59 * selector that is unconditionally matched for all domains.) |
| 59 /** | 60 */ |
| 60 * Lookup table, inactive filter IDs by domain. | 61 var filtersBySelector = Object.create(null); |
| 61 */ | 62 |
| 62 var inactiveFilterIdsByDomain = Object.create(null); | 63 /** |
| 63 | 64 * This array caches the keys of filtersBySelector table (selectors which |
| 64 /** | 65 * unconditionally apply on all domains). It will be null if the cache needs to |
| 65 * Lookup table, filter ID by filter text. | 66 * be rebuilt. |
| 66 */ | 67 */ |
| 67 var filterIdByFilterText = Object.create(null); | 68 var unconditionalSelectors = null; |
| 68 | 69 |
| 69 /** | 70 /** |
| 70 * Object to be used instead when a filter has a blank domains property. | 71 * Object to be used instead when a filter has a blank domains property. |
| 71 */ | 72 */ |
| 72 var defaultDomains = Object.create(null); | 73 var defaultDomains = Object.create(null); |
| 73 defaultDomains[""] = true; | 74 defaultDomains[""] = true; |
| 74 | 75 |
| 75 /** | 76 /** |
| 76 * Lookup table, keys are known element hiding exceptions | 77 * Lookup table, keys are known element hiding exceptions |
| 77 * @type Object | 78 * @type Object |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 let styleFile = IO.resolveFilePath(Prefs.data_directory); | 124 let styleFile = IO.resolveFilePath(Prefs.data_directory); |
| 124 styleFile.append("elemhide.css"); | 125 styleFile.append("elemhide.css"); |
| 125 styleURL = Services.io.newFileURI(styleFile).QueryInterface(Ci.nsIFileURL); | 126 styleURL = Services.io.newFileURI(styleFile).QueryInterface(Ci.nsIFileURL); |
| 126 }, | 127 }, |
| 127 | 128 |
| 128 /** | 129 /** |
| 129 * Removes all known filters | 130 * Removes all known filters |
| 130 */ | 131 */ |
| 131 clear: function() | 132 clear: function() |
| 132 { | 133 { |
| 133 filterByKey = Object.create(null); | 134 filterByKey = []; |
| 134 keyByFilter = Object.create(null); | 135 keyByFilter = Object.create(null); |
| 135 selectorByFilterId = []; | 136 filtersByDomain = Object.create(null); |
| 136 activeFilterIdsByDomain = Object.create(null); | 137 filtersBySelector = Object.create(null); |
| 137 inactiveFilterIdsByDomain = Object.create(null); | 138 unconditionalSelectors = null; |
| 138 filterIdByFilterText = Object.create(null); | |
| 139 knownExceptions = Object.create(null); | 139 knownExceptions = Object.create(null); |
| 140 exceptions = Object.create(null); | 140 exceptions = Object.create(null); |
| 141 ElemHide.isDirty = false; | 141 ElemHide.isDirty = false; |
| 142 ElemHide.unapply(); | 142 ElemHide.unapply(); |
| 143 }, | 143 }, |
| 144 | 144 |
| 145 _addToFiltersByDomain: function(filter) |
| 146 { |
| 147 let key = keyByFilter[filter.text]; |
| 148 let domains = filter.domains || defaultDomains; |
| 149 for (let domain in domains) |
| 150 { |
| 151 let filters = filtersByDomain[domain]; |
| 152 if (!filters) |
| 153 filters = filtersByDomain[domain] = Object.create(null); |
| 154 |
| 155 if (domains[domain]) |
| 156 filters[key] = filter; |
| 157 else |
| 158 filters[key] = false; |
| 159 } |
| 160 }, |
| 161 |
| 145 /** | 162 /** |
| 146 * Add a new element hiding filter | 163 * Add a new element hiding filter |
| 147 * @param {ElemHideFilter} filter | 164 * @param {ElemHideFilter} filter |
| 148 */ | 165 */ |
| 149 add: function(filter) | 166 add: function(filter) |
| 150 { | 167 { |
| 151 if (filter instanceof ElemHideException) | 168 if (filter instanceof ElemHideException) |
| 152 { | 169 { |
| 153 if (filter.text in knownExceptions) | 170 if (filter.text in knownExceptions) |
| 154 return; | 171 return; |
| 155 | 172 |
| 156 let selector = filter.selector; | 173 let selector = filter.selector; |
| 157 if (!(selector in exceptions)) | 174 if (!(selector in exceptions)) |
| 158 exceptions[selector] = []; | 175 exceptions[selector] = []; |
| 159 exceptions[selector].push(filter); | 176 exceptions[selector].push(filter); |
| 177 |
| 178 if (usingGetSelectorsForDomain) |
| 179 { |
| 180 // If this is the first exception for a previously unconditionally |
| 181 // applied element hiding selector we need to take care to update the |
| 182 // lookups. |
| 183 let unconditionalFilters = filtersBySelector[selector]; |
| 184 if (unconditionalFilters) |
| 185 { |
| 186 for (let f of unconditionalFilters) |
| 187 this._addToFiltersByDomain(f); |
| 188 delete filtersBySelector[selector]; |
| 189 unconditionalSelectors = null; |
| 190 } |
| 191 } |
| 192 |
| 160 knownExceptions[filter.text] = true; | 193 knownExceptions[filter.text] = true; |
| 161 } | 194 } |
| 162 else | 195 else |
| 163 { | 196 { |
| 164 if (filter.text in keyByFilter) | 197 if (filter.text in keyByFilter) |
| 165 return; | 198 return; |
| 166 | 199 |
| 167 let key; | 200 let key = filterByKey.push(filter) - 1; |
| 168 do { | |
| 169 key = Math.random().toFixed(15).substr(5); | |
| 170 } while (key in filterByKey); | |
| 171 | |
| 172 filterByKey[key] = filter; | |
| 173 keyByFilter[filter.text] = key; | 201 keyByFilter[filter.text] = key; |
| 174 | 202 |
| 175 if (usingGetSelectorsByDomain && !(filter.text in filterIdByFilterText)) | 203 if (usingGetSelectorsForDomain) |
| 176 { | 204 { |
| 177 let filterId = selectorByFilterId.push(filter.selector) - 1; | 205 if (!(filter.domains || filter.selector in exceptions)) |
| 178 filterIdByFilterText[filter.text] = filterId; | 206 { |
| 179 | 207 // The new filter's selector is unconditionally applied to all domains |
| 180 let domains = filter.domains || defaultDomains; | 208 let filters = filtersBySelector[filter.selector]; |
| 181 for (let domain in domains) | 209 if (filters) |
| 182 { | 210 { |
| 183 let lookup; | 211 filters.push(filter); |
| 184 if (domains[domain]) | 212 } |
| 185 lookup = activeFilterIdsByDomain; | |
| 186 else | 213 else |
| 187 lookup = inactiveFilterIdsByDomain; | 214 { |
| 188 | 215 filtersBySelector[filter.selector] = [filter]; |
| 189 let filterIds = lookup[domain]; | 216 unconditionalSelectors = null; |
| 190 if (filterIds == undefined) | 217 } |
| 191 filterIds = lookup[domain] = []; | 218 } |
| 192 | 219 else |
| 193 filterIds.push(filterId); | 220 { |
| 221 // The new filter's selector only applies to some domains |
| 222 this._addToFiltersByDomain(filter); |
| 194 } | 223 } |
| 195 } | 224 } |
| 196 | 225 |
| 197 ElemHide.isDirty = true; | 226 ElemHide.isDirty = true; |
| 198 } | 227 } |
| 199 }, | 228 }, |
| 200 | 229 |
| 201 /** | 230 /** |
| 202 * Removes an element hiding filter | 231 * Removes an element hiding filter |
| 203 * @param {ElemHideFilter} filter | 232 * @param {ElemHideFilter} filter |
| (...skipping 14 matching lines...) Expand all Loading... |
| 218 else | 247 else |
| 219 { | 248 { |
| 220 if (!(filter.text in keyByFilter)) | 249 if (!(filter.text in keyByFilter)) |
| 221 return; | 250 return; |
| 222 | 251 |
| 223 let key = keyByFilter[filter.text]; | 252 let key = keyByFilter[filter.text]; |
| 224 delete filterByKey[key]; | 253 delete filterByKey[key]; |
| 225 delete keyByFilter[filter.text]; | 254 delete keyByFilter[filter.text]; |
| 226 ElemHide.isDirty = true; | 255 ElemHide.isDirty = true; |
| 227 | 256 |
| 228 if (usingGetSelectorsByDomain) | 257 if (usingGetSelectorsForDomain) |
| 229 { | 258 { |
| 230 let filterId = filterIdByFilterText[filter.text]; | 259 let filters = filtersBySelector[filter.selector]; |
| 231 if (filterId) | 260 if (filters) |
| 232 { | 261 { |
| 233 delete filterIdByFilterText[filter.text]; | 262 if (filters.length > 1) |
| 234 delete selectorByFilterId[filterId]; | 263 { |
| 235 | 264 let index = filters.indexOf(filter); |
| 265 filters.splice(index, 1); |
| 266 } |
| 267 else |
| 268 { |
| 269 delete filtersBySelector[filter.selector]; |
| 270 unconditionalSelectors = null; |
| 271 } |
| 272 } |
| 273 else |
| 274 { |
| 236 let domains = filter.domains || defaultDomains; | 275 let domains = filter.domains || defaultDomains; |
| 237 let filterIdsByDomainLookups = [activeFilterIdsByDomain, | |
| 238 inactiveFilterIdsByDomain]; | |
| 239 for (let domain in domains) | 276 for (let domain in domains) |
| 240 { | 277 { |
| 241 for (let filterIdsByDomain of filterIdsByDomainLookups) | 278 let filters = filtersByDomain[domain]; |
| 242 { | 279 if (filters) |
| 243 let lookup = filterIdsByDomain[domain]; | 280 delete filters[key]; |
| 244 if (lookup) | |
| 245 { | |
| 246 let index = lookup.indexOf(filterId); | |
| 247 if (index != -1) | |
| 248 lookup.splice(index, 1); | |
| 249 } | |
| 250 } | |
| 251 } | 281 } |
| 252 } | 282 } |
| 253 } | 283 } |
| 254 } | 284 } |
| 255 }, | 285 }, |
| 256 | 286 |
| 257 /** | 287 /** |
| 258 * Checks whether an exception rule is registered for a filter on a particular | 288 * Checks whether an exception rule is registered for a filter on a particular |
| 259 * domain. | 289 * domain. |
| 260 */ | 290 */ |
| 261 getException: function(/**String*/ selector, /**String*/ docDomain) /**ElemHid
eException*/ | 291 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE
xception*/ |
| 262 { | 292 { |
| 263 if (!(selector in exceptions)) | 293 if (!(filter.selector in exceptions)) |
| 264 return null; | 294 return null; |
| 265 | 295 |
| 266 let list = exceptions[selector]; | 296 let list = exceptions[filter.selector]; |
| 267 for (let i = list.length - 1; i >= 0; i--) | 297 for (let i = list.length - 1; i >= 0; i--) |
| 268 if (list[i].isActiveOnDomain(docDomain)) | 298 if (list[i].isActiveOnDomain(docDomain)) |
| 269 return list[i]; | 299 return list[i]; |
| 270 | 300 |
| 271 return null; | 301 return null; |
| 272 }, | 302 }, |
| 273 | 303 |
| 274 /** | 304 /** |
| 275 * Will be set to true if apply() is running (reentrance protection). | 305 * Will be set to true if apply() is running (reentrance protection). |
| 276 * @type Boolean | 306 * @type Boolean |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 }, | 396 }, |
| 367 | 397 |
| 368 _generateCSSContent: function*() | 398 _generateCSSContent: function*() |
| 369 { | 399 { |
| 370 // Grouping selectors by domains | 400 // Grouping selectors by domains |
| 371 let domains = Object.create(null); | 401 let domains = Object.create(null); |
| 372 let hasFilters = false; | 402 let hasFilters = false; |
| 373 for (let key in filterByKey) | 403 for (let key in filterByKey) |
| 374 { | 404 { |
| 375 let filter = filterByKey[key]; | 405 let filter = filterByKey[key]; |
| 406 let selector = filter.selector; |
| 407 if (!selector) |
| 408 continue; |
| 409 |
| 376 let domain = filter.selectorDomain || ""; | 410 let domain = filter.selectorDomain || ""; |
| 377 | 411 |
| 378 let list; | 412 let list; |
| 379 if (domain in domains) | 413 if (domain in domains) |
| 380 list = domains[domain]; | 414 list = domains[domain]; |
| 381 else | 415 else |
| 382 { | 416 { |
| 383 list = Object.create(null); | 417 list = Object.create(null); |
| 384 domains[domain] = list; | 418 domains[domain] = list; |
| 385 } | 419 } |
| 386 list[filter.selector] = key; | 420 list[selector] = key; |
| 387 hasFilters = true; | 421 hasFilters = true; |
| 388 } | 422 } |
| 389 | 423 |
| 390 if (!hasFilters) | 424 if (!hasFilters) |
| 391 throw Cr.NS_ERROR_NOT_AVAILABLE; | 425 throw Cr.NS_ERROR_NOT_AVAILABLE; |
| 392 | 426 |
| 393 function escapeChar(match) | 427 function escapeChar(match) |
| 394 { | 428 { |
| 395 return "\\" + match.charCodeAt(0).toString(16) + " "; | 429 return "\\" + match.charCodeAt(0).toString(16) + " "; |
| 396 } | 430 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 /** | 483 /** |
| 450 * Retrieves an element hiding filter by the corresponding protocol key | 484 * Retrieves an element hiding filter by the corresponding protocol key |
| 451 */ | 485 */ |
| 452 getFilterByKey: function(/**String*/ key) /**Filter*/ | 486 getFilterByKey: function(/**String*/ key) /**Filter*/ |
| 453 { | 487 { |
| 454 return (key in filterByKey ? filterByKey[key] : null); | 488 return (key in filterByKey ? filterByKey[key] : null); |
| 455 }, | 489 }, |
| 456 | 490 |
| 457 /** | 491 /** |
| 458 * Returns a list of all selectors active on a particular domain, must not be | 492 * Returns a list of all selectors active on a particular domain, must not be |
| 459 * used in Firefox (when usingFiltersByDomain is false). | 493 * used in Firefox (when usingGetSelectorsForDomain is false). |
| 460 */ | 494 */ |
| 461 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) | 495 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) |
| 462 { | 496 { |
| 463 if (!usingGetSelectorsByDomain) | 497 if (!usingGetSelectorsForDomain) |
| 464 throw new Error("getSelectorsForDomain can not be used in Firefox!"); | 498 throw new Error("getSelectorsForDomain can not be used in Firefox!"); |
| 465 | 499 |
| 466 let selectors = []; | 500 if (!unconditionalSelectors) |
| 467 | 501 unconditionalSelectors = Object.keys(filtersBySelector); |
| 468 let seenFilterIds = Object.create(null); | 502 let selectors = specificOnly ? [] : unconditionalSelectors.slice(); |
| 503 |
| 504 let seenFilters = Object.create(null); |
| 469 let currentDomain = domain ? domain.toUpperCase() : ""; | 505 let currentDomain = domain ? domain.toUpperCase() : ""; |
| 470 while (true) | 506 while (true) |
| 471 { | 507 { |
| 472 if (specificOnly && currentDomain == "") | 508 if (specificOnly && currentDomain == "") |
| 473 break; | 509 break; |
| 474 | 510 |
| 475 let inactiveFilterIds = inactiveFilterIdsByDomain[currentDomain]; | 511 let filters = filtersByDomain[currentDomain]; |
| 476 if (inactiveFilterIds) | 512 if (filters) |
| 477 for (let filterId of inactiveFilterIds) | 513 { |
| 478 seenFilterIds[filterId] = true; | 514 for (let filterKey in filters) |
| 479 | 515 { |
| 480 let activeFilterIds = activeFilterIdsByDomain[currentDomain]; | 516 if (filterKey in seenFilters) |
| 481 if (activeFilterIds) | |
| 482 { | |
| 483 for (let filterId of activeFilterIds) | |
| 484 { | |
| 485 if (filterId in seenFilterIds) | |
| 486 continue; | 517 continue; |
| 487 seenFilterIds[filterId] = true; | 518 seenFilters[filterKey] = true; |
| 488 | 519 |
| 489 let selector = selectorByFilterId[filterId]; | 520 let filter = filters[filterKey]; |
| 490 if (!this.getException(selector, domain)) | 521 if (filter && !this.getException(filter, domain)) |
| 491 selectors.push(selector); | 522 selectors.push(filter.selector); |
| 492 } | 523 } |
| 493 } | 524 } |
| 494 | 525 |
| 495 if (currentDomain == "") | 526 if (currentDomain == "") |
| 496 break; | 527 break; |
| 497 | 528 |
| 498 let nextDot = currentDomain.indexOf("."); | 529 let nextDot = currentDomain.indexOf("."); |
| 499 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | 530 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); |
| 500 } | 531 } |
| 501 | 532 |
| 502 return selectors; | 533 return selectors; |
| 503 } | 534 } |
| 504 }; | 535 }; |
| LEFT | RIGHT |