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

Side by Side Diff: lib/elemHide.js

Issue 29882562: Issue 6956 - Move extension's style sheet generation into core (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Sept. 16, 2018, 5:19 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/elemHide.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 {ElemHideExceptions} = require("./elemHideExceptions"); 24 const {ElemHideExceptions} = require("./elemHideExceptions");
25 const {filterNotifier} = require("./filterNotifier"); 25 const {filterNotifier} = require("./filterNotifier");
26 26
27 /** 27 /**
28 * The maximum number of selectors in a CSS rule. This is used by
29 * <code>{@link ElemHide.createStyleSheet}</code> to split up a long list of
30 * selectors into multiple rules.
31 * @const {number}
32 * @default
33 */
34 const selectorGroupSize = 1024;
35
36 /**
28 * Lookup table, active flag, by filter by domain. 37 * Lookup table, active flag, by filter by domain.
29 * (Only contains filters that aren't unconditionally matched for all domains.) 38 * (Only contains filters that aren't unconditionally matched for all domains.)
30 * @type {Map.<string,Map.<Filter,boolean>>} 39 * @type {Map.<string,Map.<Filter,boolean>>}
31 */ 40 */
32 let filtersByDomain = new Map(); 41 let filtersByDomain = new Map();
33 42
34 /** 43 /**
35 * Lookup table, filter by selector. (Only used for selectors that are 44 * Lookup table, filter by selector. (Only used for selectors that are
36 * unconditionally matched for all domains.) 45 * unconditionally matched for all domains.)
37 * @type {Map.<string,Filter>} 46 * @type {Map.<string,Filter>}
(...skipping 15 matching lines...) Expand all
53 */ 62 */
54 let defaultDomains = new Map([["", true]]); 63 let defaultDomains = new Map([["", true]]);
55 64
56 /** 65 /**
57 * Set containing known element hiding filters 66 * Set containing known element hiding filters
58 * @type {Set.<ElemHideFilter>} 67 * @type {Set.<ElemHideFilter>}
59 */ 68 */
60 let knownFilters = new Set(); 69 let knownFilters = new Set();
61 70
62 /** 71 /**
72 * Splits a list of selectors into groups determined by the value of
73 * <code>{@link selectorGroupSize}</code>.
74 *
75 * @param {Array.<string>} selectors
76 * @yields {Array.<string>}
77 */
78 function* splitSelectors(selectors)
79 {
80 // Chromium's Blink engine supports only up to 8,192 simple selectors, and
81 // even fewer compound selectors, in a rule. The exact number of selectors
82 // that would work depends on their sizes (e.g. "#foo .bar" has a size of 2).
83 // Since we don't know the sizes of the selectors here, we simply split them
84 // into groups of 1,024, based on the reasonable assumption that the average
85 // selector won't have a size greater than 8. The alternative would be to
86 // calculate the sizes of the selectors and divide them up accordingly, but
87 // this approach is more efficient and has worked well in practice. In theory
88 // this could still lead to some selectors not working on Chromium, but it is
89 // highly unlikely.
90 // See issue #6298 and https://crbug.com/804179
91 for (let i = 0; i < selectors.length; i += selectorGroupSize)
92 yield selectors.slice(i, i + selectorGroupSize);
Sebastian Noack 2018/09/17 18:59:57 Apparently this code is currently already using ge
93 }
94
95 /**
96 * Creates element hiding CSS rules for a given list of selectors. Each rule
97 * contains no more than the maximum number of selectors as determined by the
98 * value of <code>{@link selectorGroupSize}</code>.
99 *
100 * @param {Array.<string>} selectors
101 * @yields {string}
102 */
103 function* createRules(selectors)
104 {
105 for (let selectorGroup of splitSelectors(selectors))
106 yield selectorGroup.join(", ") + " {display: none !important;}";
107 }
108
109 /**
63 * Adds a filter to the lookup table of filters by domain. 110 * Adds a filter to the lookup table of filters by domain.
64 * @param {Filter} filter 111 * @param {Filter} filter
65 */ 112 */
66 function addToFiltersByDomain(filter) 113 function addToFiltersByDomain(filter)
67 { 114 {
68 let domains = filter.domains || defaultDomains; 115 let domains = filter.domains || defaultDomains;
69 for (let [domain, isIncluded] of domains) 116 for (let [domain, isIncluded] of domains)
70 { 117 {
71 // There's no need to note that a filter is generically disabled. 118 // There's no need to note that a filter is generically disabled.
72 if (!isIncluded && domain == "") 119 if (!isIncluded && domain == "")
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 break; 279 break;
233 280
234 let nextDot = currentDomain.indexOf("."); 281 let nextDot = currentDomain.indexOf(".");
235 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 282 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
236 } 283 }
237 284
238 if (!specificOnly) 285 if (!specificOnly)
239 selectors = getUnconditionalSelectors().concat(selectors); 286 selectors = getUnconditionalSelectors().concat(selectors);
240 287
241 return selectors; 288 return selectors;
289 },
290
291 /**
292 * Creates an element hiding CSS style sheet from a given list of selectors.
293 * @param {Array.<string>} selectors
294 * @returns {string}
295 */
296 createStyleSheet(selectors)
297 {
298 return [...createRules(selectors)].join("\n");
242 } 299 }
243 }; 300 };
OLDNEW
« no previous file with comments | « no previous file | test/elemHide.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld