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: Addressed comments Created Jan. 22, 2015, 9:40 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 if (chr == "\\")
170 j++;
171 else if (chr == sep)
172 sep = "";
173 else if (chr == '"' || chr == "'")
174 sep = chr;
175 else if (chr == "," && sep == "")
176 {
177 result.push(prefix + selector.substring(start, j));
178 start = j + 1;
179 }
180 }
181
182 result.push(prefix + selector.substring(start));
183 }
184
185 return result;
186 }
187
155 function init(document) 188 function init(document)
156 { 189 {
157 // Use Shadow DOM if available to don't mess with web pages that rely on 190 // Use Shadow DOM if available to don't mess with web pages that rely on
158 // the order of their own <style> tags (#309). 191 // the order of their own <style> tags (#309).
159 // 192 //
160 // However, creating a shadow root breaks running CSS transitions. So we 193 // However, creating a shadow root breaks running CSS transitions. So we
161 // have to create the shadow root before transistions might start (#452). 194 // have to create the shadow root before transistions might start (#452).
162 // 195 //
163 // Also, we can't use shadow DOM on Google Docs, since it breaks printing 196 // Also, we can't use shadow DOM on Google Docs, since it breaks printing
164 // there (#1770). 197 // there (#1770).
165 var shadow = null; 198 var shadow = null;
166 if ("createShadowRoot" in document.documentElement && document.domain != "docs .google.com") 199 if ("createShadowRoot" in document.documentElement && document.domain != "docs .google.com")
167 { 200 {
168 shadow = document.documentElement.createShadowRoot(); 201 shadow = document.documentElement.createShadowRoot();
169 shadow.appendChild(document.createElement("shadow")); 202 shadow.appendChild(document.createElement("shadow"));
170 } 203 }
171 204
172 // Sets the currently used CSS rules for elemhide filters 205 // Sets the currently used CSS rules for elemhide filters
173 var setElemhideCSSRules = function(selectors) 206 var setElemhideCSSRules = function(selectors)
174 { 207 {
175 if (selectors.length == 0) 208 if (selectors.length == 0)
176 return; 209 return;
177 210
178 var style = document.createElement("style"); 211 var style = document.createElement("style");
179 style.setAttribute("type", "text/css"); 212 style.setAttribute("type", "text/css");
180 213
181 if (shadow) 214 if (shadow)
182 { 215 {
183 shadow.appendChild(style); 216 shadow.appendChild(style);
184 217 selectors = convertSelectorsForShadowDOM(selectors);
185 for (var i = 0; i < selectors.length; i++)
186 selectors[i] = "::content " + selectors[i];
187 } 218 }
188 else 219 else
189 { 220 {
190 // Try to insert the style into the <head> tag, inserting directly under t he 221 // Try to insert the style into the <head> tag, inserting directly under t he
191 // document root breaks dev tools functionality: 222 // document root breaks dev tools functionality:
192 // http://code.google.com/p/chromium/issues/detail?id=178109 223 // http://code.google.com/p/chromium/issues/detail?id=178109
193 (document.head || document.documentElement).appendChild(style); 224 (document.head || document.documentElement).appendChild(style);
194 } 225 }
195 226
196 var setRules = function() 227 var setRules = function()
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 }, true); 273 }, true);
243 274
244 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules); 275 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules);
245 } 276 }
246 277
247 if (document instanceof HTMLDocument) 278 if (document instanceof HTMLDocument)
248 { 279 {
249 checkSitekey(); 280 checkSitekey();
250 init(document); 281 init(document);
251 } 282 }
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