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 * Domains that are known not to be specifically excluded from any generic |
| 82 * filters |
| 83 * @type {Set.<string>} |
| 84 */ |
| 85 let genericFriendlyDomains = new Set(); |
| 86 |
| 87 /** |
69 * Adds a filter to the lookup table of filters by domain. | 88 * Adds a filter to the lookup table of filters by domain. |
70 * @param {Filter} filter | 89 * @param {Filter} filter |
71 */ | 90 */ |
72 function addToFiltersByDomain(filter) | 91 function addToFiltersByDomain(filter) |
73 { | 92 { |
74 let domains = filter.domains || defaultDomains; | 93 let domains = filter.domains || defaultDomains; |
75 for (let [domain, isIncluded] of domains) | 94 if (filter instanceof ElemHideException) |
76 { | 95 { |
77 // There's no need to note that a filter is generically disabled. | 96 for (let domain of domains.keys()) |
78 if (!isIncluded && domain == "") | 97 { |
79 continue; | 98 // Add an entry for each domain, but without any filters. This makes |
80 | 99 // the domain "known" and helps us avoid certain optimizations that |
| 100 // would otherwise yield incorrect results. |
| 101 if (domain != "" && !filtersByDomain.has(domain)) |
| 102 filtersByDomain.set(domain, null); |
| 103 } |
| 104 } |
| 105 else |
| 106 { |
| 107 for (let [domain, isIncluded] of domains) |
| 108 { |
| 109 // There's no need to note that a filter is generically disabled. |
| 110 if (!isIncluded && domain == "") |
| 111 continue; |
| 112 |
| 113 let filters = filtersByDomain.get(domain); |
| 114 if (!filters) |
| 115 filtersByDomain.set(domain, filters = new Map()); |
| 116 filters.set(filter, isIncluded); |
| 117 } |
| 118 } |
| 119 } |
| 120 |
| 121 /** |
| 122 * Checks whether a filter applies on a domain |
| 123 * @param {Filter} filter |
| 124 * @param {string} [domain] |
| 125 * @param {Set.<Filter>} excludeSet |
| 126 * @returns {boolean} |
| 127 */ |
| 128 function doesFilterApply(filter, domain, excludeSet) |
| 129 { |
| 130 return (excludeSet.size == 0 || !excludeSet.has(filter)) && |
| 131 !ElemHide.getException(filter, domain); |
| 132 } |
| 133 |
| 134 /** |
| 135 * Returns a list of domain-specific filters matching a domain |
| 136 * @param {string} [domain] |
| 137 * @returns {Array.<{domain: string, filters: ?Map.<Filter,boolean>}>} |
| 138 */ |
| 139 function getSpecificFiltersForDomain(domain) |
| 140 { |
| 141 let filtersList = []; |
| 142 |
| 143 if (domain) |
| 144 domain = domain.toUpperCase(); |
| 145 |
| 146 while (domain) |
| 147 { |
81 let filters = filtersByDomain.get(domain); | 148 let filters = filtersByDomain.get(domain); |
82 if (!filters) | 149 if (typeof filters != "undefined") |
83 filtersByDomain.set(domain, filters = new Map()); | 150 filtersList.push({domain, filters}); |
84 filters.set(filter, isIncluded); | 151 |
85 } | 152 let nextDot = domain.indexOf("."); |
| 153 domain = nextDot == -1 ? null : domain.substring(nextDot + 1); |
| 154 } |
| 155 |
| 156 return filtersList; |
| 157 } |
| 158 |
| 159 /** |
| 160 * Returns a list of selectors that apply on a domain |
| 161 * @param {string} [domain] |
| 162 * @param {boolean} specificOnly |
| 163 * @returns {string[]} |
| 164 */ |
| 165 function getConditionalSelectorsForDomain(domain, specificOnly) |
| 166 { |
| 167 let specificFilters = getSpecificFiltersForDomain(domain); |
| 168 |
| 169 // If there are no specific filters (nor any specific exceptions), we can |
| 170 // just return the selectors from all the generic filters modulo any generic |
| 171 // exceptions. |
| 172 if (specificFilters.length == 0) |
| 173 return specificOnly ? [] : getConditionalGenericSelectors(); |
| 174 |
| 175 let specificSelectors = []; |
| 176 |
| 177 let excludeSet = new Set(); |
| 178 |
| 179 // This code is a performance hot-spot, which is why we've made certain |
| 180 // micro-optimisations. Please be careful before making changes. |
| 181 for (let i = 0; i < specificFilters.length; i++) |
| 182 { |
| 183 for (let [filter, isIncluded] of specificFilters[i].filters || []) |
| 184 { |
| 185 if (!isIncluded) |
| 186 excludeSet.add(filter); |
| 187 else if (doesFilterApply(filter, domain, excludeSet)) |
| 188 specificSelectors.push(filter.selector); |
| 189 } |
| 190 } |
| 191 |
| 192 if (specificOnly) |
| 193 return specificSelectors; |
| 194 |
| 195 // We use the longest subdomain of this domain found in our data structures |
| 196 // as the key to check if the domain is "generic friendly." For example, |
| 197 // given foo.example.com, there may be an entry for example.com in our data |
| 198 // structures (e.g. "example.com###foo"), so we use that subdomain as the |
| 199 // key. This way we make only one entry and it works for all subdomains of |
| 200 // example.com, except those that have specific entries |
| 201 // (e.g. "~bar.example.com##.no-bar"). |
| 202 let domainKey = specificFilters[0].domain; |
| 203 |
| 204 if (genericFriendlyDomains.has(domainKey)) |
| 205 return specificSelectors.concat(getConditionalGenericSelectors()); |
| 206 |
| 207 let genericFilters = filtersByDomain.get(""); |
| 208 if (!genericFilters) |
| 209 return specificSelectors; |
| 210 |
| 211 let genericSelectors = []; |
| 212 |
| 213 for (let filter of genericFilters.keys()) |
| 214 { |
| 215 if (doesFilterApply(filter, domain, excludeSet)) |
| 216 genericSelectors.push(filter.selector); |
| 217 } |
| 218 |
| 219 // If the number of conditional generic selectors that apply on this domain |
| 220 // is the same as the total number of conditional generic selectors, the |
| 221 // domain is "generic friendly" (i.e. all generic filters apply, except those |
| 222 // with generic exceptions). In that case, we mark it is as such for faster |
| 223 // lookups. |
| 224 if (genericSelectors.length == (conditionalGenericSelectors || {}).length) |
| 225 genericFriendlyDomains.add(domainKey); |
| 226 |
| 227 return specificSelectors.concat(genericSelectors); |
| 228 } |
| 229 |
| 230 /** |
| 231 * Returns a list of selectors that apply on any unknown domain |
| 232 * @returns {string[]} |
| 233 */ |
| 234 function getConditionalGenericSelectors() |
| 235 { |
| 236 if (conditionalGenericSelectors) |
| 237 return conditionalGenericSelectors; |
| 238 |
| 239 conditionalGenericSelectors = []; |
| 240 |
| 241 let filters = filtersByDomain.get(""); |
| 242 if (!filters) |
| 243 return conditionalGenericSelectors; |
| 244 |
| 245 for (let {selector} of filters.keys()) |
| 246 { |
| 247 if (genericExceptions.size == 0 || !genericExceptions.has(selector)) |
| 248 conditionalGenericSelectors.push(selector); |
| 249 } |
| 250 |
| 251 return conditionalGenericSelectors; |
86 } | 252 } |
87 | 253 |
88 /** | 254 /** |
89 * Returns a list of selectors that apply on each website unconditionally. | 255 * Returns a list of selectors that apply on each website unconditionally. |
90 * @returns {string[]} | 256 * @returns {string[]} |
91 */ | 257 */ |
92 function getUnconditionalSelectors() | 258 function getUnconditionalSelectors() |
93 { | 259 { |
94 if (!unconditionalSelectors) | 260 if (!unconditionalSelectors) |
95 unconditionalSelectors = [...filterBySelector.keys()]; | 261 unconditionalSelectors = [...filterBySelector.keys()]; |
96 | 262 |
97 return unconditionalSelectors; | 263 return unconditionalSelectors; |
98 } | 264 } |
99 | 265 |
100 /** | 266 /** |
101 * Container for element hiding filters | 267 * Container for element hiding filters |
102 * @class | 268 * @class |
103 */ | 269 */ |
104 let ElemHide = exports.ElemHide = { | 270 let ElemHide = exports.ElemHide = { |
105 /** | 271 /** |
106 * Removes all known filters | 272 * Removes all known filters |
107 */ | 273 */ |
108 clear() | 274 clear() |
109 { | 275 { |
110 for (let collection of [filtersByDomain, filterBySelector, | 276 for (let collection of [filtersByDomain, filterBySelector, |
111 knownFilters, exceptions]) | 277 knownFilters, exceptions, |
| 278 genericExceptions, genericFriendlyDomains]) |
112 { | 279 { |
113 collection.clear(); | 280 collection.clear(); |
114 } | 281 } |
115 unconditionalSelectors = null; | 282 unconditionalSelectors = null; |
| 283 conditionalGenericSelectors = null; |
116 FilterNotifier.emit("elemhideupdate"); | 284 FilterNotifier.emit("elemhideupdate"); |
117 }, | 285 }, |
118 | 286 |
119 /** | 287 /** |
120 * Add a new element hiding filter | 288 * Add a new element hiding filter |
121 * @param {ElemHideBase} filter | 289 * @param {ElemHideBase} filter |
122 */ | 290 */ |
123 add(filter) | 291 add(filter) |
124 { | 292 { |
125 if (knownFilters.has(filter)) | 293 if (knownFilters.has(filter)) |
126 return; | 294 return; |
127 | 295 |
| 296 conditionalGenericSelectors = null; |
| 297 genericFriendlyDomains.clear(); |
| 298 |
128 if (filter instanceof ElemHideException) | 299 if (filter instanceof ElemHideException) |
129 { | 300 { |
130 let {selector} = filter; | 301 let {selector, domains} = filter; |
| 302 |
131 let list = exceptions.get(selector); | 303 let list = exceptions.get(selector); |
132 if (list) | 304 if (list) |
133 list.push(filter); | 305 list.push(filter); |
134 else | 306 else |
135 exceptions.set(selector, [filter]); | 307 exceptions.set(selector, [filter]); |
136 | 308 |
| 309 if (domains) |
| 310 addToFiltersByDomain(filter); |
| 311 |
| 312 if (filter.isGeneric()) |
| 313 { |
| 314 list = genericExceptions.get(selector); |
| 315 if (list) |
| 316 list.push(filter); |
| 317 else |
| 318 genericExceptions.set(selector, [filter]); |
| 319 } |
| 320 |
137 // If this is the first exception for a previously unconditionally | 321 // If this is the first exception for a previously unconditionally |
138 // applied element hiding selector we need to take care to update the | 322 // applied element hiding selector we need to take care to update the |
139 // lookups. | 323 // lookups. |
140 let unconditionalFilterForSelector = filterBySelector.get(selector); | 324 let unconditionalFilterForSelector = filterBySelector.get(selector); |
141 if (unconditionalFilterForSelector) | 325 if (unconditionalFilterForSelector) |
142 { | 326 { |
143 addToFiltersByDomain(unconditionalFilterForSelector); | 327 addToFiltersByDomain(unconditionalFilterForSelector); |
144 filterBySelector.delete(selector); | 328 filterBySelector.delete(selector); |
145 unconditionalSelectors = null; | 329 unconditionalSelectors = null; |
146 } | 330 } |
(...skipping 16 matching lines...) Expand all Loading... |
163 | 347 |
164 /** | 348 /** |
165 * Removes an element hiding filter | 349 * Removes an element hiding filter |
166 * @param {ElemHideBase} filter | 350 * @param {ElemHideBase} filter |
167 */ | 351 */ |
168 remove(filter) | 352 remove(filter) |
169 { | 353 { |
170 if (!knownFilters.has(filter)) | 354 if (!knownFilters.has(filter)) |
171 return; | 355 return; |
172 | 356 |
| 357 conditionalGenericSelectors = null; |
| 358 genericFriendlyDomains.clear(); |
| 359 |
173 // Whitelisting filters | 360 // Whitelisting filters |
174 if (filter instanceof ElemHideException) | 361 if (filter instanceof ElemHideException) |
175 { | 362 { |
176 let list = exceptions.get(filter.selector); | 363 let list = exceptions.get(filter.selector); |
177 let index = list.indexOf(filter); | 364 let index = list.indexOf(filter); |
178 if (index >= 0) | 365 if (index >= 0) |
179 list.splice(index, 1); | 366 list.splice(index, 1); |
| 367 |
| 368 if (filter.isGeneric()) |
| 369 { |
| 370 list = genericExceptions.get(filter.selector); |
| 371 index = list.indexOf(filter); |
| 372 if (index >= 0) |
| 373 list.splice(index, 1); |
| 374 |
| 375 // It's important to delete the entry here so the selector no longer |
| 376 // appears to have any generic exceptions. |
| 377 if (list.length == 0) |
| 378 genericExceptions.delete(filter.selector); |
| 379 } |
180 } | 380 } |
181 // Unconditially applied element hiding filters | 381 // Unconditially applied element hiding filters |
182 else if (filterBySelector.get(filter.selector) == filter) | 382 else if (filterBySelector.get(filter.selector) == filter) |
183 { | 383 { |
184 filterBySelector.delete(filter.selector); | 384 filterBySelector.delete(filter.selector); |
185 unconditionalSelectors = null; | 385 unconditionalSelectors = null; |
186 } | 386 } |
187 // Conditionally applied element hiding filters | 387 // Conditionally applied element hiding filters |
188 else | 388 else |
189 { | 389 { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 * on a particular host name. | 451 * on a particular host name. |
252 * @param {string} domain | 452 * @param {string} domain |
253 * @param {number} [criteria] | 453 * @param {number} [criteria] |
254 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or | 454 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or |
255 * ElemHide.SPECIFIC_ONLY. | 455 * ElemHide.SPECIFIC_ONLY. |
256 * @returns {string[]} | 456 * @returns {string[]} |
257 * List of selectors. | 457 * List of selectors. |
258 */ | 458 */ |
259 getSelectorsForDomain(domain, criteria = ElemHide.ALL_MATCHING) | 459 getSelectorsForDomain(domain, criteria = ElemHide.ALL_MATCHING) |
260 { | 460 { |
261 let selectors = []; | 461 let specificOnly = criteria >= ElemHide.SPECIFIC_ONLY; |
262 | 462 let selectors = getConditionalSelectorsForDomain(domain, specificOnly); |
263 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); | |
264 let excluded = new Set(); | |
265 let currentDomain = domain ? domain.toUpperCase() : ""; | |
266 | |
267 // This code is a performance hot-spot, which is why we've made certain | |
268 // micro-optimisations. Please be careful before making changes. | |
269 while (true) | |
270 { | |
271 if (specificOnly && currentDomain == "") | |
272 break; | |
273 | |
274 let filters = filtersByDomain.get(currentDomain); | |
275 if (filters) | |
276 { | |
277 for (let [filter, isIncluded] of filters) | |
278 { | |
279 if (!isIncluded) | |
280 { | |
281 excluded.add(filter); | |
282 } | |
283 else if ((excluded.size == 0 || !excluded.has(filter)) && | |
284 !this.getException(filter, domain)) | |
285 { | |
286 selectors.push(filter.selector); | |
287 } | |
288 } | |
289 } | |
290 | |
291 if (currentDomain == "") | |
292 break; | |
293 | |
294 let nextDot = currentDomain.indexOf("."); | |
295 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | |
296 } | |
297 | 463 |
298 if (criteria < ElemHide.NO_UNCONDITIONAL) | 464 if (criteria < ElemHide.NO_UNCONDITIONAL) |
299 selectors = getUnconditionalSelectors().concat(selectors); | 465 selectors = getUnconditionalSelectors().concat(selectors); |
| 466 else if (criteria == ElemHide.NO_UNCONDITIONAL) |
| 467 selectors = selectors.slice(); |
300 | 468 |
301 return selectors; | 469 return selectors; |
302 } | 470 } |
303 }; | 471 }; |
OLD | NEW |