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 19 matching lines...) Expand all Loading... |
30 /** | 30 /** |
31 * The maximum number of selectors in a CSS rule. This is used by | 31 * The maximum number of selectors in a CSS rule. This is used by |
32 * <code>{@link createStyleSheet}</code> to split up a long list of selectors | 32 * <code>{@link createStyleSheet}</code> to split up a long list of selectors |
33 * into multiple rules. | 33 * into multiple rules. |
34 * @const {number} | 34 * @const {number} |
35 * @default | 35 * @default |
36 */ | 36 */ |
37 const selectorGroupSize = 1024; | 37 const selectorGroupSize = 1024; |
38 | 38 |
39 /** | 39 /** |
40 * Lookup table, active flag, by filter by domain. | 40 * Lookup table, selector by filter text by domain. |
41 * (Only contains filters that aren't unconditionally matched for all domains.) | 41 * (Only contains filter text for filters that aren't unconditionally matched |
42 * @type {Map.<string,Map.<Filter,boolean>>} | 42 * for all domains.) |
43 */ | 43 * @type {Map.<string,Map.<string,string>>} |
44 let filtersByDomain = new Map(); | 44 */ |
45 | 45 let filterTextByDomain = new Map(); |
46 /** | 46 |
47 * Lookup table, filter by selector. (Only used for selectors that are | 47 /** |
| 48 * Lookup table, filter text by selector. (Only used for selectors that are |
48 * unconditionally matched for all domains.) | 49 * unconditionally matched for all domains.) |
49 * @type {Map.<string,Filter>} | 50 * @type {Map.<string,string>} |
50 */ | 51 */ |
51 let filterBySelector = new Map(); | 52 let filterTextBySelector = new Map(); |
52 | 53 |
53 /** | 54 /** |
54 * This array caches the keys of filterBySelector table (selectors | 55 * This array caches the keys of filterTextBySelector table (selectors |
55 * which unconditionally apply on all domains). It will be null if the | 56 * which unconditionally apply on all domains). It will be null if the |
56 * cache needs to be rebuilt. | 57 * cache needs to be rebuilt. |
57 * @type {?string[]} | 58 * @type {?string[]} |
58 */ | 59 */ |
59 let unconditionalSelectors = null; | 60 let unconditionalSelectors = null; |
60 | 61 |
61 /** | 62 /** |
62 * The default style sheet that applies on all domains. This is based on the | 63 * The default style sheet that applies on all domains. This is based on the |
63 * value of <code>{@link unconditionalSelectors}</code>. | 64 * value of <code>{@link unconditionalSelectors}</code>. |
64 * @type {?string} | 65 * @type {?string} |
(...skipping 18 matching lines...) Expand all Loading... |
83 let styleSheetCache = new Cache(100); | 84 let styleSheetCache = new Cache(100); |
84 | 85 |
85 /** | 86 /** |
86 * Map to be used instead when a filter has a blank domains property. | 87 * Map to be used instead when a filter has a blank domains property. |
87 * @type {Map.<string,boolean>} | 88 * @type {Map.<string,boolean>} |
88 * @const | 89 * @const |
89 */ | 90 */ |
90 let defaultDomains = new Map([["", true]]); | 91 let defaultDomains = new Map([["", true]]); |
91 | 92 |
92 /** | 93 /** |
93 * Set containing known element hiding filters | 94 * Set containing text of known element hiding filters |
94 * @type {Set.<ElemHideFilter>} | 95 * @type {Set.<string>} |
95 */ | 96 */ |
96 let knownFilters = new Set(); | 97 let knownFilterText = new Set(); |
97 | 98 |
98 /** | 99 /** |
99 * All domains known to occur in exceptions | 100 * All domains known to occur in exceptions |
100 * @type {Set.<string>} | 101 * @type {Set.<string>} |
101 */ | 102 */ |
102 let knownExceptionDomains = new Set(); | 103 let knownExceptionDomains = new Set(); |
103 | 104 |
104 /** | 105 /** |
105 * Returns the suffix of the given domain that is known. If no suffix is known, | 106 * Returns the suffix of the given domain that is known. If no suffix is known, |
106 * an empty string is returned. | 107 * an empty string is returned. |
107 * @param {?string} domain | 108 * @param {?string} domain |
108 * @returns {string} | 109 * @returns {string} |
109 */ | 110 */ |
110 function getKnownSuffix(domain) | 111 function getKnownSuffix(domain) |
111 { | 112 { |
112 while (domain && !filtersByDomain.has(domain) && | 113 while (domain && !filterTextByDomain.has(domain) && |
113 !knownExceptionDomains.has(domain)) | 114 !knownExceptionDomains.has(domain)) |
114 { | 115 { |
115 let index = domain.indexOf("."); | 116 let index = domain.indexOf("."); |
116 domain = index == -1 ? "" : domain.substring(index + 1); | 117 domain = index == -1 ? "" : domain.substring(index + 1); |
117 } | 118 } |
118 | 119 |
119 return domain; | 120 return domain; |
120 } | 121 } |
121 | 122 |
122 /** | 123 /** |
123 * Adds a filter to the lookup table of filters by domain. | 124 * Adds a filter to the lookup table of filters by domain. |
124 * @param {Filter} filter | 125 * @param {Filter} filter |
125 * @param {?Map.<string,boolean>} [domains] | 126 * @param {?Map.<string,boolean>} [domains] |
126 */ | 127 */ |
127 function addToFiltersByDomain(filter, domains = filter.domains) | 128 function addToFiltersByDomain(filter, domains = filter.domains) |
128 { | 129 { |
129 for (let [domain, isIncluded] of domains || defaultDomains) | 130 for (let [domain, isIncluded] of domains || defaultDomains) |
130 { | 131 { |
131 // There's no need to note that a filter is generically disabled. | 132 // There's no need to note that a filter is generically disabled. |
132 if (!isIncluded && domain == "") | 133 if (!isIncluded && domain == "") |
133 continue; | 134 continue; |
134 | 135 |
135 let filters = filtersByDomain.get(domain); | 136 let filters = filterTextByDomain.get(domain); |
136 if (!filters) | 137 if (!filters) |
137 filtersByDomain.set(domain, filters = new Map()); | 138 filterTextByDomain.set(domain, filters = new Map()); |
138 filters.set(filter.text, isIncluded ? filter.selector : null); | 139 filters.set(filter.text, isIncluded ? filter.selector : null); |
139 } | 140 } |
140 } | 141 } |
141 | 142 |
142 /** | 143 /** |
143 * Returns a list of selectors that apply on each website unconditionally. | 144 * Returns a list of selectors that apply on each website unconditionally. |
144 * @returns {string[]} | 145 * @returns {string[]} |
145 */ | 146 */ |
146 function getUnconditionalSelectors() | 147 function getUnconditionalSelectors() |
147 { | 148 { |
148 if (!unconditionalSelectors) | 149 if (!unconditionalSelectors) |
149 unconditionalSelectors = [...filterBySelector.keys()]; | 150 unconditionalSelectors = [...filterTextBySelector.keys()]; |
150 | 151 |
151 return unconditionalSelectors; | 152 return unconditionalSelectors; |
152 } | 153 } |
153 | 154 |
154 /** | 155 /** |
155 * Returns the list of selectors that apply on a given domain from the subset | 156 * Returns the list of selectors that apply on a given domain from the subset |
156 * of filters that do not apply unconditionally on all domains. | 157 * of filters that do not apply unconditionally on all domains. |
157 * | 158 * |
158 * @param {string} domain The domain. | 159 * @param {string} domain The domain. |
159 * @param {boolean} specificOnly Whether selectors from generic filters should | 160 * @param {boolean} specificOnly Whether selectors from generic filters should |
160 * be included. | 161 * be included. |
161 * | 162 * |
162 * @returns {Array.<string>} The list of selectors. | 163 * @returns {Array.<string>} The list of selectors. |
163 */ | 164 */ |
164 function getConditionalSelectors(domain, specificOnly) | 165 function getConditionalSelectors(domain, specificOnly) |
165 { | 166 { |
166 let selectors = []; | 167 let selectors = []; |
167 | 168 |
168 let excluded = new Set(); | 169 let excluded = new Set(); |
169 | 170 |
170 for (let currentDomain of domainSuffixes(domain, !specificOnly)) | 171 for (let currentDomain of domainSuffixes(domain, !specificOnly)) |
171 { | 172 { |
172 let filters = filtersByDomain.get(currentDomain); | 173 let filters = filterTextByDomain.get(currentDomain); |
173 if (filters) | 174 if (filters) |
174 { | 175 { |
175 for (let [text, selector] of filters) | 176 for (let [text, selector] of filters) |
176 { | 177 { |
177 if (!selector) | 178 if (!selector) |
178 { | 179 { |
179 excluded.add(text); | 180 excluded.add(text); |
180 } | 181 } |
181 else if ((excluded.size == 0 || !excluded.has(text)) && | 182 else if ((excluded.size == 0 || !excluded.has(text)) && |
182 !ElemHideExceptions.getException(selector, domain)) | 183 !ElemHideExceptions.getException(selector, domain)) |
(...skipping 21 matching lines...) Expand all Loading... |
204 */ | 205 */ |
205 function getConditionalSelectorsWithExceptions(domain, specificOnly) | 206 function getConditionalSelectorsWithExceptions(domain, specificOnly) |
206 { | 207 { |
207 let selectors = []; | 208 let selectors = []; |
208 let exceptions = []; | 209 let exceptions = []; |
209 | 210 |
210 let excluded = new Set(); | 211 let excluded = new Set(); |
211 | 212 |
212 for (let currentDomain of domainSuffixes(domain, !specificOnly)) | 213 for (let currentDomain of domainSuffixes(domain, !specificOnly)) |
213 { | 214 { |
214 let filters = filtersByDomain.get(currentDomain); | 215 let filters = filterTextByDomain.get(currentDomain); |
215 if (filters) | 216 if (filters) |
216 { | 217 { |
217 for (let [text, selector] of filters) | 218 for (let [text, selector] of filters) |
218 { | 219 { |
219 if (!selector) | 220 if (!selector) |
220 { | 221 { |
221 excluded.add(text); | 222 excluded.add(text); |
222 } | 223 } |
223 else if (excluded.size == 0 || !excluded.has(text)) | 224 else if (excluded.size == 0 || !excluded.has(text)) |
224 { | 225 { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 // Note: Once an exception domain is known it never becomes unknown, even | 294 // Note: Once an exception domain is known it never becomes unknown, even |
294 // when all the exceptions containing that domain are removed. This is a | 295 // when all the exceptions containing that domain are removed. This is a |
295 // best-case optimization. | 296 // best-case optimization. |
296 if (domain != "") | 297 if (domain != "") |
297 knownExceptionDomains.add(domain); | 298 knownExceptionDomains.add(domain); |
298 } | 299 } |
299 } | 300 } |
300 | 301 |
301 // If this is the first exception for a previously unconditionally applied | 302 // If this is the first exception for a previously unconditionally applied |
302 // element hiding selector we need to take care to update the lookups. | 303 // element hiding selector we need to take care to update the lookups. |
303 let unconditionalFilterForSelector = filterBySelector.get(selector); | 304 let unconditionalFilterForSelector = filterTextBySelector.get(selector); |
304 if (unconditionalFilterForSelector) | 305 if (unconditionalFilterForSelector) |
305 { | 306 { |
306 addToFiltersByDomain(Filter.fromText(unconditionalFilterForSelector, | 307 addToFiltersByDomain(Filter.fromText(unconditionalFilterForSelector, |
307 false)); | 308 false)); |
308 filterBySelector.delete(selector); | 309 filterTextBySelector.delete(selector); |
309 unconditionalSelectors = null; | 310 unconditionalSelectors = null; |
310 defaultStyleSheet = null; | 311 defaultStyleSheet = null; |
311 } | 312 } |
312 }); | 313 }); |
313 | 314 |
314 /** | 315 /** |
315 * Container for element hiding filters | 316 * Container for element hiding filters |
316 * @class | 317 * @class |
317 */ | 318 */ |
318 exports.ElemHide = { | 319 exports.ElemHide = { |
319 /** | 320 /** |
320 * Removes all known filters | 321 * Removes all known filters |
321 */ | 322 */ |
322 clear() | 323 clear() |
323 { | 324 { |
324 commonStyleSheet = null; | 325 commonStyleSheet = null; |
325 | 326 |
326 for (let collection of [styleSheetCache, filtersByDomain, filterBySelector, | 327 for (let collection of [styleSheetCache, filterTextByDomain, |
327 knownFilters, knownExceptionDomains]) | 328 filterTextBySelector, knownFilterText, |
| 329 knownExceptionDomains]) |
328 { | 330 { |
329 collection.clear(); | 331 collection.clear(); |
330 } | 332 } |
331 | 333 |
332 unconditionalSelectors = null; | 334 unconditionalSelectors = null; |
333 defaultStyleSheet = null; | 335 defaultStyleSheet = null; |
334 | 336 |
335 filterNotifier.emit("elemhideupdate"); | 337 filterNotifier.emit("elemhideupdate"); |
336 }, | 338 }, |
337 | 339 |
338 /** | 340 /** |
339 * Add a new element hiding filter | 341 * Add a new element hiding filter |
340 * @param {ElemHideFilter} filter | 342 * @param {ElemHideFilter} filter |
341 */ | 343 */ |
342 add(filter) | 344 add(filter) |
343 { | 345 { |
344 let {text} = filter; | 346 let {text} = filter; |
345 | 347 |
346 if (knownFilters.has(text)) | 348 if (knownFilterText.has(text)) |
347 return; | 349 return; |
348 | 350 |
349 styleSheetCache.clear(); | 351 styleSheetCache.clear(); |
350 commonStyleSheet = null; | 352 commonStyleSheet = null; |
351 | 353 |
352 let {domains, selector} = filter; | 354 let {domains, selector} = filter; |
353 | 355 |
354 if (!(domains || ElemHideExceptions.hasExceptions(selector))) | 356 if (!(domains || ElemHideExceptions.hasExceptions(selector))) |
355 { | 357 { |
356 // The new filter's selector is unconditionally applied to all domains | 358 // The new filter's selector is unconditionally applied to all domains |
357 filterBySelector.set(selector, text); | 359 filterTextBySelector.set(selector, text); |
358 unconditionalSelectors = null; | 360 unconditionalSelectors = null; |
359 defaultStyleSheet = null; | 361 defaultStyleSheet = null; |
360 } | 362 } |
361 else | 363 else |
362 { | 364 { |
363 // The new filter's selector only applies to some domains | 365 // The new filter's selector only applies to some domains |
364 addToFiltersByDomain(filter, domains); | 366 addToFiltersByDomain(filter, domains); |
365 } | 367 } |
366 | 368 |
367 knownFilters.add(text); | 369 knownFilterText.add(text); |
368 filterNotifier.emit("elemhideupdate"); | 370 filterNotifier.emit("elemhideupdate"); |
369 }, | 371 }, |
370 | 372 |
371 /** | 373 /** |
372 * Removes an element hiding filter | 374 * Removes an element hiding filter |
373 * @param {ElemHideFilter} filter | 375 * @param {ElemHideFilter} filter |
374 */ | 376 */ |
375 remove(filter) | 377 remove(filter) |
376 { | 378 { |
377 let {text} = filter; | 379 let {text} = filter; |
378 | 380 |
379 if (!knownFilters.has(text)) | 381 if (!knownFilterText.has(text)) |
380 return; | 382 return; |
381 | 383 |
382 styleSheetCache.clear(); | 384 styleSheetCache.clear(); |
383 commonStyleSheet = null; | 385 commonStyleSheet = null; |
384 | 386 |
385 let {selector} = filter; | 387 let {selector} = filter; |
386 | 388 |
387 // Unconditially applied element hiding filters | 389 // Unconditially applied element hiding filters |
388 if (filterBySelector.get(selector) == text) | 390 if (filterTextBySelector.get(selector) == text) |
389 { | 391 { |
390 filterBySelector.delete(selector); | 392 filterTextBySelector.delete(selector); |
391 unconditionalSelectors = null; | 393 unconditionalSelectors = null; |
392 defaultStyleSheet = null; | 394 defaultStyleSheet = null; |
393 } | 395 } |
394 // Conditionally applied element hiding filters | 396 // Conditionally applied element hiding filters |
395 else | 397 else |
396 { | 398 { |
397 let domains = filter.domains || defaultDomains; | 399 let domains = filter.domains || defaultDomains; |
398 for (let domain of domains.keys()) | 400 for (let domain of domains.keys()) |
399 { | 401 { |
400 let filters = filtersByDomain.get(domain); | 402 let filters = filterTextByDomain.get(domain); |
401 if (filters) | 403 if (filters) |
402 { | 404 { |
403 filters.delete(text); | 405 filters.delete(text); |
404 | 406 |
405 if (filters.size == 0) | 407 if (filters.size == 0) |
406 filtersByDomain.delete(domain); | 408 filterTextByDomain.delete(domain); |
407 } | 409 } |
408 } | 410 } |
409 } | 411 } |
410 | 412 |
411 knownFilters.delete(text); | 413 knownFilterText.delete(text); |
412 filterNotifier.emit("elemhideupdate"); | 414 filterNotifier.emit("elemhideupdate"); |
413 }, | 415 }, |
414 | 416 |
415 /** | 417 /** |
416 * @typedef {object} ElemHideStyleSheet | 418 * @typedef {object} ElemHideStyleSheet |
417 * @property {string} code CSS code. | 419 * @property {string} code CSS code. |
418 * @property {Array.<string>} selectors List of selectors. | 420 * @property {Array.<string>} selectors List of selectors. |
419 */ | 421 */ |
420 | 422 |
421 /** | 423 /** |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
578 { | 580 { |
579 let styleSheet = ""; | 581 let styleSheet = ""; |
580 | 582 |
581 for (let selectorGroup of splitSelectors(selectors)) | 583 for (let selectorGroup of splitSelectors(selectors)) |
582 styleSheet += createRule(selectorGroup); | 584 styleSheet += createRule(selectorGroup); |
583 | 585 |
584 return styleSheet; | 586 return styleSheet; |
585 } | 587 } |
586 | 588 |
587 exports.createStyleSheet = createStyleSheet; | 589 exports.createStyleSheet = createStyleSheet; |
LEFT | RIGHT |