OLD | NEW |
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 Loading... |
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)) |
| 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 return [...createRules(selectors)].join("\n"); |
| 300 } |
| 301 |
| 302 exports.createStyleSheet = createStyleSheet; |
OLD | NEW |