| 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 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 /** | 41 /** |
| 42 * This array caches the keys of filterBySelector table (selectors | 42 * This array caches the keys of filterBySelector table (selectors |
| 43 * which unconditionally apply on all domains). It will be null if the | 43 * which unconditionally apply on all domains). It will be null if the |
| 44 * cache needs to be rebuilt. | 44 * cache needs to be rebuilt. |
| 45 * @type {?string[]} | 45 * @type {?string[]} |
| 46 */ | 46 */ |
| 47 let unconditionalSelectors = null; | 47 let unconditionalSelectors = null; |
| 48 | 48 |
| 49 /** | 49 /** |
| 50 * Cached list of selectors that apply on any domain that neither has any |
| 51 * domain-specific filters or exceptions nor is specifically excluded from any |
| 52 * generic filters or exceptions. |
| 53 * @type {?string[]} |
| 54 */ |
| 55 let commonSelectors = null; |
| 56 |
| 57 /** |
| 50 * Map to be used instead when a filter has a blank domains property. | 58 * Map to be used instead when a filter has a blank domains property. |
| 51 * @type {Map.<string,boolean>} | 59 * @type {Map.<string,boolean>} |
| 52 * @const | 60 * @const |
| 53 */ | 61 */ |
| 54 let defaultDomains = new Map([["", true]]); | 62 let defaultDomains = new Map([["", true]]); |
| 55 | 63 |
| 56 /** | 64 /** |
| 57 * Set containing known element hiding filters | 65 * Set containing known element hiding filters |
| 58 * @type {Set.<ElemHideFilter>} | 66 * @type {Set.<ElemHideFilter>} |
| 59 */ | 67 */ |
| (...skipping 26 matching lines...) Expand all Loading... |
| 86 function getUnconditionalSelectors() | 94 function getUnconditionalSelectors() |
| 87 { | 95 { |
| 88 if (!unconditionalSelectors) | 96 if (!unconditionalSelectors) |
| 89 unconditionalSelectors = [...filterBySelector.keys()]; | 97 unconditionalSelectors = [...filterBySelector.keys()]; |
| 90 | 98 |
| 91 return unconditionalSelectors; | 99 return unconditionalSelectors; |
| 92 } | 100 } |
| 93 | 101 |
| 94 ElemHideExceptions.on("added", ({selector}) => | 102 ElemHideExceptions.on("added", ({selector}) => |
| 95 { | 103 { |
| 104 commonSelectors = null; |
| 105 |
| 96 // If this is the first exception for a previously unconditionally applied | 106 // If this is the first exception for a previously unconditionally applied |
| 97 // element hiding selector we need to take care to update the lookups. | 107 // element hiding selector we need to take care to update the lookups. |
| 98 let unconditionalFilterForSelector = filterBySelector.get(selector); | 108 let unconditionalFilterForSelector = filterBySelector.get(selector); |
| 99 if (unconditionalFilterForSelector) | 109 if (unconditionalFilterForSelector) |
| 100 { | 110 { |
| 101 addToFiltersByDomain(unconditionalFilterForSelector); | 111 addToFiltersByDomain(unconditionalFilterForSelector); |
| 102 filterBySelector.delete(selector); | 112 filterBySelector.delete(selector); |
| 103 unconditionalSelectors = null; | 113 unconditionalSelectors = null; |
| 104 } | 114 } |
| 105 }); | 115 }); |
| 106 | 116 |
| 107 /** | 117 /** |
| 108 * Container for element hiding filters | 118 * Container for element hiding filters |
| 109 * @class | 119 * @class |
| 110 */ | 120 */ |
| 111 exports.ElemHide = { | 121 exports.ElemHide = { |
| 112 /** | 122 /** |
| 113 * Removes all known filters | 123 * Removes all known filters |
| 114 */ | 124 */ |
| 115 clear() | 125 clear() |
| 116 { | 126 { |
| 117 for (let collection of [filtersByDomain, filterBySelector, knownFilters]) | 127 for (let collection of [filtersByDomain, filterBySelector, knownFilters]) |
| 118 collection.clear(); | 128 collection.clear(); |
| 119 | 129 |
| 130 commonSelectors = null; |
| 120 unconditionalSelectors = null; | 131 unconditionalSelectors = null; |
| 132 |
| 121 filterNotifier.emit("elemhideupdate"); | 133 filterNotifier.emit("elemhideupdate"); |
| 122 }, | 134 }, |
| 123 | 135 |
| 124 /** | 136 /** |
| 125 * Add a new element hiding filter | 137 * Add a new element hiding filter |
| 126 * @param {ElemHideFilter} filter | 138 * @param {ElemHideFilter} filter |
| 127 */ | 139 */ |
| 128 add(filter) | 140 add(filter) |
| 129 { | 141 { |
| 130 if (knownFilters.has(filter)) | 142 if (knownFilters.has(filter)) |
| 131 return; | 143 return; |
| 132 | 144 |
| 145 commonSelectors = null; |
| 146 |
| 133 let {selector} = filter; | 147 let {selector} = filter; |
| 134 | 148 |
| 135 if (!(filter.domains || ElemHideExceptions.hasExceptions(selector))) | 149 if (!(filter.domains || ElemHideExceptions.hasExceptions(selector))) |
| 136 { | 150 { |
| 137 // The new filter's selector is unconditionally applied to all domains | 151 // The new filter's selector is unconditionally applied to all domains |
| 138 filterBySelector.set(selector, filter); | 152 filterBySelector.set(selector, filter); |
| 139 unconditionalSelectors = null; | 153 unconditionalSelectors = null; |
| 140 } | 154 } |
| 141 else | 155 else |
| 142 { | 156 { |
| 143 // The new filter's selector only applies to some domains | 157 // The new filter's selector only applies to some domains |
| 144 addToFiltersByDomain(filter); | 158 addToFiltersByDomain(filter); |
| 145 } | 159 } |
| 146 | 160 |
| 147 knownFilters.add(filter); | 161 knownFilters.add(filter); |
| 148 filterNotifier.emit("elemhideupdate"); | 162 filterNotifier.emit("elemhideupdate"); |
| 149 }, | 163 }, |
| 150 | 164 |
| 151 /** | 165 /** |
| 152 * Removes an element hiding filter | 166 * Removes an element hiding filter |
| 153 * @param {ElemHideFilter} filter | 167 * @param {ElemHideFilter} filter |
| 154 */ | 168 */ |
| 155 remove(filter) | 169 remove(filter) |
| 156 { | 170 { |
| 157 if (!knownFilters.has(filter)) | 171 if (!knownFilters.has(filter)) |
| 158 return; | 172 return; |
| 159 | 173 |
| 174 commonSelectors = null; |
| 175 |
| 160 let {selector} = filter; | 176 let {selector} = filter; |
| 161 | 177 |
| 162 // Unconditially applied element hiding filters | 178 // Unconditially applied element hiding filters |
| 163 if (filterBySelector.get(selector) == filter) | 179 if (filterBySelector.get(selector) == filter) |
| 164 { | 180 { |
| 165 filterBySelector.delete(selector); | 181 filterBySelector.delete(selector); |
| 166 unconditionalSelectors = null; | 182 unconditionalSelectors = null; |
| 167 } | 183 } |
| 168 // Conditionally applied element hiding filters | 184 // Conditionally applied element hiding filters |
| 169 else | 185 else |
| (...skipping 23 matching lines...) Expand all Loading... |
| 193 * @param {boolean} [specificOnly] true if generic filters should not apply. | 209 * @param {boolean} [specificOnly] true if generic filters should not apply. |
| 194 * @returns {string[]} List of selectors. | 210 * @returns {string[]} List of selectors. |
| 195 */ | 211 */ |
| 196 getSelectorsForDomain(domain, specificOnly = false) | 212 getSelectorsForDomain(domain, specificOnly = false) |
| 197 { | 213 { |
| 198 let selectors = []; | 214 let selectors = []; |
| 199 | 215 |
| 200 let excluded = new Set(); | 216 let excluded = new Set(); |
| 201 let currentDomain = domain ? domain.replace(/\.+$/, "").toLowerCase() : ""; | 217 let currentDomain = domain ? domain.replace(/\.+$/, "").toLowerCase() : ""; |
| 202 | 218 |
| 219 let commonCase = !specificOnly; |
| 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 |
| 228 if (commonCase && currentDomain != "" && |
| 229 ElemHideExceptions.domainHasExceptions(currentDomain)) |
| 230 { |
| 231 commonCase = false; |
| 232 } |
| 233 |
| 210 let filters = filtersByDomain.get(currentDomain); | 234 let filters = filtersByDomain.get(currentDomain); |
| 211 if (filters) | 235 if (filters) |
| 212 { | 236 { |
| 237 if (currentDomain != "") |
| 238 commonCase = false; |
| 239 |
| 213 for (let [filter, isIncluded] of filters) | 240 for (let [filter, isIncluded] of filters) |
| 214 { | 241 { |
| 215 if (!isIncluded) | 242 if (!isIncluded) |
| 216 { | 243 { |
| 217 excluded.add(filter); | 244 excluded.add(filter); |
| 218 } | 245 } |
| 219 else | 246 else |
| 220 { | 247 { |
| 221 let {selector} = filter; | 248 let {selector} = filter; |
| 222 if ((excluded.size == 0 || !excluded.has(filter)) && | 249 if ((excluded.size == 0 || !excluded.has(filter)) && |
| 223 !ElemHideExceptions.getException(selector, domain)) | 250 !ElemHideExceptions.getException(selector, domain)) |
| 224 { | 251 { |
| 225 selectors.push(selector); | 252 selectors.push(selector); |
| 226 } | 253 } |
| 227 } | 254 } |
| 228 } | 255 } |
| 229 } | 256 } |
| 230 | 257 |
| 231 if (currentDomain == "") | 258 if (currentDomain == "") |
| 232 break; | 259 break; |
| 233 | 260 |
| 234 let nextDot = currentDomain.indexOf("."); | 261 let nextDot = currentDomain.indexOf("."); |
| 235 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | 262 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); |
| 236 } | 263 } |
| 237 | 264 |
| 238 if (!specificOnly) | 265 if (!specificOnly) |
| 239 selectors = getUnconditionalSelectors().concat(selectors); | 266 { |
| 267 // Use the cache of common selectors if there are no filters or |
| 268 // exceptions that apply or don't apply specifically to this domain. |
| 269 if (commonCase) |
| 270 { |
| 271 if (!commonSelectors) |
| 272 { |
| 273 commonSelectors = |
| 274 Object.freeze(getUnconditionalSelectors().concat(selectors)); |
| 275 } |
| 276 |
| 277 selectors = commonSelectors; |
| 278 } |
| 279 else |
| 280 { |
| 281 selectors = getUnconditionalSelectors().concat(selectors); |
| 282 } |
| 283 } |
| 240 | 284 |
| 241 return selectors; | 285 return selectors; |
| 242 } | 286 } |
| 243 }; | 287 }; |
| OLD | NEW |