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

Delta Between Two Patch Sets: lib/cssInjection.js

Issue 29679796: Issue 6298 - Split injected CSS hiding rule into groups of 1,024 selectors (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Created Jan. 25, 2018, 5:12 p.m.
Right Patch Set: Update comment explaining why we split the selectors into groups of 1,024 Created Jan. 30, 2018, 1:39 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 | « include.preload.js ('k') | no next file » | 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
(...skipping 11 matching lines...) Expand all
22 const {RegExpFilter} = require("filterClasses"); 22 const {RegExpFilter} = require("filterClasses");
23 const {ElemHide} = require("elemHide"); 23 const {ElemHide} = require("elemHide");
24 const {ElemHideEmulation} = require("elemHideEmulation"); 24 const {ElemHideEmulation} = require("elemHideEmulation");
25 const {checkWhitelisted} = require("whitelisting"); 25 const {checkWhitelisted} = require("whitelisting");
26 const {extractHostFromFrame} = require("url"); 26 const {extractHostFromFrame} = require("url");
27 const {port} = require("messaging"); 27 const {port} = require("messaging");
28 const devtools = require("devtools"); 28 const devtools = require("devtools");
29 29
30 const userStyleSheetsSupported = "extensionTypes" in browser && 30 const userStyleSheetsSupported = "extensionTypes" in browser &&
31 "CSSOrigin" in browser.extensionTypes; 31 "CSSOrigin" in browser.extensionTypes;
32 const selectorGroupSize = 200; 32 const selectorGroupSize = 1024;
33
34 function* splitSelectors(selectors)
35 {
36 // Chromium's Blink engine supports only up to 8,192 simple selectors, and
37 // even fewer compound selectors, in a rule. The exact number of selectors
38 // that would work depends on their sizes (e.g. "#foo .bar" has a size of 2).
39 // Since we don't know the sizes of the selectors here, we simply split them
40 // into groups of 1,024, based on the reasonable assumption that the average
41 // selector won't have a size greater than 8. The alternative would be to
42 // calculate the sizes of the selectors and divide them up accordingly, but
43 // this approach is more efficient and has worked well in practice. In theory
44 // this could still lead to some selectors not working on Chromium, but it is
45 // highly unlikely.
46 // See issue #6298 and https://crbug.com/804179
47 for (let i = 0; i < selectors.length; i += selectorGroupSize)
48 yield selectors.slice(i, i + selectorGroupSize);
49 }
50
51 function* createRules(selectors)
52 {
53 for (let selectorGroup of splitSelectors(selectors))
54 yield selectorGroup.join(", ") + " {display: none !important;}";
55 }
33 56
34 function createStyleSheet(selectors) 57 function createStyleSheet(selectors)
35 { 58 {
36 let styleSheet = ""; 59 return Array.from(createRules(selectors)).join("\n");
37
38 // Chromium's Blink engine supports only up to 8,192 selectors; more
kzar 2018/01/25 17:46:47 If we're limited to 8192 why do we go with 200?
Manish Jethani 2018/01/25 18:42:41 It's "up to 8,192" selectors. If you have 8,192 s
Manish Jethani 2018/01/25 18:44:28 This was not technically true by the way but now [
Manish Jethani 2018/01/30 06:19:43 Now that I think about it, maybe we should increas
kzar1 2018/01/30 11:39:59 Sounds good to me, but let's rewrite the comment t
Manish Jethani 2018/01/30 13:15:43 Unfortunately this comment is confusing and techni
39 // specifically, it ignores any selectors that start at index 8192 or beyond
40 // in the list of plain selectors. In order to avoid spilling outside of this
41 // range, we simply add multiple rules in groups of up to 200 selectors each.
42 // See issue #6298 and https://crbug.com/804179
43 for (let i = 0; i < selectors.length; i += selectorGroupSize)
44 {
45 styleSheet += selectors.slice(i, i + selectorGroupSize).join(", ") +
46 " {display: none !important;}\n";
47 }
48
49 return styleSheet;
50 } 60 }
51 61
52 function addStyleSheet(tabId, frameId, styleSheet) 62 function addStyleSheet(tabId, frameId, styleSheet)
53 { 63 {
54 browser.tabs.insertCSS(tabId, { 64 browser.tabs.insertCSS(tabId, {
55 code: styleSheet, 65 code: styleSheet,
56 cssOrigin: "user", 66 cssOrigin: "user",
57 frameId, 67 frameId,
58 matchAboutBlank: true, 68 matchAboutBlank: true,
59 runAt: "document_start" 69 runAt: "document_start"
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 response.selectors = selectors; 143 response.selectors = selectors;
134 144
135 return response; 145 return response;
136 }); 146 });
137 147
138 port.on("elemhide.injectSelectors", (message, sender) => 148 port.on("elemhide.injectSelectors", (message, sender) =>
139 { 149 {
140 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 150 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
141 message.groupName); 151 message.groupName);
142 }); 152 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld