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: Created May 4, 2018, 2:17 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 getSelectorForUserStyleSheet(element)
135 { 135 {
136 if (mediatype != "MEDIA") 136 // Setting the "display" CSS property to "none" doesn't have any effect on
137 return false; 137 // <frame> elements (in framesets). So we have to hide it inline through
138 // the "visisiblity" CSS property.
139 if (element.localName == "frame")
140 return null;
138 141
139 if (!element.getAttribute("src")) 142 // If the <video> or <audio> element contains any <source> or <track>
140 return false; 143 // children, we cannot address it in CSS by the source URL; in that case we
141 144 // don't "collapse" it using a CSS selector but rather hide it directly by
142 for (let child of element.children) 145 // setting the style="..." attribute.
146 if (element.localName == "video" || element.localName == "audio")
143 { 147 {
144 // If the <video> or <audio> element contains any <source> or <track> 148 for (let child of element.children)
145 // children, we cannot address it in CSS by the source URL; in that case we 149 {
146 // don't "collapse" it using a CSS selector but rather hide it directly by 150 if (child.localName == "source" || child.localName == "track")
147 // setting the style="..." attribute. 151 return null;
148 if (child.localName == "source" || child.localName == "track") 152 }
149 return false;
150 } 153 }
151 154
152 return true; 155 let selector = element.localName;
153 } 156 let hasSrc = false;
157 for (let attr of ["src", "srcset"])
158 {
159 if (attr in element)
160 {
161 let value = element.getAttribute(attr);
162 if (value)
163 {
164 selector += "[" + attr + "=" + CSS.escape(value) + "]";
165 hasSrc = true;
166 }
167 }
168 }
154 169
155 function collapseMediaElement(element, srcValue) 170 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 } 171 }
170 172
171 function hideElement(element) 173 function hideElement(element)
172 { 174 {
173 function doHide() 175 function doHide()
174 { 176 {
175 let propertyName = "display"; 177 let propertyName = "display";
176 let propertyValue = "none"; 178 let propertyValue = "none";
177 if (element.localName == "frame") 179 if (element.localName == "frame")
178 { 180 {
(...skipping 19 matching lines...) Expand all
198 function checkCollapse(element) 200 function checkCollapse(element)
199 { 201 {
200 let mediatype = typeMap.get(element.localName); 202 let mediatype = typeMap.get(element.localName);
201 if (!mediatype) 203 if (!mediatype)
202 return; 204 return;
203 205
204 let urls = getURLsFromElement(element); 206 let urls = getURLsFromElement(element);
205 if (urls.length == 0) 207 if (urls.length == 0)
206 return; 208 return;
207 209
208 let collapsibleMediaElement = isCollapsibleMediaElement(element, mediatype); 210 // Construct the selector here, because the attributes it relies on can change
209 211 // 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 212 let selector = getSelectorForUserStyleSheet(element);
211 // when we get the response from the background page.
212 let srcValue = collapsibleMediaElement ? element.getAttribute("src") : null;
213 213
214 browser.runtime.sendMessage( 214 browser.runtime.sendMessage(
215 { 215 {
216 type: "filters.collapse", 216 type: "filters.collapse",
217 urls, 217 urls,
218 mediatype, 218 mediatype,
219 baseURL: document.location.href 219 baseURL: document.location.href
220 }, 220 },
221 collapse => 221 collapse =>
222 { 222 {
223 if (collapse) 223 if (collapse)
224 { 224 {
225 if (collapsibleMediaElement) 225 if (selector)
226 collapseMediaElement(element, srcValue); 226 {
227 if (!collapsingSelectors.has(selector))
228 {
229 collapsingSelectors.add(selector);
230 elemhide.addSelectors([selector], null, "collapsing", true);
231 }
232 }
227 else 233 else
234 {
228 hideElement(element); 235 hideElement(element);
236 }
229 } 237 }
230 } 238 }
231 ); 239 );
232 } 240 }
233 241
234 function checkSitekey() 242 function checkSitekey()
235 { 243 {
236 let attr = document.documentElement.getAttribute("data-adblockkey"); 244 let attr = document.documentElement.getAttribute("data-adblockkey");
237 if (attr) 245 if (attr)
238 browser.runtime.sendMessage({type: "filters.addKey", token: attr}); 246 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; 606 let element = event.target;
599 if (/^i?frame$/.test(element.localName)) 607 if (/^i?frame$/.test(element.localName))
600 checkCollapse(element); 608 checkCollapse(element);
601 }, true); 609 }, true);
602 } 610 }
603 611
604 window.checkCollapse = checkCollapse; 612 window.checkCollapse = checkCollapse;
605 window.elemhide = elemhide; 613 window.elemhide = elemhide;
606 window.typeMap = typeMap; 614 window.typeMap = typeMap;
607 window.getURLsFromElement = getURLsFromElement; 615 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