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

Delta Between Two Patch Sets: include.preload.js

Issue 29770568: Issue 6645 - Collapse elements through user style sheets if possible (Closed)
Left Patch Set: Handle missing CSS.escape() on Microsoft Edge Created May 4, 2018, 3:08 p.m.
Right Patch Set: Got rif of hasSrc variable Created May 10, 2018, 11:45 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 <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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 124
125 for (let i = 0; i < urls.length; i++) 125 for (let i = 0; i < urls.length; i++)
126 { 126 {
127 if (/^(?!https?:)[\w-]+:/i.test(urls[i])) 127 if (/^(?!https?:)[\w-]+:/i.test(urls[i]))
128 urls.splice(i--, 1); 128 urls.splice(i--, 1);
129 } 129 }
130 130
131 return urls; 131 return urls;
132 } 132 }
133 133
134 function getSelectorForUserStyleSheet(element) 134 function getSelectorForBlockedElement(element)
135 { 135 {
136 // Microsoft Edge does not support CSS.escape(). However, it doesn't
137 // support user style sheets either. So the selector would be added
138 // with an author style sheet anyway, which doesn't provide any benefits.
139 if (!("escape" in CSS))
140 return null;
141
136 // Setting the "display" CSS property to "none" doesn't have any effect on 142 // Setting the "display" CSS property to "none" doesn't have any effect on
137 // <frame> elements (in framesets). So we have to hide it inline through 143 // <frame> elements (in framesets). So we have to hide it inline through
138 // the "visisiblity" CSS property. 144 // the "visibility" CSS property.
139 if (element.localName == "frame") 145 if (element.localName == "frame")
140 return null; 146 return null;
141 147
142 // If the <video> or <audio> element contains any <source> or <track> 148 // If the <video> or <audio> element contains any <source> or <track>
143 // children, we cannot address it in CSS by the source URL; in that case we 149 // children, we cannot address it in CSS by the source URL; in that case we
144 // don't "collapse" it using a CSS selector but rather hide it directly by 150 // don't "collapse" it using a CSS selector but rather hide it directly by
145 // setting the style="..." attribute. 151 // setting the style="..." attribute.
146 if (element.localName == "video" || element.localName == "audio") 152 if (element.localName == "video" || element.localName == "audio")
147 { 153 {
148 for (let child of element.children) 154 for (let child of element.children)
149 { 155 {
150 if (child.localName == "source" || child.localName == "track") 156 if (child.localName == "source" || child.localName == "track")
151 return null; 157 return null;
152 } 158 }
153 } 159 }
154 160
155 // Microsoft Edge does not support CSS.escape(). However, it doesn't 161 let selector = "";
156 // support user stylesheets either. So the selector would be added as
157 // an author style sheet anyway.
158 if (!("escape" in CSS))
159 return null;
160
161 let selector = element.localName;
162 let hasSrc = false;
163 for (let attr of ["src", "srcset"]) 162 for (let attr of ["src", "srcset"])
164 { 163 {
165 if (attr in element) 164 let value = element.getAttribute(attr);
166 { 165 if (value && attr in element)
167 let value = element.getAttribute(attr); 166 selector += "[" + attr + "=" + CSS.escape(value) + "]";
168 if (value) 167 }
169 { 168
170 selector += "[" + attr + "=" + CSS.escape(value) + "]"; 169 return selector ? element.localName + selector : null;
171 hasSrc = true;
172 }
173 }
174 }
175
176 return hasSrc ? selector : null;
177 } 170 }
178 171
179 function hideElement(element) 172 function hideElement(element)
180 { 173 {
181 function doHide() 174 function doHide()
182 { 175 {
183 let propertyName = "display"; 176 let propertyName = "display";
184 let propertyValue = "none"; 177 let propertyValue = "none";
185 if (element.localName == "frame") 178 if (element.localName == "frame")
186 { 179 {
(...skipping 21 matching lines...) Expand all
208 let mediatype = typeMap.get(element.localName); 201 let mediatype = typeMap.get(element.localName);
209 if (!mediatype) 202 if (!mediatype)
210 return; 203 return;
211 204
212 let urls = getURLsFromElement(element); 205 let urls = getURLsFromElement(element);
213 if (urls.length == 0) 206 if (urls.length == 0)
214 return; 207 return;
215 208
216 // Construct the selector here, because the attributes it relies on can change 209 // Construct the selector here, because the attributes it relies on can change
217 // between now and when we get the response from the background page. 210 // between now and when we get the response from the background page.
218 let selector = getSelectorForUserStyleSheet(element); 211 let selector = getSelectorForBlockedElement(element);
219 212
220 browser.runtime.sendMessage( 213 browser.runtime.sendMessage(
221 { 214 {
222 type: "filters.collapse", 215 type: "filters.collapse",
223 urls, 216 urls,
224 mediatype, 217 mediatype,
225 baseURL: document.location.href 218 baseURL: document.location.href
226 }, 219 },
227 collapse => 220 collapse =>
228 { 221 {
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 297
305 break nodes; 298 break nodes;
306 } 299 }
307 } 300 }
308 } 301 }
309 } 302 }
310 303
311 if (selectors.length > 0 || filters.length > 0) 304 if (selectors.length > 0 || filters.length > 0)
312 { 305 {
313 browser.runtime.sendMessage({ 306 browser.runtime.sendMessage({
314 type: "devtools.traceElemHide", 307 type: "hitLogger.traceElemHide",
315 selectors, filters 308 selectors, filters
316 }); 309 });
317 } 310 }
318 }, 311 },
319 312
320 onTimeout() 313 onTimeout()
321 { 314 {
322 this.checkNodes(this.changedNodes, this.selectors); 315 this.checkNodes(this.changedNodes, this.selectors);
323 this.changedNodes = []; 316 this.changedNodes = [];
324 this.timeout = null; 317 this.timeout = null;
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 }, 544 },
552 545
553 hideElements(elements, filters) 546 hideElements(elements, filters)
554 { 547 {
555 for (let element of elements) 548 for (let element of elements)
556 hideElement(element); 549 hideElement(element);
557 550
558 if (this.tracer) 551 if (this.tracer)
559 { 552 {
560 browser.runtime.sendMessage({ 553 browser.runtime.sendMessage({
561 type: "devtools.traceElemHide", 554 type: "hitLogger.traceElemHide",
562 selectors: [], 555 selectors: [],
563 filters 556 filters
564 }); 557 });
565 } 558 }
566 }, 559 },
567 560
568 apply() 561 apply()
569 { 562 {
570 browser.runtime.sendMessage({type: "elemhide.getSelectors"}, response => 563 browser.runtime.sendMessage({type: "elemhide.getSelectors"}, response =>
571 { 564 {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 let element = event.target; 605 let element = event.target;
613 if (/^i?frame$/.test(element.localName)) 606 if (/^i?frame$/.test(element.localName))
614 checkCollapse(element); 607 checkCollapse(element);
615 }, true); 608 }, true);
616 } 609 }
617 610
618 window.checkCollapse = checkCollapse; 611 window.checkCollapse = checkCollapse;
619 window.elemhide = elemhide; 612 window.elemhide = elemhide;
620 window.typeMap = typeMap; 613 window.typeMap = typeMap;
621 window.getURLsFromElement = getURLsFromElement; 614 window.getURLsFromElement = getURLsFromElement;
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