Browse Source

url: fix handling of ? in URLSearchParams creation

PR-URL: https://github.com/nodejs/node/pull/11372
Fixes: https://github.com/nodejs/node/issues/11093
Ref: https://github.com/whatwg/url/issues/248
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
v6
Timothy Gu 8 years ago
parent
commit
fa41dd117d
  1. 9
      lib/internal/url.js
  2. 20
      test/parallel/test-whatwg-url-constructor.js

9
lib/internal/url.js

@ -95,12 +95,11 @@ function onParseComplete(flags, protocol, username, password,
ctx.query = query; ctx.query = query;
ctx.fragment = fragment; ctx.fragment = fragment;
ctx.host = host; ctx.host = host;
if (this[searchParams]) { // invoked from href setter if (!this[searchParams]) { // invoked from URL constructor
initSearchParams(this[searchParams], query); this[searchParams] = new URLSearchParams();
} else {
this[searchParams] = new URLSearchParams(query);
}
this[searchParams][context] = this; this[searchParams][context] = this;
}
initSearchParams(this[searchParams], query);
} }
// Reused by URL constructor and URL#href setter. // Reused by URL constructor and URL#href setter.

20
test/parallel/test-whatwg-url-constructor.js

@ -119,22 +119,22 @@ function runURLSearchParamTests() {
// And in the other direction, altering searchParams propagates // And in the other direction, altering searchParams propagates
// back to 'search'. // back to 'search'.
// searchParams.append('i', ' j ') searchParams.append('i', ' j ')
// assert_equals(url.search, '?e=f&g=h&i=+j+') // assert_equals(url.search, '?e=f&g=h&i=+j+')
// assert_equals(url.searchParams.toString(), 'e=f&g=h&i=+j+') // assert_equals(url.searchParams.toString(), 'e=f&g=h&i=+j+')
// assert_equals(searchParams.get('i'), ' j ') assert_equals(searchParams.get('i'), ' j ')
// searchParams.set('e', 'updated') searchParams.set('e', 'updated')
// assert_equals(url.search, '?e=updated&g=h&i=+j+') // assert_equals(url.search, '?e=updated&g=h&i=+j+')
// assert_equals(searchParams.get('e'), 'updated') assert_equals(searchParams.get('e'), 'updated')
// var url2 = bURL('http://example.org/file??a=b&c=d') var url2 = bURL('http://example.org/file??a=b&c=d')
// assert_equals(url2.search, '??a=b&c=d') assert_equals(url2.search, '??a=b&c=d')
// assert_equals(url2.searchParams.toString(), '%3Fa=b&c=d') assert_equals(url2.searchParams.toString(), '%3Fa=b&c=d')
// url2.href = 'http://example.org/file??a=b' url2.href = 'http://example.org/file??a=b'
// assert_equals(url2.search, '??a=b') assert_equals(url2.search, '??a=b')
// assert_equals(url2.searchParams.toString(), '%3Fa=b') assert_equals(url2.searchParams.toString(), '%3Fa=b')
}, 'URL.searchParams and URL.search setters, update propagation') }, 'URL.searchParams and URL.search setters, update propagation')
} }
runURLSearchParamTests() runURLSearchParamTests()

Loading…
Cancel
Save