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: Support -adb-properties inside :has(). Created March 22, 2017, 1:45 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
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 // We are currently limited to ECMAScript 5 in this file, because it is being 18 "use strict";
19 // used in the browser tests. See https://issues.adblockplus.org/ticket/4796 19
20 20 /* globals ElemHideEmulation, splitSelector,
21 // TODO: This should be using document.currentScript once supported by 21 parseSelectorContent,
22 // PhantomJS. 22 parseSelector, positionInParent, makeSelector,
23 var myUrl = document.head.lastChild.src; 23 PlainSelector, HasSelector, PropsSelector */
24 24
25 exports.setUp = function(callback) 25 let myUrl = document.currentScript.src;
26 {
27 // The URL object in PhantomJS 2.1.7 does not implement any properties, so
28 // we need a polyfill.
29 if (!URL || !("origin" in URL))
30 {
31 var doc = document.implementation.createHTMLDocument();
32 var anchor = doc.createElement("a");
33 doc.body.appendChild(anchor);
34 URL = function(url)
35 {
36 if (!url)
37 throw "Invalid URL";
38 anchor.href = url;
39 this.origin = anchor.origin;
40 };
41 }
42
43 callback();
44 };
45 26
46 exports.tearDown = function(callback) 27 exports.tearDown = function(callback)
47 { 28 {
48 var styleElements = document.head.getElementsByTagName("style"); 29 let styleElements = document.head.getElementsByTagName("style");
49 while (styleElements.length) 30 while (styleElements.length)
50 styleElements[0].parentNode.removeChild(styleElements[0]); 31 styleElements[0].parentNode.removeChild(styleElements[0]);
32
33 let child;
34 while (child = document.body.firstChild)
35 child.parentNode.removeChild(child);
36
51 callback(); 37 callback();
52 }; 38 };
39
40 function unexpectedError(error)
41 {
42 console.error(error);
43 this.ok(false, "Unexpected error: " + error);
44 }
53 45
54 function expectHidden(test, element) 46 function expectHidden(test, element)
55 { 47 {
56 test.equal(window.getComputedStyle(element).display, "none", 48 test.equal(window.getComputedStyle(element).display, "none",
57 "The element's display property should be set to 'none'"); 49 "The element's display property should be set to 'none'");
58 } 50 }
59 51
60 function expectVisible(test, element) 52 function expectVisible(test, element)
61 { 53 {
62 test.notEqual(window.getComputedStyle(element).display, "none", 54 test.notEqual(window.getComputedStyle(element).display, "none",
63 "The element's display property should not be set to 'none'"); 55 "The element's display property should not be set to 'none'");
64 } 56 }
65 57
66 function findUniqueId() 58 function findUniqueId()
67 { 59 {
68 var id = "elemHideEmulationTest-" + Math.floor(Math.random() * 10000); 60 let id = "elemHideEmulationTest-" + Math.floor(Math.random() * 10000);
69 if (!document.getElementById(id)) 61 if (!document.getElementById(id))
70 return id; 62 return id;
71 return findUniqueId(); 63 return findUniqueId();
72 } 64 }
73 65
74 function insertStyleRule(rule) 66 function insertStyleRule(rule)
75 { 67 {
76 var styleElement; 68 let styleElement;
77 var styleElements = document.head.getElementsByTagName("style"); 69 let styleElements = document.head.getElementsByTagName("style");
78 if (styleElements.length) 70 if (styleElements.length)
79 styleElement = styleElements[0]; 71 styleElement = styleElements[0];
80 else 72 else
81 { 73 {
82 styleElement = document.createElement("style"); 74 styleElement = document.createElement("style");
83 document.head.appendChild(styleElement); 75 document.head.appendChild(styleElement);
84 } 76 }
85 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); 77 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length);
86 } 78 }
87 79
88 // insert a <div> with a unique id and and empty CSS rule 80 // insert a <div> with a unique id and a CSS rule
89 // for the the selector matching the id. 81 // for the the selector matching the id.
90 function createElementWithStyle(styleBlock, parent) 82 function createElementWithStyle(styleBlock, parent)
91 { 83 {
92 var element = document.createElement("div"); 84 let element = document.createElement("div");
93 element.id = findUniqueId(); 85 element.id = findUniqueId();
94 if (!parent) 86 if (!parent)
95 document.body.appendChild(element); 87 document.body.appendChild(element);
96 else 88 else
97 parent.appendChild(element); 89 parent.appendChild(element);
98 insertStyleRule("#" + element.id + " " + styleBlock); 90 insertStyleRule("#" + element.id + " " + styleBlock);
99 return element; 91 return element;
100 } 92 }
101 93
102 // Will ensure the class ElemHideEmulation is loaded 94 // Will ensure the class ElemHideEmulation is loaded.
103 // and then will call the callback. 95 // NOTE: if it never loads, this will probably hang.
104 // NOTE: if it never loads, this will probably hang in an infinite 96 function loadElemHideEmulation()
105 // loop
106 function loadElemHideEmulation(callback)
107 { 97 {
108 if (typeof ElemHideEmulation == "undefined") 98 if (typeof ElemHideEmulation == "undefined")
109 { 99 {
110 loadScript(myUrl + "/../../../lib/common.js", function() 100 return loadScript(myUrl + "/../../../lib/common.js").then(() =>
111 { 101 {
112 loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js", 102 return loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js") ;
113 function() 103 }).then(() =>
114 { 104 {
115 loadElemHideEmulation(callback); 105 return loadElemHideEmulation();
116 });
117 }); 106 });
118 return;
119 } 107 }
120 108
121 callback(); 109 return Promise.resolve();
122 } 110 }
123 111
124 // instantiate a ElemHideEmulation with @selectors. 112 // Create a new ElemHideEmulation instance with @selectors.
125 function applyElemHideEmulation(selectors, callback) 113 function applyElemHideEmulation(selectors)
126 { 114 {
127 loadElemHideEmulation(function() 115 return loadElemHideEmulation().then(() =>
128 { 116 {
129 var elemHideEmulation = new ElemHideEmulation( 117 let elemHideEmulation = new ElemHideEmulation(
130 window, 118 window,
131 function(callback) 119 callback =>
132 { 120 {
133 var patterns = []; 121 let patterns = [];
134 selectors.forEach(function(selector) 122 selectors.forEach(selector =>
135 { 123 {
136 patterns.push({selector: selector}); 124 patterns.push({selector});
137 }); 125 });
138 callback(patterns); 126 callback(patterns);
139 }, 127 },
140 function(selectors) 128 newSelectors =>
141 { 129 {
142 if (!selectors.length) 130 if (!newSelectors.length)
143 return; 131 return;
144 var selector = selectors.join(", "); 132 let selector = newSelectors.join(", ");
145 insertStyleRule(selector + "{display: none !important;}"); 133 insertStyleRule(selector + "{display: none !important;}");
146 }, 134 },
147 function(elements) 135 elems =>
148 { 136 {
149 if (!elements.length) 137 for (let elem of elems)
150 return; 138 elem.style.display = "none";
151 for (var i = 0; i < elements.length; i++)
152 elements[i].style.display = "none";
153 } 139 }
154 ); 140 );
155 141
156 elemHideEmulation.apply(); 142 elemHideEmulation.apply();
157 callback(); 143 return Promise.resolve(elemHideEmulation);
158 }.bind(this));
159 }
160
161 exports.testPseudoHasRule = function(test)
162 {
163 loadElemHideEmulation(function()
164 {
165 var selectors = ["div:has(span)"];
166 // testing the regexp
167 var match = pseudoClassHasSelectorRegExp.exec(selectors[0]);
168 test.ok(match);
169 test.equal(match[1], "span");
170
171 selectors = [":has(div.inside)"];
172 match = pseudoClassHasSelectorRegExp.exec(selectors[0]);
173 test.ok(match);
174 test.equal(match[1], "div.inside");
175
176 test.done();
177 }); 144 });
178 }; 145 }
179 146
180 exports.testExtraFirstSelector = function(test) 147 // internals testing
181 { 148
182 loadElemHideEmulation(function() 149 exports.testParseSelectorContent = function(test)
183 { 150 {
184 var myselector = "elem.class1.class2"; 151 loadElemHideEmulation().then(() =>
185 var shortSel = extractFirstSelector(myselector); 152 {
186 153 let parsed = parseSelectorContent(":-abp-has(> div) > div", 10);
187 test.equal(shortSel, myselector); 154 test.equal(parsed.text, "> div");
188 155 test.equal(parsed.end, 15);
189 myselector = "elem > elem2"; 156
190 shortSel = extractFirstSelector(myselector); 157 parsed = parseSelectorContent("> div) > div", 0);
191 158 test.equal(parsed.text, "> div");
192 test.equal(shortSel, "elem"); 159 test.equal(parsed.end, 5);
193 160
194 myselector = "elem+elem2 > elem3"; 161 // parens not closed.
195 shortSel = extractFirstSelector(myselector); 162 parsed = parseSelectorContent("> div > div", 0);
196 163 test.equal(parsed, null);
197 test.equal(shortSel, "elem"); 164 }).catch(unexpectedError.bind(test)).then(() => test.done());
198 165 };
199 myselector = "elem~elem2 > elem3"; 166
200 shortSel = extractFirstSelector(myselector); 167 exports.testParseSelector = function(test)
201 168 {
202 test.equal(shortSel, "elem"); 169 loadElemHideEmulation().then(() =>
203 170 {
204 test.done(); 171 let selectors = parseSelector("");
205 }); 172 test.equal(selectors.length, 0);
173
174 let selector = "div > :-abp-properties('background-color: rgb(0, 0, 0)')";
175 selectors = parseSelector(selector);
176 test.equal(selectors.length, 2);
177 test.ok(selectors[0] instanceof PlainSelector);
178 test.ok(selectors[1] instanceof PropsSelector);
179
180 selector = "div > :-abp-has(> div.inside) > div";
181 selectors = parseSelector(selector);
182 test.equal(selectors.length, 3);
183 test.ok(selectors[0] instanceof PlainSelector);
184 test.ok(selectors[1] instanceof HasSelector);
185 test.ok(selectors[2] instanceof PlainSelector);
186
187 selector = "div > div:-abp-has(> div.inside) > div";
188 selectors = parseSelector(selector);
189
190 test.equal(selectors.length, 3);
191 test.ok(selectors[0] instanceof PlainSelector);
192 test.ok(selectors[1] instanceof HasSelector);
193 test.ok(selectors[2] instanceof PlainSelector);
194
195 selector = "div > :-abp-has(> div.inside) > :-abp-properties(background-colo r: rgb(0, 0, 0))";
196 selectors = parseSelector(selector);
197
198 test.equal(selectors.length, 4);
199 test.ok(selectors[0] instanceof PlainSelector);
200 test.ok(selectors[1] instanceof HasSelector);
201 test.ok(selectors[2] instanceof PlainSelector);
202 test.ok(selectors[3] instanceof PropsSelector);
203
204 selector = "div > :-abp-has(> div.inside > :-abp-properties(background-color : rgb(0, 0, 0))";
205 selectors = parseSelector(selector);
206 test.equal(selectors, null);
207
208 // -abp-has-unsupported() is unknown. Ensure we fail parsing.
209 selector = 'div[arial-label="Story"]:-abp-has(> div > div > span > span:-abp -unsupported("Suggested Post"))';
210 selectors = parseSelector(selector);
211 test.equal(selectors, null);
212 }).catch(unexpectedError.bind(test)).then(() => test.done());
213 };
214
215 function buildDom(doc)
216 {
217 doc.body.innerHTML = `<div id="parent">
218 <div id="middle">
219 <div id="middle1"><div id="inside" class="inside"></div></div>
220 </div>
221 <div id="sibling">
222 <div id="tohide">to hide</div>
223 </div>
224 <div id="sibling2">
225 <div id="sibling21"><div id="sibling211" class="inside"></div></div>
226 </div>
227 </div>`;
228 let parent = document.getElementById("parent");
229 let middle = document.getElementById("middle");
230 let middle1 = document.getElementById("middle1");
231 let inside = document.getElementById("inside");
232 let sibling = document.getElementById("sibling");
233 let sibling2 = document.getElementById("sibling2");
234 let toHide = document.getElementById("tohide");
235 return {parent, middle, middle1, inside, sibling, sibling2, toHide};
236 }
237
238 exports.testPositionInParent = function(test)
239 {
240 let nodes = buildDom(document);
241
242 loadElemHideEmulation().then(() =>
243 {
244 test.equal(positionInParent(nodes.middle1), 1);
245 test.equal(positionInParent(nodes.inside), 1);
246 test.equal(positionInParent(nodes.sibling2), 3);
247 }).catch(unexpectedError.bind(test)).then(() => test.done());
248 };
249
250 exports.testMakeSelector = function(test)
251 {
252 let nodes = buildDom(document);
253
254 loadElemHideEmulation().then(() =>
255 {
256 test.equal(makeSelector(nodes.middle, ""),
257 ":root > BODY:nth-child(2) > DIV:nth-child(1) > DIV:nth-child(1)" );
258 test.equal(makeSelector(nodes.toHide, ""),
259 ":root > BODY:nth-child(2) > DIV:nth-child(1) > DIV:nth-child(2) > DIV:nth-child(1)");
260 }).catch(unexpectedError.bind(test)).then(() => test.done());
261 };
262
263 exports.testPlainSelector = function(test)
264 {
265 buildDom(document);
266
267 loadElemHideEmulation().then(() =>
268 {
269 let selector = new PlainSelector("div > div");
270
271 let iter = selector.getSelectors("foo > ");
272 let value = iter.next();
273 test.equal(value.value[0], "foo > div > div");
274 test.ok(iter.next().done);
275 }).catch(unexpectedError.bind(test)).then(() => test.done());
276 };
277
278 exports.testHasSelector = function(test)
279 {
280 buildDom(document);
281
282 loadElemHideEmulation().then(() =>
283 {
284 let selector = new HasSelector("> div.inside");
285
286 let iter = selector.getSelectors("", document, document.sheet);
287 let value = iter.next();
288 test.ok(!value.done);
289 test.equal(value.value[0],
290 ":root > BODY:nth-child(2) > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(1)");
291
292 iter = selector.getElements("", document, document.sheet);
293 value = iter.next();
294 test.ok(!value.done);
295 test.equal(value.value.id, "middle1");
296 value = iter.next();
297 test.ok(!value.done);
298 test.equal(value.value.id, "sibling21");
299 value = iter.next();
300 test.ok(value.done);
301
302 selector = new HasSelector(":-abp-has(> div.inside)");
303
304 test.ok(selector._innerSelectors);
305 }).catch(unexpectedError.bind(test)).then(() => test.done());
206 }; 306 };
207 307
208 exports.testSplitStyleRule = function(test) 308 exports.testSplitStyleRule = function(test)
209 { 309 {
210 loadElemHideEmulation(function() 310 loadElemHideEmulation().then(() =>
211 { 311 {
212 var selectors = splitSelector("div:has(div) > [-abp-properties='background-c olor: rgb(0, 0, 0)'] > span"); 312 let selectors = splitSelector("div:-abp-has(div) > [-abp-properties='backgro und-color: rgb(0, 0, 0)'] > span");
213 test.ok(selectors); 313 test.ok(selectors);
214 test.equal(selectors.length, 1, "There is only one selector"); 314 test.equal(selectors.length, 1, "There is only one selector");
215 315
216 selectors = splitSelector("div:has(div), [-abp-properties='background-color: rgb(0, 0, 0)']"); 316 selectors = splitSelector("div:-abp-has(div), [-abp-properties='background-c olor: rgb(0, 0, 0)']");
217 test.ok(selectors); 317 test.ok(selectors);
218 test.equal(selectors.length, 2, "There are two selectors"); 318 test.equal(selectors.length, 2, "There are two selectors");
219 319 }).catch(unexpectedError.bind(test)).then(() => test.done());
220 test.done(); 320 };
221 }); 321
222 }; 322 // API testing
223 323
224 exports.testVerbatimPropertySelector = function(test) 324 exports.testVerbatimPropertySelector = function(test)
225 { 325 {
226 var toHide = createElementWithStyle("{background-color: #000}"); 326 let toHide = createElementWithStyle("{background-color: #000}");
227 applyElemHideEmulation( 327 applyElemHideEmulation(
228 ["[-abp-properties='background-color: rgb(0, 0, 0)']"], 328 [":-abp-properties(background-color: rgb(0, 0, 0))"]
229 function() 329 ).then(() =>
330 {
331 expectHidden(test, toHide);
332 }).catch(unexpectedError.bind(test)).then(() => test.done());
333 };
334
335 exports.testVerbatimPropertySelectorWithPrefix = function(test)
336 {
337 let parent = createElementWithStyle("{background-color: #000}");
338 let toHide = createElementWithStyle("{background-color: #000}", parent);
339 applyElemHideEmulation(
340 ["div > :-abp-properties(background-color: rgb(0, 0, 0))"]
341 ).then(() =>
342 {
343 expectVisible(test, parent);
344 expectHidden(test, toHide);
345 }).catch(unexpectedError.bind(test)).then(() => test.done());
346 };
347
348 exports.testVerbatimPropertySelectorWithPrefixNoMatch = function(test)
349 {
350 let parent = createElementWithStyle("{background-color: #000}");
351 let toHide = createElementWithStyle("{background-color: #fff}", parent);
352 applyElemHideEmulation(
353 ["div > :-abp-properties(background-color: rgb(0, 0, 0))"]
354 ).then(() =>
355 {
356 expectVisible(test, parent);
357 expectVisible(test, toHide);
358 }).catch(unexpectedError.bind(test)).then(() => test.done());
359 };
360
361 exports.testVerbatimPropertySelectorWithSuffix = function(test)
362 {
363 let parent = createElementWithStyle("{background-color: #000}");
364 let toHide = createElementWithStyle("{background-color: #000}", parent);
365 applyElemHideEmulation(
366 [":-abp-properties(background-color: rgb(0, 0, 0)) > div"]
367 ).then(() =>
368 {
369 expectVisible(test, parent);
370 expectHidden(test, toHide);
371 }).catch(unexpectedError.bind(test)).then(() => test.done());
372 };
373
374 exports.testVerbatimPropertyPseudoSelectorWithPrefixAndSuffix = function(test)
375 {
376 let parent = createElementWithStyle("{background-color: #000}");
377 let middle = createElementWithStyle("{background-color: #000}", parent);
378 let toHide = createElementWithStyle("{background-color: #000}", middle);
379 applyElemHideEmulation(
380 ["div > :-abp-properties(background-color: rgb(0, 0, 0)) > div"]
381 ).then(() =>
382 {
383 expectVisible(test, parent);
384 expectVisible(test, middle);
385 expectHidden(test, toHide);
386 }).catch(unexpectedError.bind(test)).then(() => test.done());
387 };
388
389 exports.testPropertySelectorWithWildcard = function(test)
390 {
391 let toHide = createElementWithStyle("{background-color: #000}");
392 applyElemHideEmulation(
393 [":-abp-properties(*color: rgb(0, 0, 0))"]
394 ).then(() =>
395 {
396 expectHidden(test, toHide);
397 }).catch(unexpectedError.bind(test)).then(() => test.done());
398 };
399
400 exports.testPropertySelectorWithRegularExpression = function(test)
401 {
402 let toHide = createElementWithStyle("{background-color: #000}");
403 applyElemHideEmulation(
404 [":-abp-properties(/.*color: rgb\\(0, 0, 0\\)/)"]
405 ).then(() =>
406 {
407 expectHidden(test, toHide);
408 }).catch(unexpectedError.bind(test)).then(() => test.done());
409 };
410
411 exports.testPropertySelectorWithEscapedBrace = function(test)
412 {
413 let toHide = createElementWithStyle("{background-color: #000}");
414 applyElemHideEmulation(
415 [":-abp-properties(/background.\\x7B 0,6\\x7D : rgb\\(0, 0, 0\\)/)"]
416 ).then(() =>
417 {
418 expectHidden(test, toHide);
419 }).catch(unexpectedError.bind(test)).then(() => test.done());
420 };
421
422 exports.testPropertySelectorWithImproperlyEscapedBrace = function(test)
423 {
424 let toHide = createElementWithStyle("{background-color: #000}");
425 applyElemHideEmulation(
426 [":-abp-properties(/background.\\x7B0,6\\x7D: rgb\\(0, 0, 0\\)/)"]
427 ).then(() =>
428 {
429 expectVisible(test, toHide);
430 }).catch(unexpectedError.bind(test)).then(() => test.done());
431 };
432
433 exports.testDynamicallyChangedProperty = function(test)
434 {
435 let toHide = createElementWithStyle("{}");
436 applyElemHideEmulation(
437 [":-abp-properties(background-color: rgb(0, 0, 0))"]
438 ).then(() =>
439 {
440 expectVisible(test, toHide);
441 insertStyleRule("#" + toHide.id + " {background-color: #000}");
442 return new Promise((resolve, reject) =>
230 { 443 {
231 expectHidden(test, toHide); 444 window.setTimeout(() =>
232 test.done();
233 }
234 );
235 };
236
237 exports.testVerbatimPropertySelectorWithPrefix = function(test)
238 {
239 var parent = createElementWithStyle("{background-color: #000}");
240 var toHide = createElementWithStyle("{background-color: #000}", parent);
241 applyElemHideEmulation(
242 ["div > [-abp-properties='background-color: rgb(0, 0, 0)']"],
243 function()
244 {
245 expectVisible(test, parent);
246 expectHidden(test, toHide);
247 test.done();
248 }
249 );
250 };
251
252 exports.testVerbatimPropertySelectorWithPrefixNoMatch = function(test)
253 {
254 var parent = createElementWithStyle("{background-color: #000}");
255 var toHide = createElementWithStyle("{background-color: #fff}", parent);
256 applyElemHideEmulation(
257 ["div > [-abp-properties='background-color: rgb(0, 0, 0)']"],
258 function()
259 {
260 expectVisible(test, parent);
261 expectVisible(test, toHide);
262 test.done();
263 }
264 );
265 };
266
267 exports.testVerbatimPropertySelectorWithSuffix = function(test)
268 {
269 var parent = createElementWithStyle("{background-color: #000}");
270 var toHide = createElementWithStyle("{background-color: #000}", parent);
271 applyElemHideEmulation(
272 ["[-abp-properties='background-color: rgb(0, 0, 0)'] > div"],
273 function()
274 {
275 expectVisible(test, parent);
276 expectHidden(test, toHide);
277 test.done();
278 }
279 );
280 };
281
282 exports.testVerbatimPropertySelectorWithPrefixAndSuffix = function(test)
283 {
284 var parent = createElementWithStyle("{background-color: #000}");
285 var middle = createElementWithStyle("{background-color: #000}", parent);
286 var toHide = createElementWithStyle("{background-color: #000}", middle);
287 applyElemHideEmulation(
288 ["div > [-abp-properties='background-color: rgb(0, 0, 0)'] > div"],
289 function()
290 {
291 expectVisible(test, parent);
292 expectVisible(test, middle);
293 expectHidden(test, toHide);
294 test.done();
295 }
296 );
297 };
298
299 exports.testPropertySelectorWithWildcard = function(test)
300 {
301 var toHide = createElementWithStyle("{background-color: #000}");
302 applyElemHideEmulation(
303 ["[-abp-properties='*color: rgb(0, 0, 0)']"],
304 function()
305 {
306 expectHidden(test, toHide);
307 test.done();
308 }
309 );
310 };
311
312 exports.testPropertySelectorWithRegularExpression = function(test)
313 {
314 var toHide = createElementWithStyle("{background-color: #000}");
315 applyElemHideEmulation(
316 ["[-abp-properties='/.*color: rgb\\(0, 0, 0\\)/']"],
317 function()
318 {
319 expectHidden(test, toHide);
320 test.done();
321 }
322 );
323 };
324
325 exports.testPropertySelectorWithEscapedBrace = function(test)
326 {
327 var toHide = createElementWithStyle("{background-color: #000}");
328 applyElemHideEmulation(
329 ["[-abp-properties='/background.\\x7B 0,6\\x7D : rgb\\(0, 0, 0\\)/']"],
330 function()
331 {
332 expectHidden(test, toHide);
333 test.done();
334 }
335 );
336 };
337
338 exports.testPropertySelectorWithImproperlyEscapedBrace = function(test)
339 {
340 var toHide = createElementWithStyle("{background-color: #000}");
341 applyElemHideEmulation(
342 ["[-abp-properties='/background.\\x7B0,6\\x7D: rgb\\(0, 0, 0\\)/']"],
343 function()
344 {
345 expectVisible(test, toHide);
346 test.done();
347 }
348 );
349 };
350
351 exports.testDynamicallyChangedProperty = function(test)
352 {
353 var toHide = createElementWithStyle("{}");
354 applyElemHideEmulation(
355 ["[-abp-properties='background-color: rgb(0, 0, 0)']"],
356 function()
357 {
358 expectVisible(test, toHide);
359 insertStyleRule("#" + toHide.id + " {background-color: #000}");
360 window.setTimeout(function()
361 { 445 {
362 expectHidden(test, toHide); 446 expectHidden(test, toHide);
363 test.done(); 447 resolve();
364 }, 0); 448 }, 0);
365 } 449 });
366 ); 450 }).catch(unexpectedError.bind(test)).then(() => test.done());
367 };
368
369 exports.testPseudoClassHasMatcher = function(test)
370 {
371 var parent = createElementWithStyle("{}");
372 var child = createElementWithStyle("{}", parent);
373 loadElemHideEmulation(function()
374 {
375 var matcher = new PseudoHasMatcher("div");
376 test.equal(matcher.match(parent).length, 1, "Parent should contain what is e xpected");
377 test.equal(matcher.match(child).length, 0, "Child shouldn't match");
378
379 var matcher2 = new PseudoHasMatcher("span");
380 test.equal(matcher2.match(parent), 0, "Doesn't have a <span> child, shouldn' t mactch");
381 test.equal(matcher2.match(child), 0, "Child shouldn't match");
382
383 test.done();
384 });
385 }; 451 };
386 452
387 exports.testPseudoClassHasSelector = function(test) 453 exports.testPseudoClassHasSelector = function(test)
388 { 454 {
389 var toHide = createElementWithStyle("{}"); 455 let toHide = createElementWithStyle("{}");
390 applyElemHideEmulation( 456 applyElemHideEmulation(
391 ["div:has(div)"], 457 ["div:-abp-has(div)"]
392 function() 458 ).then(() =>
393 { 459 {
394 expectVisible(test, toHide); 460 expectVisible(test, toHide);
395 test.done(); 461 }).catch(unexpectedError.bind(test)).then(() => test.done());
396 }
397 );
398 }; 462 };
399 463
400 exports.testPseudoClassHasSelectorWithPrefix = function(test) 464 exports.testPseudoClassHasSelectorWithPrefix = function(test)
401 { 465 {
402 var parent = createElementWithStyle("{}"); 466 let parent = createElementWithStyle("{}");
403 var child = createElementWithStyle("{}", parent); 467 let child = createElementWithStyle("{}", parent);
404 applyElemHideEmulation( 468 applyElemHideEmulation(
405 ["div:has(div)"], 469 ["div:-abp-has(div)"]
406 function() 470 ).then(() =>
407 { 471 {
408 expectHidden(test, parent); 472 expectHidden(test, parent);
409 expectVisible(test, child); 473 expectVisible(test, child);
410 test.done(); 474 }).catch(unexpectedError.bind(test)).then(() => test.done());
411 }
412 );
413 }; 475 };
414 476
415 exports.testPseudoClassHasSelectorWithSuffix = function(test) 477 exports.testPseudoClassHasSelectorWithSuffix = function(test)
416 { 478 {
417 var parent = createElementWithStyle("{}"); 479 let parent = createElementWithStyle("{}");
418 var middle = createElementWithStyle("{}", parent); 480 let middle = createElementWithStyle("{}", parent);
419 var child = createElementWithStyle("{}", middle); 481 let child = createElementWithStyle("{}", middle);
420 applyElemHideEmulation( 482 applyElemHideEmulation(
421 ["div:has(div) > div"], 483 ["div:-abp-has(div) > div"]
422 function() 484 ).then(() =>
423 { 485 {
424 expectVisible(test, parent); 486 expectVisible(test, parent);
425 expectVisible(test, middle); 487 expectHidden(test, middle);
426 expectHidden(test, child); 488 expectHidden(test, child);
427 test.done(); 489 }).catch(unexpectedError.bind(test)).then(() => test.done());
428 }
429 );
430 }; 490 };
431 491
432 exports.testPseudoClassHasSelectorWithSuffixSibling = function(test) 492 exports.testPseudoClassHasSelectorWithSuffixSibling = function(test)
433 { 493 {
434 var parent = createElementWithStyle("{}"); 494 let parent = createElementWithStyle("{}");
435 var middle = createElementWithStyle("{}", parent); 495 let middle = createElementWithStyle("{}", parent);
436 var toHide = createElementWithStyle("{}", parent); 496 let toHide = createElementWithStyle("{}");
437 applyElemHideEmulation( 497 applyElemHideEmulation(
438 ["div:has(div) + div"], 498 ["div:-abp-has(div) + div"]
439 function() 499 ).then(() =>
440 { 500 {
441 expectVisible(test, parent); 501 expectVisible(test, parent);
442 expectVisible(test, middle); 502 expectVisible(test, middle);
443 expectHidden(test, toHide); 503 expectHidden(test, toHide);
444 test.done(); 504 }).catch(unexpectedError.bind(test)).then(() => test.done());
445 } 505 };
446 ); 506
447 }; 507 exports.testPseudoClassHasSelectorWithSuffixSiblingChild = function(test)
448
449 exports.testPseudoClassHasSelectorWithSuffixSibling = function(test)
450 { 508 {
451 // <div> 509 // <div>
452 // <div></div> 510 // <div></div>
453 // <div> 511 // <div>
454 // <div>to hide</div> 512 // <div>to hide</div>
455 // </div> 513 // </div>
456 // </div> 514 // </div>
457 var parent = createElementWithStyle("{}"); 515 let parent = createElementWithStyle("{}");
458 var middle = createElementWithStyle("{}", parent); 516 let middle = createElementWithStyle("{}", parent);
459 var sibling = createElementWithStyle("{}", parent); 517 let sibling = createElementWithStyle("{}");
460 var toHide = createElementWithStyle("{}", sibling); 518 let toHide = createElementWithStyle("{}", sibling);
461 applyElemHideEmulation( 519 applyElemHideEmulation(
462 ["div:has(div) + div > div"], 520 ["div:-abp-has(div) + div > div"]
463 function() 521 ).then(() =>
464 { 522 {
465 expectVisible(test, parent); 523 expectVisible(test, parent);
466 expectVisible(test, middle); 524 expectVisible(test, middle);
467 expectVisible(test, sibling); 525 expectVisible(test, sibling);
468 expectHidden(test, toHide); 526 expectHidden(test, toHide);
469 test.done(); 527 }).catch(unexpectedError.bind(test)).then(() => test.done());
470 } 528 };
471 ); 529
472 }; 530 function runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling(test, selector )
531 {
532 document.body.innerHTML = `<div id="parent">
533 <div id="middle">
534 <div id="middle1"><div id="inside" class="inside"></div></div>
535 </div>
536 <div id="sibling">
537 <div id="tohide">to hide</div>
538 </div>
539 <div id="sibling2">
540 <div id="sibling21"><div id="sibling211" class="inside"></div></div>
541 </div>
542 </div>`;
543 let parent = document.getElementById("parent");
544 let middle = document.getElementById("middle");
545 let inside = document.getElementById("inside");
546 let sibling = document.getElementById("sibling");
547 let sibling2 = document.getElementById("sibling2");
548 let toHide = document.getElementById("tohide");
549
550 insertStyleRule(".inside {}");
551
552 applyElemHideEmulation(
553 [selector]
554 ).then(() =>
555 {
556 expectVisible(test, parent);
557 expectVisible(test, middle);
558 expectVisible(test, inside);
559 expectVisible(test, sibling);
560 expectVisible(test, sibling2);
561 expectHidden(test, toHide);
562 }).catch(unexpectedError.bind(test)).then(() => test.done());
563 }
473 564
474 exports.testPseudoClassHasSelectorWithHasAndWithSuffixSibling = function(test) 565 exports.testPseudoClassHasSelectorWithHasAndWithSuffixSibling = function(test)
475 { 566 {
476 // <div> 567 runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling(test, "div:-abp-has(: -abp-has(div.inside)) + div > div");
477 // <div><div class="inside"></div></div> 568 };
478 // <div> 569
479 // <div>to hide</div> 570 exports.testPseudoClassHasSelectorWithHasAndWithSuffixSibling2 = function(test)
480 // </div> 571 {
481 // </div> 572 runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling(test, "div:-abp-has(: -abp-has(> div.inside)) + div > div");
482 var parent = createElementWithStyle("{}"); 573 };
483 var middle = createElementWithStyle("{}", parent);
484 var inside = createElementWithStyle("{}", middle);
485 inside.className = "inside";
486 var sibling = createElementWithStyle("{}", parent);
487 var toHide = createElementWithStyle("{}", sibling);
488 applyElemHideEmulation(
489 ["div:has(:has(div.inside)) + div > div"],
490 function()
491 {
492 expectVisible(test, parent);
493 expectVisible(test, middle);
494 expectVisible(test, inside);
495 expectVisible(test, sibling);
496 expectHidden(test, toHide);
497 test.done();
498 }
499 );
500 };
501
502 574
503 exports.testPseudoClassHasSelectorWithPropSelector = function(test) 575 exports.testPseudoClassHasSelectorWithPropSelector = function(test)
504 { 576 {
505 var parent = createElementWithStyle("{}"); 577 let parent = createElementWithStyle("{}");
506 var child = createElementWithStyle("{background-color: #000}", parent); 578 let child = createElementWithStyle("{background-color: #000}", parent);
507 applyElemHideEmulation( 579 applyElemHideEmulation(
508 ["div:has([-abp-properties='background-color: rgb(0, 0, 0)'])"], 580 ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"]
509 function() 581 ).then(() =>
510 { 582 {
511 expectVisible(test, child); 583 expectVisible(test, child);
512 expectHidden(test, parent); 584 expectHidden(test, parent);
513 test.done(); 585 }).catch(unexpectedError.bind(test)).then(() => test.done());
514 } 586 };
515 );
516 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld