Left: | ||
Right: |
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[]>} |
kzar
2018/05/15 13:26:31
I guess you need to rebase again.
Manish Jethani
2018/05/15 16:00:08
Done.
| |
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; | |
kzar
2018/05/15 13:26:31
I think this cache is probably a good idea. I figu
Manish Jethani
2018/05/15 16:00:07
It also applies to known domains (see my other com
| |
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); | |
kzar
2018/05/15 13:26:30
Am I missing something or do we never remove the d
Manish Jethani
2018/05/15 16:00:08
You're right, we never remove a domain like that f
kzar
2018/05/18 14:34:53
Acknowledged.
| |
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) | |
Manish Jethani
2018/05/15 16:20:59
Note: getSpecificFiltersForDomain could be reused
kzar
2018/05/18 14:34:53
Acknowledged.
| |
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 from a given list of | |
161 * filters | |
162 * @param {string} [domain] | |
163 * @param {Array.<{domain: string, filters: ?Map.<Filter,boolean>}>} | |
164 * @param {Set.<Filter>} excludeSet | |
165 * @returns {string[]} | |
166 */ | |
167 function matchSelectors(domain, filtersList, excludeSet) | |
Manish Jethani
2018/05/15 16:20:59
Note: matchSelectors could be reused by ElemHideEm
| |
168 { | |
169 let matches = []; | |
170 | |
171 // This code is a performance hot-spot, which is why we've made certain | |
172 // micro-optimisations. Please be careful before making changes. | |
173 for (let i = 0; i < filtersList.length; i++) | |
174 { | |
175 let filters = filtersList[i].filters; | |
176 if (filters) | |
177 { | |
178 for (let [filter, isIncluded] of filters) | |
179 { | |
180 if (!isIncluded) | |
181 excludeSet.add(filter); | |
182 else if (doesFilterApply(filter, domain, excludeSet)) | |
183 matches.push(filter.selector); | |
184 } | |
185 } | |
186 } | |
187 | |
188 return matches; | |
189 } | |
190 | |
191 /** | |
192 * Returns a list of selectors that apply on a domain | |
193 * @param {string} [domain] | |
194 * @param {boolean} specificOnly | |
195 * @returns {string[]} | |
196 */ | |
197 function getConditionalSelectorsForDomain(domain, specificOnly) | |
198 { | |
199 let specificFilters = getSpecificFiltersForDomain(domain); | |
200 | |
201 // If there are no specific filters (nor any specific exceptions), we can | |
202 // just return the selectors from all the generic filters modulo any generic | |
203 // exceptions. | |
204 if (specificFilters.length == 0) | |
205 return specificOnly ? [] : getConditionalGenericSelectors(); | |
kzar
2018/05/18 14:34:53
Nit: Seems a waste to create a new empty Array whe
| |
206 | |
207 let excludeSet = new Set(); | |
208 let specificSelectors = matchSelectors(domain, specificFilters, excludeSet); | |
209 | |
210 if (specificOnly) | |
211 return specificSelectors; | |
212 | |
213 // We use the longest subdomain of this domain found in our data structures | |
214 // as the key to check if the domain is "generic friendly." For example, | |
215 // given foo.example.com, there may be an entry for example.com in our data | |
216 // structures (e.g. "example.com###foo"), so we use that subdomain as the | |
217 // key. This way we make only one entry and it works for all subdomains of | |
218 // example.com, except those that have specific entries | |
219 // (e.g. "~bar.example.com##.no-bar"). | |
220 let domainKey = specificFilters[0].domain; | |
221 | |
222 if (genericFriendlyDomains.has(domainKey)) | |
223 return specificSelectors.concat(getConditionalGenericSelectors()); | |
224 | |
225 let genericFilters = [{filters: filtersByDomain.get("")}]; | |
226 let genericSelectors = matchSelectors(domain, genericFilters, excludeSet); | |
227 | |
228 // If the number of conditional generic selectors that apply on this domain | |
229 // is the same as the total number of conditional generic selectors, the | |
230 // domain is "generic friendly" (i.e. all generic filters apply, except those | |
231 // with generic exceptions). In that case, we mark it is as such for faster | |
232 // lookups. | |
233 if (genericSelectors.length == (conditionalGenericSelectors || {}).length) | |
234 genericFriendlyDomains.add(domainKey); | |
235 | |
236 return specificSelectors.concat(genericSelectors); | |
237 } | |
238 | |
239 /** | |
240 * Returns a list of selectors that apply on any unknown domain | |
241 * @returns {string[]} | |
242 */ | |
243 function getConditionalGenericSelectors() | |
244 { | |
245 if (conditionalGenericSelectors) | |
246 return conditionalGenericSelectors; | |
247 | |
248 conditionalGenericSelectors = []; | |
249 | |
250 let filters = filtersByDomain.get(""); | |
251 if (!filters) | |
252 return conditionalGenericSelectors; | |
253 | |
254 for (let {selector} of filters.keys()) | |
255 { | |
256 if (genericExceptions.size == 0 || !genericExceptions.has(selector)) | |
257 conditionalGenericSelectors.push(selector); | |
258 } | |
259 | |
260 return conditionalGenericSelectors; | |
86 } | 261 } |
87 | 262 |
88 /** | 263 /** |
89 * Returns a list of selectors that apply on each website unconditionally. | 264 * Returns a list of selectors that apply on each website unconditionally. |
90 * @returns {string[]} | 265 * @returns {string[]} |
91 */ | 266 */ |
92 function getUnconditionalSelectors() | 267 function getUnconditionalSelectors() |
93 { | 268 { |
94 if (!unconditionalSelectors) | 269 if (!unconditionalSelectors) |
95 unconditionalSelectors = [...filterBySelector.keys()]; | 270 unconditionalSelectors = [...filterBySelector.keys()]; |
96 | 271 |
97 return unconditionalSelectors; | 272 return unconditionalSelectors; |
98 } | 273 } |
99 | 274 |
100 /** | 275 /** |
101 * Container for element hiding filters | 276 * Container for element hiding filters |
102 * @class | 277 * @class |
103 */ | 278 */ |
104 let ElemHide = exports.ElemHide = { | 279 let ElemHide = exports.ElemHide = { |
105 /** | 280 /** |
106 * Removes all known filters | 281 * Removes all known filters |
107 */ | 282 */ |
108 clear() | 283 clear() |
109 { | 284 { |
110 for (let collection of [filtersByDomain, filterBySelector, | 285 for (let collection of [filtersByDomain, filterBySelector, |
111 knownFilters, exceptions]) | 286 knownFilters, exceptions, |
287 genericExceptions, genericFriendlyDomains]) | |
112 { | 288 { |
113 collection.clear(); | 289 collection.clear(); |
114 } | 290 } |
115 unconditionalSelectors = null; | 291 unconditionalSelectors = null; |
292 conditionalGenericSelectors = null; | |
116 FilterNotifier.emit("elemhideupdate"); | 293 FilterNotifier.emit("elemhideupdate"); |
117 }, | 294 }, |
118 | 295 |
119 /** | 296 /** |
120 * Add a new element hiding filter | 297 * Add a new element hiding filter |
121 * @param {ElemHideBase} filter | 298 * @param {ElemHideBase} filter |
122 */ | 299 */ |
123 add(filter) | 300 add(filter) |
124 { | 301 { |
125 if (knownFilters.has(filter)) | 302 if (knownFilters.has(filter)) |
126 return; | 303 return; |
127 | 304 |
305 conditionalGenericSelectors = null; | |
306 genericFriendlyDomains.clear(); | |
307 | |
128 if (filter instanceof ElemHideException) | 308 if (filter instanceof ElemHideException) |
129 { | 309 { |
130 let {selector} = filter; | 310 let {selector, domains} = filter; |
311 | |
131 let list = exceptions.get(selector); | 312 let list = exceptions.get(selector); |
132 if (list) | 313 if (list) |
133 list.push(filter); | 314 list.push(filter); |
134 else | 315 else |
135 exceptions.set(selector, [filter]); | 316 exceptions.set(selector, [filter]); |
136 | 317 |
318 if (domains) | |
319 addToFiltersByDomain(filter); | |
320 | |
321 if (filter.isGeneric()) | |
322 { | |
323 list = genericExceptions.get(selector); | |
324 if (list) | |
325 list.push(filter); | |
326 else | |
327 genericExceptions.set(selector, [filter]); | |
328 } | |
329 | |
137 // If this is the first exception for a previously unconditionally | 330 // If this is the first exception for a previously unconditionally |
138 // applied element hiding selector we need to take care to update the | 331 // applied element hiding selector we need to take care to update the |
139 // lookups. | 332 // lookups. |
140 let unconditionalFilterForSelector = filterBySelector.get(selector); | 333 let unconditionalFilterForSelector = filterBySelector.get(selector); |
141 if (unconditionalFilterForSelector) | 334 if (unconditionalFilterForSelector) |
142 { | 335 { |
143 addToFiltersByDomain(unconditionalFilterForSelector); | 336 addToFiltersByDomain(unconditionalFilterForSelector); |
144 filterBySelector.delete(selector); | 337 filterBySelector.delete(selector); |
145 unconditionalSelectors = null; | 338 unconditionalSelectors = null; |
146 } | 339 } |
(...skipping 16 matching lines...) Expand all Loading... | |
163 | 356 |
164 /** | 357 /** |
165 * Removes an element hiding filter | 358 * Removes an element hiding filter |
166 * @param {ElemHideBase} filter | 359 * @param {ElemHideBase} filter |
167 */ | 360 */ |
168 remove(filter) | 361 remove(filter) |
169 { | 362 { |
170 if (!knownFilters.has(filter)) | 363 if (!knownFilters.has(filter)) |
171 return; | 364 return; |
172 | 365 |
366 conditionalGenericSelectors = null; | |
367 genericFriendlyDomains.clear(); | |
368 | |
173 // Whitelisting filters | 369 // Whitelisting filters |
174 if (filter instanceof ElemHideException) | 370 if (filter instanceof ElemHideException) |
175 { | 371 { |
176 let list = exceptions.get(filter.selector); | 372 let list = exceptions.get(filter.selector); |
177 let index = list.indexOf(filter); | 373 let index = list.indexOf(filter); |
178 if (index >= 0) | 374 if (index >= 0) |
179 list.splice(index, 1); | 375 list.splice(index, 1); |
376 | |
377 if (filter.isGeneric()) | |
378 { | |
379 list = genericExceptions.get(filter.selector); | |
380 index = list.indexOf(filter); | |
381 if (index >= 0) | |
382 list.splice(index, 1); | |
383 | |
384 // It's important to delete the entry here so the selector no longer | |
385 // appears to have any generic exceptions. | |
386 if (list.length == 0) | |
387 genericExceptions.delete(filter.selector); | |
388 } | |
180 } | 389 } |
181 // Unconditially applied element hiding filters | 390 // Unconditially applied element hiding filters |
182 else if (filterBySelector.get(filter.selector) == filter) | 391 else if (filterBySelector.get(filter.selector) == filter) |
183 { | 392 { |
184 filterBySelector.delete(filter.selector); | 393 filterBySelector.delete(filter.selector); |
185 unconditionalSelectors = null; | 394 unconditionalSelectors = null; |
186 } | 395 } |
187 // Conditionally applied element hiding filters | 396 // Conditionally applied element hiding filters |
188 else | 397 else |
189 { | 398 { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
251 * on a particular host name. | 460 * on a particular host name. |
252 * @param {string} domain | 461 * @param {string} domain |
253 * @param {number} [criteria] | 462 * @param {number} [criteria] |
254 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or | 463 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or |
255 * ElemHide.SPECIFIC_ONLY. | 464 * ElemHide.SPECIFIC_ONLY. |
256 * @returns {string[]} | 465 * @returns {string[]} |
257 * List of selectors. | 466 * List of selectors. |
258 */ | 467 */ |
259 getSelectorsForDomain(domain, criteria = ElemHide.ALL_MATCHING) | 468 getSelectorsForDomain(domain, criteria = ElemHide.ALL_MATCHING) |
260 { | 469 { |
261 let selectors = []; | 470 let specificOnly = criteria >= ElemHide.SPECIFIC_ONLY; |
262 | 471 let selectors = getConditionalSelectorsForDomain(domain, specificOnly); |
kzar
2018/05/15 13:26:31
I feel like some of these changes (like this one,
Manish Jethani
2018/05/15 16:00:08
Yeah this patch has been updated a lot. Now the fu
Manish Jethani
2018/05/15 16:16:15
Actually there are three return statements (out of
Manish Jethani
2018/05/15 16:20:59
I meant getConditionalSelectorsForDomain of course
| |
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 == "") | |
kzar
2018/05/15 13:26:30
Why don't we instead keep track of if filtersByDom
Manish Jethani
2018/05/15 16:00:08
filtersByDomain.has(domain) will be true for ~11,0
kzar
2018/05/18 14:34:53
Alright, how about this, perhaps a dumb idea but h
Manish Jethani
2018/05/18 17:18:26
If I understanding the suggestion correctly, you'r
| |
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 | 472 |
298 if (criteria < ElemHide.NO_UNCONDITIONAL) | 473 if (criteria < ElemHide.NO_UNCONDITIONAL) |
299 selectors = getUnconditionalSelectors().concat(selectors); | 474 selectors = getUnconditionalSelectors().concat(selectors); |
475 else if (criteria == ElemHide.NO_UNCONDITIONAL) | |
476 selectors = selectors.slice(); | |
300 | 477 |
301 return selectors; | 478 return selectors; |
302 } | 479 } |
303 }; | 480 }; |
OLD | NEW |