OLD | NEW |
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 30 matching lines...) Expand all Loading... |
41 } | 41 } |
42 | 42 |
43 callback(); | 43 callback(); |
44 }; | 44 }; |
45 | 45 |
46 exports.tearDown = function(callback) | 46 exports.tearDown = function(callback) |
47 { | 47 { |
48 var styleElements = document.head.getElementsByTagName("style"); | 48 var styleElements = document.head.getElementsByTagName("style"); |
49 while (styleElements.length) | 49 while (styleElements.length) |
50 styleElements[0].parentNode.removeChild(styleElements[0]); | 50 styleElements[0].parentNode.removeChild(styleElements[0]); |
| 51 var child; |
| 52 while (child = document.body.firstChild) |
| 53 { |
| 54 document.body.removeChild(child); |
| 55 } |
51 callback(); | 56 callback(); |
52 }; | 57 }; |
53 | 58 |
54 function expectHidden(test, element) | 59 function expectHidden(test, element) |
55 { | 60 { |
56 test.equal(window.getComputedStyle(element).display, "none", | 61 test.equal(window.getComputedStyle(element).display, "none", |
57 "The element's display property should be set to 'none'"); | 62 "The element's display property should be set to 'none'"); |
58 } | 63 } |
59 | 64 |
60 function expectVisible(test, element) | 65 function expectVisible(test, element) |
(...skipping 17 matching lines...) Expand all Loading... |
78 if (styleElements.length) | 83 if (styleElements.length) |
79 styleElement = styleElements[0]; | 84 styleElement = styleElements[0]; |
80 else | 85 else |
81 { | 86 { |
82 styleElement = document.createElement("style"); | 87 styleElement = document.createElement("style"); |
83 document.head.appendChild(styleElement); | 88 document.head.appendChild(styleElement); |
84 } | 89 } |
85 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); | 90 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); |
86 } | 91 } |
87 | 92 |
88 function createElementWithStyle(styleBlock) | 93 // insert a <div> with a unique id and and empty CSS rule |
| 94 // for the the selector matching the id. |
| 95 function createElementWithStyle(styleBlock, parent) |
89 { | 96 { |
90 var element = document.createElement("div"); | 97 var element = document.createElement("div"); |
91 element.id = findUniqueId(); | 98 element.id = findUniqueId(); |
92 document.body.appendChild(element); | 99 if (!parent) |
| 100 document.body.appendChild(element); |
| 101 else |
| 102 parent.appendChild(element); |
93 insertStyleRule("#" + element.id + " " + styleBlock); | 103 insertStyleRule("#" + element.id + " " + styleBlock); |
94 return element; | 104 return element; |
95 } | 105 } |
96 | 106 |
97 function applyElemHideEmulation(selectors, callback) | 107 // Will ensure the class ElemHideEmulation is loaded |
| 108 // and then will call the callback. |
| 109 // NOTE: if it never loads, this will probably hang in an infinite |
| 110 // loop |
| 111 function loadElemHideEmulation(callback) |
98 { | 112 { |
99 if (typeof ElemHideEmulation == "undefined") | 113 if (typeof ElemHideEmulation == "undefined") |
100 { | 114 { |
101 loadScript(myUrl + "/../../../lib/common.js", function() | 115 loadScript(myUrl + "/../../../lib/common.js", function() |
102 { | 116 { |
103 loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js", | 117 loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js", |
104 function() | 118 function() |
105 { | 119 { |
106 applyElemHideEmulation(selectors, callback); | 120 loadElemHideEmulation(callback); |
107 }); | 121 }); |
108 }); | 122 }); |
109 return; | 123 return; |
110 } | 124 } |
111 | 125 |
112 var elemHideEmulation = new ElemHideEmulation( | 126 callback(); |
113 window, | 127 } |
114 function(callback) | 128 |
115 { | 129 // instantiate a ElemHideEmulation with @selectors. |
116 var patterns = []; | 130 function applyElemHideEmulation(selectors, callback) |
117 selectors.forEach(function(selector) | 131 { |
| 132 loadElemHideEmulation(function() |
| 133 { |
| 134 var elemHideEmulation = new ElemHideEmulation( |
| 135 window, |
| 136 function(callback) |
118 { | 137 { |
119 patterns.push({selector: selector}); | 138 var patterns = []; |
120 }); | 139 selectors.forEach(function(selector) |
121 callback(patterns); | 140 { |
122 }, | 141 patterns.push({selector: selector}); |
123 function(selectors) | 142 }); |
124 { | 143 callback(patterns); |
125 if (!selectors.length) | 144 }, |
126 return; | 145 function(selectors) |
127 var selector = selectors.join(", "); | 146 { |
128 insertStyleRule(selector + "{display: none !important;}"); | 147 if (!selectors.length) |
129 } | 148 return; |
130 ); | 149 var selector = selectors.join(", "); |
131 | 150 insertStyleRule(selector + "{display: none !important;}"); |
132 elemHideEmulation.apply(); | 151 }, |
133 callback(); | 152 function(elements) |
| 153 { |
| 154 if (!elements.length) |
| 155 return; |
| 156 for (var i = 0; i < elements.length; i++) |
| 157 elements[i].style.display = "none"; |
| 158 } |
| 159 ); |
| 160 |
| 161 elemHideEmulation.apply(); |
| 162 callback(); |
| 163 }.bind(this)); |
| 164 } |
| 165 |
| 166 exports.testPseudoHasRule = function(test) |
| 167 { |
| 168 loadElemHideEmulation(function() |
| 169 { |
| 170 var selector = "div:has(span)"; |
| 171 // testing the regexp |
| 172 var match = pseudoClassHasSelectorRegExp.exec(selector); |
| 173 test.ok(match); |
| 174 test.equal(match[1], "span"); |
| 175 |
| 176 selector = ":has(div.inside)"; |
| 177 match = pseudoClassHasSelectorRegExp.exec(selector); |
| 178 test.ok(match); |
| 179 test.equal(match[1], "div.inside"); |
| 180 |
| 181 selector = "div[-abp-selector=':has(div.inside)']"; |
| 182 match = abpSelectorRegExp.exec(selector); |
| 183 test.ok(match); |
| 184 test.equal(match[2], ":has(div.inside)"); |
| 185 |
| 186 selector = "[-abp-selector=':has(div.inside)']"; |
| 187 match = abpSelectorRegExp.exec(selector); |
| 188 test.ok(match); |
| 189 test.equal(match[2], ":has(div.inside)"); |
| 190 |
| 191 selector = "div[-abp-selector=':has(:-abp-properties(\"background-color: rgb
(0, 0, 0)\"))']"; |
| 192 match = abpSelectorRegExp.exec(selector); |
| 193 test.ok(match); |
| 194 test.equal(match[2], ":has(:-abp-properties(\"background-color: rgb(0, 0, 0)
\"))"); |
| 195 |
| 196 test.done(); |
| 197 }); |
| 198 }; |
| 199 |
| 200 exports.testSplitStyleRule = function(test) |
| 201 { |
| 202 loadElemHideEmulation(function() |
| 203 { |
| 204 var selectors = splitSelector("div:has(div) > [-abp-properties='background-c
olor: rgb(0, 0, 0)'] > span"); |
| 205 test.ok(selectors); |
| 206 test.equal(selectors.length, 1, "There is only one selector"); |
| 207 |
| 208 selectors = splitSelector("div:has(div), [-abp-properties='background-color:
rgb(0, 0, 0)']"); |
| 209 test.ok(selectors); |
| 210 test.equal(selectors.length, 2, "There are two selectors"); |
| 211 |
| 212 test.done(); |
| 213 }); |
| 214 }; |
| 215 |
| 216 function runTestVerbatimPropertySelector(test, selector) |
| 217 { |
| 218 var toHide = createElementWithStyle("{background-color: #000}"); |
| 219 applyElemHideEmulation( |
| 220 [selector], |
| 221 function() |
| 222 { |
| 223 expectHidden(test, toHide); |
| 224 test.done(); |
| 225 } |
| 226 ); |
134 } | 227 } |
135 | 228 |
136 exports.testVerbatimPropertySelector = function(test) | 229 exports.testVerbatimPropertySelector = function(test) |
137 { | 230 { |
| 231 runTestVerbatimPropertySelector(test, "[-abp-properties='background-color: rgb
(0, 0, 0)']"); |
| 232 }; |
| 233 |
| 234 /* Testing the new syntax */ |
| 235 exports.testVerbatimPropertyPseudoSelector = function(test) |
| 236 { |
| 237 runTestVerbatimPropertySelector(test, "[-abp-selector=':-abp-properties(\"back
ground-color: rgb(0, 0, 0)\")']"); |
| 238 }; |
| 239 |
| 240 function runTestVerbatimPropertySelectorWithPrefix(test, selector) |
| 241 { |
| 242 var parent = createElementWithStyle("{background-color: #000}"); |
| 243 var toHide = createElementWithStyle("{background-color: #000}", parent); |
| 244 applyElemHideEmulation( |
| 245 [selector], |
| 246 function() |
| 247 { |
| 248 expectVisible(test, parent); |
| 249 expectHidden(test, toHide); |
| 250 test.done(); |
| 251 } |
| 252 ); |
| 253 } |
| 254 |
| 255 exports.testVerbatimPropertySelectorWithPrefix = function(test) |
| 256 { |
| 257 runTestVerbatimPropertySelectorWithPrefix(test, "div > [-abp-properties='backg
round-color: rgb(0, 0, 0)']"); |
| 258 }; |
| 259 |
| 260 // testing the new syntax |
| 261 exports.testVerbatimPropertyPseudoSelectorWithPrefix = function(test) |
| 262 { |
| 263 runTestVerbatimPropertySelectorWithPrefix(test, "div > [-abp-selector=':-abp-p
roperties(\"background-color: rgb(0, 0, 0)\")']"); |
| 264 }; |
| 265 |
| 266 exports.testVerbatimPropertySelectorWithPrefixNoMatch = function(test) |
| 267 { |
| 268 var parent = createElementWithStyle("{background-color: #000}"); |
| 269 var toHide = createElementWithStyle("{background-color: #fff}", parent); |
| 270 applyElemHideEmulation( |
| 271 ["div > [-abp-properties='background-color: rgb(0, 0, 0)']"], |
| 272 function() |
| 273 { |
| 274 expectVisible(test, parent); |
| 275 expectVisible(test, toHide); |
| 276 test.done(); |
| 277 } |
| 278 ); |
| 279 }; |
| 280 |
| 281 function runTestVerbatimPropertySelectorWithSuffix(test, selector) |
| 282 { |
| 283 var parent = createElementWithStyle("{background-color: #000}"); |
| 284 var toHide = createElementWithStyle("{background-color: #000}", parent); |
| 285 applyElemHideEmulation( |
| 286 [selector], |
| 287 function() |
| 288 { |
| 289 expectVisible(test, parent); |
| 290 expectHidden(test, toHide); |
| 291 test.done(); |
| 292 } |
| 293 ); |
| 294 } |
| 295 |
| 296 exports.testVerbatimPropertySelectorWithSuffix = function(test) |
| 297 { |
| 298 runTestVerbatimPropertySelectorWithSuffix(test, "[-abp-properties='background-
color: rgb(0, 0, 0)'] > div"); |
| 299 }; |
| 300 |
| 301 exports.testVerbatimPropertyPseudoSelectorWithSuffix = function(test) |
| 302 { |
| 303 runTestVerbatimPropertySelectorWithSuffix(test, "[-abp-selector=':-abp-propert
ies(\"background-color: rgb(0, 0, 0)\")'] > div"); |
| 304 }; |
| 305 |
| 306 function runTestVerbatimPropertySelectorWithPrefixAndSuffix(test, selector) |
| 307 { |
| 308 var parent = createElementWithStyle("{background-color: #000}"); |
| 309 var middle = createElementWithStyle("{background-color: #000}", parent); |
| 310 var toHide = createElementWithStyle("{background-color: #000}", middle); |
| 311 applyElemHideEmulation( |
| 312 [selector], |
| 313 function() |
| 314 { |
| 315 expectVisible(test, parent); |
| 316 expectVisible(test, middle); |
| 317 expectHidden(test, toHide); |
| 318 test.done(); |
| 319 } |
| 320 ); |
| 321 } |
| 322 |
| 323 exports.testVerbatimPropertySelectorWithPrefixAndSuffix = function(test) |
| 324 { |
| 325 runTestVerbatimPropertySelectorWithPrefixAndSuffix(test, "div > [-abp-properti
es='background-color: rgb(0, 0, 0)'] > div"); |
| 326 }; |
| 327 |
| 328 exports.testVerbatimPropertyPseudoSelectorWithPrefixAndSuffix = function(test) |
| 329 { |
| 330 runTestVerbatimPropertySelectorWithPrefixAndSuffix(test, "div > [-abp-selector
=':-abp-properties(\"background-color: rgb(0, 0, 0)\")'] > div"); |
| 331 }; |
| 332 |
| 333 exports.testPropertySelectorWithWildcard = function(test) |
| 334 { |
138 var toHide = createElementWithStyle("{background-color: #000}"); | 335 var toHide = createElementWithStyle("{background-color: #000}"); |
139 applyElemHideEmulation( | 336 applyElemHideEmulation( |
140 ["[-abp-properties='background-color: rgb(0, 0, 0)']"], | |
141 function() | |
142 { | |
143 expectHidden(test, toHide); | |
144 test.done(); | |
145 } | |
146 ); | |
147 }; | |
148 | |
149 exports.testPropertySelectorWithWildcard = function(test) | |
150 { | |
151 var toHide = createElementWithStyle("{background-color: #000}"); | |
152 applyElemHideEmulation( | |
153 ["[-abp-properties='*color: rgb(0, 0, 0)']"], | 337 ["[-abp-properties='*color: rgb(0, 0, 0)']"], |
154 function() | 338 function() |
155 { | 339 { |
156 expectHidden(test, toHide); | 340 expectHidden(test, toHide); |
157 test.done(); | 341 test.done(); |
158 } | 342 } |
159 ); | 343 ); |
160 }; | 344 }; |
161 | 345 |
162 exports.testPropertySelectorWithRegularExpression = function(test) | 346 exports.testPropertySelectorWithRegularExpression = function(test) |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 expectVisible(test, toHide); | 392 expectVisible(test, toHide); |
209 insertStyleRule("#" + toHide.id + " {background-color: #000}"); | 393 insertStyleRule("#" + toHide.id + " {background-color: #000}"); |
210 window.setTimeout(function() | 394 window.setTimeout(function() |
211 { | 395 { |
212 expectHidden(test, toHide); | 396 expectHidden(test, toHide); |
213 test.done(); | 397 test.done(); |
214 }, 0); | 398 }, 0); |
215 } | 399 } |
216 ); | 400 ); |
217 }; | 401 }; |
| 402 |
| 403 exports.testPseudoClassHasMatcher = function(test) |
| 404 { |
| 405 var parent = createElementWithStyle("{}"); |
| 406 var child = createElementWithStyle("{}", parent); |
| 407 loadElemHideEmulation(function() |
| 408 { |
| 409 var matcher = new PseudoHasMatcher("div"); |
| 410 test.equal(matcher.match(parent), true, "Parent should contain what is expec
ted"); |
| 411 test.equal(matcher.match(child), false, "Child shouldn't match"); |
| 412 |
| 413 var matcher2 = new PseudoHasMatcher("span"); |
| 414 test.equal(matcher2.match(parent), false, "Doesn't have a <span> child, shou
ldn't match"); |
| 415 test.equal(matcher2.match(child), false, "Child shouldn't match"); |
| 416 |
| 417 test.done(); |
| 418 }); |
| 419 }; |
| 420 |
| 421 exports.testPseudoClassHasSelector = function(test) |
| 422 { |
| 423 var toHide = createElementWithStyle("{}"); |
| 424 applyElemHideEmulation( |
| 425 ["div[-abp-selector=':has(div)']"], |
| 426 function() |
| 427 { |
| 428 expectVisible(test, toHide); |
| 429 test.done(); |
| 430 } |
| 431 ); |
| 432 }; |
| 433 |
| 434 exports.testPseudoClassHasSelectorWithPrefix = function(test) |
| 435 { |
| 436 var parent = createElementWithStyle("{}"); |
| 437 var child = createElementWithStyle("{}", parent); |
| 438 applyElemHideEmulation( |
| 439 ["div[-abp-selector=':has(div)']"], |
| 440 function() |
| 441 { |
| 442 expectHidden(test, parent); |
| 443 expectVisible(test, child); |
| 444 test.done(); |
| 445 } |
| 446 ); |
| 447 }; |
| 448 |
| 449 exports.testPseudoClassHasSelectorWithSuffix = function(test) |
| 450 { |
| 451 var parent = createElementWithStyle("{}"); |
| 452 var middle = createElementWithStyle("{}", parent); |
| 453 var child = createElementWithStyle("{}", middle); |
| 454 applyElemHideEmulation( |
| 455 ["div[-abp-selector=':has(div)'] > div"], |
| 456 function() |
| 457 { |
| 458 expectVisible(test, parent); |
| 459 expectHidden(test, middle); |
| 460 expectHidden(test, child); |
| 461 test.done(); |
| 462 } |
| 463 ); |
| 464 }; |
| 465 |
| 466 exports.testPseudoClassHasSelectorWithSuffixSibling = function(test) |
| 467 { |
| 468 var parent = createElementWithStyle("{}"); |
| 469 var middle = createElementWithStyle("{}", parent); |
| 470 var toHide = createElementWithStyle("{}"); |
| 471 applyElemHideEmulation( |
| 472 ["div[-abp-selector=':has(div)'] + div"], |
| 473 function() |
| 474 { |
| 475 expectVisible(test, parent); |
| 476 expectVisible(test, middle); |
| 477 expectHidden(test, toHide); |
| 478 test.done(); |
| 479 } |
| 480 ); |
| 481 }; |
| 482 |
| 483 exports.testPseudoClassHasSelectorWithSuffixSiblingChild = function(test) |
| 484 { |
| 485 // <div> |
| 486 // <div></div> |
| 487 // <div> |
| 488 // <div>to hide</div> |
| 489 // </div> |
| 490 // </div> |
| 491 var parent = createElementWithStyle("{}"); |
| 492 var middle = createElementWithStyle("{}", parent); |
| 493 var sibling = createElementWithStyle("{}"); |
| 494 var toHide = createElementWithStyle("{}", sibling); |
| 495 applyElemHideEmulation( |
| 496 ["div[-abp-selector=':has(div)'] + div > div"], |
| 497 function() |
| 498 { |
| 499 expectVisible(test, parent); |
| 500 expectVisible(test, middle); |
| 501 expectVisible(test, sibling); |
| 502 expectHidden(test, toHide); |
| 503 test.done(); |
| 504 } |
| 505 ); |
| 506 }; |
| 507 |
| 508 function runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling(test, selector
) |
| 509 { |
| 510 document.body.innerHTML = '<div id="parent">' + |
| 511 '<div id="middle">' + |
| 512 '<div id="middle.1"><div id="inside" class="inside"></div></div>' + |
| 513 '</div>' + |
| 514 '<div id="sibling">' + |
| 515 '<div id="tohide">to hide</div>' + |
| 516 '</div>' + |
| 517 '<div id="sibling2">' + |
| 518 '<div id="sibling2.1"><div id="sibling2.1.1" class="inside"></div></div>'
+ |
| 519 '</div>' + |
| 520 '</div>'; |
| 521 var parent = document.getElementById("parent"); |
| 522 var middle = document.getElementById("middle"); |
| 523 var inside = document.getElementById("inside"); |
| 524 var sibling = document.getElementById("sibling"); |
| 525 var sibling2 = document.getElementById("sibling2"); |
| 526 var toHide = document.getElementById("tohide"); |
| 527 |
| 528 applyElemHideEmulation( |
| 529 [selector], |
| 530 function() |
| 531 { |
| 532 expectVisible(test, parent); |
| 533 expectVisible(test, middle); |
| 534 expectVisible(test, inside); |
| 535 expectVisible(test, sibling); |
| 536 expectVisible(test, sibling2); |
| 537 expectHidden(test, toHide); |
| 538 test.done(); |
| 539 } |
| 540 ); |
| 541 } |
| 542 |
| 543 exports.testPseudoClassHasSelectorWithHasAndWithSuffixSibling = function(test) |
| 544 { |
| 545 runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling(test, "div[-abp-selec
tor=':has(:has(div.inside))'] + div > div"); |
| 546 }; |
| 547 |
| 548 exports.testPseudoClassHasSelectorWithHasAndWithSuffixSibling2 = function(test) |
| 549 { |
| 550 runTestPseudoClassHasSelectorWithHasAndWithSuffixSibling(test, "div[-abp-selec
tor=':has(:has(> div.inside))'] + div > div"); |
| 551 }; |
| 552 |
| 553 exports.testPseudoClassHasSelectorWithPropSelector = function(test) |
| 554 { |
| 555 var parent = createElementWithStyle("{}"); |
| 556 var child = createElementWithStyle("{background-color: #000}", parent); |
| 557 applyElemHideEmulation( |
| 558 ["div[-abp-selector=':has(:-abp-properties(\"background-color: rgb(0, 0, 0)\
"))']"], |
| 559 function() |
| 560 { |
| 561 expectVisible(test, child); |
| 562 expectHidden(test, parent); |
| 563 test.done(); |
| 564 } |
| 565 ); |
| 566 }; |
OLD | NEW |