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

Delta Between Two Patch Sets: include.preload.js

Issue 5504296699297792: Issue 1802 - Fix element hiding filters with multiple CSS selectors when using shadow DOM (Closed)
Left Patch Set: Added comment explaining regex Created Jan. 15, 2015, 12:56 p.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | 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 <https://adblockplus.org/>,
3 * Copyright (C) 2006-2014 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
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 *
(...skipping 131 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 // Add ::content pseudo-selector (to break out of the shadow DOM)
186 // before every selector, also considering element hiding filters
187 // that specify multiple CSS selectors seperated by comma, while
188 // ignoring commas inside quotes.
189 for (var i = 0; i < selectors.length; i++)
190 selectors[i] = selectors[i].replace(/(\s*)((?:[^,"']|"(?:\\"|[^"])*"|'(? :\\'|[^'])*')+)/g, "$1::content $2");
Wladimir Palant 2015/01/19 20:49:17 Trying to parse CSS selectors via regular expressi
Wladimir Palant 2015/01/19 20:58:15 Just my quick attempt at writing that state machin
Sebastian Noack 2015/01/22 07:37:12 Unfortunately it seems we in fact need a state mac
191 } 218 }
192 else 219 else
193 { 220 {
194 // 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
195 // document root breaks dev tools functionality: 222 // document root breaks dev tools functionality:
196 // http://code.google.com/p/chromium/issues/detail?id=178109 223 // http://code.google.com/p/chromium/issues/detail?id=178109
197 (document.head || document.documentElement).appendChild(style); 224 (document.head || document.documentElement).appendChild(style);
198 } 225 }
199 226
200 var setRules = function() 227 var setRules = function()
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 }, true); 273 }, true);
247 274
248 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules); 275 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules);
249 } 276 }
250 277
251 if (document instanceof HTMLDocument) 278 if (document instanceof HTMLDocument)
252 { 279 {
253 checkSitekey(); 280 checkSitekey();
254 init(document); 281 init(document);
255 } 282 }
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld