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

Side by Side Diff: include.preload.js

Issue 29401596: Issue 5094 - Implement support for :has() in chrome extension (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Properly implement the tracer. Improve the element hiding logic. Created April 5, 2017, 9:07 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 | « dependencies ('k') | lib/filterValidation.js » ('j') | 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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 120
121 for (let i = 0; i < urls.length; i++) 121 for (let i = 0; i < urls.length; i++)
122 { 122 {
123 if (/^(?!https?:)[\w-]+:/i.test(urls[i])) 123 if (/^(?!https?:)[\w-]+:/i.test(urls[i]))
124 urls.splice(i--, 1); 124 urls.splice(i--, 1);
125 } 125 }
126 126
127 return urls; 127 return urls;
128 } 128 }
129 129
130 /* collapse the element and ensure it stays that way */
Sebastian Noack 2017/04/05 14:58:49 If you feel that this function needs to be documen
131 function collapseElement(element)
Sebastian Noack 2017/04/05 14:58:49 "element collapsing" is a special term in the cont
132 {
133 function doCollapse(element)
134 {
135 let propertyName = "display";
136 let propertyValue = "none";
137 if (element.localName == "frame")
138 {
139 propertyName = "visibility";
140 propertyValue = "hidden";
141 }
142
143 if (element.style.getPropertyValue(propertyName) != propertyValue ||
144 element.style.getPropertyPriority(propertyName) != "important")
145 element.style.setProperty(propertyName, propertyValue, "important");
146 }
147
148 doCollapse(element);
149
150 new MutationObserver(doCollapse).observe(
151 element, {
152 attributes: true,
153 attributeFilter: ["style"]
154 }
155 );
156 }
157
130 function checkCollapse(element) 158 function checkCollapse(element)
131 { 159 {
132 let mediatype = typeMap.get(element.localName); 160 let mediatype = typeMap.get(element.localName);
133 if (!mediatype) 161 if (!mediatype)
134 return; 162 return;
135 163
136 let urls = getURLsFromElement(element); 164 let urls = getURLsFromElement(element);
137 if (urls.length == 0) 165 if (urls.length == 0)
138 return; 166 return;
139 167
140 ext.backgroundPage.sendMessage( 168 ext.backgroundPage.sendMessage(
141 { 169 {
142 type: "filters.collapse", 170 type: "filters.collapse",
143 urls, 171 urls,
144 mediatype, 172 mediatype,
145 baseURL: document.location.href 173 baseURL: document.location.href
146 }, 174 },
147 175
148 collapse => 176 collapse =>
149 { 177 {
150 function collapseElement()
151 {
152 let propertyName = "display";
153 let propertyValue = "none";
154 if (element.localName == "frame")
155 {
156 propertyName = "visibility";
157 propertyValue = "hidden";
158 }
159
160 if (element.style.getPropertyValue(propertyName) != propertyValue ||
161 element.style.getPropertyPriority(propertyName) != "important")
162 element.style.setProperty(propertyName, propertyValue, "important");
163 }
164
165 if (collapse) 178 if (collapse)
166 { 179 {
167 collapseElement(); 180 collapseElement(element);
168
169 new MutationObserver(collapseElement).observe(
170 element, {
171 attributes: true,
172 attributeFilter: ["style"]
173 }
174 );
175 } 181 }
176 } 182 }
177 ); 183 );
178 } 184 }
179 185
180 function checkSitekey() 186 function checkSitekey()
181 { 187 {
182 let attr = document.documentElement.getAttribute("data-adblockkey"); 188 let attr = document.documentElement.getAttribute("data-adblockkey");
183 if (attr) 189 if (attr)
184 ext.backgroundPage.sendMessage({type: "filters.addKey", token: attr}); 190 ext.backgroundPage.sendMessage({type: "filters.addKey", token: attr});
(...skipping 18 matching lines...) Expand all
203 ElementHidingTracer.prototype = { 209 ElementHidingTracer.prototype = {
204 addSelectors(selectors, filters) 210 addSelectors(selectors, filters)
205 { 211 {
206 if (document.readyState != "loading") 212 if (document.readyState != "loading")
207 this.checkNodes([document], selectors, filters); 213 this.checkNodes([document], selectors, filters);
208 214
209 this.selectors.push(...selectors); 215 this.selectors.push(...selectors);
210 this.filters.push(...filters); 216 this.filters.push(...filters);
211 }, 217 },
212 218
219 hideElements(filters)
Sebastian Noack 2017/04/05 14:58:49 The name of this function isn't optimal. It doesn'
hub 2017/04/06 10:15:20 ok
220 {
221 let matchedSelectors = [];
222 for (let filter of filters)
223 matchedSelectors.push(filter.replace(/^.*?##/, ""));
Sebastian Noack 2017/04/05 14:58:49 This logic is redundant with code in checkNodes().
hub 2017/04/06 10:15:20 Acknowledged.
224
225 if (document.readyState != "loading")
Sebastian Noack 2017/04/05 14:58:49 This check seems wrong. ElementHidingTracer.addSel
hub 2017/04/06 10:15:20 Acknowledged.
226 ext.backgroundPage.sendMessage({
227 type: "devtools.traceElemHide",
228 selectors: matchedSelectors
229 });
230 },
231
213 checkNodes(nodes, selectors, filters) 232 checkNodes(nodes, selectors, filters)
214 { 233 {
215 let matchedSelectors = []; 234 let matchedSelectors = [];
216 235
217 for (let i = 0; i < selectors.length; i++) 236 for (let i = 0; i < selectors.length; i++)
218 { 237 {
219 nodes: for (let node of nodes) 238 nodes: for (let node of nodes)
220 { 239 {
221 let elements = node.querySelectorAll(selectors[i]); 240 let elements = node.querySelectorAll(selectors[i]);
222 241
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 442
424 this.elemHideEmulation = new ElemHideEmulation( 443 this.elemHideEmulation = new ElemHideEmulation(
425 window, 444 window,
426 callback => 445 callback =>
427 { 446 {
428 ext.backgroundPage.sendMessage({ 447 ext.backgroundPage.sendMessage({
429 type: "filters.get", 448 type: "filters.get",
430 what: "elemhideemulation" 449 what: "elemhideemulation"
431 }, callback); 450 }, callback);
432 }, 451 },
433 this.addSelectors.bind(this) 452 this.addSelectors.bind(this),
453 this.hideElements.bind(this)
434 ); 454 );
435 } 455 }
436 ElemHide.prototype = { 456 ElemHide.prototype = {
437 selectorGroupSize: 200, 457 selectorGroupSize: 200,
438 458
439 createShadowTree() 459 createShadowTree()
440 { 460 {
441 // Use Shadow DOM if available as to not mess with with web pages that 461 // Use Shadow DOM if available as to not mess with with web pages that
442 // rely on the order of their own <style> tags (#309). However, creating 462 // rely on the order of their own <style> tags (#309). However, creating
443 // a shadow root breaks running CSS transitions. So we have to create 463 // a shadow root breaks running CSS transitions. So we have to create
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 i, i + this.selectorGroupSize 554 i, i + this.selectorGroupSize
535 ).join(", "); 555 ).join(", ");
536 this.style.sheet.insertRule(selector + "{display: none !important;}", 556 this.style.sheet.insertRule(selector + "{display: none !important;}",
537 this.style.sheet.cssRules.length); 557 this.style.sheet.cssRules.length);
538 } 558 }
539 559
540 if (this.tracer) 560 if (this.tracer)
541 this.tracer.addSelectors(selectors, filters || selectors); 561 this.tracer.addSelectors(selectors, filters || selectors);
542 }, 562 },
543 563
564 hideElements(elements, filters)
565 {
566 for (let element of elements)
567 collapseElement(element);
568
569 if (this.tracer)
570 this.tracer.hideElements(filters);
571 },
572
544 apply() 573 apply()
545 { 574 {
546 ext.backgroundPage.sendMessage({type: "get-selectors"}, response => 575 ext.backgroundPage.sendMessage({type: "get-selectors"}, response =>
547 { 576 {
548 if (this.tracer) 577 if (this.tracer)
549 this.tracer.disconnect(); 578 this.tracer.disconnect();
550 this.tracer = null; 579 this.tracer = null;
551 580
552 if (this.style && this.style.parentElement) 581 if (this.style && this.style.parentElement)
553 this.style.parentElement.removeChild(this.style); 582 this.style.parentElement.removeChild(this.style);
(...skipping 21 matching lines...) Expand all
575 checkCollapse(event.target); 604 checkCollapse(event.target);
576 }, true); 605 }, true);
577 606
578 document.addEventListener("load", event => 607 document.addEventListener("load", event =>
579 { 608 {
580 let element = event.target; 609 let element = event.target;
581 if (/^i?frame$/.test(element.localName)) 610 if (/^i?frame$/.test(element.localName))
582 checkCollapse(element); 611 checkCollapse(element);
583 }, true); 612 }, true);
584 } 613 }
OLDNEW
« no previous file with comments | « dependencies ('k') | lib/filterValidation.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld