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

Delta Between Two Patch Sets: test/browser/elemHideEmulation.js

Issue 29383960: Issue 3143 - Filter elements with :-abp-has() (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Left Patch Set: Last comments. Created June 8, 2017, 12:15 a.m.
Right Patch Set: Fix reportError and the error message Created June 13, 2017, 1:52 p.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 | « chrome/content/elemHideEmulation.js ('k') | 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-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 14 matching lines...) Expand all
25 let myUrl = document.currentScript.src; 25 let myUrl = document.currentScript.src;
26 26
27 exports.tearDown = function(callback) 27 exports.tearDown = function(callback)
28 { 28 {
29 let styleElements = document.head.getElementsByTagName("style"); 29 let styleElements = document.head.getElementsByTagName("style");
30 while (styleElements.length) 30 while (styleElements.length)
31 styleElements[0].parentNode.removeChild(styleElements[0]); 31 styleElements[0].parentNode.removeChild(styleElements[0]);
32 32
33 let child; 33 let child;
34 while (child = document.body.firstChild) 34 while (child = document.body.firstChild)
35 document.body.removeChild(child); 35 child.parentNode.removeChild(child);
Wladimir Palant 2017/06/08 13:12:42 If you store the child in an intermediate variable
hub 2017/06/08 15:45:48 Done.
36 36
37 callback(); 37 callback();
38 }; 38 };
39 39
40 function unexpectedError(error) 40 function unexpectedError(error)
41 { 41 {
42 console.error(error); 42 console.error(error);
43 this.ok(false, "Unexpected error: " + error); 43 this.ok(false, "Unexpected error: " + error);
44 } 44 }
45 45
(...skipping 24 matching lines...) Expand all
70 if (styleElements.length) 70 if (styleElements.length)
71 styleElement = styleElements[0]; 71 styleElement = styleElements[0];
72 else 72 else
73 { 73 {
74 styleElement = document.createElement("style"); 74 styleElement = document.createElement("style");
75 document.head.appendChild(styleElement); 75 document.head.appendChild(styleElement);
76 } 76 }
77 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); 77 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length);
78 } 78 }
79 79
80 // insert a <div> with a unique id and and empty CSS rule 80 // insert a <div> with a unique id and a CSS rule
Wladimir Palant 2017/06/08 13:12:43 You have "and" twice here. Also, how is this an em
hub 2017/06/08 15:45:50 Done.
81 // for the the selector matching the id. 81 // for the the selector matching the id.
82 function createElementWithStyle(styleBlock, parent) 82 function createElementWithStyle(styleBlock, parent)
83 { 83 {
84 let element = document.createElement("div"); 84 let element = document.createElement("div");
85 element.id = findUniqueId(); 85 element.id = findUniqueId();
86 if (!parent) 86 if (!parent)
87 document.body.appendChild(element); 87 document.body.appendChild(element);
88 else 88 else
89 parent.appendChild(element); 89 parent.appendChild(element);
90 insertStyleRule("#" + element.id + " " + styleBlock); 90 insertStyleRule("#" + element.id + " " + styleBlock);
91 return element; 91 return element;
92 } 92 }
93 93
94 // Will ensure the class ElemHideEmulation is loaded 94 // Will ensure the class ElemHideEmulation is loaded.
95 // and then will call the callback. 95 // NOTE: if it never loads, this will probably hang.
Wladimir Palant 2017/06/08 13:12:41 There are no callbacks here, this function returns
hub 2017/06/08 15:45:50 I forgot to update the comment when we switched th
96 // NOTE: if it never loads, this will probably hang in an infinite
97 // loop
Wladimir Palant 2017/06/08 13:12:42 There is no loop here. But - yes, loadScript() mig
hub 2017/06/08 15:45:52 Done.
98 function loadElemHideEmulation() 96 function loadElemHideEmulation()
99 { 97 {
100 if (typeof ElemHideEmulation == "undefined") 98 if (typeof ElemHideEmulation == "undefined")
101 { 99 {
102 return loadScript(myUrl + "/../../../lib/common.js").then(() => 100 return loadScript(myUrl + "/../../../lib/common.js").then(() =>
103 { 101 {
104 return loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js") ; 102 return loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js") ;
105 }).then(() => 103 }).then(() =>
106 { 104 {
107 return loadElemHideEmulation(); 105 return loadElemHideEmulation();
108 }); 106 });
109 } 107 }
110 108
111 return Promise.resolve(); 109 return Promise.resolve();
112 } 110 }
113 111
114 // instantiate a ElemHideEmulation with @selectors. 112 // Create a new ElemHideEmulation instance with @selectors.
Wladimir Palant 2017/06/08 13:12:43 "an ElemHideEmulation instance"?
hub 2017/06/08 15:45:48 Done.
115 function applyElemHideEmulation(selectors) 113 function applyElemHideEmulation(selectors)
116 { 114 {
117 return loadElemHideEmulation().then(() => 115 return loadElemHideEmulation().then(() =>
118 { 116 {
119 let elemHideEmulation = new ElemHideEmulation( 117 let elemHideEmulation = new ElemHideEmulation(
120 window, 118 window,
121 callback => 119 callback =>
122 { 120 {
123 let patterns = []; 121 let patterns = [];
124 selectors.forEach(selector => 122 selectors.forEach(selector =>
125 { 123 {
126 patterns.push({selector}); 124 patterns.push({selector});
127 }); 125 });
128 callback(patterns); 126 callback(patterns);
129 }, 127 },
130 newSelectors => 128 newSelectors =>
131 { 129 {
132 if (!newSelectors.length) 130 if (!newSelectors.length)
133 return; 131 return;
134 let selector = newSelectors.join(", "); 132 let selector = newSelectors.join(", ");
135 insertStyleRule(selector + "{display: none !important;}"); 133 insertStyleRule(selector + "{display: none !important;}");
136 }, 134 },
137 elems => 135 elems =>
138 { 136 {
139 if (!elems.length)
140 return;
Wladimir Palant 2017/06/08 13:12:42 This check is pointless, we can just let the loop
hub 2017/06/08 15:45:49 Done.
141 for (let elem of elems) 137 for (let elem of elems)
142 elem.style.display = "none"; 138 elem.style.display = "none";
143 } 139 }
144 ); 140 );
145 141
146 elemHideEmulation.apply(); 142 elemHideEmulation.apply();
147 return Promise.resolve(); 143 return Promise.resolve(elemHideEmulation);
Wladimir Palant 2017/06/08 13:12:43 While it was like that before your changes already
hub 2017/06/08 15:45:50 Done.
148 }); 144 });
149 } 145 }
146
147 // internals testing
150 148
151 exports.testParseSelectorContent = function(test) 149 exports.testParseSelectorContent = function(test)
152 { 150 {
153 loadElemHideEmulation().then(() => 151 loadElemHideEmulation().then(() =>
154 { 152 {
155 let parsed = parseSelectorContent(":-abp-has(> div) > div", 10); 153 let parsed = parseSelectorContent(":-abp-has(> div) > div", 10);
156 test.equal(parsed.text, "> div"); 154 test.equal(parsed.text, "> div");
157 test.equal(parsed.end, 15); 155 test.equal(parsed.end, 15);
158 156
159 parsed = parseSelectorContent("\"> div\") > div", 0); 157 parsed = parseSelectorContent("> div) > div", 0);
160 test.equal(parsed.text, "\"> div\""); 158 test.equal(parsed.text, "> div");
161 test.equal(parsed.end, 7); 159 test.equal(parsed.end, 5);
162 parsed = parseSelectorContent(" \"> div\" ) > div", 0);
163 test.equal(parsed.text, " \"> div\" ");
164 test.equal(parsed.end, 9);
165 160
166 // parens not closed. 161 // parens not closed.
167 parsed = parseSelectorContent("> div > div", 0); 162 parsed = parseSelectorContent("> div > div", 0);
168 test.equal(parsed, null); 163 test.equal(parsed, null);
169 }).catch(unexpectedError.bind(test)).then(() => test.done()); 164 }).catch(unexpectedError.bind(test)).then(() => test.done());
Wladimir Palant 2017/06/08 13:12:40 I'm not really a friend of testing implementation
hub 2017/06/08 15:45:49 I actually think that *also* testing internal func
Wladimir Palant 2017/06/13 13:16:25 As you wish but I certainly won't object to anybod
170 }; 165 };
171 166
172 exports.testParseSelector = function(test) 167 exports.testParseSelector = function(test)
173 { 168 {
174 loadElemHideEmulation().then(() => 169 loadElemHideEmulation().then(() =>
175 { 170 {
176 let selectors = parseSelector(""); 171 let selectors = parseSelector("");
177 test.equal(selectors.length, 0); 172 test.equal(selectors.length, 0);
178 173
179 let selector = "div > :-abp-properties('background-color: rgb(0, 0, 0)')"; 174 let selector = "div > :-abp-properties('background-color: rgb(0, 0, 0)')";
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 let selectors = splitSelector("div:-abp-has(div) > [-abp-properties='backgro und-color: rgb(0, 0, 0)'] > span"); 312 let selectors = splitSelector("div:-abp-has(div) > [-abp-properties='backgro und-color: rgb(0, 0, 0)'] > span");
318 test.ok(selectors); 313 test.ok(selectors);
319 test.equal(selectors.length, 1, "There is only one selector"); 314 test.equal(selectors.length, 1, "There is only one selector");
320 315
321 selectors = splitSelector("div:-abp-has(div), [-abp-properties='background-c olor: rgb(0, 0, 0)']"); 316 selectors = splitSelector("div:-abp-has(div), [-abp-properties='background-c olor: rgb(0, 0, 0)']");
322 test.ok(selectors); 317 test.ok(selectors);
323 test.equal(selectors.length, 2, "There are two selectors"); 318 test.equal(selectors.length, 2, "There are two selectors");
324 }).catch(unexpectedError.bind(test)).then(() => test.done()); 319 }).catch(unexpectedError.bind(test)).then(() => test.done());
325 }; 320 };
326 321
322 // API testing
323
327 exports.testVerbatimPropertySelector = function(test) 324 exports.testVerbatimPropertySelector = function(test)
328 { 325 {
329 let toHide = createElementWithStyle("{background-color: #000}"); 326 let toHide = createElementWithStyle("{background-color: #000}");
330 applyElemHideEmulation( 327 applyElemHideEmulation(
331 [":-abp-properties(background-color: rgb(0, 0, 0))"] 328 [":-abp-properties(background-color: rgb(0, 0, 0))"]
332 ).then(() => 329 ).then(() =>
333 { 330 {
334 expectHidden(test, toHide); 331 expectHidden(test, toHide);
335 }).catch(unexpectedError.bind(test)).then(() => test.done()); 332 }).catch(unexpectedError.bind(test)).then(() => test.done());
336 }; 333 };
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
580 let parent = createElementWithStyle("{}"); 577 let parent = createElementWithStyle("{}");
581 let child = createElementWithStyle("{background-color: #000}", parent); 578 let child = createElementWithStyle("{background-color: #000}", parent);
582 applyElemHideEmulation( 579 applyElemHideEmulation(
583 ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"] 580 ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"]
584 ).then(() => 581 ).then(() =>
585 { 582 {
586 expectVisible(test, child); 583 expectVisible(test, child);
587 expectHidden(test, parent); 584 expectHidden(test, parent);
588 }).catch(unexpectedError.bind(test)).then(() => test.done()); 585 }).catch(unexpectedError.bind(test)).then(() => test.done());
589 }; 586 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld