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

Side by Side Diff: test/browser/elemHideEmulation.js

Issue 29383960: Issue 3143 - Filter elements with :-abp-has() (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Patch Set: Rebased on master. Handled all the feedback. Created June 1, 2017, 6:21 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
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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 /* globals ElemHideEmulation */ 20 /* globals ElemHideEmulation, splitSelector,
21 parseSelectorContent,
22 parseSelector, positionInParent, makeSelector,
23 PlainSelector, HasSelector, PropsSelector */
21 24
22 let myUrl = document.currentScript.src; 25 let myUrl = document.currentScript.src;
23 26
24 exports.tearDown = function(callback) 27 exports.tearDown = function(callback)
25 { 28 {
26 let styleElements = document.head.getElementsByTagName("style"); 29 let styleElements = document.head.getElementsByTagName("style");
27 while (styleElements.length) 30 while (styleElements.length)
28 styleElements[0].parentNode.removeChild(styleElements[0]); 31 styleElements[0].parentNode.removeChild(styleElements[0]);
32
33 let child;
34 while (child = document.body.firstChild)
35 document.body.removeChild(child);
36
29 callback(); 37 callback();
30 }; 38 };
31 39
32 function unexpectedError(error) 40 function unexpectedError(error)
33 { 41 {
34 console.error(error); 42 console.error(error);
35 this.ok(false, "Unexpected error: " + error); 43 this.ok(false, "Unexpected error: " + error);
36 } 44 }
37 45
38 function expectHidden(test, element) 46 function expectHidden(test, element)
(...skipping 23 matching lines...) Expand all
62 if (styleElements.length) 70 if (styleElements.length)
63 styleElement = styleElements[0]; 71 styleElement = styleElements[0];
64 else 72 else
65 { 73 {
66 styleElement = document.createElement("style"); 74 styleElement = document.createElement("style");
67 document.head.appendChild(styleElement); 75 document.head.appendChild(styleElement);
68 } 76 }
69 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); 77 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length);
70 } 78 }
71 79
72 function createElementWithStyle(styleBlock) 80 // insert a <div> with a unique id and and empty CSS rule
81 // for the the selector matching the id.
82 function createElementWithStyle(styleBlock, parent)
73 { 83 {
74 let element = document.createElement("div"); 84 let element = document.createElement("div");
75 element.id = findUniqueId(); 85 element.id = findUniqueId();
76 document.body.appendChild(element); 86 if (!parent)
87 document.body.appendChild(element);
88 else
89 parent.appendChild(element);
77 insertStyleRule("#" + element.id + " " + styleBlock); 90 insertStyleRule("#" + element.id + " " + styleBlock);
78 return element; 91 return element;
79 } 92 }
80 93
81 function applyElemHideEmulation(selectors) 94 // Will ensure the class ElemHideEmulation is loaded
95 // and then will call the callback.
96 // NOTE: if it never loads, this will probably hang in an infinite
97 // loop
98 function loadElemHideEmulation()
82 { 99 {
83 if (typeof ElemHideEmulation == "undefined") 100 if (typeof ElemHideEmulation == "undefined")
84 { 101 {
85 return loadScript(myUrl + "/../../../lib/common.js").then(() => 102 return loadScript(myUrl + "/../../../lib/common.js").then(() =>
86 { 103 {
87 return loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js") ; 104 return loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js") ;
88 }).then(() => 105 }).then(() =>
89 { 106 {
90 return applyElemHideEmulation(selectors); 107 return loadElemHideEmulation();
91 }); 108 });
92 } 109 }
93 110
94 let elemHideEmulation = new ElemHideEmulation( 111 return Promise.resolve();
95 window, 112 }
96 callback => 113
97 { 114 // instantiate a ElemHideEmulation with @selectors.
98 let patterns = []; 115 function applyElemHideEmulation(selectors)
99 selectors.forEach(selector => 116 {
117 return loadElemHideEmulation().then(() =>
118 {
119 let elemHideEmulation = new ElemHideEmulation(
120 window,
121 callback =>
100 { 122 {
101 patterns.push({selector}); 123 let patterns = [];
102 }); 124 selectors.forEach(selector =>
103 callback(patterns); 125 {
104 }, newSelectors => 126 patterns.push({selector});
105 { 127 });
106 if (!newSelectors.length) 128 callback(patterns);
107 return; 129 },
108 let selector = newSelectors.join(", "); 130 newSelectors =>
109 insertStyleRule(selector + "{display: none !important;}"); 131 {
110 } 132 if (!newSelectors.length)
111 ); 133 return;
112 134 let selector = newSelectors.join(", ");
113 elemHideEmulation.apply(); 135 insertStyleRule(selector + "{display: none !important;}");
114 return Promise.resolve(); 136 },
115 } 137 elems =>
138 {
139 if (!elems.length)
140 return;
141 for (let elem of elems)
142 elem.style.display = "none";
143 }
144 );
145
146 elemHideEmulation.apply();
147 return Promise.resolve();
148 });
149 }
150
151 exports.testParseSelectorContent = function(test)
152 {
153 loadElemHideEmulation().then(() =>
154 {
155 let parsed = parseSelectorContent("> div) > div");
156 test.equal(parsed.text, "> div");
157 test.equal(parsed.end, 5);
158
159 parsed = parseSelectorContent("\"> div\") > div");
160 test.equal(parsed.text, "\"> div\"");
161 test.equal(parsed.end, 7);
162 parsed = parseSelectorContent(" \"> div\" ) > div");
163 test.equal(parsed.text, " \"> div\" ");
164 test.equal(parsed.end, 9);
165
166 // parens not closed.
167 parsed = parseSelectorContent("> div > div");
168 test.equal(parsed, null);
169 }).catch(unexpectedError.bind(test)).then(() => test.done());
170 };
171
172 exports.testParseSelector = function(test)
173 {
174 loadElemHideEmulation().then(() =>
175 {
176 let {selectors, hide} = parseSelector("");
177 test.equal(selectors.length, 0);
178 test.ok(!hide);
179
180 let selector = "div > :-abp-properties('background-color: rgb(0, 0, 0)')";
181 let parsed = parseSelector(selector);
182 test.equal(parsed.selectors.length, 2);
183 test.ok(!parsed.hide);
184 test.ok(parsed.selectors[0] instanceof PlainSelector);
185 test.ok(parsed.selectors[1] instanceof PropsSelector);
186
187 selector = "div > :-abp-has(> div.inside) > div";
188 parsed = parseSelector(selector);
189 test.equal(parsed.selectors.length, 3);
190 test.ok(parsed.hide);
191 test.ok(parsed.selectors[0] instanceof PlainSelector);
192 test.ok(parsed.selectors[1] instanceof HasSelector);
193 test.ok(parsed.selectors[2] instanceof PlainSelector);
194
195 selector = "div > div:-abp-has(> div.inside) > div";
196 parsed = parseSelector(selector);
197
198 test.equal(parsed.selectors.length, 3);
199 test.ok(parsed.hide);
200 test.ok(parsed.selectors[0] instanceof PlainSelector);
201 test.ok(parsed.selectors[1] instanceof HasSelector);
202 test.ok(parsed.selectors[2] instanceof PlainSelector);
203
204 selector = "div > :-abp-has(> div.inside) > :-abp-properties(background-colo r: rgb(0, 0, 0))";
205 parsed = parseSelector(selector);
206
207 test.equal(parsed.selectors.length, 4);
208 test.ok(parsed.hide);
209 test.ok(parsed.selectors[0] instanceof PlainSelector);
210 test.ok(parsed.selectors[1] instanceof HasSelector);
211 test.ok(parsed.selectors[2] instanceof PlainSelector);
212 test.ok(parsed.selectors[3] instanceof PropsSelector);
213
214 selector = "div > :-abp-has(> div.inside > :-abp-properties(background-color : rgb(0, 0, 0))";
215 parsed = parseSelector(selector);
216 test.equal(parsed.selectors, null);
217
218 // -abp-has-unsupported() is unknown. Ensure we fail parsing.
219 selector = 'div[arial-label="Story"]:-abp-has(> div > div > span > span:-abp -unsupported("Suggested Post"))';
220 parsed = parseSelector(selector);
221 test.equal(parsed.selectors, null);
222 }).catch(unexpectedError.bind(test)).then(() => test.done());
223 };
224
225 function buildDom(doc)
226 {
227 doc.body.innerHTML = `<div id="parent">
228 <div id="middle">
229 <div id="middle1"><div id="inside" class="inside"></div></div>
230 </div>
231 <div id="sibling">
232 <div id="tohide">to hide</div>
233 </div>
234 <div id="sibling2">
235 <div id="sibling21"><div id="sibling211" class="inside"></div></div>
236 </div>
237 </div>`;
238 let parent = document.getElementById("parent");
239 let middle = document.getElementById("middle");
240 let middle1 = document.getElementById("middle1");
241 let inside = document.getElementById("inside");
242 let sibling = document.getElementById("sibling");
243 let sibling2 = document.getElementById("sibling2");
244 let toHide = document.getElementById("tohide");
245 return {parent, middle, middle1, inside, sibling, sibling2, toHide};
246 }
247
248 exports.testPositionInParent = function(test)
249 {
250 let nodes = buildDom(document);
251
252 loadElemHideEmulation().then(() =>
253 {
254 test.equal(positionInParent(nodes.middle1), 1);
255 test.equal(positionInParent(nodes.inside), 1);
256 test.equal(positionInParent(nodes.sibling2), 3);
257 }).catch(unexpectedError.bind(test)).then(() => test.done());
258 };
259
260 exports.testMakeSelector = function(test)
261 {
262 let nodes = buildDom(document);
263
264 loadElemHideEmulation().then(() =>
265 {
266 test.equal(makeSelector(nodes.middle, ""),
267 ":root > BODY:nth-child(2) > DIV:nth-child(1) > DIV:nth-child(1)" );
268 test.equal(makeSelector(nodes.toHide, ""),
269 ":root > BODY:nth-child(2) > DIV:nth-child(1) > DIV:nth-child(2) > DIV:nth-child(1)");
270 }).catch(unexpectedError.bind(test)).then(() => test.done());
271 };
272
273 exports.testPlainSelector = function(test)
274 {
275 buildDom(document);
276
277 loadElemHideEmulation().then(() =>
278 {
279 let selector = new PlainSelector("div > div");
280
281 let iter = selector.getSelectors("foo > ");
282 let value = iter.next();
283 test.equal(value.value[0], "foo > div > div");
284 test.ok(iter.next().done);
285 }).catch(unexpectedError.bind(test)).then(() => test.done());
286 };
287
288 exports.testHasSelector = function(test)
289 {
290 buildDom(document);
291
292 loadElemHideEmulation().then(() =>
293 {
294 let selector = new HasSelector("> div.inside");
295
296 let iter = selector.getSelectors("", document, document.sheet);
297 let value = iter.next();
298 test.ok(!value.done);
299 test.equal(value.value[0],
300 ":root > BODY:nth-child(2) > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(1)");
301
302 iter = selector.getElements("", document, document.sheet);
303 value = iter.next();
304 test.ok(!value.done);
305 test.equal(value.value.id, "middle1");
306 value = iter.next();
307 test.ok(!value.done);
308 test.equal(value.value.id, "sibling21");
309 value = iter.next();
310 test.ok(value.done);
311
312 selector = new HasSelector(":-abp-has(> div.inside)");
313
314 test.ok(selector._innerSelectors);
315 }).catch(unexpectedError.bind(test)).then(() => test.done());
316 };
317
318 exports.testSplitStyleRule = function(test)
319 {
320 loadElemHideEmulation().then(() =>
321 {
322 let selectors = splitSelector("div:-abp-has(div) > [-abp-properties='backgro und-color: rgb(0, 0, 0)'] > span");
323 test.ok(selectors);
324 test.equal(selectors.length, 1, "There is only one selector");
325
326 selectors = splitSelector("div:-abp-has(div), [-abp-properties='background-c olor: rgb(0, 0, 0)']");
327 test.ok(selectors);
328 test.equal(selectors.length, 2, "There are two selectors");
329 }).catch(unexpectedError.bind(test)).then(() => test.done());
330 };
116 331
117 exports.testVerbatimPropertySelector = function(test) 332 exports.testVerbatimPropertySelector = function(test)
118 { 333 {
119 let toHide = createElementWithStyle("{background-color: #000}"); 334 let toHide = createElementWithStyle("{background-color: #000}");
120 applyElemHideEmulation( 335 applyElemHideEmulation(
121 [":-abp-properties(background-color: rgb(0, 0, 0))"] 336 [":-abp-properties(background-color: rgb(0, 0, 0))"]
122 ).then(() => 337 ).then(() =>
123 { 338 {
124 expectHidden(test, toHide); 339 expectHidden(test, toHide);
125 }).catch(unexpectedError.bind(test)).then(() => test.done()); 340 }).catch(unexpectedError.bind(test)).then(() => test.done());
126 }; 341 };
127 342
343 exports.testVerbatimPropertySelectorWithPrefix = function(test)
344 {
345 let parent = createElementWithStyle("{background-color: #000}");
346 let toHide = createElementWithStyle("{background-color: #000}", parent);
347 applyElemHideEmulation(
348 ["div > :-abp-properties(background-color: rgb(0, 0, 0))"]
349 ).then(() =>
350 {
351 expectVisible(test, parent);
352 expectHidden(test, toHide);
353 }).catch(unexpectedError.bind(test)).then(() => test.done());
354 };
355
356 exports.testVerbatimPropertySelectorWithPrefixNoMatch = function(test)
357 {
358 let parent = createElementWithStyle("{background-color: #000}");
359 let toHide = createElementWithStyle("{background-color: #fff}", parent);
360 applyElemHideEmulation(
361 ["div > :-abp-properties(background-color: rgb(0, 0, 0))"]
362 ).then(() =>
363 {
364 expectVisible(test, parent);
365 expectVisible(test, toHide);
366 }).catch(unexpectedError.bind(test)).then(() => test.done());
367 };
368
369 exports.testVerbatimPropertySelectorWithSuffix = function(test)
370 {
371 let parent = createElementWithStyle("{background-color: #000}");
372 let toHide = createElementWithStyle("{background-color: #000}", parent);
373 applyElemHideEmulation(
374 [":-abp-properties(background-color: rgb(0, 0, 0)) > div"]
375 ).then(() =>
376 {
377 expectVisible(test, parent);
378 expectHidden(test, toHide);
379 }).catch(unexpectedError.bind(test)).then(() => test.done());
380 };
381
382 exports.testVerbatimPropertyPseudoSelectorWithPrefixAndSuffix = function(test)
383 {
384 let parent = createElementWithStyle("{background-color: #000}");
385 let middle = createElementWithStyle("{background-color: #000}", parent);
386 let toHide = createElementWithStyle("{background-color: #000}", middle);
387 applyElemHideEmulation(
388 ["div > :-abp-properties(background-color: rgb(0, 0, 0)) > div"]
389 ).then(() =>
390 {
391 expectVisible(test, parent);
392 expectVisible(test, middle);
393 expectHidden(test, toHide);
394 }).catch(unexpectedError.bind(test)).then(() => test.done());
395 };
396
128 exports.testPropertySelectorWithWildcard = function(test) 397 exports.testPropertySelectorWithWildcard = function(test)
129 { 398 {
130 let toHide = createElementWithStyle("{background-color: #000}"); 399 let toHide = createElementWithStyle("{background-color: #000}");
131 applyElemHideEmulation( 400 applyElemHideEmulation(
132 [":-abp-properties(*color: rgb(0, 0, 0))"] 401 [":-abp-properties(*color: rgb(0, 0, 0))"]
133 ).then(() => 402 ).then(() =>
134 { 403 {
135 expectHidden(test, toHide); 404 expectHidden(test, toHide);
136 }).catch(unexpectedError.bind(test)).then(() => test.done()); 405 }).catch(unexpectedError.bind(test)).then(() => test.done());
137 }; 406 };
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 return new Promise((resolve, reject) => 450 return new Promise((resolve, reject) =>
182 { 451 {
183 window.setTimeout(() => 452 window.setTimeout(() =>
184 { 453 {
185 expectHidden(test, toHide); 454 expectHidden(test, toHide);
186 resolve(); 455 resolve();
187 }, 0); 456 }, 0);
188 }); 457 });
189 }).catch(unexpectedError.bind(test)).then(() => test.done()); 458 }).catch(unexpectedError.bind(test)).then(() => test.done());
190 }; 459 };
460
461 exports.testPseudoClassHasSelector = function(test)
462 {
463 let toHide = createElementWithStyle("{}");
464 applyElemHideEmulation(
465 ["div:-abp-has(div)"]
466 ).then(() =>
467 {
468 expectVisible(test, toHide);
469 }).catch(unexpectedError.bind(test)).then(() => test.done());
470 };
471
472 exports.testPseudoClassHasSelectorWithPrefix = function(test)
473 {
474 let parent = createElementWithStyle("{}");
475 let child = createElementWithStyle("{}", parent);
476 applyElemHideEmulation(
477 ["div:-abp-has(div)"]
478 ).then(() =>
479 {
480 expectHidden(test, parent);
481 expectVisible(test, child);
482 }).catch(unexpectedError.bind(test)).then(() => test.done());
483 };
484
485 exports.testPseudoClassHasSelectorWithSuffix = function(test)
486 {
487 let parent = createElementWithStyle("{}");
488 let middle = createElementWithStyle("{}", parent);
489 let child = createElementWithStyle("{}", middle);
490 applyElemHideEmulation(
491 ["div:-abp-has(div) > div"]
492 ).then(() =>
493 {
494 expectVisible(test, parent);
495 expectHidden(test, middle);
496 expectHidden(test, child);
497 }).catch(unexpectedError.bind(test)).then(() => test.done());
498 };
499
500 exports.testPseudoClassHasSelectorWithSuffixSibling = function(test)
501 {
502 let parent = createElementWithStyle("{}");
503 let middle = createElementWithStyle("{}", parent);
504 let toHide = createElementWithStyle("{}");
505 applyElemHideEmulation(
506 ["div:-abp-has(div) + div"]
507 ).then(() =>
508 {
509 expectVisible(test, parent);
510 expectVisible(test, middle);
511 expectHidden(test, toHide);
512 }).catch(unexpectedError.bind(test)).then(() => test.done());
513 };
514
515 exports.testPseudoClassHasSelectorWithSuffixSiblingChild = function(test)
516 {
517 // <div>
518 // <div></div>
519 // <div>
520 // <div>to hide</div>
521 // </div>
522 // </div>
523 let parent = createElementWithStyle("{}");
524 let middle = createElementWithStyle("{}", parent);
525 let sibling = createElementWithStyle("{}");
526 let toHide = createElementWithStyle("{}", sibling);
527 applyElemHideEmulation(
528 ["div:-abp-has(div) + div > div"]
529 ).then(() =>
530 {
531 expectVisible(test, parent);
532 expectVisible(test, middle);
533 expectVisible(test, sibling);
534 expectHidden(test, toHide);
535 }).catch(unexpectedError.bind(test)).then(() => test.done());
536 };
537
538 function runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling(test, selector )
539 {
540 document.body.innerHTML = `<div id="parent">
541 <div id="middle">
542 <div id="middle1"><div id="inside" class="inside"></div></div>
543 </div>
544 <div id="sibling">
545 <div id="tohide">to hide</div>
546 </div>
547 <div id="sibling2">
548 <div id="sibling21"><div id="sibling211" class="inside"></div></div>
549 </div>
550 </div>`;
551 let parent = document.getElementById("parent");
552 let middle = document.getElementById("middle");
553 let inside = document.getElementById("inside");
554 let sibling = document.getElementById("sibling");
555 let sibling2 = document.getElementById("sibling2");
556 let toHide = document.getElementById("tohide");
557
558 insertStyleRule(".inside {}");
559
560 applyElemHideEmulation(
561 [selector]
562 ).then(() =>
563 {
564 expectVisible(test, parent);
565 expectVisible(test, middle);
566 expectVisible(test, inside);
567 expectVisible(test, sibling);
568 expectVisible(test, sibling2);
569 expectHidden(test, toHide);
570 }).catch(unexpectedError.bind(test)).then(() => test.done());
571 }
572
573 exports.testPseudoClassHasSelectorWithHasAndWithSuffixSibling = function(test)
574 {
575 runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling(test, "div:-abp-has(: -abp-has(div.inside)) + div > div");
576 };
577
578 exports.testPseudoClassHasSelectorWithHasAndWithSuffixSibling2 = function(test)
579 {
580 runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling(test, "div:-abp-has(: -abp-has(> div.inside)) + div > div");
581 };
582
583 exports.testPseudoClassHasSelectorWithPropSelector = function(test)
584 {
585 let parent = createElementWithStyle("{}");
586 let child = createElementWithStyle("{background-color: #000}", parent);
587 applyElemHideEmulation(
588 ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"]
589 ).then(() =>
590 {
591 expectVisible(test, child);
592 expectHidden(test, parent);
593 }).catch(unexpectedError.bind(test)).then(() => test.done());
594 };
OLDNEW
« chrome/content/elemHideEmulation.js ('K') | « chrome/content/elemHideEmulation.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld