Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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>>} |
kzar
2018/04/20 18:52:38
I am unsure about this one. Previously we needed t
Manish Jethani
2018/04/21 11:11:57
Unless I'm missing something, filtersByDomain is o
kzar
2018/04/21 12:01:00
On 2018/04/21 11:11:57, Manish Jethani wrote:
Manish Jethani
2018/04/22 11:35:42
Sorry, I meant getSelectorForDomain there. getSele
kzar
2018/04/26 12:04:18
OK, I read back through the old discussions to ref
| |
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 |
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 */ | 45 */ |
46 let unconditionalSelectors = null; | 46 let unconditionalSelectors = null; |
47 | 47 |
48 /** | 48 /** |
49 * Map to be used instead when a filter has a blank domains property. | 49 * Map to be used instead when a filter has a blank domains property. |
50 */ | 50 */ |
51 let defaultDomains = new Map([["", true]]); | 51 let defaultDomains = new Map([["", true]]); |
52 | 52 |
53 /** | 53 /** |
54 * Set containing known element hiding exceptions | 54 * Set containing known element hiding and exception filters |
55 * @type {Set.<string>} | 55 * @type {Set.<ElemHideBase>} |
56 */ | 56 */ |
57 let knownExceptions = new Set(); | 57 let knownFilters = new Set(); |
58 | 58 |
59 /** | 59 /** |
60 * Lookup table, lists of element hiding exceptions by selector | 60 * Lookup table, lists of element hiding exceptions by selector |
61 * @type {Map.<string,Filter>} | 61 * @type {Map.<string,Filter>} |
62 */ | 62 */ |
63 let exceptions = new Map(); | 63 let exceptions = new Map(); |
64 | 64 |
65 /** | 65 /** |
66 * Container for element hiding filters | 66 * Container for element hiding filters |
67 * @class | 67 * @class |
68 */ | 68 */ |
69 let ElemHide = exports.ElemHide = { | 69 let ElemHide = exports.ElemHide = { |
70 /** | 70 /** |
71 * Removes all known filters | 71 * Removes all known filters |
72 */ | 72 */ |
73 clear() | 73 clear() |
74 { | 74 { |
75 for (let collection of [filtersByDomain, filterBySelector, | 75 for (let collection of [filtersByDomain, filterBySelector, |
76 knownExceptions, exceptions]) | 76 knownFilters, exceptions]) |
77 { | 77 { |
78 collection.clear(); | 78 collection.clear(); |
79 } | 79 } |
80 unconditionalSelectors = null; | 80 unconditionalSelectors = null; |
81 FilterNotifier.emit("elemhideupdate"); | 81 FilterNotifier.emit("elemhideupdate"); |
82 }, | 82 }, |
83 | 83 |
84 _addToFiltersByDomain(filter) | 84 _addToFiltersByDomain(filter) |
85 { | 85 { |
86 let domains = filter.domains || defaultDomains; | 86 let domains = filter.domains || defaultDomains; |
87 for (let [domain, isIncluded] of domains) | 87 for (let [domain, isIncluded] of domains) |
88 { | 88 { |
89 // There's no need to note that a filter is generically disabled. | |
90 if (!isIncluded && domain == "") | |
91 continue; | |
92 | |
89 let filters = filtersByDomain.get(domain); | 93 let filters = filtersByDomain.get(domain); |
90 if (!filters) | 94 if (!filters) |
91 filtersByDomain.set(domain, filters = new Map()); | 95 filtersByDomain.set(domain, filters = new Map()); |
92 filters.set(filter, isIncluded); | 96 filters.set(filter, isIncluded); |
93 } | 97 } |
94 }, | 98 }, |
95 | 99 |
96 /** | 100 /** |
97 * Add a new element hiding filter | 101 * Add a new element hiding filter |
98 * @param {ElemHideFilter} filter | 102 * @param {ElemHideBase} filter |
99 */ | 103 */ |
100 add(filter) | 104 add(filter) |
101 { | 105 { |
106 if (knownFilters.has(filter)) | |
107 return; | |
108 | |
102 if (filter instanceof ElemHideException) | 109 if (filter instanceof ElemHideException) |
103 { | 110 { |
104 if (knownExceptions.has(filter.text)) | |
105 return; | |
106 | |
107 let {selector} = filter; | 111 let {selector} = filter; |
108 let list = exceptions.get(selector); | 112 let list = exceptions.get(selector); |
109 if (list) | 113 if (list) |
110 list.push(filter); | 114 list.push(filter); |
111 else | 115 else |
112 exceptions.set(selector, [filter]); | 116 exceptions.set(selector, [filter]); |
113 | 117 |
114 // If this is the first exception for a previously unconditionally | 118 // If this is the first exception for a previously unconditionally |
115 // applied element hiding selector we need to take care to update the | 119 // applied element hiding selector we need to take care to update the |
116 // lookups. | 120 // lookups. |
117 let unconditionalFilterForSelector = filterBySelector.get(selector); | 121 let unconditionalFilterForSelector = filterBySelector.get(selector); |
118 if (unconditionalFilterForSelector) | 122 if (unconditionalFilterForSelector) |
119 { | 123 { |
120 this._addToFiltersByDomain(unconditionalFilterForSelector); | 124 this._addToFiltersByDomain(unconditionalFilterForSelector); |
121 filterBySelector.delete(selector); | 125 filterBySelector.delete(selector); |
122 unconditionalSelectors = null; | 126 unconditionalSelectors = null; |
123 } | 127 } |
124 | |
125 knownExceptions.add(filter.text); | |
126 } | 128 } |
127 else if (!(filter.domains || exceptions.has(filter.selector))) | 129 else if (!(filter.domains || exceptions.has(filter.selector))) |
128 { | 130 { |
129 // The new filter's selector is unconditionally applied to all domains | 131 // The new filter's selector is unconditionally applied to all domains |
130 filterBySelector.set(filter.selector, filter); | 132 filterBySelector.set(filter.selector, filter); |
131 unconditionalSelectors = null; | 133 unconditionalSelectors = null; |
132 } | 134 } |
133 else | 135 else |
134 { | 136 { |
135 // The new filter's selector only applies to some domains | 137 // The new filter's selector only applies to some domains |
136 this._addToFiltersByDomain(filter); | 138 this._addToFiltersByDomain(filter); |
137 } | 139 } |
138 | 140 |
141 knownFilters.add(filter); | |
139 FilterNotifier.emit("elemhideupdate"); | 142 FilterNotifier.emit("elemhideupdate"); |
140 }, | 143 }, |
141 | 144 |
142 /** | 145 /** |
143 * Removes an element hiding filter | 146 * Removes an element hiding filter |
144 * @param {ElemHideFilter} filter | 147 * @param {ElemHideBase} filter |
145 */ | 148 */ |
146 remove(filter) | 149 remove(filter) |
147 { | 150 { |
151 if (!knownFilters.has(filter)) | |
152 return; | |
153 | |
154 // Whitelisting filters | |
148 if (filter instanceof ElemHideException) | 155 if (filter instanceof ElemHideException) |
149 { | 156 { |
150 if (!knownExceptions.has(filter.text)) | |
151 return; | |
152 | |
153 let list = exceptions.get(filter.selector); | 157 let list = exceptions.get(filter.selector); |
154 let index = list.indexOf(filter); | 158 let index = list.indexOf(filter); |
155 if (index >= 0) | 159 if (index >= 0) |
156 list.splice(index, 1); | 160 list.splice(index, 1); |
157 knownExceptions.delete(filter.text); | 161 } |
158 } | 162 // Unconditially applied element hiding filters |
163 else if (filterBySelector.get(filter.selector) == filter) | |
164 { | |
165 filterBySelector.delete(filter.selector); | |
166 unconditionalSelectors = null; | |
167 } | |
168 // Conditionally applied element hiding filters | |
159 else | 169 else |
160 { | 170 { |
161 if (filterBySelector.get(filter.selector) === filter) | |
162 { | |
163 filterBySelector.delete(filter.selector); | |
164 unconditionalSelectors = null; | |
165 return; | |
kzar
2018/04/26 12:04:18
Note: I think this was a bug, we wouldn't emit the
| |
166 } | |
167 | |
168 // We haven't found this filter in unconditional filters, look in | |
169 // filtersByDomain. | |
170 let domains = filter.domains || defaultDomains; | 171 let domains = filter.domains || defaultDomains; |
171 for (let domain of domains.keys()) | 172 for (let domain of domains.keys()) |
172 { | 173 { |
173 let filters = filtersByDomain.get(domain); | 174 let filters = filtersByDomain.get(domain); |
174 if (filters) | 175 if (filters) |
175 filters.delete(filter); | 176 filters.delete(filter); |
176 } | 177 } |
177 } | 178 } |
178 | 179 |
180 knownFilters.delete(filter); | |
179 FilterNotifier.emit("elemhideupdate"); | 181 FilterNotifier.emit("elemhideupdate"); |
180 }, | 182 }, |
181 | 183 |
182 /** | 184 /** |
183 * Checks whether an exception rule is registered for a filter on a particular | 185 * Checks whether an exception rule is registered for a filter on a particular |
184 * domain. | 186 * domain. |
185 * @param {Filter} filter | 187 * @param {Filter} filter |
186 * @param {string} docDomain | 188 * @param {string} docDomain |
187 * @return {ElemHideException} | 189 * @return {ElemHideException} |
188 */ | 190 */ |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 getSelectorsForDomain(domain, criteria) | 245 getSelectorsForDomain(domain, criteria) |
244 { | 246 { |
245 let selectors = []; | 247 let selectors = []; |
246 | 248 |
247 if (typeof criteria == "undefined") | 249 if (typeof criteria == "undefined") |
248 criteria = ElemHide.ALL_MATCHING; | 250 criteria = ElemHide.ALL_MATCHING; |
249 if (criteria < ElemHide.NO_UNCONDITIONAL) | 251 if (criteria < ElemHide.NO_UNCONDITIONAL) |
250 selectors = this.getUnconditionalSelectors(); | 252 selectors = this.getUnconditionalSelectors(); |
251 | 253 |
252 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); | 254 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); |
253 let seenFilters = new Set(); | 255 let excluded = new Set(); |
254 let currentDomain = domain ? domain.toUpperCase() : ""; | 256 let currentDomain = domain ? domain.toUpperCase() : ""; |
257 | |
258 // This code is a performance hot-spot, which is why we've made certain | |
259 // micro-optimisations. Please be careful before making changes. | |
255 while (true) | 260 while (true) |
256 { | 261 { |
257 if (specificOnly && currentDomain == "") | 262 if (specificOnly && currentDomain == "") |
258 break; | 263 break; |
259 | 264 |
260 let filters = filtersByDomain.get(currentDomain); | 265 let filters = filtersByDomain.get(currentDomain); |
261 if (filters) | 266 if (filters) |
262 { | 267 { |
263 for (let [filter, isIncluded] of filters) | 268 for (let [filter, isIncluded] of filters) |
264 { | 269 { |
265 if (seenFilters.has(filter)) | 270 if (!isIncluded) |
266 continue; | 271 { |
267 seenFilters.add(filter); | 272 excluded.add(filter); |
268 | 273 } |
269 if (isIncluded && !this.getException(filter, domain)) | 274 else if ((excluded.size == 0 || !excluded.has(filter)) && |
275 !this.getException(filter, domain)) | |
276 { | |
270 selectors.push(filter.selector); | 277 selectors.push(filter.selector); |
278 } | |
271 } | 279 } |
272 } | 280 } |
273 | 281 |
274 if (currentDomain == "") | 282 if (currentDomain == "") |
275 break; | 283 break; |
276 | 284 |
277 let nextDot = currentDomain.indexOf("."); | 285 let nextDot = currentDomain.indexOf("."); |
278 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | 286 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); |
279 } | 287 } |
280 | 288 |
281 return selectors; | 289 return selectors; |
282 } | 290 } |
283 }; | 291 }; |
LEFT | RIGHT |