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

Delta Between Two Patch Sets: include.preload.js

Issue 5989914281771008: Use Shadow DOM for element hiding (Closed)
Left Patch Set: Created April 15, 2014, 9:48 a.m.
Right Patch Set: Fixed Shadow DOM usage and simplified code Created April 15, 2014, 7:06 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 | « background.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 <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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 var SELECTOR_GROUP_SIZE = 20; 18 var SELECTOR_GROUP_SIZE = 20;
19 19
20 var elemhideElt = null;
21
22 // Sets the currently used CSS rules for elemhide filters 20 // Sets the currently used CSS rules for elemhide filters
23 function setElemhideCSSRules(selectors) 21 function setElemhideCSSRules(selectors)
24 { 22 {
25 if (elemhideElt && elemhideElt.parentNode)
26 elemhideElt.parentNode.removeChild(elemhideElt);
27
28 if (selectors.length == 0) 23 if (selectors.length == 0)
Sebastian Noack 2014/04/15 09:58:06 Skip inserting a style tag, if there are no elemen
Wladimir Palant 2014/04/15 13:13:14 That doesn't make much difference in reality - if
Sebastian Noack 2014/04/15 19:21:54 I am aware that at least when using EASYList there
29 return; 24 return;
30 25
31 // Try to insert the style into the <head> tag, inserting directly under the 26 var style = document.createElement("style");
32 // document root breaks dev tools functionality: 27 style.setAttribute("type", "text/css");
33 // http://code.google.com/p/chromium/issues/detail?id=178109
34 var container = document.head || document.documentElement;
35 28
36 // Use Shadow DOM if available to don't mess with web pages 29 // Use Shadow DOM if available to don't mess with web pages
37 // that rely on the order of their own <style> tags (#309) 30 // that rely on the order of their own <style> tags (#309)
38 if ("createShadowRoot" in container) 31 if ("webkitCreateShadowRoot" in document.documentElement)
39 container = container.createShadowRoot(); 32 {
Wladimir Palant 2014/04/15 13:13:14 Given that this standard is still very much in flu
Sebastian Noack 2014/04/15 19:21:54 Done.
40 else if ("webkitCreateShadowRoot" in container) 33 var shadow = document.documentElement.webkitCreateShadowRoot();
41 container = container.webkitCreateShadowRoot(); 34 shadow.appendChild(document.createElement("shadow"));
Wladimir Palant 2014/04/15 13:13:14 Please add a <shadow> element to our shadow root t
Sebastian Noack 2014/04/15 19:21:54 Done.
35 shadow.appendChild(style);
42 36
43 elemhideElt = document.createElement("style"); 37 for (var i = 0; i < selectors.length; i++)
44 elemhideElt.setAttribute("type", "text/css"); 38 selectors[i] = "::-webkit-distributed(" + selectors[i] + ")";
45 container.appendChild(elemhideElt); 39 }
Wladimir Palant 2014/04/15 13:13:14 Will these styles affect the regular elements or o
Sebastian Noack 2014/04/15 19:21:54 You are right, it actually didn't worked with the
40 else
41 {
42 // Try to insert the style into the <head> tag, inserting directly under the
43 // document root breaks dev tools functionality:
44 // http://code.google.com/p/chromium/issues/detail?id=178109
45 (document.head || document.documentElement).appendChild(style);
46 }
46 47
47 var elt = elemhideElt; // Use a local variable to avoid racing conditions 48 // WebKit apparently chokes when the selector list in a CSS rule is huge.
48 function setRules() 49 // So we split the elemhide selectors into groups.
50 for (var i = 0; selectors.length > 0; i++)
49 { 51 {
50 if (!elt.sheet) 52 var selector = selectors.splice(0, SELECTOR_GROUP_SIZE).join(", ");
Sebastian Noack 2014/04/15 19:21:54 I also got rid of that code. The sheet property is
Wladimir Palant 2014/04/15 21:19:47 I didn't remember but hg blame points to https://a
51 { 53 style.sheet.insertRule(selector + " { display: none !important; }", i);
52 // Stylesheet didn't initialize yet, wait a little longer
53 window.setTimeout(setRules, 0);
54 return;
55 }
56
57 // WebKit apparently chokes when the selector list in a CSS rule is huge.
58 // So we split the elemhide selectors into groups.
59 for (var i = 0, j = 0; i < selectors.length; i += SELECTOR_GROUP_SIZE, j++)
60 {
61 var selector = selectors.slice(i, i + SELECTOR_GROUP_SIZE).join(", ");
62 elt.sheet.insertRule(selector + " { display: none !important; }", j);
63 }
64 } 54 }
65 setRules();
66 } 55 }
67 56
68 var typeMap = { 57 var typeMap = {
69 "img": "IMAGE", 58 "img": "IMAGE",
70 "input": "IMAGE", 59 "input": "IMAGE",
71 "audio": "MEDIA", 60 "audio": "MEDIA",
72 "video": "MEDIA", 61 "video": "MEDIA",
73 "frame": "SUBDOCUMENT", 62 "frame": "SUBDOCUMENT",
74 "iframe": "SUBDOCUMENT" 63 "iframe": "SUBDOCUMENT"
75 }; 64 };
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 ext.backgroundPage.sendMessage({type: "add-key-exception", token: attr}); 111 ext.backgroundPage.sendMessage({type: "add-key-exception", token: attr});
123 112
124 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules); 113 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules);
125 } 114 }
126 115
127 // In Chrome 18 the document might not be initialized yet 116 // In Chrome 18 the document might not be initialized yet
128 if (document.documentElement) 117 if (document.documentElement)
129 init(); 118 init();
130 else 119 else
131 window.setTimeout(init, 0); 120 window.setTimeout(init, 0);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld