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

Side by Side Diff: include.preload.js

Issue 29770568: Issue 6645 - Collapse elements through user style sheets if possible (Closed)
Patch Set: Addressed Manish's feedback Created May 9, 2018, 4:28 p.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-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 isCollapsibleMediaElement(element, mediatype) 134 function getSelectorForBlockedElement(element)
135 { 135 {
136 if (mediatype != "MEDIA") 136 // Microsoft Edge does not support CSS.escape(). However, it doesn't
137 return false; 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;
138 141
139 if (!element.getAttribute("src")) 142 // Setting the "display" CSS property to "none" doesn't have any effect on
140 return false; 143 // <frame> elements (in framesets). So we have to hide it inline through
144 // the "visibility" CSS property.
145 if (element.localName == "frame")
146 return null;
141 147
142 for (let child of element.children) 148 // If the <video> or <audio> element contains any <source> or <track>
149 // children, we cannot address it in CSS by the source URL; in that case we
150 // don't "collapse" it using a CSS selector but rather hide it directly by
151 // setting the style="..." attribute.
152 if (element.localName == "video" || element.localName == "audio")
143 { 153 {
144 // If the <video> or <audio> element contains any <source> or <track> 154 for (let child of element.children)
145 // children, we cannot address it in CSS by the source URL; in that case we 155 {
146 // don't "collapse" it using a CSS selector but rather hide it directly by 156 if (child.localName == "source" || child.localName == "track")
147 // setting the style="..." attribute. 157 return null;
148 if (child.localName == "source" || child.localName == "track") 158 }
149 return false;
150 } 159 }
151 160
152 return true; 161 let selector = element.localName;
153 } 162 let hasSrc = false;
Manish Jethani 2018/05/10 01:57:17 Only one small nit, and this is totally a nit. We
Sebastian Noack 2018/05/10 11:45:33 Done.
163 for (let attr of ["src", "srcset"])
Manish Jethani 2018/05/09 23:11:09 If I understand correctly [1], srcset is only rele
Sebastian Noack 2018/05/10 01:08:22 #2 is what my current implementation does. Also co
Manish Jethani 2018/05/10 01:57:17 Ah, I didn't realize you were not breaking from th
164 {
165 let value = element.getAttribute(attr);
166 if (value && attr in element)
167 {
168 selector += "[" + attr + "=" + CSS.escape(value) + "]";
169 hasSrc = true;
170 }
171 }
154 172
155 function collapseMediaElement(element, srcValue) 173 return hasSrc ? selector : null;
156 {
157 if (!srcValue)
158 return;
159
160 let selector = element.localName + "[src=" + CSS.escape(srcValue) + "]";
161
162 // Adding selectors is expensive so do it only if we really have a new
163 // selector.
164 if (!collapsingSelectors.has(selector))
165 {
166 collapsingSelectors.add(selector);
167 elemhide.addSelectors([selector], null, "collapsing", true);
168 }
169 } 174 }
170 175
171 function hideElement(element) 176 function hideElement(element)
172 { 177 {
173 function doHide() 178 function doHide()
174 { 179 {
175 let propertyName = "display"; 180 let propertyName = "display";
176 let propertyValue = "none"; 181 let propertyValue = "none";
177 if (element.localName == "frame") 182 if (element.localName == "frame")
178 { 183 {
(...skipping 19 matching lines...) Expand all
198 function checkCollapse(element) 203 function checkCollapse(element)
199 { 204 {
200 let mediatype = typeMap.get(element.localName); 205 let mediatype = typeMap.get(element.localName);
201 if (!mediatype) 206 if (!mediatype)
202 return; 207 return;
203 208
204 let urls = getURLsFromElement(element); 209 let urls = getURLsFromElement(element);
205 if (urls.length == 0) 210 if (urls.length == 0)
206 return; 211 return;
207 212
208 let collapsibleMediaElement = isCollapsibleMediaElement(element, mediatype); 213 // Construct the selector here, because the attributes it relies on can change
209 214 // between now and when we get the response from the background page.
210 // Save the value of the src attribute because it can change between now and 215 let selector = getSelectorForBlockedElement(element);
211 // when we get the response from the background page.
212 let srcValue = collapsibleMediaElement ? element.getAttribute("src") : null;
213 216
214 browser.runtime.sendMessage( 217 browser.runtime.sendMessage(
215 { 218 {
216 type: "filters.collapse", 219 type: "filters.collapse",
217 urls, 220 urls,
218 mediatype, 221 mediatype,
219 baseURL: document.location.href 222 baseURL: document.location.href
220 }, 223 },
221 collapse => 224 collapse =>
222 { 225 {
223 if (collapse) 226 if (collapse)
224 { 227 {
225 if (collapsibleMediaElement) 228 if (selector)
226 collapseMediaElement(element, srcValue); 229 {
230 if (!collapsingSelectors.has(selector))
231 {
232 collapsingSelectors.add(selector);
233 elemhide.addSelectors([selector], null, "collapsing", true);
234 }
235 }
227 else 236 else
237 {
228 hideElement(element); 238 hideElement(element);
239 }
229 } 240 }
230 } 241 }
231 ); 242 );
232 } 243 }
233 244
234 function checkSitekey() 245 function checkSitekey()
235 { 246 {
236 let attr = document.documentElement.getAttribute("data-adblockkey"); 247 let attr = document.documentElement.getAttribute("data-adblockkey");
237 if (attr) 248 if (attr)
238 browser.runtime.sendMessage({type: "filters.addKey", token: attr}); 249 browser.runtime.sendMessage({type: "filters.addKey", token: attr});
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 let element = event.target; 609 let element = event.target;
599 if (/^i?frame$/.test(element.localName)) 610 if (/^i?frame$/.test(element.localName))
600 checkCollapse(element); 611 checkCollapse(element);
601 }, true); 612 }, true);
602 } 613 }
603 614
604 window.checkCollapse = checkCollapse; 615 window.checkCollapse = checkCollapse;
605 window.elemhide = elemhide; 616 window.elemhide = elemhide;
606 window.typeMap = typeMap; 617 window.typeMap = typeMap;
607 window.getURLsFromElement = getURLsFromElement; 618 window.getURLsFromElement = getURLsFromElement;
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