Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/browser/elemHideEmulation.js

Issue 29367181: Issue 4726 - Add tests for the element hiding emulation content script (Closed) Base URL: https://bitbucket.org/fhd/adblockpluscore
Patch Set: Rebased on master, added tests for braces in regexp property selectors Created Dec. 13, 2016, 5:26 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(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++)
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.
25 document.head.removeChild(styleElements[0]);
26 }
27 });
28
29 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.
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;
kzar 2017/01/09 08:28:43 Nit: `let` rather than `var` throughout?
Felix Dahlke 2017/01/10 09:16:48 See above.
52 var styleElements = document.head.getElementsByTagName("style");
53 if (styleElements.length)
54 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
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)
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.
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)']"],
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
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 });
OLDNEW
« package.json ('K') | « package.json ('k') | test/browser/index.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld