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: Created May 4, 2018, 2:17 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 let selector = element.localName; 161 let selector = "";
156 let hasSrc = false;
157 for (let attr of ["src", "srcset"]) 162 for (let attr of ["src", "srcset"])
158 { 163 {
159 if (attr in element) 164 let value = element.getAttribute(attr);
160 { 165 if (value && attr in element)
161 let value = element.getAttribute(attr); 166 selector += "[" + attr + "=" + CSS.escape(value) + "]";
162 if (value) 167 }
163 { 168
164 selector += "[" + attr + "=" + CSS.escape(value) + "]"; 169 return selector ? element.localName + selector : null;
165 hasSrc = true;
166 }
167 }
168 }
169
170 return hasSrc ? selector : null;
171 } 170 }
172 171
173 function hideElement(element) 172 function hideElement(element)
174 { 173 {
175 function doHide() 174 function doHide()
176 { 175 {
177 let propertyName = "display"; 176 let propertyName = "display";
178 let propertyValue = "none"; 177 let propertyValue = "none";
179 if (element.localName == "frame") 178 if (element.localName == "frame")
180 { 179 {
(...skipping 21 matching lines...) Expand all
202 let mediatype = typeMap.get(element.localName); 201 let mediatype = typeMap.get(element.localName);
203 if (!mediatype) 202 if (!mediatype)
204 return; 203 return;
205 204
206 let urls = getURLsFromElement(element); 205 let urls = getURLsFromElement(element);
207 if (urls.length == 0) 206 if (urls.length == 0)
208 return; 207 return;
209 208
210 // 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
211 // 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.
212 let selector = getSelectorForUserStyleSheet(element); 211 let selector = getSelectorForBlockedElement(element);
213 212
214 browser.runtime.sendMessage( 213 browser.runtime.sendMessage(
215 { 214 {
216 type: "filters.collapse", 215 type: "filters.collapse",
217 urls, 216 urls,
218 mediatype, 217 mediatype,
219 baseURL: document.location.href 218 baseURL: document.location.href
220 }, 219 },
221 collapse => 220 collapse =>
222 { 221 {
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 297
299 break nodes; 298 break nodes;
300 } 299 }
301 } 300 }
302 } 301 }
303 } 302 }
304 303
305 if (selectors.length > 0 || filters.length > 0) 304 if (selectors.length > 0 || filters.length > 0)
306 { 305 {
307 browser.runtime.sendMessage({ 306 browser.runtime.sendMessage({
308 type: "devtools.traceElemHide", 307 type: "hitLogger.traceElemHide",
309 selectors, filters 308 selectors, filters
310 }); 309 });
311 } 310 }
312 }, 311 },
313 312
314 onTimeout() 313 onTimeout()
315 { 314 {
316 this.checkNodes(this.changedNodes, this.selectors); 315 this.checkNodes(this.changedNodes, this.selectors);
317 this.changedNodes = []; 316 this.changedNodes = [];
318 this.timeout = null; 317 this.timeout = null;
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
545 }, 544 },
546 545
547 hideElements(elements, filters) 546 hideElements(elements, filters)
548 { 547 {
549 for (let element of elements) 548 for (let element of elements)
550 hideElement(element); 549 hideElement(element);
551 550
552 if (this.tracer) 551 if (this.tracer)
553 { 552 {
554 browser.runtime.sendMessage({ 553 browser.runtime.sendMessage({
555 type: "devtools.traceElemHide", 554 type: "hitLogger.traceElemHide",
556 selectors: [], 555 selectors: [],
557 filters 556 filters
558 }); 557 });
559 } 558 }
560 }, 559 },
561 560
562 apply() 561 apply()
563 { 562 {
564 browser.runtime.sendMessage({type: "elemhide.getSelectors"}, response => 563 browser.runtime.sendMessage({type: "elemhide.getSelectors"}, response =>
565 { 564 {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
606 let element = event.target; 605 let element = event.target;
607 if (/^i?frame$/.test(element.localName)) 606 if (/^i?frame$/.test(element.localName))
608 checkCollapse(element); 607 checkCollapse(element);
609 }, true); 608 }, true);
610 } 609 }
611 610
612 window.checkCollapse = checkCollapse; 611 window.checkCollapse = checkCollapse;
613 window.elemhide = elemhide; 612 window.elemhide = elemhide;
614 window.typeMap = typeMap; 613 window.typeMap = typeMap;
615 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