| 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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 "use strict"; | 18 "use strict"; |
| 19 | 19 |
| 20 /** | 20 /** |
| 21 * @fileOverview Element hiding implementation. | 21 * @fileOverview Element hiding implementation. |
| 22 */ | 22 */ |
| 23 | 23 |
| 24 const {ElemHideException} = require("./filterClasses"); | 24 const {ElemHideException} = require("./filterClasses"); |
| 25 const {FilterNotifier} = require("./filterNotifier"); | 25 const {FilterNotifier} = require("./filterNotifier"); |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * Lookup table, active flag, by filter by domain. | 28 * Lookup table, active flag, by filter by domain. |
| 29 * (Only contains filters that aren't unconditionally matched for all domains.) | 29 * (Only contains filters that aren't unconditionally matched for all domains.) |
| 30 * @type {Map.<string,Map.<Filter,boolean>>} | 30 * @type {Map.<string,?Map.<Filter,boolean>>} |
| 31 */ | 31 */ |
| 32 let filtersByDomain = new Map(); | 32 let filtersByDomain = new Map(); |
| 33 | 33 |
| 34 /** | 34 /** |
| 35 * Lookup table, filter by selector. (Only used for selectors that are | 35 * Lookup table, filter by selector. (Only used for selectors that are |
| 36 * unconditionally matched for all domains.) | 36 * unconditionally matched for all domains.) |
| 37 * @type {Map.<string,Filter>} | 37 * @type {Map.<string,Filter>} |
| 38 */ | 38 */ |
| 39 let filterBySelector = new Map(); | 39 let filterBySelector = new Map(); |
| 40 | 40 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 54 let defaultDomains = new Map([["", true]]); | 54 let defaultDomains = new Map([["", true]]); |
| 55 | 55 |
| 56 /** | 56 /** |
| 57 * Set containing known element hiding and exception filters | 57 * Set containing known element hiding and exception filters |
| 58 * @type {Set.<ElemHideBase>} | 58 * @type {Set.<ElemHideBase>} |
| 59 */ | 59 */ |
| 60 let knownFilters = new Set(); | 60 let knownFilters = new Set(); |
| 61 | 61 |
| 62 /** | 62 /** |
| 63 * Lookup table, lists of element hiding exceptions by selector | 63 * Lookup table, lists of element hiding exceptions by selector |
| 64 * @type {Map.<string,Filter>} | 64 * @type {Map.<string,Filter[]>} |
| 65 */ | 65 */ |
| 66 let exceptions = new Map(); | 66 let exceptions = new Map(); |
| 67 | 67 |
| 68 /** | 68 /** |
| 69 * Lookup table, lists of generic element hiding exceptions by selector | |
| 70 * @type {Map.<string,Filter[]>} | |
| 71 */ | |
| 72 let genericExceptions = new Map(); | |
| 73 | |
| 74 /** | |
| 75 * List of selectors that apply on any unknown domain | |
| 76 * @type {?string[]} | |
| 77 */ | |
| 78 let conditionalGenericSelectors = null; | |
| 79 | |
| 80 /** | |
| 81 * Returns a list of domain-specific filters matching a domain | |
| 82 * @param {string} [domain] | |
| 83 * @returns {Array.<?Map.<Filter,boolean>>} | |
| 84 */ | |
| 85 function getSpecificFiltersForDomain(domain) | |
| 86 { | |
| 87 let filtersList = []; | |
| 88 | |
| 89 if (domain) | |
| 90 domain = domain.toUpperCase(); | |
| 91 | |
| 92 while (domain) | |
| 93 { | |
| 94 // Note that we also push null values into the list, because | |
| 95 // ElemHide.getSelectorsForDomain still needs to know if there are any | |
| 96 // entries for the domain. | |
| 97 let filters = filtersByDomain.get(domain); | |
| 98 if (typeof filters != "undefined") | |
| 99 filtersList.push(filters); | |
| 100 | |
| 101 let nextDot = domain.indexOf("."); | |
| 102 domain = nextDot == -1 ? null : domain.substring(nextDot + 1); | |
| 103 } | |
| 104 | |
| 105 return filtersList; | |
| 106 } | |
| 107 | |
| 108 /** | |
| 109 * Returns a list of selectors from a given list of filters that apply on a | |
| 110 * domain | |
| 111 * @param {string} [domain] | |
| 112 * @param {Array.<?Map.<Filter,boolean>>} filtersList | |
| 113 * @returns {string[]} | |
| 114 */ | |
| 115 function getConditionalSelectorsForDomain(domain, filtersList) | |
| 116 { | |
| 117 let selectors = []; | |
| 118 | |
| 119 let excluded = new Set(); | |
| 120 | |
| 121 // This code is a performance hot-spot, which is why we've made certain | |
| 122 // micro-optimisations. Please be careful before making changes. | |
| 123 for (let i = 0; i < filtersList.length; i++) | |
| 124 { | |
| 125 if (!filtersList[i]) | |
| 126 continue; | |
| 127 | |
| 128 for (let [filter, isIncluded] of filtersList[i]) | |
| 129 { | |
| 130 if (!isIncluded) | |
| 131 { | |
| 132 excluded.add(filter); | |
| 133 } | |
| 134 else if ((excluded.size == 0 || !excluded.has(filter)) && | |
| 135 !ElemHide.getException(filter, domain)) | |
| 136 { | |
| 137 selectors.push(filter.selector); | |
| 138 } | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 return selectors; | |
| 143 } | |
| 144 | |
| 145 /** | |
| 146 * Returns a list of selectors that apply on any unknown domain | |
| 147 * @returns {string[]} | |
| 148 */ | |
| 149 function getConditionalGenericSelectors() | |
| 150 { | |
| 151 if (conditionalGenericSelectors) | |
|
Manish Jethani
2018/05/11 10:00:31
Return cached value. It's always the same each tim
| |
| 152 return conditionalGenericSelectors.slice(); | |
| 153 | |
| 154 conditionalGenericSelectors = []; | |
| 155 | |
| 156 let filters = filtersByDomain.get(""); | |
| 157 if (!filters) | |
| 158 return conditionalGenericSelectors; | |
| 159 | |
| 160 for (let {selector} of filters.keys()) | |
| 161 { | |
| 162 if (genericExceptions.size == 0 || !genericExceptions.has(selector)) | |
| 163 conditionalGenericSelectors.push(selector); | |
| 164 } | |
| 165 | |
| 166 return conditionalGenericSelectors; | |
| 167 } | |
| 168 | |
| 169 /** | |
| 69 * Returns a list of selectors that apply on each website unconditionally. | 170 * Returns a list of selectors that apply on each website unconditionally. |
| 70 * @returns {string[]} | 171 * @returns {string[]} |
| 71 */ | 172 */ |
| 72 function getUnconditionalSelectors() | 173 function getUnconditionalSelectors() |
| 73 { | 174 { |
| 74 if (!unconditionalSelectors) | 175 if (!unconditionalSelectors) |
| 75 unconditionalSelectors = [...filterBySelector.keys()]; | 176 unconditionalSelectors = [...filterBySelector.keys()]; |
| 76 | 177 |
| 77 return unconditionalSelectors; | 178 return unconditionalSelectors; |
| 78 } | 179 } |
| 79 | 180 |
| 80 /** | 181 /** |
| 81 * Container for element hiding filters | 182 * Container for element hiding filters |
| 82 * @class | 183 * @class |
| 83 */ | 184 */ |
| 84 let ElemHide = exports.ElemHide = { | 185 let ElemHide = exports.ElemHide = { |
| 85 /** | 186 /** |
| 86 * Removes all known filters | 187 * Removes all known filters |
| 87 */ | 188 */ |
| 88 clear() | 189 clear() |
| 89 { | 190 { |
| 90 for (let collection of [filtersByDomain, filterBySelector, | 191 for (let collection of [filtersByDomain, filterBySelector, |
| 91 knownFilters, exceptions]) | 192 knownFilters, exceptions, |
| 193 genericExceptions]) | |
| 92 { | 194 { |
| 93 collection.clear(); | 195 collection.clear(); |
| 94 } | 196 } |
| 95 unconditionalSelectors = null; | 197 unconditionalSelectors = null; |
| 96 FilterNotifier.emit("elemhideupdate"); | 198 FilterNotifier.emit("elemhideupdate"); |
| 97 }, | 199 }, |
| 98 | 200 |
| 99 _addToFiltersByDomain(filter) | 201 _addToFiltersByDomain(filter) |
| 100 { | 202 { |
| 101 let domains = filter.domains || defaultDomains; | 203 let domains = filter.domains || defaultDomains; |
| 102 for (let [domain, isIncluded] of domains) | 204 if (filter instanceof ElemHideException) |
| 103 { | 205 { |
| 104 // There's no need to note that a filter is generically disabled. | 206 for (let domain of domains.keys()) |
| 105 if (!isIncluded && domain == "") | 207 { |
| 106 continue; | 208 // Add an entry for each domain, but without any filters. This makes |
| 209 // the domain "known" and helps us avoid certain optimizations that | |
| 210 // would otherwise yield incorrect results. | |
| 211 if (domain != "" && !filtersByDomain.has(domain)) | |
| 212 filtersByDomain.set(domain, null); | |
| 213 } | |
| 214 } | |
| 215 else | |
| 216 { | |
| 217 for (let [domain, isIncluded] of domains) | |
| 218 { | |
| 219 // There's no need to note that a filter is generically disabled. | |
| 220 if (!isIncluded && domain == "") | |
| 221 continue; | |
| 107 | 222 |
| 108 let filters = filtersByDomain.get(domain); | 223 let filters = filtersByDomain.get(domain); |
| 109 if (!filters) | 224 if (!filters) |
| 110 filtersByDomain.set(domain, filters = new Map()); | 225 filtersByDomain.set(domain, filters = new Map()); |
| 111 filters.set(filter, isIncluded); | 226 filters.set(filter, isIncluded); |
| 227 } | |
| 112 } | 228 } |
| 113 }, | 229 }, |
| 114 | 230 |
| 115 /** | 231 /** |
| 116 * Add a new element hiding filter | 232 * Add a new element hiding filter |
| 117 * @param {ElemHideBase} filter | 233 * @param {ElemHideBase} filter |
| 118 */ | 234 */ |
| 119 add(filter) | 235 add(filter) |
| 120 { | 236 { |
| 121 if (knownFilters.has(filter)) | 237 if (knownFilters.has(filter)) |
| 122 return; | 238 return; |
| 123 | 239 |
| 240 conditionalGenericSelectors = null; | |
|
Manish Jethani
2018/05/11 10:00:31
We could be more intelligent about when to clear t
| |
| 241 | |
| 124 if (filter instanceof ElemHideException) | 242 if (filter instanceof ElemHideException) |
| 125 { | 243 { |
| 126 let {selector} = filter; | 244 let {selector, domains} = filter; |
| 245 | |
| 127 let list = exceptions.get(selector); | 246 let list = exceptions.get(selector); |
| 128 if (list) | 247 if (list) |
| 129 list.push(filter); | 248 list.push(filter); |
| 130 else | 249 else |
| 131 exceptions.set(selector, [filter]); | 250 exceptions.set(selector, [filter]); |
| 132 | 251 |
| 252 if (domains) | |
| 253 this._addToFiltersByDomain(filter); | |
| 254 | |
| 255 if (filter.isGeneric()) | |
| 256 { | |
| 257 list = genericExceptions.get(selector); | |
| 258 if (list) | |
| 259 list.push(filter); | |
| 260 else | |
| 261 genericExceptions.set(selector, [filter]); | |
| 262 } | |
| 263 | |
| 133 // If this is the first exception for a previously unconditionally | 264 // If this is the first exception for a previously unconditionally |
| 134 // applied element hiding selector we need to take care to update the | 265 // applied element hiding selector we need to take care to update the |
| 135 // lookups. | 266 // lookups. |
| 136 let unconditionalFilterForSelector = filterBySelector.get(selector); | 267 let unconditionalFilterForSelector = filterBySelector.get(selector); |
| 137 if (unconditionalFilterForSelector) | 268 if (unconditionalFilterForSelector) |
| 138 { | 269 { |
| 139 this._addToFiltersByDomain(unconditionalFilterForSelector); | 270 this._addToFiltersByDomain(unconditionalFilterForSelector); |
| 140 filterBySelector.delete(selector); | 271 filterBySelector.delete(selector); |
| 141 unconditionalSelectors = null; | 272 unconditionalSelectors = null; |
| 142 } | 273 } |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 159 | 290 |
| 160 /** | 291 /** |
| 161 * Removes an element hiding filter | 292 * Removes an element hiding filter |
| 162 * @param {ElemHideBase} filter | 293 * @param {ElemHideBase} filter |
| 163 */ | 294 */ |
| 164 remove(filter) | 295 remove(filter) |
| 165 { | 296 { |
| 166 if (!knownFilters.has(filter)) | 297 if (!knownFilters.has(filter)) |
| 167 return; | 298 return; |
| 168 | 299 |
| 300 conditionalGenericSelectors = null; | |
| 301 | |
| 169 // Whitelisting filters | 302 // Whitelisting filters |
| 170 if (filter instanceof ElemHideException) | 303 if (filter instanceof ElemHideException) |
| 171 { | 304 { |
| 172 let list = exceptions.get(filter.selector); | 305 let list = exceptions.get(filter.selector); |
| 173 let index = list.indexOf(filter); | 306 let index = list.indexOf(filter); |
| 174 if (index >= 0) | 307 if (index >= 0) |
| 175 list.splice(index, 1); | 308 list.splice(index, 1); |
| 309 | |
| 310 if (filter.isGeneric()) | |
| 311 { | |
| 312 list = genericExceptions.get(filter.selector); | |
| 313 index = list.indexOf(filter); | |
| 314 if (index >= 0) | |
| 315 list.splice(index, 1); | |
| 316 | |
| 317 if (list.length == 0) | |
| 318 genericExceptions.delete(filter.selector); | |
| 319 } | |
| 176 } | 320 } |
| 177 // Unconditially applied element hiding filters | 321 // Unconditially applied element hiding filters |
| 178 else if (filterBySelector.get(filter.selector) == filter) | 322 else if (filterBySelector.get(filter.selector) == filter) |
| 179 { | 323 { |
| 180 filterBySelector.delete(filter.selector); | 324 filterBySelector.delete(filter.selector); |
| 181 unconditionalSelectors = null; | 325 unconditionalSelectors = null; |
| 182 } | 326 } |
| 183 // Conditionally applied element hiding filters | 327 // Conditionally applied element hiding filters |
| 184 else | 328 else |
| 185 { | 329 { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 249 * @param {number} [criteria] | 393 * @param {number} [criteria] |
| 250 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or | 394 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or |
| 251 * ElemHide.SPECIFIC_ONLY. | 395 * ElemHide.SPECIFIC_ONLY. |
| 252 * @returns {string[]} | 396 * @returns {string[]} |
| 253 * List of selectors. | 397 * List of selectors. |
| 254 */ | 398 */ |
| 255 getSelectorsForDomain(domain, criteria = ElemHide.ALL_MATCHING) | 399 getSelectorsForDomain(domain, criteria = ElemHide.ALL_MATCHING) |
| 256 { | 400 { |
| 257 let selectors = []; | 401 let selectors = []; |
| 258 | 402 |
| 259 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); | 403 let specificOnly = criteria >= ElemHide.SPECIFIC_ONLY; |
| 260 let excluded = new Set(); | 404 let filtersList = getSpecificFiltersForDomain(domain); |
| 261 let currentDomain = domain ? domain.toUpperCase() : ""; | |
| 262 | 405 |
| 263 // This code is a performance hot-spot, which is why we've made certain | 406 if (filtersList.length > 0) |
| 264 // micro-optimisations. Please be careful before making changes. | |
| 265 while (true) | |
| 266 { | 407 { |
| 267 if (specificOnly && currentDomain == "") | 408 if (!specificOnly) |
| 268 break; | 409 filtersList.push(filtersByDomain.get("")); |
| 269 | 410 |
| 270 let filters = filtersByDomain.get(currentDomain); | 411 selectors = getConditionalSelectorsForDomain(domain, filtersList); |
| 271 if (filters) | 412 } |
| 272 { | 413 else if (!specificOnly) |
| 273 for (let [filter, isIncluded] of filters) | 414 { |
| 274 { | 415 selectors = getConditionalGenericSelectors(); |
| 275 if (!isIncluded) | |
| 276 { | |
| 277 excluded.add(filter); | |
| 278 } | |
| 279 else if ((excluded.size == 0 || !excluded.has(filter)) && | |
| 280 !this.getException(filter, domain)) | |
| 281 { | |
| 282 selectors.push(filter.selector); | |
| 283 } | |
| 284 } | |
| 285 } | |
| 286 | |
| 287 if (currentDomain == "") | |
| 288 break; | |
| 289 | |
| 290 let nextDot = currentDomain.indexOf("."); | |
| 291 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | |
| 292 } | 416 } |
| 293 | 417 |
| 294 if (criteria < ElemHide.NO_UNCONDITIONAL) | 418 if (criteria < ElemHide.NO_UNCONDITIONAL) |
| 295 selectors = getUnconditionalSelectors().concat(selectors); | 419 selectors = getUnconditionalSelectors().concat(selectors); |
| 296 | 420 |
| 297 return selectors; | 421 return selectors; |
| 298 } | 422 } |
| 299 }; | 423 }; |
| OLD | NEW |