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

Side by Side Diff: include.preload.js

Issue 5504296699297792: Issue 1802 - Fix element hiding filters with multiple CSS selectors when using shadow DOM (Closed)
Patch Set: Replaced regex with state machine Created Jan. 22, 2015, 7:33 a.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 | no next file » | 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-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 // because we don't know how many rules are already in there 145 // because we don't know how many rules are already in there
146 stylesheet.addRule(selector, "display: none !important;"); 146 stylesheet.addRule(selector, "display: none !important;");
147 } 147 }
148 } 148 }
149 ); 149 );
150 }); 150 });
151 151
152 observer.observe(style.parentNode, {childList: true}); 152 observer.observe(style.parentNode, {childList: true});
153 } 153 }
154 154
155 function convertSelectorsForShadowDOM(selectors)
156 {
157 var result = [];
158 var prefix = "::content ";
159
160 for (var i = 0; i < selectors.length; i++)
161 {
162 var selector = selectors[i];
163 var start = 0;
164 var sep = "";
165
166 for (var j = 0; j < selector.length; j++)
167 {
168 var chr = selector[j];
169 switch (chr)
170 {
171 case "\\":
172 j++;
173 break;
174 case sep:
175 sep = "";
176 break;
177 case '"':
178 case "'":
179 sep = chr;
180 break;
Wladimir Palant 2015/01/22 08:15:52 Note that technically there might be more separato
181 case ",":
182 if (sep == "")
183 {
184 result.push(prefix + selector.substring(start, j));
185 start = j + 1;
186 }
Wladimir Palant 2015/01/22 08:15:52 Nit: please add a break statement here, for consis
Sebastian Noack 2015/01/22 09:40:59 Ah, let's go with if/else then. It's more compact,
187 }
188 }
189
190 result.push(prefix + selector.substring(start, j));
Wladimir Palant 2015/01/22 08:15:52 The second parameter is unnecessary here, can be s
Sebastian Noack 2015/01/22 09:40:59 Oh, you are right.
191 }
192
193 return result;
194 }
195
155 function init(document) 196 function init(document)
156 { 197 {
157 // Use Shadow DOM if available to don't mess with web pages that rely on 198 // Use Shadow DOM if available to don't mess with web pages that rely on
158 // the order of their own <style> tags (#309). 199 // the order of their own <style> tags (#309).
159 // 200 //
160 // However, creating a shadow root breaks running CSS transitions. So we 201 // However, creating a shadow root breaks running CSS transitions. So we
161 // have to create the shadow root before transistions might start (#452). 202 // have to create the shadow root before transistions might start (#452).
162 // 203 //
163 // Also, we can't use shadow DOM on Google Docs, since it breaks printing 204 // Also, we can't use shadow DOM on Google Docs, since it breaks printing
164 // there (#1770). 205 // there (#1770).
165 var shadow = null; 206 var shadow = null;
166 if ("createShadowRoot" in document.documentElement && document.domain != "docs .google.com") 207 if ("createShadowRoot" in document.documentElement && document.domain != "docs .google.com")
167 { 208 {
168 shadow = document.documentElement.createShadowRoot(); 209 shadow = document.documentElement.createShadowRoot();
169 shadow.appendChild(document.createElement("shadow")); 210 shadow.appendChild(document.createElement("shadow"));
170 } 211 }
171 212
172 // Sets the currently used CSS rules for elemhide filters 213 // Sets the currently used CSS rules for elemhide filters
173 var setElemhideCSSRules = function(selectors) 214 var setElemhideCSSRules = function(selectors)
174 { 215 {
175 if (selectors.length == 0) 216 if (selectors.length == 0)
176 return; 217 return;
177 218
178 var style = document.createElement("style"); 219 var style = document.createElement("style");
179 style.setAttribute("type", "text/css"); 220 style.setAttribute("type", "text/css");
180 221
181 if (shadow) 222 if (shadow)
182 { 223 {
183 shadow.appendChild(style); 224 shadow.appendChild(style);
184 225 selectors = convertSelectorsForShadowDOM(selectors);
185 for (var i = 0; i < selectors.length; i++)
186 selectors[i] = "::content " + selectors[i];
187 } 226 }
188 else 227 else
189 { 228 {
190 // Try to insert the style into the <head> tag, inserting directly under t he 229 // Try to insert the style into the <head> tag, inserting directly under t he
191 // document root breaks dev tools functionality: 230 // document root breaks dev tools functionality:
192 // http://code.google.com/p/chromium/issues/detail?id=178109 231 // http://code.google.com/p/chromium/issues/detail?id=178109
193 (document.head || document.documentElement).appendChild(style); 232 (document.head || document.documentElement).appendChild(style);
194 } 233 }
195 234
196 var setRules = function() 235 var setRules = function()
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 }, true); 281 }, true);
243 282
244 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules); 283 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules);
245 } 284 }
246 285
247 if (document instanceof HTMLDocument) 286 if (document instanceof HTMLDocument)
248 { 287 {
249 checkSitekey(); 288 checkSitekey();
250 init(document); 289 init(document);
251 } 290 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld