-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
url: extend URLSearchParams constructor #11060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
2300a51
e8a3c95
f1cc4f3
1fcac02
1bcb813
2813063
4fcb60d
25a60ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Fixes: #10635 Refs: whatwg/url#175 Refs: web-platform-tests/wpt#4523
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -649,10 +649,49 @@ function getObjectFromParams(array) { | |
|
|
||
| class URLSearchParams { | ||
| constructor(init = '') { | ||
| if (init instanceof URLSearchParams) { | ||
| const childParams = init[searchParams]; | ||
| this[searchParams] = childParams.slice(); | ||
| if (init === null || init === undefined) { | ||
| // record<USVString, USVString> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment can be removed I believe.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
| this[searchParams] = []; | ||
| } else if (typeof init === 'object') { | ||
| const method = init[Symbol.iterator]; | ||
| if (method === this[Symbol.iterator]) { | ||
| // While the spec does not have this branch, we can use it as a | ||
| // shortcut to avoid having to go through the costly generic iterator. | ||
| const childParams = init[searchParams]; | ||
| this[searchParams] = childParams.slice(); | ||
| } else if (method !== null && method !== undefined) { | ||
| if (typeof method !== 'function') { | ||
| throw new TypeError('Query pairs must be iterable'); | ||
| } | ||
|
|
||
| // sequence<sequence<USVString>> | ||
| // Note: per spec we have to first exhaust the lists then process them | ||
| const pairs = []; | ||
| for (const pair of init) { | ||
| if (typeof pair !== 'object' || | ||
| typeof pair[Symbol.iterator] !== 'function') { | ||
| throw new TypeError('Each query pair must be iterable'); | ||
| } | ||
| pairs.push(Array.from(pair)); | ||
| } | ||
|
|
||
| this[searchParams] = []; | ||
| for (const pair of pairs) { | ||
| if (pair.length !== 2) { | ||
| throw new TypeError('Each query pair must be a name/value tuple'); | ||
| } | ||
| this[searchParams].push(String(pair[0]), String(pair[1])); | ||
| } | ||
| } else { | ||
| // record<USVString, USVString> | ||
| this[searchParams] = []; | ||
| for (const key of Object.keys(init)) { | ||
| const value = String(init[key]); | ||
| this[searchParams].push(key, value); | ||
| } | ||
| } | ||
| } else { | ||
| // USVString | ||
| init = String(init); | ||
| if (init[0] === '?') init = init.slice(1); | ||
| initSearchParams(this, init); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,23 +16,24 @@ assert.strictEqual(params + '', 'a=b'); | |
| params = new URLSearchParams(params); | ||
| assert.strictEqual(params + '', 'a=b'); | ||
|
|
||
| // URLSearchParams constructor, empty. | ||
| // URLSearchParams constructor, no arguments | ||
| params = new URLSearchParams(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not yet, nor do we have a |
||
| assert.strictEqual(params.toString(), ''); | ||
|
|
||
| assert.throws(() => URLSearchParams(), TypeError, | ||
| 'Calling \'URLSearchParams\' without \'new\' should throw.'); | ||
| // assert.throws(() => new URLSearchParams(DOMException.prototype), TypeError); | ||
| assert.throws(() => { | ||
| new URLSearchParams({ | ||
| toString() { throw new TypeError('Illegal invocation'); } | ||
| }); | ||
| }, TypeError); | ||
|
|
||
| // URLSearchParams constructor, empty string as argument | ||
| params = new URLSearchParams(''); | ||
| assert.notStrictEqual(params, null, 'constructor returned non-null value.'); | ||
| // eslint-disable-next-line no-restricted-properties | ||
| assert.notEqual(params, null, 'constructor returned non-null value.'); | ||
| // eslint-disable-next-line no-proto | ||
| assert.strictEqual(params.__proto__, URLSearchParams.prototype, | ||
| 'expected URLSearchParams.prototype as prototype.'); | ||
|
|
||
| // URLSearchParams constructor, {} as argument | ||
| params = new URLSearchParams({}); | ||
| // assert.strictEqual(params + '', '%5Bobject+Object%5D='); | ||
| assert.strictEqual(params + '', '%5Bobject%20Object%5D='); | ||
| assert.strictEqual(params + '', ''); | ||
|
|
||
| // URLSearchParams constructor, string. | ||
| params = new URLSearchParams('a=b'); | ||
|
|
@@ -128,3 +129,60 @@ params = new URLSearchParams('a=b%f0%9f%92%a9c'); | |
| assert.strictEqual(params.get('a'), 'b\uD83D\uDCA9c'); | ||
| params = new URLSearchParams('a%f0%9f%92%a9b=c'); | ||
| assert.strictEqual(params.get('a\uD83D\uDCA9b'), 'c'); | ||
|
|
||
| // Constructor with sequence of sequences of strings | ||
| params = new URLSearchParams([]); | ||
| // eslint-disable-next-line no-restricted-properties | ||
| assert.notEqual(params, null, 'constructor returned non-null value.'); | ||
| params = new URLSearchParams([['a', 'b'], ['c', 'd']]); | ||
| assert.strictEqual(params.get('a'), 'b'); | ||
| assert.strictEqual(params.get('c'), 'd'); | ||
| assert.throws(() => new URLSearchParams([[1]]), | ||
| /^TypeError: Each query pair must be a name\/value tuple$/); | ||
| assert.throws(() => new URLSearchParams([[1, 2, 3]]), | ||
| /^TypeError: Each query pair must be a name\/value tuple$/); | ||
|
|
||
| [ | ||
| // Further confirmation needed | ||
| // https://github.com/w3c/web-platform-tests/pull/4523#discussion_r98337513 | ||
| // { | ||
| // input: {'+': '%C2'}, | ||
| // output: [[' ', '\uFFFD']], | ||
| // name: 'object with +' | ||
| // }, | ||
| { | ||
| input: {c: 'x', a: '?'}, | ||
| output: [['c', 'x'], ['a', '?']], | ||
| name: 'object with two keys' | ||
| }, | ||
| { | ||
| input: [['c', 'x'], ['a', '?']], | ||
| output: [['c', 'x'], ['a', '?']], | ||
| name: 'array with two keys' | ||
| } | ||
| ].forEach((val) => { | ||
| const params = new URLSearchParams(val.input); | ||
| let i = 0; | ||
| for (const param of params) { | ||
| assert.deepStrictEqual(param, val.output[i], | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. Fixed also. |
||
| `Construct with ${val.name}`); | ||
| i++; | ||
| } | ||
| }); | ||
|
|
||
| // Custom [Symbol.iterator] | ||
| params = new URLSearchParams(); | ||
| params[Symbol.iterator] = function *() { | ||
| yield ['a', 'b']; | ||
| }; | ||
| const params2 = new URLSearchParams(params); | ||
| assert.strictEqual(params2.get('a'), 'b'); | ||
|
|
||
| assert.throws(() => new URLSearchParams({ [Symbol.iterator]: 42 }), | ||
| /^TypeError: Query pairs must be iterable$/); | ||
| assert.throws(() => new URLSearchParams([{}]), | ||
| /^TypeError: Each query pair must be iterable$/); | ||
| assert.throws(() => new URLSearchParams(['a']), | ||
| /^TypeError: Each query pair must be iterable$/); | ||
| assert.throws(() => new URLSearchParams([{ [Symbol.iterator]: 42 }]), | ||
| /^TypeError: Each query pair must be iterable$/); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we have
init = ''as default theninitcan't beundefined?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The user can do
new URLSearchParams(undefined). The default is there to echo the= ""in the spec, though now I agree it's a bit pointless.