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: Avoid temporary array and join Created Sept. 18, 2018, 3:11 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 createStyleSheet}</code> to split up a long list of selectors
30 * 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 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 let nextDot = currentDomain.indexOf("."); 243 let nextDot = currentDomain.indexOf(".");
235 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 244 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
236 } 245 }
237 246
238 if (!specificOnly) 247 if (!specificOnly)
239 selectors = getUnconditionalSelectors().concat(selectors); 248 selectors = getUnconditionalSelectors().concat(selectors);
240 249
241 return selectors; 250 return selectors;
242 } 251 }
243 }; 252 };
253
254 /**
255 * Splits a list of selectors into groups determined by the value of
256 * <code>{@link selectorGroupSize}</code>.
257 *
258 * @param {Array.<string>} selectors
259 * @yields {Array.<string>}
260 */
261 function* splitSelectors(selectors)
262 {
263 // Chromium's Blink engine supports only up to 8,192 simple selectors, and
264 // even fewer compound selectors, in a rule. The exact number of selectors
265 // that would work depends on their sizes (e.g. "#foo .bar" has a size of 2).
266 // Since we don't know the sizes of the selectors here, we simply split them
267 // into groups of 1,024, based on the reasonable assumption that the average
268 // selector won't have a size greater than 8. The alternative would be to
269 // calculate the sizes of the selectors and divide them up accordingly, but
270 // this approach is more efficient and has worked well in practice. In theory
271 // this could still lead to some selectors not working on Chromium, but it is
272 // highly unlikely.
273 // See issue #6298 and https://crbug.com/804179
274 for (let i = 0; i < selectors.length; i += selectorGroupSize)
275 yield selectors.slice(i, i + selectorGroupSize);
276 }
277
278 /**
279 * Creates element hiding CSS rules for a given list of selectors. Each rule
280 * contains no more than the maximum number of selectors as determined by the
281 * value of <code>{@link selectorGroupSize}</code>.
282 *
283 * @param {Array.<string>} selectors
284 * @yields {string}
285 */
286 function* createRules(selectors)
287 {
288 for (let selectorGroup of splitSelectors(selectors))
Sebastian Noack 2018/09/18 15:33:24 Since splitSelectors() is only called from one cod
Manish Jethani 2018/09/18 15:50:02 I tried this and it seems to actually perform slig
Sebastian Noack 2018/09/18 16:41:45 V8 optimizes functions regardless of size, however
Manish Jethani 2018/09/18 17:24:07 There is a specific optimization in TurboFan calle
Sebastian Noack 2018/09/18 17:40:54 This appears rather odd to me. On which version of
Manish Jethani 2018/09/18 17:47:46 I did it on Chrome 71 (Canary). It's not going to
Manish Jethani 2018/09/18 19:51:57 Alright, here we go. The alternative version of t
Sebastian Noack 2018/09/18 21:21:03 I think the main target for any benchmark should a
Manish Jethani 2018/09/19 09:39:00 A lot of the best programmers would disagree that
Manish Jethani 2018/09/19 10:18:33 Since I was modifying the `createStyleSheet` funct
Manish Jethani 2018/09/19 10:29:08 Also: https://arstechnica.com/information-technolo
Sebastian Noack 2018/09/19 11:05:53 I'm obviously not trying to argue that functions s
Manish Jethani 2018/09/19 13:14:41 If they are logically separate (as in, the program
Jon Sonesen 2018/09/19 16:36:26 FWIW I would say that Manish is right about contex
289 yield selectorGroup.join(", ") + " {display: none !important;}";
290 }
291
292 /**
293 * Creates an element hiding CSS style sheet from a given list of selectors.
294 * @param {Array.<string>} selectors
295 * @returns {string}
296 */
297 function createStyleSheet(selectors)
298 {
299 let styleSheet = "";
300
301 for (let rule of createRules(selectors))
302 styleSheet += rule + "\n";
303
304 return styleSheet;
305 }
306
307 exports.createStyleSheet = createStyleSheet;
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