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: 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:
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 = "";
153 } 162 for (let attr of ["src", "srcset"])
163 {
164 let value = element.getAttribute(attr);
165 if (value && attr in element)
166 selector += "[" + attr + "=" + CSS.escape(value) + "]";
167 }
154 168
155 function collapseMediaElement(element, srcValue) 169 return selector ? element.localName + 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 } 170 }
170 171
171 function hideElement(element) 172 function hideElement(element)
172 { 173 {
173 function doHide() 174 function doHide()
174 { 175 {
175 let propertyName = "display"; 176 let propertyName = "display";
176 let propertyValue = "none"; 177 let propertyValue = "none";
177 if (element.localName == "frame") 178 if (element.localName == "frame")
178 { 179 {
(...skipping 19 matching lines...) Expand all
198 function checkCollapse(element) 199 function checkCollapse(element)
199 { 200 {
200 let mediatype = typeMap.get(element.localName); 201 let mediatype = typeMap.get(element.localName);
201 if (!mediatype) 202 if (!mediatype)
202 return; 203 return;
203 204
204 let urls = getURLsFromElement(element); 205 let urls = getURLsFromElement(element);
205 if (urls.length == 0) 206 if (urls.length == 0)
206 return; 207 return;
207 208
208 let collapsibleMediaElement = isCollapsibleMediaElement(element, mediatype); 209 // Construct the selector here, because the attributes it relies on can change
209 210 // 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 211 let selector = getSelectorForBlockedElement(element);
211 // when we get the response from the background page.
212 let srcValue = collapsibleMediaElement ? element.getAttribute("src") : null;
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 {
223 if (collapse) 222 if (collapse)
224 { 223 {
225 if (collapsibleMediaElement) 224 if (selector)
226 collapseMediaElement(element, srcValue); 225 {
226 if (!collapsingSelectors.has(selector))
227 {
228 collapsingSelectors.add(selector);
229 elemhide.addSelectors([selector], null, "collapsing", true);
230 }
231 }
227 else 232 else
233 {
228 hideElement(element); 234 hideElement(element);
235 }
229 } 236 }
230 } 237 }
231 ); 238 );
232 } 239 }
233 240
234 function checkSitekey() 241 function checkSitekey()
235 { 242 {
236 let attr = document.documentElement.getAttribute("data-adblockkey"); 243 let attr = document.documentElement.getAttribute("data-adblockkey");
237 if (attr) 244 if (attr)
238 browser.runtime.sendMessage({type: "filters.addKey", token: attr}); 245 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; 605 let element = event.target;
599 if (/^i?frame$/.test(element.localName)) 606 if (/^i?frame$/.test(element.localName))
600 checkCollapse(element); 607 checkCollapse(element);
601 }, true); 608 }, true);
602 } 609 }
603 610
604 window.checkCollapse = checkCollapse; 611 window.checkCollapse = checkCollapse;
605 window.elemhide = elemhide; 612 window.elemhide = elemhide;
606 window.typeMap = typeMap; 613 window.typeMap = typeMap;
607 window.getURLsFromElement = getURLsFromElement; 614 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