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 with regular expression containing escaped brace", |
| 138 function(assert) |
| 139 { |
| 140 var done = assert.async(); |
| 141 var toHide = createElementWithStyle("{background-color: #000}"); |
| 142 applyElemHideEmulation( |
| 143 ["[-abp-properties='/background.\\x7B 0,6\\x7D : rgb\\(0, 0, 0\\)/']"], |
| 144 function() |
| 145 { |
| 146 assert.hidden(toHide); |
| 147 done(); |
| 148 }); |
| 149 }); |
| 150 |
| 151 QUnit.test("Property selector with regular expression containing improperly \ |
| 152 escaped brace", |
| 153 function(assert) |
| 154 { |
| 155 var done = assert.async(); |
| 156 var toHide = createElementWithStyle("{background-color: #000}"); |
| 157 applyElemHideEmulation( |
| 158 ["[-abp-properties='/background.\\x7B0,6\\x7D: rgb\\(0, 0, 0\\)/']"], |
| 159 function() |
| 160 { |
| 161 assert.visible(toHide); |
| 162 done(); |
| 163 }); |
| 164 }); |
| 165 |
| 166 QUnit.test("Property selector works for dynamically changed property", |
| 167 function(assert) |
| 168 { |
| 169 var done = assert.async(); |
| 170 var toHide = createElementWithStyle("{}"); |
| 171 applyElemHideEmulation(["[-abp-properties='background-color: rgb(0, 0, 0)']"], |
| 172 function() |
| 173 { |
| 174 assert.visible(toHide); |
| 175 insertStyleRule("#" + toHide.id + " {background-color: #000}"); |
| 176 setTimeout(function() |
| 177 { |
| 178 assert.hidden(toHide); |
| 179 done(); |
| 180 }); |
| 181 }); |
| 182 }); |
OLD | NEW |