Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: lib/elemHide.js

Issue 29935564: Issue 7452 - Do not cache element hiding filter objects by default Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Rename filter* to filterText* Created Nov. 23, 2018, 2:01 a.m.
Right Patch Set: Rename filter* to filterText* Created April 8, 2019, 9:59 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | lib/filterClasses.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 text by domain. 40 * Lookup table, selector by filter text by domain.
39 * (Only contains text for filters that aren't unconditionally matched for all 41 * (Only contains filter text for filters that aren't unconditionally matched
40 * domains.) 42 * for all domains.)
41 * @type {Map.<string,Map.<string,boolean>>} 43 * @type {Map.<string,Map.<string,string>>}
42 */ 44 */
43 let filterTextByDomain = new Map(); 45 let filterTextByDomain = new Map();
44 46
45 /** 47 /**
46 * Lookup table, filter text by selector. (Only used for selectors that are 48 * Lookup table, filter text by selector. (Only used for selectors that are
47 * unconditionally matched for all domains.) 49 * unconditionally matched for all domains.)
48 * @type {Map.<string,string>} 50 * @type {Map.<string,string>}
49 */ 51 */
50 let filterTextBySelector = new Map(); 52 let filterTextBySelector = new Map();
51 53
(...skipping 15 matching lines...) Expand all
67 /** 69 /**
68 * 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
69 * 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
70 * on selectors from all generic filters that are not in the 72 * on selectors from all generic filters that are not in the
71 * <code>{@link unconditionalSelectors}</code> list. 73 * <code>{@link unconditionalSelectors}</code> list.
72 * @type {?string} 74 * @type {?string}
73 */ 75 */
74 let commonStyleSheet = null; 76 let commonStyleSheet = null;
75 77
76 /** 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 /**
77 * 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.
78 * @type {Map.<string,boolean>} 88 * @type {Map.<string,boolean>}
79 * @const 89 * @const
80 */ 90 */
81 let defaultDomains = new Map([["", true]]); 91 let defaultDomains = new Map([["", true]]);
82 92
83 /** 93 /**
84 * Set containing the text of known element hiding filters 94 * Set containing text of known element hiding filters
85 * @type {Set.<string>} 95 * @type {Set.<string>}
86 */ 96 */
87 let knownFilterText = new Set(); 97 let knownFilterText = new Set();
88 98
89 /** 99 /**
90 * All domains known to occur in exceptions 100 * All domains known to occur in exceptions
91 * @type {Set.<string>} 101 * @type {Set.<string>}
92 */ 102 */
93 let knownExceptionDomains = new Set(); 103 let knownExceptionDomains = new Set();
94 104
95 /** 105 /**
96 * 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,
97 * an empty string is returned. 107 * an empty string is returned.
98 * @param {?string} domain 108 * @param {?string} domain
99 * @returns {string} 109 * @returns {string}
100 */ 110 */
101 function getKnownSuffix(domain) 111 function getKnownSuffix(domain)
102 { 112 {
103 while (domain && !filterTextByDomain.has(domain) && 113 while (domain && !filterTextByDomain.has(domain) &&
104 !knownExceptionDomains.has(domain)) 114 !knownExceptionDomains.has(domain))
105 { 115 {
106 let index = domain.indexOf("."); 116 let index = domain.indexOf(".");
107 domain = index == -1 ? "" : domain.substring(index + 1); 117 domain = index == -1 ? "" : domain.substring(index + 1);
108 } 118 }
109 119
110 return domain; 120 return domain;
111 } 121 }
112 122
113 /** 123 /**
114 * Adds a filter to the lookup table of filter text by domain. 124 * Adds a filter to the lookup table of filters by domain.
115 * @param {Filter} filter 125 * @param {Filter} filter
116 * @param {?Map.<string,boolean>} [domains] 126 * @param {?Map.<string,boolean>} [domains]
117 */ 127 */
118 function addToFiltersByDomain(filter, domains = filter.domains) 128 function addToFiltersByDomain(filter, domains = filter.domains)
119 { 129 {
120 for (let [domain, isIncluded] of domains || defaultDomains) 130 for (let [domain, isIncluded] of domains || defaultDomains)
121 { 131 {
122 // 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.
123 if (!isIncluded && domain == "") 133 if (!isIncluded && domain == "")
124 continue; 134 continue;
125 135
126 let map = filterTextByDomain.get(domain); 136 let filters = filterTextByDomain.get(domain);
127 if (!map) 137 if (!filters)
128 filterTextByDomain.set(domain, map = new Map()); 138 filterTextByDomain.set(domain, filters = new Map());
129 map.set(filter.text, isIncluded ? filter.selector : null); 139 filters.set(filter.text, isIncluded ? filter.selector : null);
130 } 140 }
131 } 141 }
132 142
133 /** 143 /**
134 * Returns a list of selectors that apply on each website unconditionally. 144 * Returns a list of selectors that apply on each website unconditionally.
135 * @returns {string[]} 145 * @returns {string[]}
136 */ 146 */
137 function getUnconditionalSelectors() 147 function getUnconditionalSelectors()
138 { 148 {
139 if (!unconditionalSelectors) 149 if (!unconditionalSelectors)
(...skipping 10 matching lines...) Expand all
150 * @param {boolean} specificOnly Whether selectors from generic filters should 160 * @param {boolean} specificOnly Whether selectors from generic filters should
151 * be included. 161 * be included.
152 * 162 *
153 * @returns {Array.<string>} The list of selectors. 163 * @returns {Array.<string>} The list of selectors.
154 */ 164 */
155 function getConditionalSelectors(domain, specificOnly) 165 function getConditionalSelectors(domain, specificOnly)
156 { 166 {
157 let selectors = []; 167 let selectors = [];
158 168
159 let excluded = new Set(); 169 let excluded = new Set();
160 let currentDomain = domain; 170
161 171 for (let currentDomain of domainSuffixes(domain, !specificOnly))
162 // This code is a performance hot-spot, which is why we've made certain 172 {
163 // micro-optimisations. Please be careful before making changes. 173 let filters = filterTextByDomain.get(currentDomain);
164 while (true) 174 if (filters)
165 { 175 {
166 if (specificOnly && currentDomain == "") 176 for (let [text, selector] of filters)
167 break; 177 {
168 178 if (!selector)
169 let map = filterTextByDomain.get(currentDomain);
170 if (map)
171 {
172 for (let [text, selector] of map)
173 {
174 if (selector == null)
175 { 179 {
176 excluded.add(text); 180 excluded.add(text);
177 } 181 }
178 else if ((excluded.size == 0 || !excluded.has(text)) && 182 else if ((excluded.size == 0 || !excluded.has(text)) &&
179 !ElemHideExceptions.getException(selector, domain)) 183 !ElemHideExceptions.getException(selector, domain))
180 { 184 {
181 selectors.push(selector); 185 selectors.push(selector);
182 } 186 }
183 } 187 }
184 } 188 }
185
186 if (currentDomain == "")
187 break;
188
189 let nextDot = currentDomain.indexOf(".");
190 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
191 } 189 }
192 190
193 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};
194 } 238 }
195 239
196 /** 240 /**
197 * Returns the default style sheet that applies on all domains. 241 * Returns the default style sheet that applies on all domains.
198 * @returns {string} 242 * @returns {string}
199 */ 243 */
200 function getDefaultStyleSheet() 244 function getDefaultStyleSheet()
201 { 245 {
202 if (!defaultStyleSheet) 246 if (!defaultStyleSheet)
203 defaultStyleSheet = createStyleSheet(getUnconditionalSelectors()); 247 defaultStyleSheet = createStyleSheet(getUnconditionalSelectors());
204 248
205 return defaultStyleSheet; 249 return defaultStyleSheet;
206 } 250 }
207 251
208 /** 252 /**
209 * Returns the common style sheet that applies on all unknown domains. 253 * Returns the common style sheet that applies on all unknown domains.
210 * @returns {string} 254 * @returns {string}
211 */ 255 */
212 function getCommonStyleSheet() 256 function getCommonStyleSheet()
213 { 257 {
214 if (!commonStyleSheet) 258 if (!commonStyleSheet)
215 { 259 {
216 commonStyleSheet = getDefaultStyleSheet() + 260 commonStyleSheet = getDefaultStyleSheet() +
217 createStyleSheet(getConditionalSelectors("", false)); 261 createStyleSheet(getConditionalSelectors("", false));
218 } 262 }
219 263
220 return commonStyleSheet; 264 return commonStyleSheet;
221 } 265 }
222 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
223 ElemHideExceptions.on("added", ({domains, selector}) => 285 ElemHideExceptions.on("added", ({domains, selector}) =>
224 { 286 {
287 styleSheetCache.clear();
225 commonStyleSheet = null; 288 commonStyleSheet = null;
226 289
227 if (domains) 290 if (domains)
228 { 291 {
229 for (let domain of domains.keys()) 292 for (let domain of domains.keys())
230 { 293 {
231 // 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
232 // 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
233 // best-case optimization. 296 // best-case optimization.
234 if (domain != "") 297 if (domain != "")
235 knownExceptionDomains.add(domain); 298 knownExceptionDomains.add(domain);
236 } 299 }
237 } 300 }
238 301
239 // If this is the first exception for a previously unconditionally applied 302 // If this is the first exception for a previously unconditionally applied
240 // 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.
241 let unconditionalFilterTextForSelector = filterTextBySelector.get(selector); 304 let unconditionalFilterForSelector = filterTextBySelector.get(selector);
242 if (unconditionalFilterTextForSelector) 305 if (unconditionalFilterForSelector)
243 { 306 {
244 addToFiltersByDomain(Filter.fromText(unconditionalFilterTextForSelector)); 307 addToFiltersByDomain(Filter.fromText(unconditionalFilterForSelector,
hub 2018/11/28 23:21:31 So here, we call Filter.fromText() while before we
Manish Jethani 2019/04/07 18:05:09 Yes, unfortunately this is the price. I looked at
Manish Jethani 2019/04/07 18:55:11 OK, so I looked into this [1], and parsing of gene
308 false));
245 filterTextBySelector.delete(selector); 309 filterTextBySelector.delete(selector);
246 unconditionalSelectors = null; 310 unconditionalSelectors = null;
247 defaultStyleSheet = null; 311 defaultStyleSheet = null;
248 } 312 }
249 }); 313 });
250 314
251 /** 315 /**
252 * Container for element hiding filters 316 * Container for element hiding filters
253 * @class 317 * @class
254 */ 318 */
255 exports.ElemHide = { 319 exports.ElemHide = {
256 /** 320 /**
257 * Removes all known filters 321 * Removes all known filters
258 */ 322 */
259 clear() 323 clear()
260 { 324 {
261 commonStyleSheet = null; 325 commonStyleSheet = null;
262 326
263 for (let collection of [filterTextByDomain, filterTextBySelector, 327 for (let collection of [styleSheetCache, filterTextByDomain,
264 knownFilterText, knownExceptionDomains]) 328 filterTextBySelector, knownFilterText,
329 knownExceptionDomains])
265 { 330 {
266 collection.clear(); 331 collection.clear();
267 } 332 }
268 333
269 unconditionalSelectors = null; 334 unconditionalSelectors = null;
270 defaultStyleSheet = null; 335 defaultStyleSheet = null;
271 336
272 filterNotifier.emit("elemhideupdate"); 337 filterNotifier.emit("elemhideupdate");
273 }, 338 },
274 339
275 /** 340 /**
276 * Add a new element hiding filter 341 * Add a new element hiding filter
277 * @param {ElemHideFilter} filter 342 * @param {ElemHideFilter} filter
278 */ 343 */
279 add(filter) 344 add(filter)
280 { 345 {
281 let {text} = filter; 346 let {text} = filter;
282 347
283 if (knownFilterText.has(text)) 348 if (knownFilterText.has(text))
284 return; 349 return;
285 350
351 styleSheetCache.clear();
286 commonStyleSheet = null; 352 commonStyleSheet = null;
287 353
288 let {domains, selector} = filter; 354 let {domains, selector} = filter;
289 355
290 if (!(domains || ElemHideExceptions.hasExceptions(selector))) 356 if (!(domains || ElemHideExceptions.hasExceptions(selector)))
291 { 357 {
292 // The new filter's selector is unconditionally applied to all domains 358 // The new filter's selector is unconditionally applied to all domains
293 filterTextBySelector.set(selector, text); 359 filterTextBySelector.set(selector, text);
294 unconditionalSelectors = null; 360 unconditionalSelectors = null;
295 defaultStyleSheet = null; 361 defaultStyleSheet = null;
(...skipping 12 matching lines...) Expand all
308 * Removes an element hiding filter 374 * Removes an element hiding filter
309 * @param {ElemHideFilter} filter 375 * @param {ElemHideFilter} filter
310 */ 376 */
311 remove(filter) 377 remove(filter)
312 { 378 {
313 let {text} = filter; 379 let {text} = filter;
314 380
315 if (!knownFilterText.has(text)) 381 if (!knownFilterText.has(text))
316 return; 382 return;
317 383
384 styleSheetCache.clear();
318 commonStyleSheet = null; 385 commonStyleSheet = null;
319 386
320 let {selector} = filter; 387 let {selector} = filter;
321 388
322 // Unconditially applied element hiding filters 389 // Unconditially applied element hiding filters
323 if (filterTextBySelector.get(selector) == text) 390 if (filterTextBySelector.get(selector) == text)
324 { 391 {
325 filterTextBySelector.delete(selector); 392 filterTextBySelector.delete(selector);
326 unconditionalSelectors = null; 393 unconditionalSelectors = null;
327 defaultStyleSheet = null; 394 defaultStyleSheet = null;
328 } 395 }
329 // Conditionally applied element hiding filters 396 // Conditionally applied element hiding filters
330 else 397 else
331 { 398 {
332 let domains = filter.domains || defaultDomains; 399 let domains = filter.domains || defaultDomains;
333 for (let domain of domains.keys()) 400 for (let domain of domains.keys())
334 { 401 {
335 let map = filterTextByDomain.get(domain); 402 let filters = filterTextByDomain.get(domain);
336 if (map) 403 if (filters)
337 { 404 {
338 map.delete(text); 405 filters.delete(text);
339 406
340 if (map.size == 0) 407 if (filters.size == 0)
341 filterTextByDomain.delete(domain); 408 filterTextByDomain.delete(domain);
342 } 409 }
343 } 410 }
344 } 411 }
345 412
346 knownFilterText.delete(text); 413 knownFilterText.delete(text);
347 filterNotifier.emit("elemhideupdate"); 414 filterNotifier.emit("elemhideupdate");
348 }, 415 },
349 416
350 /** 417 /**
351 * @typedef {object} ElemHideStyleSheet 418 * @typedef {object} ElemHideStyleSheet
352 * @property {string} code CSS code. 419 * @property {string} code CSS code.
353 * @property {Array.<string>} selectors List of selectors. 420 * @property {Array.<string>} selectors List of selectors.
354 */ 421 */
355 422
356 /** 423 /**
357 * 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
358 * filters. 425 * filters.
359 * 426 *
360 * @param {string} domain The domain. 427 * @param {string} domain The domain.
361 * @param {boolean} [specificOnly=false] Whether selectors from generic 428 * @param {boolean} [specificOnly=false] Whether selectors from generic
362 * filters should be included. 429 * filters should be included.
363 * @param {boolean} [includeSelectors=false] Whether the return value should 430 * @param {boolean} [includeSelectors=false] Whether the return value should
364 * 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.
365 * 434 *
366 * @returns {ElemHideStyleSheet} An object containing the CSS code and the 435 * @returns {ElemHideStyleSheet} An object containing the CSS code and the
367 * list of selectors. 436 * list of selectors.
368 */ 437 */
369 generateStyleSheetForDomain(domain, specificOnly = false, 438 generateStyleSheetForDomain(domain, specificOnly = false,
370 includeSelectors = false) 439 includeSelectors = false,
440 includeExceptions = false)
371 { 441 {
372 let code = null; 442 let code = null;
373 let selectors = null; 443 let selectors = null;
374 444 let exceptions = null;
375 if (domain[domain.length - 1] == ".") 445
376 domain = domain.replace(/\.+$/, ""); 446 domain = normalizeHostname(domain);
377
378 domain = domain.toLowerCase();
379 447
380 if (specificOnly) 448 if (specificOnly)
381 { 449 {
382 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
383 code = createStyleSheet(selectors); 463 code = createStyleSheet(selectors);
384 } 464 }
385 else 465 else
386 { 466 {
387 let knownSuffix = getKnownSuffix(domain); 467 let knownSuffix = getKnownSuffix(domain);
388 468
389 if (includeSelectors) 469 if (includeSelectors || includeExceptions)
390 { 470 {
391 selectors = getConditionalSelectors(knownSuffix, false); 471 let selectorsAndExceptions =
472 getConditionalSelectorsWithExceptions(knownSuffix, false);
473
474 selectors = selectorsAndExceptions.selectors;
475 exceptions = selectorsAndExceptions.exceptions;
392 code = knownSuffix == "" ? getCommonStyleSheet() : 476 code = knownSuffix == "" ? getCommonStyleSheet() :
393 (getDefaultStyleSheet() + createStyleSheet(selectors)); 477 (getDefaultStyleSheet() + createStyleSheet(selectors));
394 478
395 selectors = getUnconditionalSelectors().concat(selectors); 479 selectors = getUnconditionalSelectors().concat(selectors);
396 } 480 }
397 else 481 else
398 { 482 {
399 code = knownSuffix == "" ? getCommonStyleSheet() : 483 code = knownSuffix == "" ? getCommonStyleSheet() :
400 (getDefaultStyleSheet() + 484 (getDefaultStyleSheet() +
401 createStyleSheet(getConditionalSelectors(knownSuffix, 485 getDomainSpecificStyleSheet(knownSuffix));
402 false))); 486 }
403 } 487 }
404 } 488
405 489 return {
406 return {code, selectors: includeSelectors ? selectors : null}; 490 code,
491 selectors: includeSelectors ? selectors : null,
492 exceptions: includeExceptions ? exceptions : null
493 };
407 } 494 }
408 }; 495 };
409 496
410 /** 497 /**
411 * Yields rules from a style sheet returned by 498 * Yields rules from a style sheet returned by
412 * <code>{@link createStyleSheet}</code>. 499 * <code>{@link createStyleSheet}</code>.
413 * 500 *
414 * @param {string} styleSheet A style sheet returned by 501 * @param {string} styleSheet A style sheet returned by
415 * <code>{@link createStyleSheet}</code>. If the given style sheet is 502 * <code>{@link createStyleSheet}</code>. If the given style sheet is
416 * <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
448 // 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
449 // 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
450 // 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
451 // highly unlikely. 538 // highly unlikely.
452 // See issue #6298 and https://crbug.com/804179 539 // See issue #6298 and https://crbug.com/804179
453 for (let i = 0; i < selectors.length; i += selectorGroupSize) 540 for (let i = 0; i < selectors.length; i += selectorGroupSize)
454 yield selectors.slice(i, i + selectorGroupSize); 541 yield selectors.slice(i, i + selectorGroupSize);
455 } 542 }
456 543
457 /** 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 /**
458 * 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.
459 * 557 *
460 * @param {Array.<string>} selectors 558 * @param {Array.<string>} selectors
461 * @returns {string} 559 * @returns {string}
462 */ 560 */
463 function createRule(selectors) 561 function createRule(selectors)
464 { 562 {
465 let rule = ""; 563 let rule = "";
466 564
467 for (let i = 0; i < selectors.length - 1; i++) 565 for (let i = 0; i < selectors.length - 1; i++)
468 rule += selectors[i] + ", "; 566 rule += escapeSelector(selectors[i]) + ", ";
469 567
470 rule += selectors[selectors.length - 1] + " {display: none !important;}\n"; 568 rule += escapeSelector(selectors[selectors.length - 1]) +
569 " {display: none !important;}\n";
471 570
472 return rule; 571 return rule;
473 } 572 }
474 573
475 /** 574 /**
476 * 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.
477 * @param {Array.<string>} selectors 576 * @param {Array.<string>} selectors
478 * @returns {string} 577 * @returns {string}
479 */ 578 */
480 function createStyleSheet(selectors) 579 function createStyleSheet(selectors)
481 { 580 {
482 let styleSheet = ""; 581 let styleSheet = "";
483 582
484 for (let selectorGroup of splitSelectors(selectors)) 583 for (let selectorGroup of splitSelectors(selectors))
485 styleSheet += createRule(selectorGroup); 584 styleSheet += createRule(selectorGroup);
486 585
487 return styleSheet; 586 return styleSheet;
488 } 587 }
489 588
490 exports.createStyleSheet = createStyleSheet; 589 exports.createStyleSheet = createStyleSheet;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld