|
1 |
|
2 <!DOCTYPE HTML> |
|
3 <html> |
|
4 <!-- |
|
5 https://bugzilla.mozilla.org/show_bug.cgi?id=887836 |
|
6 --> |
|
7 <head> |
|
8 <meta charset="utf-8"> |
|
9 <title>Test for Bug 887836</title> |
|
10 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
11 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> |
|
12 </head> |
|
13 <body> |
|
14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=887836">Mozilla Bug 887836</a> |
|
15 <p id="display"></p> |
|
16 <div id="content" style="display: none"> |
|
17 <iframe name="x" id="x"></iframe> |
|
18 <iframe name="y" id="y"></iframe> |
|
19 </div> |
|
20 <pre id="test"> |
|
21 </pre> |
|
22 <a href="http://www.example.net?a=b&c=d" id="anchor">foobar</a> |
|
23 <area href="http://www.example.net?a=b&c=d" id="area">foobar</area> |
|
24 <script type="application/javascript"> |
|
25 |
|
26 /** Test for Bug 887836 **/ |
|
27 ok("URLSearchParams" in window, "window.URLSearchParams exists"); |
|
28 |
|
29 function testSimpleURLSearchParams() { |
|
30 var u = new URLSearchParams(); |
|
31 ok(u, "URLSearchParams created"); |
|
32 is(u.has('foo'), false, 'URLSearchParams.has(foo)'); |
|
33 is(u.get('foo'), '', 'URLSearchParams.get(foo)'); |
|
34 is(u.getAll('foo').length, 0, 'URLSearchParams.getAll(foo)'); |
|
35 |
|
36 u.append('foo', 'bar'); |
|
37 is(u.has('foo'), true, 'URLSearchParams.has(foo)'); |
|
38 is(u.get('foo'), 'bar', 'URLSearchParams.get(foo)'); |
|
39 is(u.getAll('foo').length, 1, 'URLSearchParams.getAll(foo)'); |
|
40 |
|
41 u.set('foo', 'bar2'); |
|
42 is(u.get('foo'), 'bar2', 'URLSearchParams.get(foo)'); |
|
43 is(u.getAll('foo').length, 1, 'URLSearchParams.getAll(foo)'); |
|
44 |
|
45 is(u + "", "foo=bar2", "stringifier"); |
|
46 |
|
47 u.delete('foo'); |
|
48 |
|
49 runTest(); |
|
50 } |
|
51 |
|
52 function testCopyURLSearchParams() { |
|
53 var u = new URLSearchParams(); |
|
54 ok(u, "URLSearchParams created"); |
|
55 u.append('foo', 'bar'); |
|
56 |
|
57 var uu = new URLSearchParams(u); |
|
58 is(uu.get('foo'), 'bar', 'uu.get()'); |
|
59 |
|
60 u.append('foo', 'bar2'); |
|
61 is(u.getAll('foo').length, 2, "u.getAll()"); |
|
62 is(uu.getAll('foo').length, 1, "uu.getAll()"); |
|
63 |
|
64 runTest(); |
|
65 } |
|
66 |
|
67 function testParserURLSearchParams() { |
|
68 var checks = [ |
|
69 { input: '', data: {} }, |
|
70 { input: 'a', data: { 'a' : [''] } }, |
|
71 { input: 'a=b', data: { 'a' : ['b'] } }, |
|
72 { input: 'a=', data: { 'a' : [''] } }, |
|
73 { input: '=b', data: { '' : ['b'] } }, |
|
74 { input: '&', data: {} }, |
|
75 { input: '&a', data: { 'a' : [''] } }, |
|
76 { input: 'a&', data: { 'a' : [''] } }, |
|
77 { input: 'a&a', data: { 'a' : ['', ''] } }, |
|
78 { input: 'a&b&c', data: { 'a' : [''], 'b' : [''], 'c' : [''] } }, |
|
79 { input: 'a=b&c=d', data: { 'a' : ['b'], 'c' : ['d'] } }, |
|
80 { input: 'a=b&c=d&', data: { 'a' : ['b'], 'c' : ['d'] } }, |
|
81 { input: '&&&a=b&&&&c=d&', data: { 'a' : ['b'], 'c' : ['d'] } }, |
|
82 { input: 'a=a&a=b&a=c', data: { 'a' : ['a', 'b', 'c'] } }, |
|
83 { input: 'a==a', data: { 'a' : ['=a'] } }, |
|
84 { input: 'a=a+b+c+d', data: { 'a' : ['a b c d'] } }, |
|
85 { input: '%=a', data: { '%' : ['a'] } }, |
|
86 { input: '%a=a', data: { '%a' : ['a'] } }, |
|
87 { input: '%a_=a', data: { '%a_' : ['a'] } }, |
|
88 { input: '%61=a', data: { 'a' : ['a'] } }, |
|
89 { input: '%=a', data: { '%' : ['a'] } }, |
|
90 { input: '%a=a', data: { '%a' : ['a'] } }, |
|
91 { input: '%a_=a', data: { '%a_' : ['a'] } }, |
|
92 { input: '%61=a', data: { 'a' : ['a'] } }, |
|
93 { input: '%61+%4d%4D=', data: { 'a MM' : [''] } }, |
|
94 ]; |
|
95 |
|
96 for (var i = 0; i < checks.length; ++i) { |
|
97 var u = new URLSearchParams(checks[i].input); |
|
98 |
|
99 var count = 0; |
|
100 for (var key in checks[i].data) { |
|
101 ++count; |
|
102 ok(u.has(key), "key " + key + " found"); |
|
103 |
|
104 var all = u.getAll(key); |
|
105 is(all.length, checks[i].data[key].length, "same number of elements"); |
|
106 |
|
107 for (var k = 0; k < all.length; ++k) { |
|
108 is(all[k], checks[i].data[key][k], "value matches"); |
|
109 } |
|
110 } |
|
111 } |
|
112 |
|
113 runTest(); |
|
114 } |
|
115 |
|
116 function testURL() { |
|
117 var url = new URL('http://www.example.net?a=b&c=d'); |
|
118 ok(url.searchParams, "URL searchParams exists!"); |
|
119 ok(url.searchParams.has('a'), "URL.searchParams.has('a')"); |
|
120 is(url.searchParams.get('a'), 'b', "URL.searchParams.get('a')"); |
|
121 ok(url.searchParams.has('c'), "URL.searchParams.has('c')"); |
|
122 is(url.searchParams.get('c'), 'd', "URL.searchParams.get('c')"); |
|
123 |
|
124 url.searchParams.set('e', 'f'); |
|
125 ok(url.href.indexOf('e=f') != 1, 'URL right'); |
|
126 |
|
127 var u = new URLSearchParams(); |
|
128 u.append('foo', 'bar'); |
|
129 url.searchParams = u; |
|
130 is(url.searchParams, u, "URL.searchParams is the same object"); |
|
131 is(url.searchParams.get('foo'), 'bar', "URL.searchParams.get('foo')"); |
|
132 is(url.href, 'http://www.example.net/?foo=bar', 'URL right'); |
|
133 |
|
134 try { |
|
135 url.searchParams = null; |
|
136 ok(false, "URLSearchParams is not nullable"); |
|
137 } catch(e) { |
|
138 ok(true, "URLSearchParams is not nullable"); |
|
139 } |
|
140 |
|
141 var url2 = new URL('http://www.example.net?e=f'); |
|
142 url.searchParams = url2.searchParams; |
|
143 is(url.searchParams, url2.searchParams, "URL.searchParams is not the same object"); |
|
144 is(url.searchParams.get('e'), 'f', "URL.searchParams.get('e')"); |
|
145 |
|
146 url.href = "http://www.example.net?bar=foo"; |
|
147 is(url.searchParams.get('bar'), 'foo', "URL.searchParams.get('bar')"); |
|
148 |
|
149 runTest(); |
|
150 } |
|
151 |
|
152 function testElement(e) { |
|
153 ok(e, 'element exists'); |
|
154 ok(e.searchParams, "e.searchParams exists!"); |
|
155 ok(e.searchParams.has('a'), "e.searchParams.has('a')"); |
|
156 is(e.searchParams.get('a'), 'b', "e.searchParams.get('a')"); |
|
157 ok(e.searchParams.has('c'), "e.searchParams.has('c')"); |
|
158 is(e.searchParams.get('c'), 'd', "e.searchParams.get('c')"); |
|
159 |
|
160 e.searchParams.set('e', 'f'); |
|
161 ok(e.href.indexOf('e=f') != 1, 'e is right'); |
|
162 |
|
163 var u = new URLSearchParams(); |
|
164 u.append('foo', 'bar'); |
|
165 e.searchParams = u; |
|
166 is(e.searchParams, u, "e.searchParams is the same object"); |
|
167 is(e.searchParams.get('foo'), 'bar', "e.searchParams.get('foo')"); |
|
168 is(e.href, 'http://www.example.net/?foo=bar', 'e is right'); |
|
169 |
|
170 try { |
|
171 e.searchParams = null; |
|
172 ok(false, "URLSearchParams is not nullable"); |
|
173 } catch(e) { |
|
174 ok(true, "URLSearchParams is not nullable"); |
|
175 } |
|
176 |
|
177 var url2 = new URL('http://www.example.net?e=f'); |
|
178 e.searchParams = url2.searchParams; |
|
179 is(e.searchParams, url2.searchParams, "e.searchParams is not the same object"); |
|
180 is(e.searchParams.get('e'), 'f', "e.searchParams.get('e')"); |
|
181 |
|
182 e.href = "http://www.example.net?bar=foo"; |
|
183 is(e.searchParams.get('bar'), 'foo', "e.searchParams.get('bar')"); |
|
184 |
|
185 e.setAttribute('href', "http://www.example.net?bar2=foo2"); |
|
186 is(e.searchParams.get('bar2'), 'foo2', "e.searchParams.get('bar2')"); |
|
187 |
|
188 runTest(); |
|
189 } |
|
190 |
|
191 function testEncoding() { |
|
192 var encoding = [ [ '1', '1' ], |
|
193 [ 'a b', 'a+b' ], |
|
194 [ '<>', '%3C%3E' ], |
|
195 [ '\u0541', '%D5%81'] ]; |
|
196 |
|
197 for (var i = 0; i < encoding.length; ++i) { |
|
198 var a = new URLSearchParams(); |
|
199 a.set('a', encoding[i][0]); |
|
200 |
|
201 var url = new URL('http://www.example.net'); |
|
202 url.searchParams = a; |
|
203 is(url.href, 'http://www.example.net/?a=' + encoding[i][1]); |
|
204 |
|
205 var url2 = new URL(url.href); |
|
206 is(url2.searchParams.get('a'), encoding[i][0], 'a is still there'); |
|
207 } |
|
208 |
|
209 runTest(); |
|
210 } |
|
211 |
|
212 function testMultiURL() { |
|
213 var a = new URL('http://www.example.net?a=b&c=d'); |
|
214 var b = new URL('http://www.example.net?e=f'); |
|
215 var c = document.createElement('a'); |
|
216 var d = document.createElement('area'); |
|
217 ok(a.searchParams.has('a'), "a.searchParams.has('a')"); |
|
218 ok(a.searchParams.has('c'), "a.searchParams.has('c')"); |
|
219 ok(b.searchParams.has('e'), "b.searchParams.has('e')"); |
|
220 ok(c.searchParams, "c.searchParams"); |
|
221 ok(d.searchParams, "d.searchParams"); |
|
222 |
|
223 var u = new URLSearchParams(); |
|
224 a.searchParams = b.searchParams = c.searchParams = d.searchParams = u; |
|
225 is(a.searchParams, u, "a.searchParams === u"); |
|
226 is(b.searchParams, u, "b.searchParams === u"); |
|
227 is(c.searchParams, u, "c.searchParams === u"); |
|
228 is(d.searchParams, u, "d.searchParams === u"); |
|
229 ok(!a.searchParams.has('a'), "!a.searchParams.has('a')"); |
|
230 ok(!a.searchParams.has('c'), "!a.searchParams.has('c')"); |
|
231 ok(!b.searchParams.has('e'), "!b.searchParams.has('e')"); |
|
232 |
|
233 u.append('foo', 'bar'); |
|
234 is(a.searchParams.get('foo'), 'bar', "a has foo=bar"); |
|
235 is(b.searchParams.get('foo'), 'bar', "b has foo=bar"); |
|
236 is(c.searchParams.get('foo'), 'bar', "c has foo=bar"); |
|
237 is(d.searchParams.get('foo'), 'bar', "d has foo=bar"); |
|
238 is(a + "", b + "", "stringify a == b"); |
|
239 is(c.searchParams + "", b.searchParams + "", "stringify c.searchParams == b.searchParams"); |
|
240 is(d.searchParams + "", b.searchParams + "", "stringify d.searchParams == b.searchParams"); |
|
241 |
|
242 a.search = "?bar=foo"; |
|
243 is(a.searchParams.get('bar'), 'foo', "a has bar=foo"); |
|
244 is(b.searchParams.get('bar'), 'foo', "b has bar=foo"); |
|
245 is(c.searchParams.get('bar'), 'foo', "c has bar=foo"); |
|
246 is(d.searchParams.get('bar'), 'foo', "d has bar=foo"); |
|
247 |
|
248 runTest(); |
|
249 } |
|
250 |
|
251 var tests = [ |
|
252 testSimpleURLSearchParams, |
|
253 testCopyURLSearchParams, |
|
254 testParserURLSearchParams, |
|
255 testURL, |
|
256 function() { testElement(document.getElementById('anchor')) }, |
|
257 function() { testElement(document.getElementById('area')) }, |
|
258 testEncoding, |
|
259 testMultiURL |
|
260 ]; |
|
261 |
|
262 function runTest() { |
|
263 if (!tests.length) { |
|
264 SimpleTest.finish(); |
|
265 return; |
|
266 } |
|
267 |
|
268 var test = tests.shift(); |
|
269 test(); |
|
270 } |
|
271 |
|
272 SimpleTest.waitForExplicitFinish(); |
|
273 runTest(); |
|
274 |
|
275 </script> |
|
276 </body> |
|
277 </html> |