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 /* |
| 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 * Checks whether a filter has an exception |
| 76 * @param {Filter} filter |
| 77 * @param {string} [domain] |
| 78 * @returns {boolean} |
| 79 */ |
| 80 function hasException(filter, domain) |
| 81 { |
| 82 if (!domain) |
| 83 return genericExceptions.has(filter.selector); |
| 84 |
| 85 return !!ElemHide.getException(filter, domain); |
| 86 } |
| 87 |
| 88 /* |
| 89 * Returns a list of domain-specific filters matching a domain |
| 90 * @param {string} [domain] |
| 91 * @returns {Array.<?Map.<Filter,boolean>>} |
| 92 */ |
| 93 function getSpecificFiltersForDomain(domain) |
| 94 { |
| 95 let filtersList = []; |
| 96 |
| 97 if (domain) |
| 98 domain = domain.toUpperCase(); |
| 99 |
| 100 while (domain) |
| 101 { |
| 102 // Note that we also push null values into the list, because |
| 103 // ElemHide.getSelectorsForDomain still needs to know if there are any |
| 104 // entries for the domain. |
| 105 let filters = filtersByDomain.get(domain); |
| 106 if (typeof filters != "undefined") |
| 107 filtersList.push(filters); |
| 108 |
| 109 let nextDot = domain.indexOf("."); |
| 110 domain = nextDot == -1 ? null : domain.substring(nextDot + 1); |
| 111 } |
| 112 |
| 113 return filtersList; |
| 114 } |
| 115 |
| 116 /** |
| 117 * Returns a list of selectors from a given list of filters that apply on a |
| 118 * domain |
| 119 * @param {string} [domain] |
| 120 * @param {Array.<?Map.<Filter,boolean>>} filtersList |
| 121 * @param {boolean} genericOnly |
| 122 * @returns {string[]} |
| 123 */ |
| 124 function getConditionalSelectorsForDomain(domain, filtersList, genericOnly) |
| 125 { |
| 126 let selectors = []; |
| 127 |
| 128 let excluded = !genericOnly ? new Set() : null; |
| 129 let domainForExceptionCheck = !genericOnly ? domain : null; |
| 130 |
| 131 // This code is a performance hot-spot, which is why we've made certain |
| 132 // micro-optimisations. Please be careful before making changes. |
| 133 for (let i = 0; i < filtersList.length; i++) |
| 134 { |
| 135 if (!filtersList[i]) |
| 136 continue; |
| 137 |
| 138 for (let [filter, isIncluded] of filtersList[i]) |
| 139 { |
| 140 if (!isIncluded) |
| 141 { |
| 142 excluded.add(filter); |
| 143 } |
| 144 else if ((!excluded || excluded.size == 0 || !excluded.has(filter)) && |
| 145 !hasException(filter, domainForExceptionCheck)) |
| 146 { |
| 147 selectors.push(filter.selector); |
| 148 } |
| 149 } |
| 150 } |
| 151 |
| 152 return selectors; |
| 153 } |
| 154 |
68 /** | 155 /** |
69 * Returns a list of selectors that apply on each website unconditionally. | 156 * Returns a list of selectors that apply on each website unconditionally. |
70 * @returns {string[]} | 157 * @returns {string[]} |
71 */ | 158 */ |
72 function getUnconditionalSelectors() | 159 function getUnconditionalSelectors() |
73 { | 160 { |
74 if (!unconditionalSelectors) | 161 if (!unconditionalSelectors) |
75 unconditionalSelectors = [...filterBySelector.keys()]; | 162 unconditionalSelectors = [...filterBySelector.keys()]; |
76 | 163 |
77 return unconditionalSelectors; | 164 return unconditionalSelectors; |
78 } | 165 } |
79 | 166 |
80 /** | 167 /** |
81 * Container for element hiding filters | 168 * Container for element hiding filters |
82 * @class | 169 * @class |
83 */ | 170 */ |
84 let ElemHide = exports.ElemHide = { | 171 let ElemHide = exports.ElemHide = { |
85 /** | 172 /** |
86 * Removes all known filters | 173 * Removes all known filters |
87 */ | 174 */ |
88 clear() | 175 clear() |
89 { | 176 { |
90 for (let collection of [filtersByDomain, filterBySelector, | 177 for (let collection of [filtersByDomain, filterBySelector, |
91 knownFilters, exceptions]) | 178 knownFilters, exceptions, |
| 179 genericExceptions]) |
92 { | 180 { |
93 collection.clear(); | 181 collection.clear(); |
94 } | 182 } |
95 unconditionalSelectors = null; | 183 unconditionalSelectors = null; |
96 FilterNotifier.emit("elemhideupdate"); | 184 FilterNotifier.emit("elemhideupdate"); |
97 }, | 185 }, |
98 | 186 |
99 _addToFiltersByDomain(filter) | 187 _addToFiltersByDomain(filter) |
100 { | 188 { |
101 let domains = filter.domains || defaultDomains; | 189 let domains = filter.domains || defaultDomains; |
102 for (let [domain, isIncluded] of domains) | 190 if (filter instanceof ElemHideException) |
103 { | 191 { |
104 // There's no need to note that a filter is generically disabled. | 192 for (let domain of domains.keys()) |
105 if (!isIncluded && domain == "") | 193 { |
106 continue; | 194 // Add an entry for each domain, but without any filters. This makes |
| 195 // the domain "known" and helps us avoid certain optimizations that |
| 196 // would otherwise yield incorrect results. |
| 197 if (domain != "" && !filtersByDomain.has(domain)) |
| 198 filtersByDomain.set(domain, null); |
| 199 } |
| 200 } |
| 201 else |
| 202 { |
| 203 for (let [domain, isIncluded] of domains) |
| 204 { |
| 205 // There's no need to note that a filter is generically disabled. |
| 206 if (!isIncluded && domain == "") |
| 207 continue; |
107 | 208 |
108 let filters = filtersByDomain.get(domain); | 209 let filters = filtersByDomain.get(domain); |
109 if (!filters) | 210 if (!filters) |
110 filtersByDomain.set(domain, filters = new Map()); | 211 filtersByDomain.set(domain, filters = new Map()); |
111 filters.set(filter, isIncluded); | 212 filters.set(filter, isIncluded); |
| 213 } |
112 } | 214 } |
113 }, | 215 }, |
114 | 216 |
115 /** | 217 /** |
116 * Add a new element hiding filter | 218 * Add a new element hiding filter |
117 * @param {ElemHideBase} filter | 219 * @param {ElemHideBase} filter |
118 */ | 220 */ |
119 add(filter) | 221 add(filter) |
120 { | 222 { |
121 if (knownFilters.has(filter)) | 223 if (knownFilters.has(filter)) |
122 return; | 224 return; |
123 | 225 |
124 if (filter instanceof ElemHideException) | 226 if (filter instanceof ElemHideException) |
125 { | 227 { |
126 let {selector} = filter; | 228 let {selector, domains} = filter; |
| 229 |
127 let list = exceptions.get(selector); | 230 let list = exceptions.get(selector); |
128 if (list) | 231 if (list) |
129 list.push(filter); | 232 list.push(filter); |
130 else | 233 else |
131 exceptions.set(selector, [filter]); | 234 exceptions.set(selector, [filter]); |
132 | 235 |
| 236 if (domains) |
| 237 this._addToFiltersByDomain(filter); |
| 238 |
| 239 if (filter.isGeneric()) |
| 240 { |
| 241 list = genericExceptions.get(selector); |
| 242 if (list) |
| 243 list.push(filter); |
| 244 else |
| 245 genericExceptions.set(selector, [filter]); |
| 246 } |
| 247 |
133 // If this is the first exception for a previously unconditionally | 248 // If this is the first exception for a previously unconditionally |
134 // applied element hiding selector we need to take care to update the | 249 // applied element hiding selector we need to take care to update the |
135 // lookups. | 250 // lookups. |
136 let unconditionalFilterForSelector = filterBySelector.get(selector); | 251 let unconditionalFilterForSelector = filterBySelector.get(selector); |
137 if (unconditionalFilterForSelector) | 252 if (unconditionalFilterForSelector) |
138 { | 253 { |
139 this._addToFiltersByDomain(unconditionalFilterForSelector); | 254 this._addToFiltersByDomain(unconditionalFilterForSelector); |
140 filterBySelector.delete(selector); | 255 filterBySelector.delete(selector); |
141 unconditionalSelectors = null; | 256 unconditionalSelectors = null; |
142 } | 257 } |
(...skipping 23 matching lines...) Expand all Loading... |
166 if (!knownFilters.has(filter)) | 281 if (!knownFilters.has(filter)) |
167 return; | 282 return; |
168 | 283 |
169 // Whitelisting filters | 284 // Whitelisting filters |
170 if (filter instanceof ElemHideException) | 285 if (filter instanceof ElemHideException) |
171 { | 286 { |
172 let list = exceptions.get(filter.selector); | 287 let list = exceptions.get(filter.selector); |
173 let index = list.indexOf(filter); | 288 let index = list.indexOf(filter); |
174 if (index >= 0) | 289 if (index >= 0) |
175 list.splice(index, 1); | 290 list.splice(index, 1); |
| 291 |
| 292 if (filter.isGeneric()) |
| 293 { |
| 294 list = genericExceptions.get(filter.selector); |
| 295 index = list.indexOf(filter); |
| 296 if (index >= 0) |
| 297 list.splice(index, 1); |
| 298 |
| 299 if (list.length == 0) |
| 300 genericExceptions.delete(filter.selector); |
| 301 } |
176 } | 302 } |
177 // Unconditially applied element hiding filters | 303 // Unconditially applied element hiding filters |
178 else if (filterBySelector.get(filter.selector) == filter) | 304 else if (filterBySelector.get(filter.selector) == filter) |
179 { | 305 { |
180 filterBySelector.delete(filter.selector); | 306 filterBySelector.delete(filter.selector); |
181 unconditionalSelectors = null; | 307 unconditionalSelectors = null; |
182 } | 308 } |
183 // Conditionally applied element hiding filters | 309 // Conditionally applied element hiding filters |
184 else | 310 else |
185 { | 311 { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 * on a particular host name. | 373 * on a particular host name. |
248 * @param {string} domain | 374 * @param {string} domain |
249 * @param {number} [criteria] | 375 * @param {number} [criteria] |
250 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or | 376 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or |
251 * ElemHide.SPECIFIC_ONLY. | 377 * ElemHide.SPECIFIC_ONLY. |
252 * @returns {string[]} | 378 * @returns {string[]} |
253 * List of selectors. | 379 * List of selectors. |
254 */ | 380 */ |
255 getSelectorsForDomain(domain, criteria = ElemHide.ALL_MATCHING) | 381 getSelectorsForDomain(domain, criteria = ElemHide.ALL_MATCHING) |
256 { | 382 { |
257 let selectors = []; | 383 let specificOnly = criteria >= ElemHide.SPECIFIC_ONLY; |
| 384 let filtersList = getSpecificFiltersForDomain(domain); |
258 | 385 |
259 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); | 386 let genericOnly = filtersList.length == 0; |
260 let excluded = new Set(); | |
261 let currentDomain = domain ? domain.toUpperCase() : ""; | |
262 | 387 |
263 // This code is a performance hot-spot, which is why we've made certain | 388 if (!specificOnly) |
264 // micro-optimisations. Please be careful before making changes. | 389 filtersList.push(filtersByDomain.get("")); |
265 while (true) | |
266 { | |
267 if (specificOnly && currentDomain == "") | |
268 break; | |
269 | 390 |
270 let filters = filtersByDomain.get(currentDomain); | 391 let selectors = getConditionalSelectorsForDomain(domain, filtersList, |
271 if (filters) | 392 genericOnly); |
272 { | |
273 for (let [filter, isIncluded] of filters) | |
274 { | |
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 } | |
293 | 393 |
294 if (criteria < ElemHide.NO_UNCONDITIONAL) | 394 if (criteria < ElemHide.NO_UNCONDITIONAL) |
295 selectors = getUnconditionalSelectors().concat(selectors); | 395 selectors = getUnconditionalSelectors().concat(selectors); |
296 | 396 |
297 return selectors; | 397 return selectors; |
298 } | 398 } |
299 }; | 399 }; |
OLD | NEW |