Index: test/browser/elemHideEmulation.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/test/browser/elemHideEmulation.js |
@@ -0,0 +1,182 @@ |
+QUnit.module("Element hiding emulation", |
+{ |
+ before: function() |
+ { |
+ // The URL object in PhantomJS 2.1.7 does not implement any properties, so |
+ // we need a polyfill. |
+ if (!URL || !("origin" in URL)) |
+ { |
+ var doc = document.implementation.createHTMLDocument(); |
+ var anchor = doc.createElement("a"); |
+ doc.body.appendChild(anchor); |
+ URL = function(url) |
+ { |
+ if (!url) |
+ throw "Invalid URL"; |
+ anchor.href = url; |
+ this.origin = anchor.origin; |
+ }; |
+ } |
+ }, |
+ afterEach: function() |
+ { |
+ var styleElements = document.head.getElementsByTagName("style"); |
+ for (var i = 0; i < styleElements.length; i++) |
kzar
2017/01/09 08:28:43
Can we not use more modern JavaScript features wit
Felix Dahlke
2017/01/10 09:16:49
Unfortunately not with version 2.1.7, that one sti
kzar
2017/01/10 11:05:33
Damn, this could be a problem in the near future s
kzar
2017/01/11 04:25:28
You missed this one? (Some other smaller ones in t
Felix Dahlke
2017/01/11 10:06:02
No, it rather sounded like you thought this could
kzar
2017/01/11 11:19:18
Please at least put a comment / note in relevant p
Felix Dahlke
2017/01/12 09:24:06
Sure! This affects chrome/content/elemHideEmulatio
kzar
2017/01/12 09:42:13
Cool, sounds good. (IMO an issue is overkill since
Felix Dahlke
2017/01/17 19:41:08
Done.
|
+ document.head.removeChild(styleElements[0]); |
+ } |
+}); |
+ |
+QUnit.assert.hidden = function(element) |
kzar
2017/01/09 08:28:43
I wonder if the URL polyfill and these utility fun
Felix Dahlke
2017/01/10 09:16:49
I personally prefer to share code once it needs to
kzar
2017/01/10 11:05:33
Fair enough.
|
+{ |
+ this.equal(getComputedStyle(element).display, "none", |
+ "The element's display property should be set to 'none'"); |
+}; |
+ |
+QUnit.assert.visible = function(element) |
+{ |
+ this.notEqual(getComputedStyle(element).display, "none", |
+ "The element's display property should not be set to 'none'"); |
+}; |
+ |
+function findUniqueId() |
+{ |
+ var id = "elemHideEmulationTest-" + Math.floor(Math.random() * 10000); |
+ if (!document.getElementById(id)) |
+ return id; |
+ return findUniqueId(); |
+} |
+ |
+function insertStyleRule(rule) |
+{ |
+ var styleElement; |
kzar
2017/01/09 08:28:43
Nit: `let` rather than `var` throughout?
Felix Dahlke
2017/01/10 09:16:48
See above.
|
+ var styleElements = document.head.getElementsByTagName("style"); |
+ if (styleElements.length) |
+ styleElement = styleElements[0]; |
kzar
2017/01/09 08:28:43
Nit: Since the else clause has braces the if claus
Felix Dahlke
2017/01/10 09:16:48
Why? I personally prefer it this way.
kzar
2017/01/10 11:05:33
I've been told to do that on codereviews before by
Felix Dahlke
2017/01/10 13:27:32
IIRC he actually prefers it that way, but since we
kzar
2017/01/11 04:25:29
OK well how's this? I have been asked to use consi
Felix Dahlke
2017/01/11 10:06:02
What others have asked in other places is not part
kzar
2017/01/11 11:19:19
I think it is relevant what Wladimir has told me i
Felix Dahlke
2017/01/17 19:41:07
You're not reviewing it for him, you're reviewing
|
+ else |
+ { |
+ styleElement = document.createElement("style"); |
+ document.head.appendChild(styleElement); |
+ } |
+ styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); |
+} |
+ |
+function createElementWithStyle(styleBlock) |
+{ |
+ var element = document.createElement("div"); |
+ element.id = findUniqueId(); |
+ document.body.appendChild(element); |
+ insertStyleRule("#" + element.id + " " + styleBlock); |
+ return element; |
+} |
+ |
+function applyElemHideEmulation(selectors, callback) |
+{ |
+ var elemHideEmulation = new ElemHideEmulation( |
+ window, |
+ function(callback) |
kzar
2017/01/09 08:28:43
Nit: Mind using arrow functions here and for the o
Felix Dahlke
2017/01/10 09:16:48
See above.
|
+ { |
+ var patterns = []; |
+ selectors.forEach(function(selector) |
+ { |
+ patterns.push({selector: selector}); |
+ }); |
+ callback(patterns); |
+ }, |
+ function(selectors) |
+ { |
+ if (!selectors.length) |
+ return; |
+ var selector = selectors.join(", "); |
+ insertStyleRule(selector + "{display: none !important;}"); |
+ } |
+ ); |
+ |
+ elemHideEmulation.load(function() |
+ { |
+ elemHideEmulation.apply(); |
+ callback(); |
+ }); |
+} |
+ |
+QUnit.test("Verbatim property selector", function(assert) |
+{ |
+ var done = assert.async(); |
+ var toHide = createElementWithStyle("{background-color: #000}"); |
+ applyElemHideEmulation(["[-abp-properties='background-color: rgb(0, 0, 0)']"], |
kzar
2017/01/09 08:28:43
Nit: This indentation looks weird. How about somet
Felix Dahlke
2017/01/10 09:16:49
Yeah I know, I did some research in our code base
kzar
2017/01/10 11:05:33
Well I'm pretty sure we've done it the way I've su
Felix Dahlke
2017/01/10 13:27:32
Then I figure we should leave it as-is for the sak
kzar
2017/01/11 04:25:29
Or alternatively you could just improve the indent
Felix Dahlke
2017/01/11 10:06:02
I don't find it very beautiful, but it does look l
kzar
2017/01/11 11:19:18
Well we "indent the function body" all the time, f
Felix Dahlke
2017/01/12 09:24:06
No, I mean the opening brace of an anonymous funct
kzar
2017/01/12 09:42:13
I really think that you're overthinking this and s
Sebastian Noack
2017/01/12 23:12:58
I agree that putting the function header on a line
Felix Dahlke
2017/01/17 19:41:07
Thanks Sebastian, done.
(On a related note: I thi
|
+ function() |
+ { |
+ assert.hidden(toHide); |
+ done(); |
+ }); |
+}); |
+ |
+QUnit.test("Property selector with wildcard", function(assert) |
+{ |
+ var done = assert.async(); |
+ var toHide = createElementWithStyle("{background-color: #000}"); |
+ applyElemHideEmulation(["[-abp-properties='*color: rgb(0, 0, 0)']"], |
+ function() |
+ { |
+ assert.hidden(toHide); |
+ done(); |
+ }); |
+}); |
+ |
+QUnit.test("Property selector with regular expression", function(assert) |
+{ |
+ var done = assert.async(); |
+ var toHide = createElementWithStyle("{background-color: #000}"); |
+ applyElemHideEmulation(["[-abp-properties='/.*color: rgb\\(0, 0, 0\\)/']"], |
+ function() |
+ { |
+ assert.hidden(toHide); |
+ done(); |
+ }); |
+}); |
+ |
+QUnit.test("Property selector with regular expression containing escaped brace", |
+ function(assert) |
+{ |
+ var done = assert.async(); |
+ var toHide = createElementWithStyle("{background-color: #000}"); |
+ applyElemHideEmulation( |
+ ["[-abp-properties='/background.\\x7B 0,6\\x7D : rgb\\(0, 0, 0\\)/']"], |
+ function() |
+ { |
+ assert.hidden(toHide); |
+ done(); |
+ }); |
+}); |
+ |
+QUnit.test("Property selector with regular expression containing improperly \ |
+escaped brace", |
+ function(assert) |
+{ |
+ var done = assert.async(); |
+ var toHide = createElementWithStyle("{background-color: #000}"); |
+ applyElemHideEmulation( |
+ ["[-abp-properties='/background.\\x7B0,6\\x7D: rgb\\(0, 0, 0\\)/']"], |
+ function() |
+ { |
+ assert.visible(toHide); |
+ done(); |
+ }); |
+}); |
+ |
+QUnit.test("Property selector works for dynamically changed property", |
+ function(assert) |
+{ |
+ var done = assert.async(); |
+ var toHide = createElementWithStyle("{}"); |
+ applyElemHideEmulation(["[-abp-properties='background-color: rgb(0, 0, 0)']"], |
+ function() |
+ { |
+ assert.visible(toHide); |
+ insertStyleRule("#" + toHide.id + " {background-color: #000}"); |
+ setTimeout(function() |
+ { |
+ assert.hidden(toHide); |
+ done(); |
+ }); |
+ }); |
+}); |