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 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 */ | 71 */ |
72 let genericExceptions = new Map(); | 72 let genericExceptions = new Map(); |
73 | 73 |
74 /** | 74 /** |
75 * List of selectors that apply on any unknown domain | 75 * List of selectors that apply on any unknown domain |
76 * @type {?string[]} | 76 * @type {?string[]} |
77 */ | 77 */ |
78 let conditionalGenericSelectors = null; | 78 let conditionalGenericSelectors = null; |
79 | 79 |
80 /** | 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 /** | |
88 * Adds a filter to the lookup table of filters by domain. | 81 * Adds a filter to the lookup table of filters by domain. |
89 * @param {Filter} filter | 82 * @param {Filter} filter |
90 */ | 83 */ |
91 function addToFiltersByDomain(filter) | 84 function addToFiltersByDomain(filter) |
92 { | 85 { |
93 let domains = filter.domains || defaultDomains; | 86 let domains = filter.domains || defaultDomains; |
94 if (filter instanceof ElemHideException) | 87 if (filter instanceof ElemHideException) |
95 { | 88 { |
96 for (let domain of domains.keys()) | 89 for (let domain of domains.keys()) |
97 { | 90 { |
(...skipping 13 matching lines...) Expand all Loading... |
111 continue; | 104 continue; |
112 | 105 |
113 let filters = filtersByDomain.get(domain); | 106 let filters = filtersByDomain.get(domain); |
114 if (!filters) | 107 if (!filters) |
115 filtersByDomain.set(domain, filters = new Map()); | 108 filtersByDomain.set(domain, filters = new Map()); |
116 filters.set(filter, isIncluded); | 109 filters.set(filter, isIncluded); |
117 } | 110 } |
118 } | 111 } |
119 } | 112 } |
120 | 113 |
121 | |
122 /** | |
123 * Checks whether a filter applies on a domain | |
124 * @param {Filter} filter | |
125 * @param {string} [domain] | |
126 * @param {Set.<Filter>} excludeSet | |
127 * @returns {boolean} | |
128 */ | |
129 function doesFilterApply(filter, domain, excludeSet) | |
130 { | |
131 return (excludeSet.size == 0 || !excludeSet.has(filter)) && | |
132 !exports.ElemHide.getException(filter, domain); | |
133 } | |
134 | |
135 /** | 114 /** |
136 * Returns a list of domain-specific filters matching a domain | 115 * Returns a list of domain-specific filters matching a domain |
137 * @param {string} [domain] | 116 * @param {string} [domain] |
138 * @returns {Array.<{domain: string, filters: ?Map.<Filter,boolean>}>} | 117 * @returns {Array.<?Map.<Filter,boolean>>} |
139 */ | 118 */ |
140 function getSpecificFiltersForDomain(domain) | 119 function getSpecificFiltersForDomain(domain) |
141 { | 120 { |
142 let filtersList = []; | 121 let filtersList = []; |
143 | 122 |
144 if (domain) | 123 if (domain) |
145 domain = domain.toUpperCase(); | 124 domain = domain.toUpperCase(); |
146 | 125 |
147 while (domain) | 126 while (domain) |
148 { | 127 { |
149 let filters = filtersByDomain.get(domain); | 128 let filters = filtersByDomain.get(domain); |
150 if (typeof filters != "undefined") | 129 if (typeof filters != "undefined") |
151 filtersList.push({domain, filters}); | 130 filtersList.push(filters); |
152 | 131 |
153 let nextDot = domain.indexOf("."); | 132 let nextDot = domain.indexOf("."); |
154 domain = nextDot == -1 ? null : domain.substring(nextDot + 1); | 133 domain = nextDot == -1 ? null : domain.substring(nextDot + 1); |
155 } | 134 } |
156 | 135 |
157 return filtersList; | 136 return filtersList; |
158 } | 137 } |
159 | 138 |
160 /** | 139 /** |
161 * Returns a list of selectors that apply on a domain from a given list of | 140 * Returns a list of selectors that apply on a domain from a given list of |
162 * filters | 141 * filters |
163 * @param {string} [domain] | 142 * @param {string} [domain] |
164 * @param {Array.<{domain: string, filters: ?Map.<Filter,boolean>}>} filtersList | 143 * @param {Array.<?Map.<Filter,boolean>>} filtersList |
165 * @param {Set.<Filter>} excludeSet | 144 * @param {Set.<Filter>} excludeSet |
166 * @returns {string[]} | 145 * @returns {string[]} |
167 */ | 146 */ |
168 function matchSelectors(domain, filtersList, excludeSet) | 147 function matchSelectors(domain, filtersList, excludeSet) |
169 { | 148 { |
170 let matches = []; | 149 let matches = []; |
171 | 150 |
172 // This code is a performance hot-spot, which is why we've made certain | 151 // This code is a performance hot-spot, which is why we've made certain |
173 // micro-optimisations. Please be careful before making changes. | 152 // micro-optimisations. Please be careful before making changes. |
174 for (let i = 0; i < filtersList.length; i++) | 153 for (let i = 0; i < filtersList.length; i++) |
175 { | 154 { |
176 let {filters} = filtersList[i]; | 155 let filters = filtersList[i]; |
177 if (filters) | 156 if (filters) |
178 { | 157 { |
179 for (let [filter, isIncluded] of filters) | 158 for (let [filter, isIncluded] of filters) |
180 { | 159 { |
181 if (!isIncluded) | 160 if (!isIncluded) |
| 161 { |
182 excludeSet.add(filter); | 162 excludeSet.add(filter); |
183 else if (doesFilterApply(filter, domain, excludeSet)) | 163 } |
| 164 else if ((excludeSet.size == 0 || !excludeSet.has(filter)) && |
| 165 !exports.ElemHide.getException(filter, domain)) |
| 166 { |
184 matches.push(filter.selector); | 167 matches.push(filter.selector); |
| 168 } |
185 } | 169 } |
186 } | 170 } |
187 } | 171 } |
188 | 172 |
189 return matches; | 173 return matches; |
190 } | |
191 | |
192 /** | |
193 * Returns a list of selectors that apply on a domain | |
194 * @param {string} [domain] | |
195 * @param {boolean} specificOnly | |
196 * @returns {string[]} | |
197 */ | |
198 function getConditionalSelectorsForDomain(domain, specificOnly) | |
199 { | |
200 let specificFilters = getSpecificFiltersForDomain(domain); | |
201 | |
202 // If there are no specific filters (nor any specific exceptions), we can | |
203 // just return the selectors from all the generic filters modulo any generic | |
204 // exceptions. | |
205 if (specificFilters.length == 0) | |
206 return specificOnly ? [] : getConditionalGenericSelectors(); | |
207 | |
208 let excludeSet = new Set(); | |
209 let specificSelectors = matchSelectors(domain, specificFilters, excludeSet); | |
210 | |
211 if (specificOnly) | |
212 return specificSelectors; | |
213 | |
214 // We use the longest subdomain of this domain found in our data structures | |
215 // as the key to check if the domain is "generic friendly." For example, | |
216 // given foo.example.com, there may be an entry for example.com in our data | |
217 // structures (e.g. "example.com###foo"), so we use that subdomain as the | |
218 // key. This way we make only one entry and it works for all subdomains of | |
219 // example.com, except those that have specific entries | |
220 // (e.g. "~bar.example.com##.no-bar"). | |
221 let domainKey = specificFilters[0].domain; | |
222 | |
223 if (genericFriendlyDomains.has(domainKey)) | |
224 return specificSelectors.concat(getConditionalGenericSelectors()); | |
225 | |
226 let genericFilters = [{filters: filtersByDomain.get("")}]; | |
227 let genericSelectors = matchSelectors(domain, genericFilters, excludeSet); | |
228 | |
229 // If the number of conditional generic selectors that apply on this domain | |
230 // is the same as the total number of conditional generic selectors, the | |
231 // domain is "generic friendly" (i.e. all generic filters apply, except those | |
232 // with generic exceptions). In that case, we mark it is as such for faster | |
233 // lookups. | |
234 if (genericSelectors.length == (conditionalGenericSelectors || {}).length) | |
235 genericFriendlyDomains.add(domainKey); | |
236 | |
237 return specificSelectors.concat(genericSelectors); | |
238 } | 174 } |
239 | 175 |
240 /** | 176 /** |
241 * Returns a list of selectors that apply on any unknown domain | 177 * Returns a list of selectors that apply on any unknown domain |
242 * @returns {string[]} | 178 * @returns {string[]} |
243 */ | 179 */ |
244 function getConditionalGenericSelectors() | 180 function getConditionalGenericSelectors() |
245 { | 181 { |
246 if (conditionalGenericSelectors) | 182 if (conditionalGenericSelectors) |
247 return conditionalGenericSelectors; | 183 return conditionalGenericSelectors; |
(...skipping 30 matching lines...) Expand all Loading... |
278 * @class | 214 * @class |
279 */ | 215 */ |
280 exports.ElemHide = { | 216 exports.ElemHide = { |
281 /** | 217 /** |
282 * Removes all known filters | 218 * Removes all known filters |
283 */ | 219 */ |
284 clear() | 220 clear() |
285 { | 221 { |
286 for (let collection of [filtersByDomain, filterBySelector, | 222 for (let collection of [filtersByDomain, filterBySelector, |
287 knownFilters, exceptions, | 223 knownFilters, exceptions, |
288 genericExceptions, genericFriendlyDomains]) | 224 genericExceptions]) |
289 { | 225 { |
290 collection.clear(); | 226 collection.clear(); |
291 } | 227 } |
292 unconditionalSelectors = null; | 228 unconditionalSelectors = null; |
293 conditionalGenericSelectors = null; | 229 conditionalGenericSelectors = null; |
294 FilterNotifier.emit("elemhideupdate"); | 230 FilterNotifier.emit("elemhideupdate"); |
295 }, | 231 }, |
296 | 232 |
297 /** | 233 /** |
298 * Add a new element hiding filter | 234 * Add a new element hiding filter |
299 * @param {ElemHideBase} filter | 235 * @param {ElemHideBase} filter |
300 */ | 236 */ |
301 add(filter) | 237 add(filter) |
302 { | 238 { |
303 if (knownFilters.has(filter)) | 239 if (knownFilters.has(filter)) |
304 return; | 240 return; |
305 | 241 |
306 conditionalGenericSelectors = null; | 242 conditionalGenericSelectors = null; |
307 genericFriendlyDomains.clear(); | |
308 | 243 |
309 if (filter instanceof ElemHideException) | 244 if (filter instanceof ElemHideException) |
310 { | 245 { |
311 let {selector, domains} = filter; | 246 let {selector, domains} = filter; |
312 | 247 |
313 let list = exceptions.get(selector); | 248 let list = exceptions.get(selector); |
314 if (list) | 249 if (list) |
315 list.push(filter); | 250 list.push(filter); |
316 else | 251 else |
317 exceptions.set(selector, [filter]); | 252 exceptions.set(selector, [filter]); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 /** | 293 /** |
359 * Removes an element hiding filter | 294 * Removes an element hiding filter |
360 * @param {ElemHideBase} filter | 295 * @param {ElemHideBase} filter |
361 */ | 296 */ |
362 remove(filter) | 297 remove(filter) |
363 { | 298 { |
364 if (!knownFilters.has(filter)) | 299 if (!knownFilters.has(filter)) |
365 return; | 300 return; |
366 | 301 |
367 conditionalGenericSelectors = null; | 302 conditionalGenericSelectors = null; |
368 genericFriendlyDomains.clear(); | |
369 | 303 |
370 // Whitelisting filters | 304 // Whitelisting filters |
371 if (filter instanceof ElemHideException) | 305 if (filter instanceof ElemHideException) |
372 { | 306 { |
373 let list = exceptions.get(filter.selector); | 307 let list = exceptions.get(filter.selector); |
374 let index = list.indexOf(filter); | 308 let index = list.indexOf(filter); |
375 if (index >= 0) | 309 if (index >= 0) |
376 list.splice(index, 1); | 310 list.splice(index, 1); |
377 | 311 |
378 if (filter.isGeneric()) | 312 if (filter.isGeneric()) |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 | 368 |
435 /** | 369 /** |
436 * Determines from the current filter list which selectors should be applied | 370 * Determines from the current filter list which selectors should be applied |
437 * on a particular host name. | 371 * on a particular host name. |
438 * @param {string} domain | 372 * @param {string} domain |
439 * @param {boolean} [specificOnly] true if generic filters should not apply. | 373 * @param {boolean} [specificOnly] true if generic filters should not apply. |
440 * @returns {string[]} List of selectors. | 374 * @returns {string[]} List of selectors. |
441 */ | 375 */ |
442 getSelectorsForDomain(domain, specificOnly = false) | 376 getSelectorsForDomain(domain, specificOnly = false) |
443 { | 377 { |
444 let selectors = getConditionalSelectorsForDomain(domain, specificOnly); | 378 let specificFilters = getSpecificFiltersForDomain(domain); |
445 | 379 |
446 if (!specificOnly) | 380 // If there are no specific filters (nor any specific exceptions), we can |
447 selectors = getUnconditionalSelectors().concat(selectors); | 381 // just return the selectors from all the generic filters modulo any |
448 | 382 // generic exceptions. |
449 return selectors; | 383 if (specificFilters.length == 0) |
| 384 { |
| 385 return specificOnly ? [] : |
| 386 getUnconditionalSelectors() |
| 387 .concat(getConditionalGenericSelectors()); |
| 388 } |
| 389 |
| 390 let excluded = new Set(); |
| 391 let selectors = matchSelectors(domain, specificFilters, excluded); |
| 392 |
| 393 if (specificOnly) |
| 394 return selectors; |
| 395 |
| 396 return getUnconditionalSelectors() |
| 397 .concat(selectors, |
| 398 matchSelectors(domain, [filtersByDomain.get("")], |
| 399 excluded)); |
450 } | 400 } |
451 }; | 401 }; |
LEFT | RIGHT |