diff options
author | 迷渡 <justjavac@gmail.com> | 2018-12-19 09:14:59 +0800 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2018-12-18 20:14:59 -0500 |
commit | 3dbd18af0950400eed7063bef7cb1f7ff27c4a01 (patch) | |
tree | 8112967e9bf1aa11aa84799312a4d93f1b45b059 /js/url_search_params.ts | |
parent | 528eb2adb38b44def28aeab006cd0ba1e2654239 (diff) |
Fix URLSearchParams set() and constructor() (#1368)
Diffstat (limited to 'js/url_search_params.ts')
-rw-r--r-- | js/url_search_params.ts | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/js/url_search_params.ts b/js/url_search_params.ts index 228c36956..06369ae29 100644 --- a/js/url_search_params.ts +++ b/js/url_search_params.ts @@ -24,6 +24,12 @@ export class URLSearchParams { } else if (Array.isArray(init)) { // Overload: sequence<sequence<USVString>> for (const tuple of init) { + // If pair does not contain exactly two items, then throw a TypeError. + if (tuple.length !== 2) { + const errMsg = + "Each query pair must be an iterable [name, value] tuple"; + throw new TypeError(errMsg); + } this.append(tuple[0], tuple[1]); } } else if (Object(init) === init) { @@ -106,8 +112,29 @@ export class URLSearchParams { * searchParams.set('name', 'value'); */ set(name: string, value: string): void { - this.delete(name); - this.append(name, value); + // If there are any name-value pairs whose name is name, in list, + // set the value of the first such name-value pair to value + // and remove the others. + let found = false; + let i = 0; + while (i < this.params.length) { + if (this.params[i][0] === name) { + if (!found) { + this.params[i][1] = value; + found = true; + } else { + this.params.splice(i, 1); + continue; + } + } + i++; + } + + // Otherwise, append a new name-value pair whose name is name + // and value is value, to list. + if (!found) { + this.append(name, value); + } } /** Sort all key/value pairs contained in this object in place and |