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

Powered by Google App Engine
This is Rietveld