From fbcab109dea4426bdd2ec29aa61e3beeeb04d27e Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Wed, 6 Jan 2021 09:20:00 +0100 Subject: [PATCH] =?UTF-8?q?url:=20move=C2=A0`URLSearchParams`=20method?= =?UTF-8?q?=C2=A0definitions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/36799 Reviewed-By: Antoine du Hamel Reviewed-By: Tiancheng "Timothy" Gu --- lib/internal/url.js | 480 ++++++++++++++++++++++---------------------- 1 file changed, 240 insertions(+), 240 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index e48811a9c3ca61..0584fd83983c62 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -259,6 +259,246 @@ class URLSearchParams { } } +defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { + append(name, value) { + if (!this || !this[searchParams] || this[searchParams][searchParams]) { + throw new ERR_INVALID_THIS('URLSearchParams'); + } + if (arguments.length < 2) { + throw new ERR_MISSING_ARGS('name', 'value'); + } + + name = toUSVString(name); + value = toUSVString(value); + ArrayPrototypePush(this[searchParams], name, value); + update(this[context], this); + }, + + delete(name) { + if (!this || !this[searchParams] || this[searchParams][searchParams]) { + throw new ERR_INVALID_THIS('URLSearchParams'); + } + if (arguments.length < 1) { + throw new ERR_MISSING_ARGS('name'); + } + + const list = this[searchParams]; + name = toUSVString(name); + for (let i = 0; i < list.length;) { + const cur = list[i]; + if (cur === name) { + list.splice(i, 2); + } else { + i += 2; + } + } + update(this[context], this); + }, + + get(name) { + if (!this || !this[searchParams] || this[searchParams][searchParams]) { + throw new ERR_INVALID_THIS('URLSearchParams'); + } + if (arguments.length < 1) { + throw new ERR_MISSING_ARGS('name'); + } + + const list = this[searchParams]; + name = toUSVString(name); + for (let i = 0; i < list.length; i += 2) { + if (list[i] === name) { + return list[i + 1]; + } + } + return null; + }, + + getAll(name) { + if (!this || !this[searchParams] || this[searchParams][searchParams]) { + throw new ERR_INVALID_THIS('URLSearchParams'); + } + if (arguments.length < 1) { + throw new ERR_MISSING_ARGS('name'); + } + + const list = this[searchParams]; + const values = []; + name = toUSVString(name); + for (let i = 0; i < list.length; i += 2) { + if (list[i] === name) { + values.push(list[i + 1]); + } + } + return values; + }, + + has(name) { + if (!this || !this[searchParams] || this[searchParams][searchParams]) { + throw new ERR_INVALID_THIS('URLSearchParams'); + } + if (arguments.length < 1) { + throw new ERR_MISSING_ARGS('name'); + } + + const list = this[searchParams]; + name = toUSVString(name); + for (let i = 0; i < list.length; i += 2) { + if (list[i] === name) { + return true; + } + } + return false; + }, + + set(name, value) { + if (!this || !this[searchParams] || this[searchParams][searchParams]) { + throw new ERR_INVALID_THIS('URLSearchParams'); + } + if (arguments.length < 2) { + throw new ERR_MISSING_ARGS('name', 'value'); + } + + const list = this[searchParams]; + name = toUSVString(name); + value = toUSVString(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; + for (let i = 0; i < list.length;) { + const cur = list[i]; + if (cur === name) { + if (!found) { + list[i + 1] = value; + found = true; + i += 2; + } else { + list.splice(i, 2); + } + } else { + i += 2; + } + } + + // Otherwise, append a new name-value pair whose name is `name` and value + // is `value`, to `list`. + if (!found) { + ArrayPrototypePush(list, name, value); + } + + update(this[context], this); + }, + + sort() { + const a = this[searchParams]; + const len = a.length; + + if (len <= 2) { + // Nothing needs to be done. + } else if (len < 100) { + // 100 is found through testing. + // Simple stable in-place insertion sort + // Derived from v8/src/js/array.js + for (let i = 2; i < len; i += 2) { + const curKey = a[i]; + const curVal = a[i + 1]; + let j; + for (j = i - 2; j >= 0; j -= 2) { + if (a[j] > curKey) { + a[j + 2] = a[j]; + a[j + 3] = a[j + 1]; + } else { + break; + } + } + a[j + 2] = curKey; + a[j + 3] = curVal; + } + } else { + // Bottom-up iterative stable merge sort + const lBuffer = new Array(len); + const rBuffer = new Array(len); + for (let step = 2; step < len; step *= 2) { + for (let start = 0; start < len - 2; start += 2 * step) { + const mid = start + step; + let end = mid + step; + end = end < len ? end : len; + if (mid > end) + continue; + merge(a, start, mid, end, lBuffer, rBuffer); + } + } + } + + update(this[context], this); + }, + + // https://heycam.github.io/webidl/#es-iterators + // Define entries here rather than [Symbol.iterator] as the function name + // must be set to `entries`. + entries() { + if (!this || !this[searchParams] || this[searchParams][searchParams]) { + throw new ERR_INVALID_THIS('URLSearchParams'); + } + + return createSearchParamsIterator(this, 'key+value'); + }, + + forEach(callback, thisArg = undefined) { + if (!this || !this[searchParams] || this[searchParams][searchParams]) { + throw new ERR_INVALID_THIS('URLSearchParams'); + } + validateCallback(callback); + + let list = this[searchParams]; + + let i = 0; + while (i < list.length) { + const key = list[i]; + const value = list[i + 1]; + callback.call(thisArg, value, key, this); + // In case the URL object's `search` is updated + list = this[searchParams]; + i += 2; + } + }, + + // https://heycam.github.io/webidl/#es-iterable + keys() { + if (!this || !this[searchParams] || this[searchParams][searchParams]) { + throw new ERR_INVALID_THIS('URLSearchParams'); + } + + return createSearchParamsIterator(this, 'key'); + }, + + values() { + if (!this || !this[searchParams] || this[searchParams][searchParams]) { + throw new ERR_INVALID_THIS('URLSearchParams'); + } + + return createSearchParamsIterator(this, 'value'); + }, + + // https://heycam.github.io/webidl/#es-stringifier + // https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior + toString() { + if (!this || !this[searchParams] || this[searchParams][searchParams]) { + throw new ERR_INVALID_THIS('URLSearchParams'); + } + + return serializeParams(this[searchParams]); + } +}); + +// https://heycam.github.io/webidl/#es-iterable-entries +ObjectDefineProperty(URLSearchParams.prototype, SymbolIterator, { + writable: true, + configurable: true, + value: URLSearchParams.prototype.entries +}); + function onParseComplete(flags, protocol, username, password, host, port, path, query, fragment) { const ctx = this[context]; @@ -944,246 +1184,6 @@ function merge(out, start, mid, end, lBuffer, rBuffer) { out[o++] = rBuffer[r++]; } -defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { - append(name, value) { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new ERR_INVALID_THIS('URLSearchParams'); - } - if (arguments.length < 2) { - throw new ERR_MISSING_ARGS('name', 'value'); - } - - name = toUSVString(name); - value = toUSVString(value); - ArrayPrototypePush(this[searchParams], name, value); - update(this[context], this); - }, - - delete(name) { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new ERR_INVALID_THIS('URLSearchParams'); - } - if (arguments.length < 1) { - throw new ERR_MISSING_ARGS('name'); - } - - const list = this[searchParams]; - name = toUSVString(name); - for (let i = 0; i < list.length;) { - const cur = list[i]; - if (cur === name) { - list.splice(i, 2); - } else { - i += 2; - } - } - update(this[context], this); - }, - - get(name) { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new ERR_INVALID_THIS('URLSearchParams'); - } - if (arguments.length < 1) { - throw new ERR_MISSING_ARGS('name'); - } - - const list = this[searchParams]; - name = toUSVString(name); - for (let i = 0; i < list.length; i += 2) { - if (list[i] === name) { - return list[i + 1]; - } - } - return null; - }, - - getAll(name) { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new ERR_INVALID_THIS('URLSearchParams'); - } - if (arguments.length < 1) { - throw new ERR_MISSING_ARGS('name'); - } - - const list = this[searchParams]; - const values = []; - name = toUSVString(name); - for (let i = 0; i < list.length; i += 2) { - if (list[i] === name) { - values.push(list[i + 1]); - } - } - return values; - }, - - has(name) { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new ERR_INVALID_THIS('URLSearchParams'); - } - if (arguments.length < 1) { - throw new ERR_MISSING_ARGS('name'); - } - - const list = this[searchParams]; - name = toUSVString(name); - for (let i = 0; i < list.length; i += 2) { - if (list[i] === name) { - return true; - } - } - return false; - }, - - set(name, value) { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new ERR_INVALID_THIS('URLSearchParams'); - } - if (arguments.length < 2) { - throw new ERR_MISSING_ARGS('name', 'value'); - } - - const list = this[searchParams]; - name = toUSVString(name); - value = toUSVString(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; - for (let i = 0; i < list.length;) { - const cur = list[i]; - if (cur === name) { - if (!found) { - list[i + 1] = value; - found = true; - i += 2; - } else { - list.splice(i, 2); - } - } else { - i += 2; - } - } - - // Otherwise, append a new name-value pair whose name is `name` and value - // is `value`, to `list`. - if (!found) { - ArrayPrototypePush(list, name, value); - } - - update(this[context], this); - }, - - sort() { - const a = this[searchParams]; - const len = a.length; - - if (len <= 2) { - // Nothing needs to be done. - } else if (len < 100) { - // 100 is found through testing. - // Simple stable in-place insertion sort - // Derived from v8/src/js/array.js - for (let i = 2; i < len; i += 2) { - const curKey = a[i]; - const curVal = a[i + 1]; - let j; - for (j = i - 2; j >= 0; j -= 2) { - if (a[j] > curKey) { - a[j + 2] = a[j]; - a[j + 3] = a[j + 1]; - } else { - break; - } - } - a[j + 2] = curKey; - a[j + 3] = curVal; - } - } else { - // Bottom-up iterative stable merge sort - const lBuffer = new Array(len); - const rBuffer = new Array(len); - for (let step = 2; step < len; step *= 2) { - for (let start = 0; start < len - 2; start += 2 * step) { - const mid = start + step; - let end = mid + step; - end = end < len ? end : len; - if (mid > end) - continue; - merge(a, start, mid, end, lBuffer, rBuffer); - } - } - } - - update(this[context], this); - }, - - // https://heycam.github.io/webidl/#es-iterators - // Define entries here rather than [Symbol.iterator] as the function name - // must be set to `entries`. - entries() { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new ERR_INVALID_THIS('URLSearchParams'); - } - - return createSearchParamsIterator(this, 'key+value'); - }, - - forEach(callback, thisArg = undefined) { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new ERR_INVALID_THIS('URLSearchParams'); - } - validateCallback(callback); - - let list = this[searchParams]; - - let i = 0; - while (i < list.length) { - const key = list[i]; - const value = list[i + 1]; - callback.call(thisArg, value, key, this); - // In case the URL object's `search` is updated - list = this[searchParams]; - i += 2; - } - }, - - // https://heycam.github.io/webidl/#es-iterable - keys() { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new ERR_INVALID_THIS('URLSearchParams'); - } - - return createSearchParamsIterator(this, 'key'); - }, - - values() { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new ERR_INVALID_THIS('URLSearchParams'); - } - - return createSearchParamsIterator(this, 'value'); - }, - - // https://heycam.github.io/webidl/#es-stringifier - // https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior - toString() { - if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new ERR_INVALID_THIS('URLSearchParams'); - } - - return serializeParams(this[searchParams]); - } -}); - -// https://heycam.github.io/webidl/#es-iterable-entries -ObjectDefineProperty(URLSearchParams.prototype, SymbolIterator, { - writable: true, - configurable: true, - value: URLSearchParams.prototype.entries -}); - // https://heycam.github.io/webidl/#dfn-default-iterator-object function createSearchParamsIterator(target, kind) { const iterator = ObjectCreate(URLSearchParamsIteratorPrototype);