| 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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 if (styleElements.length) | 78 if (styleElements.length) |
| 79 styleElement = styleElements[0]; | 79 styleElement = styleElements[0]; |
| 80 else | 80 else |
| 81 { | 81 { |
| 82 styleElement = document.createElement("style"); | 82 styleElement = document.createElement("style"); |
| 83 document.head.appendChild(styleElement); | 83 document.head.appendChild(styleElement); |
| 84 } | 84 } |
| 85 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); | 85 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); |
| 86 } | 86 } |
| 87 | 87 |
| 88 function createElementWithStyle(styleBlock) | 88 // insert a <div> with a unique id and and empty CSS rule |
| 89 // for the the selector matching the id. |
| 90 function createElementWithStyle(styleBlock, parent) |
| 89 { | 91 { |
| 90 var element = document.createElement("div"); | 92 var element = document.createElement("div"); |
| 91 element.id = findUniqueId(); | 93 element.id = findUniqueId(); |
| 92 document.body.appendChild(element); | 94 if (!parent) |
| 95 document.body.appendChild(element); |
| 96 else |
| 97 parent.appendChild(element); |
| 93 insertStyleRule("#" + element.id + " " + styleBlock); | 98 insertStyleRule("#" + element.id + " " + styleBlock); |
| 94 return element; | 99 return element; |
| 95 } | 100 } |
| 96 | 101 |
| 97 function applyElemHideEmulation(selectors, callback) | 102 // Will ensure the class ElemHideEmulation is loaded |
| 103 // and then will call the callback. |
| 104 // NOTE: if it never loads, this will probably hang in an infinite |
| 105 // loop |
| 106 function loadElemHideEmulation(callback) |
| 98 { | 107 { |
| 99 if (typeof ElemHideEmulation == "undefined") | 108 if (typeof ElemHideEmulation == "undefined") |
| 100 { | 109 { |
| 101 loadScript(myUrl + "/../../../lib/common.js", function() | 110 loadScript(myUrl + "/../../../lib/common.js", function() |
| 102 { | 111 { |
| 103 loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js", | 112 loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js", |
| 104 function() | 113 function() |
| 105 { | 114 { |
| 106 applyElemHideEmulation(selectors, callback); | 115 loadElemHideEmulation(callback); |
| 107 }); | 116 }); |
| 108 }); | 117 }); |
| 109 return; | 118 return; |
| 110 } | 119 } |
| 111 | 120 |
| 112 var elemHideEmulation = new ElemHideEmulation( | 121 callback(); |
| 113 window, | 122 } |
| 114 function(callback) | 123 |
| 115 { | 124 // instantiate a ElemHideEmulation with @selectors. |
| 116 var patterns = []; | 125 function applyElemHideEmulation(selectors, callback) |
| 117 selectors.forEach(function(selector) | 126 { |
| 127 loadElemHideEmulation(function() |
| 128 { |
| 129 var elemHideEmulation = new ElemHideEmulation( |
| 130 window, |
| 131 function(callback) |
| 118 { | 132 { |
| 119 patterns.push({selector: selector}); | 133 var patterns = []; |
| 120 }); | 134 selectors.forEach(function(selector) |
| 121 callback(patterns); | 135 { |
| 122 }, | 136 patterns.push({selector: selector}); |
| 123 function(selectors) | 137 }); |
| 124 { | 138 callback(patterns); |
| 125 if (!selectors.length) | 139 }, |
| 126 return; | 140 function(selectors) |
| 127 var selector = selectors.join(", "); | 141 { |
| 128 insertStyleRule(selector + "{display: none !important;}"); | 142 if (!selectors.length) |
| 129 } | 143 return; |
| 130 ); | 144 var selector = selectors.join(", "); |
| 131 | 145 insertStyleRule(selector + "{display: none !important;}"); |
| 132 elemHideEmulation.apply(); | 146 }, |
| 133 callback(); | 147 function(elements) |
| 134 } | 148 { |
| 149 if (!elements.length) |
| 150 return; |
| 151 for (var i = 0; i < elements.length; i++) |
| 152 elements[i].style.display = "none"; |
| 153 } |
| 154 ); |
| 155 |
| 156 elemHideEmulation.apply(); |
| 157 callback(); |
| 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 }); |
| 178 }; |
| 179 |
| 180 exports.testExtraFirstSelector = function(test) |
| 181 { |
| 182 loadElemHideEmulation(function() |
| 183 { |
| 184 var myselector = "elem.class1.class2"; |
| 185 var shortSel = extractFirstSelector(myselector); |
| 186 |
| 187 test.equal(shortSel, myselector); |
| 188 |
| 189 myselector = "elem > elem2"; |
| 190 shortSel = extractFirstSelector(myselector); |
| 191 |
| 192 test.equal(shortSel, "elem"); |
| 193 |
| 194 myselector = "elem+elem2 > elem3"; |
| 195 shortSel = extractFirstSelector(myselector); |
| 196 |
| 197 test.equal(shortSel, "elem"); |
| 198 |
| 199 myselector = "elem~elem2 > elem3"; |
| 200 shortSel = extractFirstSelector(myselector); |
| 201 |
| 202 test.equal(shortSel, "elem"); |
| 203 |
| 204 test.done(); |
| 205 }); |
| 206 }; |
| 207 |
| 208 exports.testSplitStyleRule = function(test) |
| 209 { |
| 210 loadElemHideEmulation(function() |
| 211 { |
| 212 var selectors = splitSelector("div:has(div) > [-abp-properties='background-c
olor: rgb(0, 0, 0)'] > span"); |
| 213 test.ok(selectors); |
| 214 test.equal(selectors.length, 1, "There is only one selector"); |
| 215 |
| 216 selectors = splitSelector("div:has(div), [-abp-properties='background-color:
rgb(0, 0, 0)']"); |
| 217 test.ok(selectors); |
| 218 test.equal(selectors.length, 2, "There are two selectors"); |
| 219 |
| 220 test.done(); |
| 221 }); |
| 222 }; |
| 135 | 223 |
| 136 exports.testVerbatimPropertySelector = function(test) | 224 exports.testVerbatimPropertySelector = function(test) |
| 137 { | 225 { |
| 138 var toHide = createElementWithStyle("{background-color: #000}"); | 226 var toHide = createElementWithStyle("{background-color: #000}"); |
| 139 applyElemHideEmulation( | 227 applyElemHideEmulation( |
| 140 ["[-abp-properties='background-color: rgb(0, 0, 0)']"], | 228 ["[-abp-properties='background-color: rgb(0, 0, 0)']"], |
| 141 function() | 229 function() |
| 142 { | 230 { |
| 143 expectHidden(test, toHide); | 231 expectHidden(test, toHide); |
| 144 test.done(); | 232 test.done(); |
| 145 } | 233 } |
| 146 ); | 234 ); |
| 147 }; | 235 }; |
| 148 | 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 |
| 149 exports.testPropertySelectorWithWildcard = function(test) | 299 exports.testPropertySelectorWithWildcard = function(test) |
| 150 { | 300 { |
| 151 var toHide = createElementWithStyle("{background-color: #000}"); | 301 var toHide = createElementWithStyle("{background-color: #000}"); |
| 152 applyElemHideEmulation( | 302 applyElemHideEmulation( |
| 153 ["[-abp-properties='*color: rgb(0, 0, 0)']"], | 303 ["[-abp-properties='*color: rgb(0, 0, 0)']"], |
| 154 function() | 304 function() |
| 155 { | 305 { |
| 156 expectHidden(test, toHide); | 306 expectHidden(test, toHide); |
| 157 test.done(); | 307 test.done(); |
| 158 } | 308 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 expectVisible(test, toHide); | 358 expectVisible(test, toHide); |
| 209 insertStyleRule("#" + toHide.id + " {background-color: #000}"); | 359 insertStyleRule("#" + toHide.id + " {background-color: #000}"); |
| 210 window.setTimeout(function() | 360 window.setTimeout(function() |
| 211 { | 361 { |
| 212 expectHidden(test, toHide); | 362 expectHidden(test, toHide); |
| 213 test.done(); | 363 test.done(); |
| 214 }, 0); | 364 }, 0); |
| 215 } | 365 } |
| 216 ); | 366 ); |
| 217 }; | 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 }; |
| 386 |
| 387 exports.testPseudoClassHasSelector = function(test) |
| 388 { |
| 389 var toHide = createElementWithStyle("{}"); |
| 390 applyElemHideEmulation( |
| 391 ["div:has(div)"], |
| 392 function() |
| 393 { |
| 394 expectVisible(test, toHide); |
| 395 test.done(); |
| 396 } |
| 397 ); |
| 398 }; |
| 399 |
| 400 exports.testPseudoClassHasSelectorWithPrefix = function(test) |
| 401 { |
| 402 var parent = createElementWithStyle("{}"); |
| 403 var child = createElementWithStyle("{}", parent); |
| 404 applyElemHideEmulation( |
| 405 ["div:has(div)"], |
| 406 function() |
| 407 { |
| 408 expectHidden(test, parent); |
| 409 expectVisible(test, child); |
| 410 test.done(); |
| 411 } |
| 412 ); |
| 413 }; |
| 414 |
| 415 exports.testPseudoClassHasSelectorWithSuffix = function(test) |
| 416 { |
| 417 var parent = createElementWithStyle("{}"); |
| 418 var middle = createElementWithStyle("{}", parent); |
| 419 var child = createElementWithStyle("{}", middle); |
| 420 applyElemHideEmulation( |
| 421 ["div:has(div) > div"], |
| 422 function() |
| 423 { |
| 424 expectVisible(test, parent); |
| 425 expectVisible(test, middle); |
| 426 expectHidden(test, child); |
| 427 test.done(); |
| 428 } |
| 429 ); |
| 430 }; |
| 431 |
| 432 exports.testPseudoClassHasSelectorWithSuffixSibling = function(test) |
| 433 { |
| 434 var parent = createElementWithStyle("{}"); |
| 435 var middle = createElementWithStyle("{}", parent); |
| 436 var toHide = createElementWithStyle("{}", parent); |
| 437 applyElemHideEmulation( |
| 438 ["div:has(div) + div"], |
| 439 function() |
| 440 { |
| 441 expectVisible(test, parent); |
| 442 expectVisible(test, middle); |
| 443 expectHidden(test, toHide); |
| 444 test.done(); |
| 445 } |
| 446 ); |
| 447 }; |
| 448 |
| 449 exports.testPseudoClassHasSelectorWithSuffixSibling = function(test) |
| 450 { |
| 451 // <div> |
| 452 // <div></div> |
| 453 // <div> |
| 454 // <div>to hide</div> |
| 455 // </div> |
| 456 // </div> |
| 457 var parent = createElementWithStyle("{}"); |
| 458 var middle = createElementWithStyle("{}", parent); |
| 459 var sibling = createElementWithStyle("{}", parent); |
| 460 var toHide = createElementWithStyle("{}", sibling); |
| 461 applyElemHideEmulation( |
| 462 ["div:has(div) + div > div"], |
| 463 function() |
| 464 { |
| 465 expectVisible(test, parent); |
| 466 expectVisible(test, middle); |
| 467 expectVisible(test, sibling); |
| 468 expectHidden(test, toHide); |
| 469 test.done(); |
| 470 } |
| 471 ); |
| 472 }; |
| 473 |
| 474 exports.testPseudoClassHasSelectorWithHasAndWithSuffixSibling = function(test) |
| 475 { |
| 476 // <div> |
| 477 // <div><div class="inside"></div></div> |
| 478 // <div> |
| 479 // <div>to hide</div> |
| 480 // </div> |
| 481 // </div> |
| 482 var parent = createElementWithStyle("{}"); |
| 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 |
| 503 exports.testPseudoClassHasSelectorWithPropSelector = function(test) |
| 504 { |
| 505 var parent = createElementWithStyle("{}"); |
| 506 var child = createElementWithStyle("{background-color: #000}", parent); |
| 507 applyElemHideEmulation( |
| 508 ["div:has([-abp-properties='background-color: rgb(0, 0, 0)'])"], |
| 509 function() |
| 510 { |
| 511 expectVisible(test, child); |
| 512 expectHidden(test, parent); |
| 513 test.done(); |
| 514 } |
| 515 ); |
| 516 }; |
| OLD | NEW |