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 {Filter} = require("./filterClasses"); | 24 const {Filter} = require("./filterClasses"); |
25 const {ElemHideExceptions} = require("./elemHideExceptions"); | 25 const {ElemHideExceptions} = require("./elemHideExceptions"); |
26 const {filterNotifier} = require("./filterNotifier"); | 26 const {filterNotifier} = require("./filterNotifier"); |
27 const {normalizeHostname, domainSuffixes} = require("./url"); | |
28 const {Cache} = require("./caching"); | |
27 | 29 |
28 /** | 30 /** |
29 * 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 |
30 * <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 |
31 * into multiple rules. | 33 * into multiple rules. |
32 * @const {number} | 34 * @const {number} |
33 * @default | 35 * @default |
34 */ | 36 */ |
35 const selectorGroupSize = 1024; | 37 const selectorGroupSize = 1024; |
36 | 38 |
37 /** | 39 /** |
38 * Lookup table, active flag, by filter by domain. | 40 * Lookup table, selector by filter text by domain. |
Manish Jethani
2018/11/03 20:01:25
Throughout this file, "filter" now means filter te
hub
2018/11/22 15:59:33
I'd update the comment to say `by filter text`
Manish Jethani
2018/11/23 02:03:33
Done.
| |
39 * (Only contains filters that aren't unconditionally matched for all domains.) | 41 * (Only contains filter text for filters that aren't unconditionally matched |
40 * @type {Map.<string,Map.<string,boolean>>} | 42 * for all domains.) |
41 */ | 43 * @type {Map.<string,Map.<string,string>>} |
42 let filtersByDomain = new Map(); | 44 */ |
43 | 45 let filterTextByDomain = new Map(); |
44 /** | 46 |
45 * 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 | |
46 * unconditionally matched for all domains.) | 49 * unconditionally matched for all domains.) |
47 * @type {Map.<string,string>} | 50 * @type {Map.<string,string>} |
48 */ | 51 */ |
49 let filterBySelector = new Map(); | 52 let filterTextBySelector = new Map(); |
50 | 53 |
51 /** | 54 /** |
52 * This array caches the keys of filterBySelector table (selectors | 55 * This array caches the keys of filterTextBySelector table (selectors |
53 * 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 |
54 * cache needs to be rebuilt. | 57 * cache needs to be rebuilt. |
55 * @type {?string[]} | 58 * @type {?string[]} |
56 */ | 59 */ |
57 let unconditionalSelectors = null; | 60 let unconditionalSelectors = null; |
58 | 61 |
59 /** | 62 /** |
60 * 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 |
61 * value of <code>{@link unconditionalSelectors}</code>. | 64 * value of <code>{@link unconditionalSelectors}</code>. |
62 * @type {?string} | 65 * @type {?string} |
63 */ | 66 */ |
64 let defaultStyleSheet = null; | 67 let defaultStyleSheet = null; |
65 | 68 |
66 /** | 69 /** |
67 * The common style sheet that applies on all unknown domains. This is a | 70 * The common style sheet that applies on all unknown domains. This is a |
68 * concatenation of the default style sheet and an additional style sheet based | 71 * concatenation of the default style sheet and an additional style sheet based |
69 * on selectors from all generic filters that are not in the | 72 * on selectors from all generic filters that are not in the |
70 * <code>{@link unconditionalSelectors}</code> list. | 73 * <code>{@link unconditionalSelectors}</code> list. |
71 * @type {?string} | 74 * @type {?string} |
72 */ | 75 */ |
73 let commonStyleSheet = null; | 76 let commonStyleSheet = null; |
74 | 77 |
75 /** | 78 /** |
79 * Cache of generated domain-specific style sheets. This contains entries for | |
80 * only known domains. If a domain is unknown, it gets | |
81 * <code>{@link commonStyleSheet}</code>. | |
82 * @type {Cache.<string, string>} | |
83 */ | |
84 let styleSheetCache = new Cache(100); | |
85 | |
86 /** | |
76 * 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. |
77 * @type {Map.<string,boolean>} | 88 * @type {Map.<string,boolean>} |
78 * @const | 89 * @const |
79 */ | 90 */ |
80 let defaultDomains = new Map([["", true]]); | 91 let defaultDomains = new Map([["", true]]); |
81 | 92 |
82 /** | 93 /** |
83 * Set containing known element hiding filters | 94 * Set containing text of known element hiding filters |
84 * @type {Set.<string>} | 95 * @type {Set.<string>} |
85 */ | 96 */ |
86 let knownFilters = new Set(); | 97 let knownFilterText = new Set(); |
87 | 98 |
88 /** | 99 /** |
89 * All domains known to occur in exceptions | 100 * All domains known to occur in exceptions |
90 * @type {Set.<string>} | 101 * @type {Set.<string>} |
91 */ | 102 */ |
92 let knownExceptionDomains = new Set(); | 103 let knownExceptionDomains = new Set(); |
93 | 104 |
94 /** | 105 /** |
95 * 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, |
96 * an empty string is returned. | 107 * an empty string is returned. |
97 * @param {?string} domain | 108 * @param {?string} domain |
98 * @returns {string} | 109 * @returns {string} |
99 */ | 110 */ |
100 function getKnownSuffix(domain) | 111 function getKnownSuffix(domain) |
101 { | 112 { |
102 while (domain && !filtersByDomain.has(domain) && | 113 while (domain && !filterTextByDomain.has(domain) && |
103 !knownExceptionDomains.has(domain)) | 114 !knownExceptionDomains.has(domain)) |
104 { | 115 { |
105 let index = domain.indexOf("."); | 116 let index = domain.indexOf("."); |
106 domain = index == -1 ? "" : domain.substring(index + 1); | 117 domain = index == -1 ? "" : domain.substring(index + 1); |
107 } | 118 } |
108 | 119 |
109 return domain; | 120 return domain; |
110 } | 121 } |
111 | 122 |
112 /** | 123 /** |
113 * Adds a filter to the lookup table of filters by domain. | 124 * Adds a filter to the lookup table of filters by domain. |
114 * @param {Filter} filter | 125 * @param {Filter} filter |
115 * @param {?Map.<string,boolean>} [domains] | 126 * @param {?Map.<string,boolean>} [domains] |
116 */ | 127 */ |
117 function addToFiltersByDomain(filter, domains = filter.domains) | 128 function addToFiltersByDomain(filter, domains = filter.domains) |
118 { | 129 { |
119 for (let [domain, isIncluded] of domains || defaultDomains) | 130 for (let [domain, isIncluded] of domains || defaultDomains) |
120 { | 131 { |
121 // 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. |
122 if (!isIncluded && domain == "") | 133 if (!isIncluded && domain == "") |
123 continue; | 134 continue; |
124 | 135 |
125 let filters = filtersByDomain.get(domain); | 136 let filters = filterTextByDomain.get(domain); |
126 if (!filters) | 137 if (!filters) |
127 filtersByDomain.set(domain, filters = new Map()); | 138 filterTextByDomain.set(domain, filters = new Map()); |
128 filters.set(filter.text, isIncluded ? filter.selector : null); | 139 filters.set(filter.text, isIncluded ? filter.selector : null); |
Manish Jethani
2018/11/03 20:01:25
Instead of true/false, we now keep the selector or
| |
129 } | 140 } |
130 } | 141 } |
131 | 142 |
132 /** | 143 /** |
133 * Returns a list of selectors that apply on each website unconditionally. | 144 * Returns a list of selectors that apply on each website unconditionally. |
134 * @returns {string[]} | 145 * @returns {string[]} |
135 */ | 146 */ |
136 function getUnconditionalSelectors() | 147 function getUnconditionalSelectors() |
137 { | 148 { |
138 if (!unconditionalSelectors) | 149 if (!unconditionalSelectors) |
139 unconditionalSelectors = [...filterBySelector.keys()]; | 150 unconditionalSelectors = [...filterTextBySelector.keys()]; |
140 | 151 |
141 return unconditionalSelectors; | 152 return unconditionalSelectors; |
142 } | 153 } |
143 | 154 |
144 /** | 155 /** |
145 * 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 |
146 * of filters that do not apply unconditionally on all domains. | 157 * of filters that do not apply unconditionally on all domains. |
147 * | 158 * |
148 * @param {string} domain The domain. | 159 * @param {string} domain The domain. |
149 * @param {boolean} specificOnly Whether selectors from generic filters should | 160 * @param {boolean} specificOnly Whether selectors from generic filters should |
150 * be included. | 161 * be included. |
151 * | 162 * |
152 * @returns {Array.<string>} The list of selectors. | 163 * @returns {Array.<string>} The list of selectors. |
153 */ | 164 */ |
154 function getConditionalSelectors(domain, specificOnly) | 165 function getConditionalSelectors(domain, specificOnly) |
155 { | 166 { |
156 let selectors = []; | 167 let selectors = []; |
157 | 168 |
158 let excluded = new Set(); | 169 let excluded = new Set(); |
159 let currentDomain = domain; | 170 |
160 | 171 for (let currentDomain of domainSuffixes(domain, !specificOnly)) |
161 // This code is a performance hot-spot, which is why we've made certain | 172 { |
162 // micro-optimisations. Please be careful before making changes. | 173 let filters = filterTextByDomain.get(currentDomain); |
163 while (true) | |
164 { | |
165 if (specificOnly && currentDomain == "") | |
166 break; | |
167 | |
168 let filters = filtersByDomain.get(currentDomain); | |
169 if (filters) | 174 if (filters) |
170 { | 175 { |
171 for (let [text, selector] of filters) | 176 for (let [text, selector] of filters) |
172 { | 177 { |
173 if (selector == null) | 178 if (!selector) |
174 { | 179 { |
175 excluded.add(text); | 180 excluded.add(text); |
176 } | 181 } |
177 else if ((excluded.size == 0 || !excluded.has(text)) && | 182 else if ((excluded.size == 0 || !excluded.has(text)) && |
178 !ElemHideExceptions.getException(selector, domain)) | 183 !ElemHideExceptions.getException(selector, domain)) |
179 { | 184 { |
180 selectors.push(selector); | 185 selectors.push(selector); |
181 } | 186 } |
182 } | 187 } |
183 } | 188 } |
184 | |
185 if (currentDomain == "") | |
186 break; | |
187 | |
188 let nextDot = currentDomain.indexOf("."); | |
189 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | |
190 } | 189 } |
191 | 190 |
192 return selectors; | 191 return selectors; |
192 } | |
193 | |
194 /** | |
195 * Returns the list of selectors and the list of exceptions that apply on a | |
196 * given domain from the subset of filters that do not apply unconditionally | |
197 * on all domains. | |
198 * | |
199 * @param {string} domain The domain. | |
200 * @param {boolean} specificOnly Whether selectors from generic filters should | |
201 * be included. | |
202 * | |
203 * @returns {{selectors: Array.<string>, exceptions: Array.<ElemHideException>}} | |
204 * An object containing the lists of selectors and exceptions. | |
205 */ | |
206 function getConditionalSelectorsWithExceptions(domain, specificOnly) | |
207 { | |
208 let selectors = []; | |
209 let exceptions = []; | |
210 | |
211 let excluded = new Set(); | |
212 | |
213 for (let currentDomain of domainSuffixes(domain, !specificOnly)) | |
214 { | |
215 let filters = filterTextByDomain.get(currentDomain); | |
216 if (filters) | |
217 { | |
218 for (let [text, selector] of filters) | |
219 { | |
220 if (!selector) | |
221 { | |
222 excluded.add(text); | |
223 } | |
224 else if (excluded.size == 0 || !excluded.has(text)) | |
225 { | |
226 let exception = ElemHideExceptions.getException(selector, domain); | |
227 | |
228 if (exception) | |
229 exceptions.push(exception); | |
230 else | |
231 selectors.push(selector); | |
232 } | |
233 } | |
234 } | |
235 } | |
236 | |
237 return {selectors, exceptions}; | |
193 } | 238 } |
194 | 239 |
195 /** | 240 /** |
196 * Returns the default style sheet that applies on all domains. | 241 * Returns the default style sheet that applies on all domains. |
197 * @returns {string} | 242 * @returns {string} |
198 */ | 243 */ |
199 function getDefaultStyleSheet() | 244 function getDefaultStyleSheet() |
200 { | 245 { |
201 if (!defaultStyleSheet) | 246 if (!defaultStyleSheet) |
202 defaultStyleSheet = createStyleSheet(getUnconditionalSelectors()); | 247 defaultStyleSheet = createStyleSheet(getUnconditionalSelectors()); |
203 | 248 |
204 return defaultStyleSheet; | 249 return defaultStyleSheet; |
205 } | 250 } |
206 | 251 |
207 /** | 252 /** |
208 * Returns the common style sheet that applies on all unknown domains. | 253 * Returns the common style sheet that applies on all unknown domains. |
209 * @returns {string} | 254 * @returns {string} |
210 */ | 255 */ |
211 function getCommonStyleSheet() | 256 function getCommonStyleSheet() |
212 { | 257 { |
213 if (!commonStyleSheet) | 258 if (!commonStyleSheet) |
214 { | 259 { |
215 commonStyleSheet = getDefaultStyleSheet() + | 260 commonStyleSheet = getDefaultStyleSheet() + |
216 createStyleSheet(getConditionalSelectors("", false)); | 261 createStyleSheet(getConditionalSelectors("", false)); |
217 } | 262 } |
218 | 263 |
219 return commonStyleSheet; | 264 return commonStyleSheet; |
220 } | 265 } |
221 | 266 |
267 /** | |
268 * Returns the domain-specific style sheet that applies on a given domain. | |
269 * @param {string} domain | |
270 * @returns {string} | |
271 */ | |
272 function getDomainSpecificStyleSheet(domain) | |
273 { | |
274 let styleSheet = styleSheetCache.get(domain); | |
275 | |
276 if (typeof styleSheet == "undefined") | |
277 { | |
278 styleSheet = createStyleSheet(getConditionalSelectors(domain, false)); | |
279 styleSheetCache.set(domain, styleSheet); | |
280 } | |
281 | |
282 return styleSheet; | |
283 } | |
284 | |
222 ElemHideExceptions.on("added", ({domains, selector}) => | 285 ElemHideExceptions.on("added", ({domains, selector}) => |
223 { | 286 { |
287 styleSheetCache.clear(); | |
224 commonStyleSheet = null; | 288 commonStyleSheet = null; |
225 | 289 |
226 if (domains) | 290 if (domains) |
227 { | 291 { |
228 for (let domain of domains.keys()) | 292 for (let domain of domains.keys()) |
229 { | 293 { |
230 // 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 |
231 // 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 |
232 // best-case optimization. | 296 // best-case optimization. |
233 if (domain != "") | 297 if (domain != "") |
234 knownExceptionDomains.add(domain); | 298 knownExceptionDomains.add(domain); |
235 } | 299 } |
236 } | 300 } |
237 | 301 |
238 // If this is the first exception for a previously unconditionally applied | 302 // If this is the first exception for a previously unconditionally applied |
239 // 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. |
240 let unconditionalFilterForSelector = filterBySelector.get(selector); | 304 let unconditionalFilterForSelector = filterTextBySelector.get(selector); |
241 if (unconditionalFilterForSelector) | 305 if (unconditionalFilterForSelector) |
242 { | 306 { |
243 addToFiltersByDomain(Filter.fromText(unconditionalFilterForSelector)); | 307 addToFiltersByDomain(Filter.fromText(unconditionalFilterForSelector, |
Manish Jethani
2018/11/03 20:01:25
This is the one time where we have to get the filt
| |
244 filterBySelector.delete(selector); | 308 false)); |
309 filterTextBySelector.delete(selector); | |
245 unconditionalSelectors = null; | 310 unconditionalSelectors = null; |
246 defaultStyleSheet = null; | 311 defaultStyleSheet = null; |
247 } | 312 } |
248 }); | 313 }); |
249 | 314 |
250 /** | 315 /** |
251 * Container for element hiding filters | 316 * Container for element hiding filters |
252 * @class | 317 * @class |
253 */ | 318 */ |
254 exports.ElemHide = { | 319 exports.ElemHide = { |
255 /** | 320 /** |
256 * Removes all known filters | 321 * Removes all known filters |
257 */ | 322 */ |
258 clear() | 323 clear() |
259 { | 324 { |
260 commonStyleSheet = null; | 325 commonStyleSheet = null; |
261 | 326 |
262 for (let collection of [filtersByDomain, filterBySelector, knownFilters, | 327 for (let collection of [styleSheetCache, filterTextByDomain, |
328 filterTextBySelector, knownFilterText, | |
263 knownExceptionDomains]) | 329 knownExceptionDomains]) |
264 { | 330 { |
265 collection.clear(); | 331 collection.clear(); |
266 } | 332 } |
267 | 333 |
268 unconditionalSelectors = null; | 334 unconditionalSelectors = null; |
269 defaultStyleSheet = null; | 335 defaultStyleSheet = null; |
270 | 336 |
271 filterNotifier.emit("elemhideupdate"); | 337 filterNotifier.emit("elemhideupdate"); |
272 }, | 338 }, |
273 | 339 |
274 /** | 340 /** |
275 * Add a new element hiding filter | 341 * Add a new element hiding filter |
276 * @param {ElemHideFilter} filter | 342 * @param {ElemHideFilter} filter |
277 */ | 343 */ |
278 add(filter) | 344 add(filter) |
279 { | 345 { |
280 if (knownFilters.has(filter.text)) | 346 let {text} = filter; |
347 | |
348 if (knownFilterText.has(text)) | |
281 return; | 349 return; |
282 | 350 |
351 styleSheetCache.clear(); | |
283 commonStyleSheet = null; | 352 commonStyleSheet = null; |
284 | 353 |
285 let {domains, selector} = filter; | 354 let {domains, selector} = filter; |
286 | 355 |
287 if (!(domains || ElemHideExceptions.hasExceptions(selector))) | 356 if (!(domains || ElemHideExceptions.hasExceptions(selector))) |
288 { | 357 { |
289 // The new filter's selector is unconditionally applied to all domains | 358 // The new filter's selector is unconditionally applied to all domains |
290 filterBySelector.set(selector, filter.text); | 359 filterTextBySelector.set(selector, text); |
291 unconditionalSelectors = null; | 360 unconditionalSelectors = null; |
292 defaultStyleSheet = null; | 361 defaultStyleSheet = null; |
293 } | 362 } |
294 else | 363 else |
295 { | 364 { |
296 // The new filter's selector only applies to some domains | 365 // The new filter's selector only applies to some domains |
297 addToFiltersByDomain(filter, domains); | 366 addToFiltersByDomain(filter, domains); |
298 } | 367 } |
299 | 368 |
300 knownFilters.add(filter.text); | 369 knownFilterText.add(text); |
301 filterNotifier.emit("elemhideupdate"); | 370 filterNotifier.emit("elemhideupdate"); |
302 }, | 371 }, |
303 | 372 |
304 /** | 373 /** |
305 * Removes an element hiding filter | 374 * Removes an element hiding filter |
306 * @param {ElemHideFilter} filter | 375 * @param {ElemHideFilter} filter |
307 */ | 376 */ |
308 remove(filter) | 377 remove(filter) |
309 { | 378 { |
310 if (!knownFilters.has(filter.text)) | 379 let {text} = filter; |
380 | |
381 if (!knownFilterText.has(text)) | |
311 return; | 382 return; |
312 | 383 |
384 styleSheetCache.clear(); | |
313 commonStyleSheet = null; | 385 commonStyleSheet = null; |
314 | 386 |
315 let {selector} = filter; | 387 let {selector} = filter; |
316 | 388 |
317 // Unconditially applied element hiding filters | 389 // Unconditially applied element hiding filters |
318 if (filterBySelector.get(selector) == filter.text) | 390 if (filterTextBySelector.get(selector) == text) |
319 { | 391 { |
320 filterBySelector.delete(selector); | 392 filterTextBySelector.delete(selector); |
321 unconditionalSelectors = null; | 393 unconditionalSelectors = null; |
322 defaultStyleSheet = null; | 394 defaultStyleSheet = null; |
323 } | 395 } |
324 // Conditionally applied element hiding filters | 396 // Conditionally applied element hiding filters |
325 else | 397 else |
326 { | 398 { |
327 let domains = filter.domains || defaultDomains; | 399 let domains = filter.domains || defaultDomains; |
328 for (let domain of domains.keys()) | 400 for (let domain of domains.keys()) |
329 { | 401 { |
330 let filters = filtersByDomain.get(domain); | 402 let filters = filterTextByDomain.get(domain); |
331 if (filters) | 403 if (filters) |
332 { | 404 { |
333 filters.delete(filter.text); | 405 filters.delete(text); |
334 | 406 |
335 if (filters.size == 0) | 407 if (filters.size == 0) |
336 filtersByDomain.delete(domain); | 408 filterTextByDomain.delete(domain); |
337 } | 409 } |
338 } | 410 } |
339 } | 411 } |
340 | 412 |
341 knownFilters.delete(filter.text); | 413 knownFilterText.delete(text); |
342 filterNotifier.emit("elemhideupdate"); | 414 filterNotifier.emit("elemhideupdate"); |
343 }, | 415 }, |
344 | 416 |
345 /** | 417 /** |
346 * @typedef {object} ElemHideStyleSheet | 418 * @typedef {object} ElemHideStyleSheet |
347 * @property {string} code CSS code. | 419 * @property {string} code CSS code. |
348 * @property {Array.<string>} selectors List of selectors. | 420 * @property {Array.<string>} selectors List of selectors. |
349 */ | 421 */ |
350 | 422 |
351 /** | 423 /** |
352 * Generates a style sheet for a given domain based on the current set of | 424 * Generates a style sheet for a given domain based on the current set of |
353 * filters. | 425 * filters. |
354 * | 426 * |
355 * @param {string} domain The domain. | 427 * @param {string} domain The domain. |
356 * @param {boolean} [specificOnly=false] Whether selectors from generic | 428 * @param {boolean} [specificOnly=false] Whether selectors from generic |
357 * filters should be included. | 429 * filters should be included. |
358 * @param {boolean} [includeSelectors=false] Whether the return value should | 430 * @param {boolean} [includeSelectors=false] Whether the return value should |
359 * include a separate list of selectors. | 431 * include a separate list of selectors. |
432 * @param {boolean} [includeExceptions=false] Whether the return value should | |
433 * include a separate list of exceptions. | |
360 * | 434 * |
361 * @returns {ElemHideStyleSheet} An object containing the CSS code and the | 435 * @returns {ElemHideStyleSheet} An object containing the CSS code and the |
362 * list of selectors. | 436 * list of selectors. |
363 */ | 437 */ |
364 generateStyleSheetForDomain(domain, specificOnly = false, | 438 generateStyleSheetForDomain(domain, specificOnly = false, |
365 includeSelectors = false) | 439 includeSelectors = false, |
440 includeExceptions = false) | |
366 { | 441 { |
367 let code = null; | 442 let code = null; |
368 let selectors = null; | 443 let selectors = null; |
369 | 444 let exceptions = null; |
370 if (domain[domain.length - 1] == ".") | 445 |
371 domain = domain.replace(/\.+$/, ""); | 446 domain = normalizeHostname(domain); |
372 | |
373 domain = domain.toLowerCase(); | |
374 | 447 |
375 if (specificOnly) | 448 if (specificOnly) |
376 { | 449 { |
377 selectors = getConditionalSelectors(domain, true); | 450 if (includeExceptions) |
451 { | |
452 let selectorsAndExceptions = | |
453 getConditionalSelectorsWithExceptions(domain, true); | |
454 | |
455 selectors = selectorsAndExceptions.selectors; | |
456 exceptions = selectorsAndExceptions.exceptions; | |
457 } | |
458 else | |
459 { | |
460 selectors = getConditionalSelectors(domain, true); | |
461 } | |
462 | |
378 code = createStyleSheet(selectors); | 463 code = createStyleSheet(selectors); |
379 } | 464 } |
380 else | 465 else |
381 { | 466 { |
382 let knownSuffix = getKnownSuffix(domain); | 467 let knownSuffix = getKnownSuffix(domain); |
383 | 468 |
384 if (includeSelectors) | 469 if (includeSelectors || includeExceptions) |
385 { | 470 { |
386 selectors = getConditionalSelectors(knownSuffix, false); | 471 let selectorsAndExceptions = |
472 getConditionalSelectorsWithExceptions(knownSuffix, false); | |
473 | |
474 selectors = selectorsAndExceptions.selectors; | |
475 exceptions = selectorsAndExceptions.exceptions; | |
387 code = knownSuffix == "" ? getCommonStyleSheet() : | 476 code = knownSuffix == "" ? getCommonStyleSheet() : |
388 (getDefaultStyleSheet() + createStyleSheet(selectors)); | 477 (getDefaultStyleSheet() + createStyleSheet(selectors)); |
389 | 478 |
390 selectors = getUnconditionalSelectors().concat(selectors); | 479 selectors = getUnconditionalSelectors().concat(selectors); |
391 } | 480 } |
392 else | 481 else |
393 { | 482 { |
394 code = knownSuffix == "" ? getCommonStyleSheet() : | 483 code = knownSuffix == "" ? getCommonStyleSheet() : |
395 (getDefaultStyleSheet() + | 484 (getDefaultStyleSheet() + |
396 createStyleSheet(getConditionalSelectors(knownSuffix, | 485 getDomainSpecificStyleSheet(knownSuffix)); |
397 false))); | 486 } |
398 } | 487 } |
399 } | 488 |
400 | 489 return { |
401 return {code, selectors: includeSelectors ? selectors : null}; | 490 code, |
491 selectors: includeSelectors ? selectors : null, | |
492 exceptions: includeExceptions ? exceptions : null | |
493 }; | |
402 } | 494 } |
403 }; | 495 }; |
404 | 496 |
405 /** | 497 /** |
406 * Yields rules from a style sheet returned by | 498 * Yields rules from a style sheet returned by |
407 * <code>{@link createStyleSheet}</code>. | 499 * <code>{@link createStyleSheet}</code>. |
408 * | 500 * |
409 * @param {string} styleSheet A style sheet returned by | 501 * @param {string} styleSheet A style sheet returned by |
410 * <code>{@link createStyleSheet}</code>. If the given style sheet is | 502 * <code>{@link createStyleSheet}</code>. If the given style sheet is |
411 * <em>not</em> a value previously returned by a call to | 503 * <em>not</em> a value previously returned by a call to |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
443 // calculate the sizes of the selectors and divide them up accordingly, but | 535 // calculate the sizes of the selectors and divide them up accordingly, but |
444 // this approach is more efficient and has worked well in practice. In theory | 536 // this approach is more efficient and has worked well in practice. In theory |
445 // this could still lead to some selectors not working on Chromium, but it is | 537 // this could still lead to some selectors not working on Chromium, but it is |
446 // highly unlikely. | 538 // highly unlikely. |
447 // See issue #6298 and https://crbug.com/804179 | 539 // See issue #6298 and https://crbug.com/804179 |
448 for (let i = 0; i < selectors.length; i += selectorGroupSize) | 540 for (let i = 0; i < selectors.length; i += selectorGroupSize) |
449 yield selectors.slice(i, i + selectorGroupSize); | 541 yield selectors.slice(i, i + selectorGroupSize); |
450 } | 542 } |
451 | 543 |
452 /** | 544 /** |
545 * Escapes curly braces to prevent CSS rule injection. | |
546 * | |
547 * @param {string} selector | |
548 * @returns {string} | |
549 */ | |
550 function escapeSelector(selector) | |
551 { | |
552 return selector.replace("{", "\\7B ").replace("}", "\\7D "); | |
553 } | |
554 | |
555 /** | |
453 * Creates an element hiding CSS rule for a given list of selectors. | 556 * Creates an element hiding CSS rule for a given list of selectors. |
454 * | 557 * |
455 * @param {Array.<string>} selectors | 558 * @param {Array.<string>} selectors |
456 * @returns {string} | 559 * @returns {string} |
457 */ | 560 */ |
458 function createRule(selectors) | 561 function createRule(selectors) |
459 { | 562 { |
460 let rule = ""; | 563 let rule = ""; |
461 | 564 |
462 for (let i = 0; i < selectors.length - 1; i++) | 565 for (let i = 0; i < selectors.length - 1; i++) |
463 rule += selectors[i] + ", "; | 566 rule += escapeSelector(selectors[i]) + ", "; |
464 | 567 |
465 rule += selectors[selectors.length - 1] + " {display: none !important;}\n"; | 568 rule += escapeSelector(selectors[selectors.length - 1]) + |
569 " {display: none !important;}\n"; | |
466 | 570 |
467 return rule; | 571 return rule; |
468 } | 572 } |
469 | 573 |
470 /** | 574 /** |
471 * Creates an element hiding CSS style sheet from a given list of selectors. | 575 * Creates an element hiding CSS style sheet from a given list of selectors. |
472 * @param {Array.<string>} selectors | 576 * @param {Array.<string>} selectors |
473 * @returns {string} | 577 * @returns {string} |
474 */ | 578 */ |
475 function createStyleSheet(selectors) | 579 function createStyleSheet(selectors) |
476 { | 580 { |
477 let styleSheet = ""; | 581 let styleSheet = ""; |
478 | 582 |
479 for (let selectorGroup of splitSelectors(selectors)) | 583 for (let selectorGroup of splitSelectors(selectors)) |
480 styleSheet += createRule(selectorGroup); | 584 styleSheet += createRule(selectorGroup); |
481 | 585 |
482 return styleSheet; | 586 return styleSheet; |
483 } | 587 } |
484 | 588 |
485 exports.createStyleSheet = createStyleSheet; | 589 exports.createStyleSheet = createStyleSheet; |
LEFT | RIGHT |