• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1function bURL(url, base) {
2  return new URL(url, base || "about:blank")
3}
4
5function runURLSearchParamTests() {
6  test(function() {
7    var url = bURL('http://example.org/?a=b')
8    assert_true("searchParams" in url)
9    var searchParams = url.searchParams
10    assert_true(url.searchParams === searchParams, 'Object identity should hold.')
11  }, 'URL.searchParams getter')
12
13  test(function() {
14    var url = bURL('http://example.org/?a=b')
15    assert_true("searchParams" in url)
16    var searchParams = url.searchParams
17    assert_equals(searchParams.toString(), 'a=b')
18
19    searchParams.set('a', 'b')
20    assert_equals(url.searchParams.toString(), 'a=b')
21    assert_equals(url.search, '?a=b')
22    url.search = ''
23    assert_equals(url.searchParams.toString(), '')
24    assert_equals(url.search, '')
25    assert_equals(searchParams.toString(), '')
26  }, 'URL.searchParams updating, clearing')
27
28  test(function() {
29    'use strict'
30    var urlString = 'http://example.org'
31    var url = bURL(urlString)
32    assert_throws(TypeError(), function() { url.searchParams = new URLSearchParams(urlString) })
33  }, 'URL.searchParams setter, invalid values')
34
35  test(function() {
36    var url = bURL('http://example.org/file?a=b&c=d')
37    assert_true("searchParams" in url)
38    var searchParams = url.searchParams
39    assert_equals(url.search, '?a=b&c=d')
40    assert_equals(searchParams.toString(), 'a=b&c=d')
41
42    // Test that setting 'search' propagates to the URL object's query object.
43    url.search = 'e=f&g=h'
44    assert_equals(url.search, '?e=f&g=h')
45    assert_equals(searchParams.toString(), 'e=f&g=h')
46
47    // ..and same but with a leading '?'.
48    url.search = '?e=f&g=h'
49    assert_equals(url.search, '?e=f&g=h')
50    assert_equals(searchParams.toString(), 'e=f&g=h')
51
52    // And in the other direction, altering searchParams propagates
53    // back to 'search'.
54    searchParams.append('i', ' j ')
55    assert_equals(url.search, '?e=f&g=h&i=+j+')
56    assert_equals(url.searchParams.toString(), 'e=f&g=h&i=+j+')
57    assert_equals(searchParams.get('i'), ' j ')
58
59    searchParams.set('e', 'updated')
60    assert_equals(url.search, '?e=updated&g=h&i=+j+')
61    assert_equals(searchParams.get('e'), 'updated')
62
63    var url2 = bURL('http://example.org/file??a=b&c=d')
64    assert_equals(url2.search, '??a=b&c=d')
65    assert_equals(url2.searchParams.toString(), '%3Fa=b&c=d')
66
67    url2.href = 'http://example.org/file??a=b'
68    assert_equals(url2.search, '??a=b')
69    assert_equals(url2.searchParams.toString(), '%3Fa=b')
70  }, 'URL.searchParams and URL.search setters, update propagation')
71}
72runURLSearchParamTests()
73