| 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 {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 | 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 | 29 * <code>{@link createStyleSheet}</code> to split up a long list of selectors |
| 30 * selectors into multiple rules. | 30 * into multiple rules. |
| 31 * @const {number} | 31 * @const {number} |
| 32 * @default | 32 * @default |
| 33 */ | 33 */ |
| 34 const selectorGroupSize = 1024; | 34 const selectorGroupSize = 1024; |
| 35 | 35 |
| 36 /** | 36 /** |
| 37 * Lookup table, active flag, by filter by domain. | 37 * Lookup table, active flag, by filter by domain. |
| 38 * (Only contains filters that aren't unconditionally matched for all domains.) | 38 * (Only contains filters that aren't unconditionally matched for all domains.) |
| 39 * @type {Map.<string,Map.<Filter,boolean>>} | 39 * @type {Map.<string,Map.<Filter,boolean>>} |
| 40 */ | 40 */ |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 60 * @type {Map.<string,boolean>} | 60 * @type {Map.<string,boolean>} |
| 61 * @const | 61 * @const |
| 62 */ | 62 */ |
| 63 let defaultDomains = new Map([["", true]]); | 63 let defaultDomains = new Map([["", true]]); |
| 64 | 64 |
| 65 /** | 65 /** |
| 66 * Set containing known element hiding filters | 66 * Set containing known element hiding filters |
| 67 * @type {Set.<ElemHideFilter>} | 67 * @type {Set.<ElemHideFilter>} |
| 68 */ | 68 */ |
| 69 let knownFilters = new Set(); | 69 let knownFilters = new Set(); |
| 70 | |
| 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 | 70 |
| 109 /** | 71 /** |
| 110 * Adds a filter to the lookup table of filters by domain. | 72 * Adds a filter to the lookup table of filters by domain. |
| 111 * @param {Filter} filter | 73 * @param {Filter} filter |
| 112 */ | 74 */ |
| 113 function addToFiltersByDomain(filter) | 75 function addToFiltersByDomain(filter) |
| 114 { | 76 { |
| 115 let domains = filter.domains || defaultDomains; | 77 let domains = filter.domains || defaultDomains; |
| 116 for (let [domain, isIncluded] of domains) | 78 for (let [domain, isIncluded] of domains) |
| 117 { | 79 { |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 279 break; | 241 break; |
| 280 | 242 |
| 281 let nextDot = currentDomain.indexOf("."); | 243 let nextDot = currentDomain.indexOf("."); |
| 282 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | 244 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); |
| 283 } | 245 } |
| 284 | 246 |
| 285 if (!specificOnly) | 247 if (!specificOnly) |
| 286 selectors = getUnconditionalSelectors().concat(selectors); | 248 selectors = getUnconditionalSelectors().concat(selectors); |
| 287 | 249 |
| 288 return selectors; | 250 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"); | |
| 299 } | 251 } |
| 300 }; | 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; | |
| LEFT | RIGHT |