| 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 {ElemHideExceptions} = require("./elemHideExceptions"); | 24 const {ElemHideExceptions} = require("./elemHideExceptions"); |
| 25 const {FilterNotifier} = require("./filterNotifier"); | 25 const {FilterNotifier} = require("./filterNotifier"); |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * Lookup table, active flag, by filter by domain. | |
| 29 * (Only contains filters that aren't unconditionally matched for all domains.) | |
| 30 * @type {Map.<string,Map.<Filter,boolean>>} | |
| 31 */ | |
| 32 let filtersByDomain = new Map(); | |
| 33 | |
| 34 /** | |
| 35 * Lookup table, filter by selector. (Only used for selectors that are | |
| 36 * unconditionally matched for all domains.) | |
| 37 * @type {Map.<string,Filter>} | |
| 38 */ | |
| 39 let filterBySelector = new Map(); | |
| 40 | |
| 41 /** | |
| 42 * This array caches the keys of filterBySelector table (selectors | |
| 43 * which unconditionally apply on all domains). It will be null if the | |
| 44 * cache needs to be rebuilt. | |
| 45 * @type {?string[]} | |
| 46 */ | |
| 47 let unconditionalSelectors = null; | |
| 48 | |
| 49 /** | |
| 50 * Map to be used instead when a filter has a blank domains property. | 28 * Map to be used instead when a filter has a blank domains property. |
| 51 * @type {Map.<string,boolean>} | 29 * @type {Map.<string,boolean>} |
| 52 * @const | 30 * @const |
| 53 */ | 31 */ |
| 54 let defaultDomains = new Map([["", true]]); | 32 let defaultDomains = new Map([["", true]]); |
| 55 | 33 |
| 56 /** | 34 /** |
| 57 * Set containing known element hiding filters | 35 * Template for element hiding and element hiding emulation filter containers |
| 58 * @type {Set.<ElemHideFilter>} | 36 * @class |
| 59 */ | 37 */ |
| 60 let knownFilters = new Set(); | 38 function ElemHideTemplate() |
| 39 { |
| 40 this._filtersByDomain = new Map(); |
| 41 this._filterBySelector = new Map(); |
| 42 this._unconditionalSelectors = null; |
| 43 this._knownFilters = new Set(); |
| 61 | 44 |
| 62 /** | 45 ElemHideExceptions.on("added", this._onExceptionAdded.bind(this)); |
| 63 * Adds a filter to the lookup table of filters by domain. | |
| 64 * @param {Filter} filter | |
| 65 */ | |
| 66 function addToFiltersByDomain(filter) | |
| 67 { | |
| 68 let domains = filter.domains || defaultDomains; | |
| 69 for (let [domain, isIncluded] of domains) | |
| 70 { | |
| 71 // There's no need to note that a filter is generically disabled. | |
| 72 if (!isIncluded && domain == "") | |
| 73 continue; | |
| 74 | |
| 75 let filters = filtersByDomain.get(domain); | |
| 76 if (!filters) | |
| 77 filtersByDomain.set(domain, filters = new Map()); | |
| 78 filters.set(filter, isIncluded); | |
| 79 } | |
| 80 } | 46 } |
| 81 | 47 |
| 82 /** | 48 ElemHideTemplate.prototype = { |
| 83 * Returns a list of selectors that apply on each website unconditionally. | 49 /** |
| 84 * @returns {string[]} | 50 * Lookup table, active flag, by filter by domain. |
| 85 */ | 51 * (Only contains filters that aren't unconditionally matched for all |
| 86 function getUnconditionalSelectors() | 52 * domains.) |
| 87 { | 53 * @type {Map.<string,Map.<Filter,boolean>>} |
| 88 if (!unconditionalSelectors) | 54 */ |
| 89 unconditionalSelectors = [...filterBySelector.keys()]; | 55 _filtersByDomain: null, |
| 90 | 56 |
| 91 return unconditionalSelectors; | 57 /** |
| 92 } | 58 * Lookup table, filter by selector. (Only used for selectors that are |
| 59 * unconditionally matched for all domains.) |
| 60 * @type {Map.<string,Filter>} |
| 61 */ |
| 62 _filterBySelector: null, |
| 93 | 63 |
| 94 ElemHideExceptions.on("added", ({selector}) => | 64 /** |
| 95 { | 65 * This array caches the keys of _filterBySelector table (selectors |
| 96 // If this is the first exception for a previously unconditionally applied | 66 * which unconditionally apply on all domains). It will be null if the |
| 97 // element hiding selector we need to take care to update the lookups. | 67 * cache needs to be rebuilt. |
| 98 let unconditionalFilterForSelector = filterBySelector.get(selector); | 68 * @type {?string[]} |
| 99 if (unconditionalFilterForSelector) | 69 */ |
| 70 _unconditionalSelectors: null, |
| 71 |
| 72 /** |
| 73 * Set containing known element hiding filters |
| 74 * @type {Set.<ElemHideFilter>} |
| 75 */ |
| 76 _knownFilters: null, |
| 77 |
| 78 /** |
| 79 * Adds a filter to the lookup table of filters by domain. |
| 80 * @param {Filter} filter |
| 81 */ |
| 82 _addToFiltersByDomain(filter) |
| 100 { | 83 { |
| 101 addToFiltersByDomain(unconditionalFilterForSelector); | 84 let domains = filter.domains || defaultDomains; |
| 102 filterBySelector.delete(selector); | 85 for (let [domain, isIncluded] of domains) |
| 103 unconditionalSelectors = null; | 86 { |
| 104 } | 87 // There's no need to note that a filter is generically disabled. |
| 88 if (!isIncluded && domain == "") |
| 89 continue; |
| 105 | 90 |
| 106 FilterNotifier.emit("elemhideupdate"); | 91 let filters = this._filtersByDomain.get(domain); |
| 107 }); | 92 if (!filters) |
| 93 this._filtersByDomain.set(domain, filters = new Map()); |
| 94 filters.set(filter, isIncluded); |
| 95 } |
| 96 }, |
| 108 | 97 |
| 109 ElemHideExceptions.on("removed", () => | 98 /** |
| 110 { | 99 * Returns a list of selectors that apply on each website unconditionally. |
| 111 FilterNotifier.emit("elemhideupdate"); | 100 * @returns {string[]} |
| 112 }); | 101 */ |
| 102 _getUnconditionalSelectors() |
| 103 { |
| 104 if (!this._unconditionalSelectors) |
| 105 this._unconditionalSelectors = [...this._filterBySelector.keys()]; |
| 113 | 106 |
| 114 /** | 107 return this._unconditionalSelectors; |
| 115 * Container for element hiding filters | 108 }, |
| 116 * @class | 109 |
| 117 */ | 110 /** |
| 118 exports.ElemHide = { | 111 * Handles the event when a new element hiding exception has been added |
| 112 * @param {ElemHideException} exception |
| 113 */ |
| 114 _onExceptionAdded(exception) |
| 115 { |
| 116 let {selector} = exception; |
| 117 |
| 118 // If this is the first exception for a previously unconditionally applied |
| 119 // element hiding selector we need to take care to update the lookups. |
| 120 let unconditionalFilterForSelector = this._filterBySelector.get(selector); |
| 121 if (unconditionalFilterForSelector) |
| 122 { |
| 123 this._addToFiltersByDomain(unconditionalFilterForSelector); |
| 124 this._filterBySelector.delete(selector); |
| 125 this._unconditionalSelectors = null; |
| 126 } |
| 127 }, |
| 128 |
| 119 /** | 129 /** |
| 120 * Removes all known filters | 130 * Removes all known filters |
| 121 */ | 131 */ |
| 122 clear() | 132 clear() |
| 123 { | 133 { |
| 124 for (let collection of [filtersByDomain, filterBySelector, | 134 for (let collection of [this._filtersByDomain, this._filterBySelector, |
| 125 knownFilters]) | 135 this._knownFilters]) |
| 126 { | 136 { |
| 127 collection.clear(); | 137 collection.clear(); |
| 128 } | 138 } |
| 129 unconditionalSelectors = null; | 139 this._unconditionalSelectors = null; |
| 130 FilterNotifier.emit("elemhideupdate"); | 140 FilterNotifier.emit("elemhideupdate"); |
| 131 }, | 141 }, |
| 132 | 142 |
| 133 /** | 143 /** |
| 134 * Add a new element hiding filter | 144 * Add a new element hiding filter |
| 135 * @param {ElemHideFilter} filter | 145 * @param {ElemHideFilter} filter |
| 136 */ | 146 */ |
| 137 add(filter) | 147 add(filter) |
| 138 { | 148 { |
| 139 if (knownFilters.has(filter)) | 149 if (this._knownFilters.has(filter)) |
| 140 return; | 150 return; |
| 141 | 151 |
| 142 if (!(filter.domains || ElemHideExceptions.hasExceptions(filter.selector))) | 152 if (!(filter.domains || ElemHideExceptions.hasExceptions(filter.selector))) |
| 143 { | 153 { |
| 144 // The new filter's selector is unconditionally applied to all domains | 154 // The new filter's selector is unconditionally applied to all domains |
| 145 filterBySelector.set(filter.selector, filter); | 155 this._filterBySelector.set(filter.selector, filter); |
| 146 unconditionalSelectors = null; | 156 this._unconditionalSelectors = null; |
| 147 } | 157 } |
| 148 else | 158 else |
| 149 { | 159 { |
| 150 // The new filter's selector only applies to some domains | 160 // The new filter's selector only applies to some domains |
| 151 addToFiltersByDomain(filter); | 161 this._addToFiltersByDomain(filter); |
| 152 } | 162 } |
| 153 | 163 |
| 154 knownFilters.add(filter); | 164 this._knownFilters.add(filter); |
| 155 FilterNotifier.emit("elemhideupdate"); | 165 FilterNotifier.emit("elemhideupdate"); |
| 156 }, | 166 }, |
| 157 | 167 |
| 158 /** | 168 /** |
| 159 * Removes an element hiding filter | 169 * Removes an element hiding filter |
| 160 * @param {ElemHideFilter} filter | 170 * @param {ElemHideFilter} filter |
| 161 */ | 171 */ |
| 162 remove(filter) | 172 remove(filter) |
| 163 { | 173 { |
| 164 if (!knownFilters.has(filter)) | 174 if (!this._knownFilters.has(filter)) |
| 165 return; | 175 return; |
| 166 | 176 |
| 167 // Unconditially applied element hiding filters | 177 // Unconditially applied element hiding filters |
| 168 if (filterBySelector.get(filter.selector) == filter) | 178 if (this._filterBySelector.get(filter.selector) == filter) |
| 169 { | 179 { |
| 170 filterBySelector.delete(filter.selector); | 180 this._filterBySelector.delete(filter.selector); |
| 171 unconditionalSelectors = null; | 181 this._unconditionalSelectors = null; |
| 172 } | 182 } |
| 173 // Conditionally applied element hiding filters | 183 // Conditionally applied element hiding filters |
| 174 else | 184 else |
| 175 { | 185 { |
| 176 let domains = filter.domains || defaultDomains; | 186 let domains = filter.domains || defaultDomains; |
| 177 for (let domain of domains.keys()) | 187 for (let domain of domains.keys()) |
| 178 { | 188 { |
| 179 let filters = filtersByDomain.get(domain); | 189 let filters = this._filtersByDomain.get(domain); |
| 180 if (filters) | 190 if (filters) |
| 181 filters.delete(filter); | 191 filters.delete(filter); |
| 182 } | 192 } |
| 183 } | 193 } |
| 184 | 194 |
| 185 knownFilters.delete(filter); | 195 this._knownFilters.delete(filter); |
| 186 FilterNotifier.emit("elemhideupdate"); | 196 FilterNotifier.emit("elemhideupdate"); |
| 187 }, | 197 } |
| 198 }; |
| 188 | 199 |
| 200 exports.ElemHideTemplate = ElemHideTemplate; |
| 201 |
| 202 /** |
| 203 * Container for element hiding filters |
| 204 * @class |
| 205 */ |
| 206 exports.ElemHide = Object.assign(new ElemHideTemplate(), { |
| 189 /** | 207 /** |
| 190 * Determines from the current filter list which selectors should be applied | 208 * Determines from the current filter list which selectors should be applied |
| 191 * on a particular host name. | 209 * on a particular host name. |
| 192 * @param {string} domain | 210 * @param {string} domain |
| 193 * @param {boolean} [specificOnly] true if generic filters should not apply. | 211 * @param {boolean} [specificOnly] true if generic filters should not apply. |
| 194 * @returns {string[]} List of selectors. | 212 * @returns {string[]} List of selectors. |
| 195 */ | 213 */ |
| 196 getSelectorsForDomain(domain, specificOnly = false) | 214 getSelectorsForDomain(domain, specificOnly = false) |
| 197 { | 215 { |
| 198 let selectors = []; | 216 let selectors = []; |
| 199 | 217 |
| 200 let excluded = new Set(); | 218 let excluded = new Set(); |
| 201 let currentDomain = domain ? domain.toUpperCase() : ""; | 219 let currentDomain = domain ? domain.toUpperCase() : ""; |
| 202 | 220 |
| 203 // This code is a performance hot-spot, which is why we've made certain | 221 // This code is a performance hot-spot, which is why we've made certain |
| 204 // micro-optimisations. Please be careful before making changes. | 222 // micro-optimisations. Please be careful before making changes. |
| 205 while (true) | 223 while (true) |
| 206 { | 224 { |
| 207 if (specificOnly && currentDomain == "") | 225 if (specificOnly && currentDomain == "") |
| 208 break; | 226 break; |
| 209 | 227 |
| 210 let filters = filtersByDomain.get(currentDomain); | 228 let filters = this._filtersByDomain.get(currentDomain); |
| 211 if (filters) | 229 if (filters) |
| 212 { | 230 { |
| 213 for (let [filter, isIncluded] of filters) | 231 for (let [filter, isIncluded] of filters) |
| 214 { | 232 { |
| 215 if (!isIncluded) | 233 if (!isIncluded) |
| 216 { | 234 { |
| 217 excluded.add(filter); | 235 excluded.add(filter); |
| 218 } | 236 } |
| 219 else if ((excluded.size == 0 || !excluded.has(filter)) && | 237 else if ((excluded.size == 0 || !excluded.has(filter)) && |
| 220 !ElemHideExceptions.getException(filter, domain)) | 238 !ElemHideExceptions.getException(filter, domain)) |
| 221 { | 239 { |
| 222 selectors.push(filter.selector); | 240 selectors.push(filter.selector); |
| 223 } | 241 } |
| 224 } | 242 } |
| 225 } | 243 } |
| 226 | 244 |
| 227 if (currentDomain == "") | 245 if (currentDomain == "") |
| 228 break; | 246 break; |
| 229 | 247 |
| 230 let nextDot = currentDomain.indexOf("."); | 248 let nextDot = currentDomain.indexOf("."); |
| 231 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | 249 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); |
| 232 } | 250 } |
| 233 | 251 |
| 234 if (!specificOnly) | 252 if (!specificOnly) |
| 235 selectors = getUnconditionalSelectors().concat(selectors); | 253 selectors = this._getUnconditionalSelectors().concat(selectors); |
| 236 | 254 |
| 237 return selectors; | 255 return selectors; |
| 238 } | 256 } |
| 239 }; | 257 }); |
| OLD | NEW |