OLD | NEW |
(Empty) | |
| 1 QUnit.module("Element hiding emulation", |
| 2 { |
| 3 before: function() |
| 4 { |
| 5 // The URL object in PhantomJS 2.1.7 does not implement any properties, so |
| 6 // we need a polyfill. |
| 7 if (!URL || !("origin" in URL)) |
| 8 { |
| 9 var doc = document.implementation.createHTMLDocument(); |
| 10 var anchor = doc.createElement("a"); |
| 11 doc.body.appendChild(anchor); |
| 12 URL = function(url) |
| 13 { |
| 14 if (!url) |
| 15 throw "Invalid URL"; |
| 16 anchor.href = url; |
| 17 this.origin = anchor.origin; |
| 18 }; |
| 19 } |
| 20 }, |
| 21 afterEach: function() |
| 22 { |
| 23 var styleElements = document.head.getElementsByTagName("style"); |
| 24 for (var i = 0; i < styleElements.length; i++) |
| 25 document.head.removeChild(styleElements[0]); |
| 26 } |
| 27 }); |
| 28 |
| 29 QUnit.assert.hidden = function(element) |
| 30 { |
| 31 this.equal(getComputedStyle(element).display, "none", |
| 32 "The element's display property should be set to 'none'"); |
| 33 }; |
| 34 |
| 35 QUnit.assert.visible = function(element) |
| 36 { |
| 37 this.notEqual(getComputedStyle(element).display, "none", |
| 38 "The element's display property should not be set to 'none'"); |
| 39 }; |
| 40 |
| 41 function findUniqueId() |
| 42 { |
| 43 var id = "elemHideEmulationTest-" + Math.floor(Math.random() * 10000); |
| 44 if (!document.getElementById(id)) |
| 45 return id; |
| 46 return findUniqueId(); |
| 47 } |
| 48 |
| 49 function insertStyleRule(rule) |
| 50 { |
| 51 var styleElement; |
| 52 var styleElements = document.head.getElementsByTagName("style"); |
| 53 if (styleElements.length) |
| 54 styleElement = styleElements[0]; |
| 55 else |
| 56 { |
| 57 styleElement = document.createElement("style"); |
| 58 document.head.appendChild(styleElement); |
| 59 } |
| 60 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); |
| 61 } |
| 62 |
| 63 function createElementWithStyle(styleBlock) |
| 64 { |
| 65 var element = document.createElement("div"); |
| 66 element.id = findUniqueId(); |
| 67 document.body.appendChild(element); |
| 68 insertStyleRule("#" + element.id + " " + styleBlock); |
| 69 return element; |
| 70 } |
| 71 |
| 72 function applyElemHideEmulation(selectors, callback) |
| 73 { |
| 74 var elemHideEmulation = new ElemHideEmulation( |
| 75 window, |
| 76 function(callback) |
| 77 { |
| 78 var patterns = []; |
| 79 selectors.forEach(function(selector) |
| 80 { |
| 81 patterns.push({selector: selector}); |
| 82 }); |
| 83 callback(patterns); |
| 84 }, |
| 85 function(selectors) |
| 86 { |
| 87 if (!selectors.length) |
| 88 return; |
| 89 var selector = selectors.join(", "); |
| 90 insertStyleRule(selector + "{display: none !important;}"); |
| 91 } |
| 92 ); |
| 93 |
| 94 elemHideEmulation.load(function() |
| 95 { |
| 96 elemHideEmulation.apply(); |
| 97 callback(); |
| 98 }); |
| 99 } |
| 100 |
| 101 QUnit.test("Verbatim property selector", function(assert) |
| 102 { |
| 103 var done = assert.async(); |
| 104 var toHide = createElementWithStyle("{background-color: #000}"); |
| 105 applyElemHideEmulation(["[-abp-properties='background-color: rgb(0, 0, 0)']"], |
| 106 function() |
| 107 { |
| 108 assert.hidden(toHide); |
| 109 done(); |
| 110 }); |
| 111 }); |
| 112 |
| 113 QUnit.test("Property selector with wildcard", function(assert) |
| 114 { |
| 115 var done = assert.async(); |
| 116 var toHide = createElementWithStyle("{background-color: #000}"); |
| 117 applyElemHideEmulation(["[-abp-properties='*color: rgb(0, 0, 0)']"], |
| 118 function() |
| 119 { |
| 120 assert.hidden(toHide); |
| 121 done(); |
| 122 }); |
| 123 }); |
| 124 |
| 125 QUnit.test("Property selector with regular expression", function(assert) |
| 126 { |
| 127 var done = assert.async(); |
| 128 var toHide = createElementWithStyle("{background-color: #000}"); |
| 129 applyElemHideEmulation(["[-abp-properties='/.*color: rgb\\(0, 0, 0\\)/']"], |
| 130 function() |
| 131 { |
| 132 assert.hidden(toHide); |
| 133 done(); |
| 134 }); |
| 135 }); |
| 136 |
| 137 QUnit.test("Property selector works for dynamically changed property", |
| 138 function(assert) |
| 139 { |
| 140 var done = assert.async(); |
| 141 var toHide = createElementWithStyle("{}"); |
| 142 applyElemHideEmulation(["[-abp-properties='background-color: rgb(0, 0, 0)']"], |
| 143 function() |
| 144 { |
| 145 assert.visible(toHide); |
| 146 insertStyleRule("#" + toHide.id + " {background-color: #000}"); |
| 147 setTimeout(function() |
| 148 { |
| 149 assert.hidden(toHide); |
| 150 done(); |
| 151 }); |
| 152 }); |
| 153 }); |
OLD | NEW |