| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 function addToFiltersByDomain(filter) |
| 69 { |
| 70 let domains = filter.domains || defaultDomains; |
| 71 for (let [domain, isIncluded] of domains) |
| 72 { |
| 73 // There's no need to note that a filter is generically disabled. |
| 74 if (!isIncluded && domain == "") |
| 75 continue; |
| 76 |
| 77 let filters = filtersByDomain.get(domain); |
| 78 if (!filters) |
| 79 filtersByDomain.set(domain, filters = new Map()); |
| 80 filters.set(filter, isIncluded); |
| 81 } |
| 82 } |
| 83 |
| 84 /** |
| 85 * Returns a list of selectors that apply on each website unconditionally. |
| 86 * @returns {string[]} |
| 87 */ |
| 88 function getUnconditionalSelectors() |
| 89 { |
| 90 if (!unconditionalSelectors) |
| 91 unconditionalSelectors = [...filterBySelector.keys()]; |
| 92 |
| 93 return unconditionalSelectors.slice(); |
| 94 } |
| 95 |
| 68 /** | 96 /** |
| 69 * Container for element hiding filters | 97 * Container for element hiding filters |
| 70 * @class | 98 * @class |
| 71 */ | 99 */ |
| 72 let ElemHide = exports.ElemHide = { | 100 let ElemHide = exports.ElemHide = { |
| 73 /** | 101 /** |
| 74 * Removes all known filters | 102 * Removes all known filters |
| 75 */ | 103 */ |
| 76 clear() | 104 clear() |
| 77 { | 105 { |
| 78 for (let collection of [filtersByDomain, filterBySelector, | 106 for (let collection of [filtersByDomain, filterBySelector, |
| 79 knownFilters, exceptions]) | 107 knownFilters, exceptions]) |
| 80 { | 108 { |
| 81 collection.clear(); | 109 collection.clear(); |
| 82 } | 110 } |
| 83 unconditionalSelectors = null; | 111 unconditionalSelectors = null; |
| 84 FilterNotifier.emit("elemhideupdate"); | 112 FilterNotifier.emit("elemhideupdate"); |
| 85 }, | 113 }, |
| 86 | 114 |
| 87 _addToFiltersByDomain(filter) | |
| 88 { | |
| 89 let domains = filter.domains || defaultDomains; | |
| 90 for (let [domain, isIncluded] of domains) | |
| 91 { | |
| 92 // There's no need to note that a filter is generically disabled. | |
| 93 if (!isIncluded && domain == "") | |
| 94 continue; | |
| 95 | |
| 96 let filters = filtersByDomain.get(domain); | |
| 97 if (!filters) | |
| 98 filtersByDomain.set(domain, filters = new Map()); | |
| 99 filters.set(filter, isIncluded); | |
| 100 } | |
| 101 }, | |
| 102 | |
| 103 /** | 115 /** |
| 104 * Add a new element hiding filter | 116 * Add a new element hiding filter |
| 105 * @param {ElemHideBase} filter | 117 * @param {ElemHideBase} filter |
| 106 */ | 118 */ |
| 107 add(filter) | 119 add(filter) |
| 108 { | 120 { |
| 109 if (knownFilters.has(filter)) | 121 if (knownFilters.has(filter)) |
| 110 return; | 122 return; |
| 111 | 123 |
| 112 if (filter instanceof ElemHideException) | 124 if (filter instanceof ElemHideException) |
| 113 { | 125 { |
| 114 let {selector} = filter; | 126 let {selector} = filter; |
| 115 let list = exceptions.get(selector); | 127 let list = exceptions.get(selector); |
| 116 if (list) | 128 if (list) |
| 117 list.push(filter); | 129 list.push(filter); |
| 118 else | 130 else |
| 119 exceptions.set(selector, [filter]); | 131 exceptions.set(selector, [filter]); |
| 120 | 132 |
| 121 // If this is the first exception for a previously unconditionally | 133 // If this is the first exception for a previously unconditionally |
| 122 // applied element hiding selector we need to take care to update the | 134 // applied element hiding selector we need to take care to update the |
| 123 // lookups. | 135 // lookups. |
| 124 let unconditionalFilterForSelector = filterBySelector.get(selector); | 136 let unconditionalFilterForSelector = filterBySelector.get(selector); |
| 125 if (unconditionalFilterForSelector) | 137 if (unconditionalFilterForSelector) |
| 126 { | 138 { |
| 127 this._addToFiltersByDomain(unconditionalFilterForSelector); | 139 addToFiltersByDomain(unconditionalFilterForSelector); |
| 128 filterBySelector.delete(selector); | 140 filterBySelector.delete(selector); |
| 129 unconditionalSelectors = null; | 141 unconditionalSelectors = null; |
| 130 } | 142 } |
| 131 } | 143 } |
| 132 else if (!(filter.domains || exceptions.has(filter.selector))) | 144 else if (!(filter.domains || exceptions.has(filter.selector))) |
| 133 { | 145 { |
| 134 // The new filter's selector is unconditionally applied to all domains | 146 // The new filter's selector is unconditionally applied to all domains |
| 135 filterBySelector.set(filter.selector, filter); | 147 filterBySelector.set(filter.selector, filter); |
| 136 unconditionalSelectors = null; | 148 unconditionalSelectors = null; |
| 137 } | 149 } |
| 138 else | 150 else |
| 139 { | 151 { |
| 140 // The new filter's selector only applies to some domains | 152 // The new filter's selector only applies to some domains |
| 141 this._addToFiltersByDomain(filter); | 153 addToFiltersByDomain(filter); |
| 142 } | 154 } |
| 143 | 155 |
| 144 knownFilters.add(filter); | 156 knownFilters.add(filter); |
| 145 FilterNotifier.emit("elemhideupdate"); | 157 FilterNotifier.emit("elemhideupdate"); |
| 146 }, | 158 }, |
| 147 | 159 |
| 148 /** | 160 /** |
| 149 * Removes an element hiding filter | 161 * Removes an element hiding filter |
| 150 * @param {ElemHideBase} filter | 162 * @param {ElemHideBase} filter |
| 151 */ | 163 */ |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 for (let i = list.length - 1; i >= 0; i--) | 212 for (let i = list.length - 1; i >= 0; i--) |
| 201 { | 213 { |
| 202 if (list[i].isActiveOnDomain(docDomain)) | 214 if (list[i].isActiveOnDomain(docDomain)) |
| 203 return list[i]; | 215 return list[i]; |
| 204 } | 216 } |
| 205 | 217 |
| 206 return null; | 218 return null; |
| 207 }, | 219 }, |
| 208 | 220 |
| 209 /** | 221 /** |
| 210 * Returns a list of selectors that apply on each website unconditionally. | |
| 211 * @returns {string[]} | |
| 212 */ | |
| 213 getUnconditionalSelectors() | |
| 214 { | |
| 215 if (!unconditionalSelectors) | |
| 216 unconditionalSelectors = [...filterBySelector.keys()]; | |
| 217 return unconditionalSelectors.slice(); | |
| 218 }, | |
| 219 | |
| 220 /** | |
| 221 * Constant used by getSelectorsForDomain to return all selectors applying to | 222 * Constant used by getSelectorsForDomain to return all selectors applying to |
| 222 * a particular hostname. | 223 * a particular hostname. |
| 223 * @type {number} | 224 * @type {number} |
| 224 * @const | 225 * @const |
| 225 */ | 226 */ |
| 226 ALL_MATCHING: 0, | 227 ALL_MATCHING: 0, |
| 227 | 228 |
| 228 /** | 229 /** |
| 229 * Constant used by getSelectorsForDomain to exclude selectors which apply to | 230 * Constant used by getSelectorsForDomain to exclude selectors which apply to |
| 230 * all websites without exception. | 231 * all websites without exception. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 249 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or | 250 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or |
| 250 * ElemHide.SPECIFIC_ONLY. | 251 * ElemHide.SPECIFIC_ONLY. |
| 251 * @returns {string[]} | 252 * @returns {string[]} |
| 252 * List of selectors. | 253 * List of selectors. |
| 253 */ | 254 */ |
| 254 getSelectorsForDomain(domain, criteria = ElemHide.ALL_MATCHING) | 255 getSelectorsForDomain(domain, criteria = ElemHide.ALL_MATCHING) |
| 255 { | 256 { |
| 256 let selectors = []; | 257 let selectors = []; |
| 257 | 258 |
| 258 if (criteria < ElemHide.NO_UNCONDITIONAL) | 259 if (criteria < ElemHide.NO_UNCONDITIONAL) |
| 259 selectors = this.getUnconditionalSelectors(); | 260 selectors = getUnconditionalSelectors(); |
| 260 | 261 |
| 261 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); | 262 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); |
| 262 let excluded = new Set(); | 263 let excluded = new Set(); |
| 263 let currentDomain = domain ? domain.toUpperCase() : ""; | 264 let currentDomain = domain ? domain.toUpperCase() : ""; |
| 264 | 265 |
| 265 // This code is a performance hot-spot, which is why we've made certain | 266 // This code is a performance hot-spot, which is why we've made certain |
| 266 // micro-optimisations. Please be careful before making changes. | 267 // micro-optimisations. Please be careful before making changes. |
| 267 while (true) | 268 while (true) |
| 268 { | 269 { |
| 269 if (specificOnly && currentDomain == "") | 270 if (specificOnly && currentDomain == "") |
| (...skipping 19 matching lines...) Expand all Loading... |
| 289 if (currentDomain == "") | 290 if (currentDomain == "") |
| 290 break; | 291 break; |
| 291 | 292 |
| 292 let nextDot = currentDomain.indexOf("."); | 293 let nextDot = currentDomain.indexOf("."); |
| 293 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | 294 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); |
| 294 } | 295 } |
| 295 | 296 |
| 296 return selectors; | 297 return selectors; |
| 297 } | 298 } |
| 298 }; | 299 }; |
| OLD | NEW |