1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/base/test/test_urlSearchParams.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,277 @@ 1.4 + 1.5 +<!DOCTYPE HTML> 1.6 +<html> 1.7 +<!-- 1.8 +https://bugzilla.mozilla.org/show_bug.cgi?id=887836 1.9 +--> 1.10 +<head> 1.11 + <meta charset="utf-8"> 1.12 + <title>Test for Bug 887836</title> 1.13 + <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.14 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 1.15 +</head> 1.16 +<body> 1.17 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=887836">Mozilla Bug 887836</a> 1.18 +<p id="display"></p> 1.19 +<div id="content" style="display: none"> 1.20 + <iframe name="x" id="x"></iframe> 1.21 + <iframe name="y" id="y"></iframe> 1.22 +</div> 1.23 +<pre id="test"> 1.24 +</pre> 1.25 +<a href="http://www.example.net?a=b&c=d" id="anchor">foobar</a> 1.26 +<area href="http://www.example.net?a=b&c=d" id="area">foobar</area> 1.27 +<script type="application/javascript"> 1.28 + 1.29 + /** Test for Bug 887836 **/ 1.30 + ok("URLSearchParams" in window, "window.URLSearchParams exists"); 1.31 + 1.32 + function testSimpleURLSearchParams() { 1.33 + var u = new URLSearchParams(); 1.34 + ok(u, "URLSearchParams created"); 1.35 + is(u.has('foo'), false, 'URLSearchParams.has(foo)'); 1.36 + is(u.get('foo'), '', 'URLSearchParams.get(foo)'); 1.37 + is(u.getAll('foo').length, 0, 'URLSearchParams.getAll(foo)'); 1.38 + 1.39 + u.append('foo', 'bar'); 1.40 + is(u.has('foo'), true, 'URLSearchParams.has(foo)'); 1.41 + is(u.get('foo'), 'bar', 'URLSearchParams.get(foo)'); 1.42 + is(u.getAll('foo').length, 1, 'URLSearchParams.getAll(foo)'); 1.43 + 1.44 + u.set('foo', 'bar2'); 1.45 + is(u.get('foo'), 'bar2', 'URLSearchParams.get(foo)'); 1.46 + is(u.getAll('foo').length, 1, 'URLSearchParams.getAll(foo)'); 1.47 + 1.48 + is(u + "", "foo=bar2", "stringifier"); 1.49 + 1.50 + u.delete('foo'); 1.51 + 1.52 + runTest(); 1.53 + } 1.54 + 1.55 + function testCopyURLSearchParams() { 1.56 + var u = new URLSearchParams(); 1.57 + ok(u, "URLSearchParams created"); 1.58 + u.append('foo', 'bar'); 1.59 + 1.60 + var uu = new URLSearchParams(u); 1.61 + is(uu.get('foo'), 'bar', 'uu.get()'); 1.62 + 1.63 + u.append('foo', 'bar2'); 1.64 + is(u.getAll('foo').length, 2, "u.getAll()"); 1.65 + is(uu.getAll('foo').length, 1, "uu.getAll()"); 1.66 + 1.67 + runTest(); 1.68 + } 1.69 + 1.70 + function testParserURLSearchParams() { 1.71 + var checks = [ 1.72 + { input: '', data: {} }, 1.73 + { input: 'a', data: { 'a' : [''] } }, 1.74 + { input: 'a=b', data: { 'a' : ['b'] } }, 1.75 + { input: 'a=', data: { 'a' : [''] } }, 1.76 + { input: '=b', data: { '' : ['b'] } }, 1.77 + { input: '&', data: {} }, 1.78 + { input: '&a', data: { 'a' : [''] } }, 1.79 + { input: 'a&', data: { 'a' : [''] } }, 1.80 + { input: 'a&a', data: { 'a' : ['', ''] } }, 1.81 + { input: 'a&b&c', data: { 'a' : [''], 'b' : [''], 'c' : [''] } }, 1.82 + { input: 'a=b&c=d', data: { 'a' : ['b'], 'c' : ['d'] } }, 1.83 + { input: 'a=b&c=d&', data: { 'a' : ['b'], 'c' : ['d'] } }, 1.84 + { input: '&&&a=b&&&&c=d&', data: { 'a' : ['b'], 'c' : ['d'] } }, 1.85 + { input: 'a=a&a=b&a=c', data: { 'a' : ['a', 'b', 'c'] } }, 1.86 + { input: 'a==a', data: { 'a' : ['=a'] } }, 1.87 + { input: 'a=a+b+c+d', data: { 'a' : ['a b c d'] } }, 1.88 + { input: '%=a', data: { '%' : ['a'] } }, 1.89 + { input: '%a=a', data: { '%a' : ['a'] } }, 1.90 + { input: '%a_=a', data: { '%a_' : ['a'] } }, 1.91 + { input: '%61=a', data: { 'a' : ['a'] } }, 1.92 + { input: '%=a', data: { '%' : ['a'] } }, 1.93 + { input: '%a=a', data: { '%a' : ['a'] } }, 1.94 + { input: '%a_=a', data: { '%a_' : ['a'] } }, 1.95 + { input: '%61=a', data: { 'a' : ['a'] } }, 1.96 + { input: '%61+%4d%4D=', data: { 'a MM' : [''] } }, 1.97 + ]; 1.98 + 1.99 + for (var i = 0; i < checks.length; ++i) { 1.100 + var u = new URLSearchParams(checks[i].input); 1.101 + 1.102 + var count = 0; 1.103 + for (var key in checks[i].data) { 1.104 + ++count; 1.105 + ok(u.has(key), "key " + key + " found"); 1.106 + 1.107 + var all = u.getAll(key); 1.108 + is(all.length, checks[i].data[key].length, "same number of elements"); 1.109 + 1.110 + for (var k = 0; k < all.length; ++k) { 1.111 + is(all[k], checks[i].data[key][k], "value matches"); 1.112 + } 1.113 + } 1.114 + } 1.115 + 1.116 + runTest(); 1.117 + } 1.118 + 1.119 + function testURL() { 1.120 + var url = new URL('http://www.example.net?a=b&c=d'); 1.121 + ok(url.searchParams, "URL searchParams exists!"); 1.122 + ok(url.searchParams.has('a'), "URL.searchParams.has('a')"); 1.123 + is(url.searchParams.get('a'), 'b', "URL.searchParams.get('a')"); 1.124 + ok(url.searchParams.has('c'), "URL.searchParams.has('c')"); 1.125 + is(url.searchParams.get('c'), 'd', "URL.searchParams.get('c')"); 1.126 + 1.127 + url.searchParams.set('e', 'f'); 1.128 + ok(url.href.indexOf('e=f') != 1, 'URL right'); 1.129 + 1.130 + var u = new URLSearchParams(); 1.131 + u.append('foo', 'bar'); 1.132 + url.searchParams = u; 1.133 + is(url.searchParams, u, "URL.searchParams is the same object"); 1.134 + is(url.searchParams.get('foo'), 'bar', "URL.searchParams.get('foo')"); 1.135 + is(url.href, 'http://www.example.net/?foo=bar', 'URL right'); 1.136 + 1.137 + try { 1.138 + url.searchParams = null; 1.139 + ok(false, "URLSearchParams is not nullable"); 1.140 + } catch(e) { 1.141 + ok(true, "URLSearchParams is not nullable"); 1.142 + } 1.143 + 1.144 + var url2 = new URL('http://www.example.net?e=f'); 1.145 + url.searchParams = url2.searchParams; 1.146 + is(url.searchParams, url2.searchParams, "URL.searchParams is not the same object"); 1.147 + is(url.searchParams.get('e'), 'f', "URL.searchParams.get('e')"); 1.148 + 1.149 + url.href = "http://www.example.net?bar=foo"; 1.150 + is(url.searchParams.get('bar'), 'foo', "URL.searchParams.get('bar')"); 1.151 + 1.152 + runTest(); 1.153 + } 1.154 + 1.155 + function testElement(e) { 1.156 + ok(e, 'element exists'); 1.157 + ok(e.searchParams, "e.searchParams exists!"); 1.158 + ok(e.searchParams.has('a'), "e.searchParams.has('a')"); 1.159 + is(e.searchParams.get('a'), 'b', "e.searchParams.get('a')"); 1.160 + ok(e.searchParams.has('c'), "e.searchParams.has('c')"); 1.161 + is(e.searchParams.get('c'), 'd', "e.searchParams.get('c')"); 1.162 + 1.163 + e.searchParams.set('e', 'f'); 1.164 + ok(e.href.indexOf('e=f') != 1, 'e is right'); 1.165 + 1.166 + var u = new URLSearchParams(); 1.167 + u.append('foo', 'bar'); 1.168 + e.searchParams = u; 1.169 + is(e.searchParams, u, "e.searchParams is the same object"); 1.170 + is(e.searchParams.get('foo'), 'bar', "e.searchParams.get('foo')"); 1.171 + is(e.href, 'http://www.example.net/?foo=bar', 'e is right'); 1.172 + 1.173 + try { 1.174 + e.searchParams = null; 1.175 + ok(false, "URLSearchParams is not nullable"); 1.176 + } catch(e) { 1.177 + ok(true, "URLSearchParams is not nullable"); 1.178 + } 1.179 + 1.180 + var url2 = new URL('http://www.example.net?e=f'); 1.181 + e.searchParams = url2.searchParams; 1.182 + is(e.searchParams, url2.searchParams, "e.searchParams is not the same object"); 1.183 + is(e.searchParams.get('e'), 'f', "e.searchParams.get('e')"); 1.184 + 1.185 + e.href = "http://www.example.net?bar=foo"; 1.186 + is(e.searchParams.get('bar'), 'foo', "e.searchParams.get('bar')"); 1.187 + 1.188 + e.setAttribute('href', "http://www.example.net?bar2=foo2"); 1.189 + is(e.searchParams.get('bar2'), 'foo2', "e.searchParams.get('bar2')"); 1.190 + 1.191 + runTest(); 1.192 + } 1.193 + 1.194 + function testEncoding() { 1.195 + var encoding = [ [ '1', '1' ], 1.196 + [ 'a b', 'a+b' ], 1.197 + [ '<>', '%3C%3E' ], 1.198 + [ '\u0541', '%D5%81'] ]; 1.199 + 1.200 + for (var i = 0; i < encoding.length; ++i) { 1.201 + var a = new URLSearchParams(); 1.202 + a.set('a', encoding[i][0]); 1.203 + 1.204 + var url = new URL('http://www.example.net'); 1.205 + url.searchParams = a; 1.206 + is(url.href, 'http://www.example.net/?a=' + encoding[i][1]); 1.207 + 1.208 + var url2 = new URL(url.href); 1.209 + is(url2.searchParams.get('a'), encoding[i][0], 'a is still there'); 1.210 + } 1.211 + 1.212 + runTest(); 1.213 + } 1.214 + 1.215 + function testMultiURL() { 1.216 + var a = new URL('http://www.example.net?a=b&c=d'); 1.217 + var b = new URL('http://www.example.net?e=f'); 1.218 + var c = document.createElement('a'); 1.219 + var d = document.createElement('area'); 1.220 + ok(a.searchParams.has('a'), "a.searchParams.has('a')"); 1.221 + ok(a.searchParams.has('c'), "a.searchParams.has('c')"); 1.222 + ok(b.searchParams.has('e'), "b.searchParams.has('e')"); 1.223 + ok(c.searchParams, "c.searchParams"); 1.224 + ok(d.searchParams, "d.searchParams"); 1.225 + 1.226 + var u = new URLSearchParams(); 1.227 + a.searchParams = b.searchParams = c.searchParams = d.searchParams = u; 1.228 + is(a.searchParams, u, "a.searchParams === u"); 1.229 + is(b.searchParams, u, "b.searchParams === u"); 1.230 + is(c.searchParams, u, "c.searchParams === u"); 1.231 + is(d.searchParams, u, "d.searchParams === u"); 1.232 + ok(!a.searchParams.has('a'), "!a.searchParams.has('a')"); 1.233 + ok(!a.searchParams.has('c'), "!a.searchParams.has('c')"); 1.234 + ok(!b.searchParams.has('e'), "!b.searchParams.has('e')"); 1.235 + 1.236 + u.append('foo', 'bar'); 1.237 + is(a.searchParams.get('foo'), 'bar', "a has foo=bar"); 1.238 + is(b.searchParams.get('foo'), 'bar', "b has foo=bar"); 1.239 + is(c.searchParams.get('foo'), 'bar', "c has foo=bar"); 1.240 + is(d.searchParams.get('foo'), 'bar', "d has foo=bar"); 1.241 + is(a + "", b + "", "stringify a == b"); 1.242 + is(c.searchParams + "", b.searchParams + "", "stringify c.searchParams == b.searchParams"); 1.243 + is(d.searchParams + "", b.searchParams + "", "stringify d.searchParams == b.searchParams"); 1.244 + 1.245 + a.search = "?bar=foo"; 1.246 + is(a.searchParams.get('bar'), 'foo', "a has bar=foo"); 1.247 + is(b.searchParams.get('bar'), 'foo', "b has bar=foo"); 1.248 + is(c.searchParams.get('bar'), 'foo', "c has bar=foo"); 1.249 + is(d.searchParams.get('bar'), 'foo', "d has bar=foo"); 1.250 + 1.251 + runTest(); 1.252 + } 1.253 + 1.254 + var tests = [ 1.255 + testSimpleURLSearchParams, 1.256 + testCopyURLSearchParams, 1.257 + testParserURLSearchParams, 1.258 + testURL, 1.259 + function() { testElement(document.getElementById('anchor')) }, 1.260 + function() { testElement(document.getElementById('area')) }, 1.261 + testEncoding, 1.262 + testMultiURL 1.263 + ]; 1.264 + 1.265 + function runTest() { 1.266 + if (!tests.length) { 1.267 + SimpleTest.finish(); 1.268 + return; 1.269 + } 1.270 + 1.271 + var test = tests.shift(); 1.272 + test(); 1.273 + } 1.274 + 1.275 + SimpleTest.waitForExplicitFinish(); 1.276 + runTest(); 1.277 + 1.278 +</script> 1.279 +</body> 1.280 +</html>