diff --git a/packages/core-js/modules/web.url-search-params.js b/packages/core-js/modules/web.url-search-params.js index d739db26b8b5..db9d5d141e71 100644 --- a/packages/core-js/modules/web.url-search-params.js +++ b/packages/core-js/modules/web.url-search-params.js @@ -99,29 +99,6 @@ var serialize = function (it) { return replace(encodeURIComponent(it), find, replacer); }; -var parseSearchParams = function (result, query) { - if (query) { - var attributes = split(query, '&'); - var index = 0; - var attribute, entry; - while (index < attributes.length) { - attribute = attributes[index++]; - if (attribute.length) { - entry = split(attribute, '='); - push(result, { - key: deserialize(shift(entry)), - value: deserialize(join(entry, '=')) - }); - } - } - } -}; - -var updateSearchParams = function (query) { - this.entries.length = 0; - parseSearchParams(this.entries, query); -}; - var validateArgumentsLength = function (passed, required) { if (passed < required) throw TypeError('Not enough arguments'); }; @@ -142,48 +119,86 @@ var URLSearchParamsIterator = createIteratorConstructor(function Iterator(params } return step; }); -// `URLSearchParams` constructor -// https://url.spec.whatwg.org/#interface-urlsearchparams -var URLSearchParamsConstructor = function URLSearchParams(/* init */) { - anInstance(this, URLSearchParamsPrototype); - var init = arguments.length > 0 ? arguments[0] : undefined; - var that = this; - var entries = []; - var iteratorMethod, iterator, next, step, entryIterator, entryNext, first, second, key; - - setInternalState(that, { - type: URL_SEARCH_PARAMS, - entries: entries, - updateURL: function () { /* empty */ }, - updateSearchParams: updateSearchParams - }); +var URLSearchParamsState = function (init) { + this.entries = []; + this.url = null; if (init !== undefined) { - if (isObject(init)) { - iteratorMethod = getIteratorMethod(init); - if (iteratorMethod) { - iterator = getIterator(init, iteratorMethod); - next = iterator.next; - while (!(step = call(next, iterator)).done) { - entryIterator = getIterator(anObject(step.value)); - entryNext = entryIterator.next; - if ( - (first = call(entryNext, entryIterator)).done || - (second = call(entryNext, entryIterator)).done || - !call(entryNext, entryIterator).done - ) throw TypeError('Expected sequence with length 2'); - push(entries, { key: $toString(first.value), value: $toString(second.value) }); + if (isObject(init)) this.parseObject(init); + else this.parseQuery(typeof init == 'string' ? charAt(init, 0) === '?' ? stringSlice(init, 1) : init : $toString(init)); + } +}; + +URLSearchParamsState.prototype = { + type: URL_SEARCH_PARAMS, + bindURL: function (url) { + this.url = url; + }, + parseObject: function (object) { + var iteratorMethod = getIteratorMethod(object); + var iterator, next, step, entryIterator, entryNext, first, second; + + if (iteratorMethod) { + iterator = getIterator(object, iteratorMethod); + next = iterator.next; + while (!(step = call(next, iterator)).done) { + entryIterator = getIterator(anObject(step.value)); + entryNext = entryIterator.next; + if ( + (first = call(entryNext, entryIterator)).done || + (second = call(entryNext, entryIterator)).done || + !call(entryNext, entryIterator).done + ) throw TypeError('Expected sequence with length 2'); + push(this.entries, { key: $toString(first.value), value: $toString(second.value) }); + } + } else for (var key in object) if (hasOwn(object, key)) { + push(this.entries, { key: key, value: $toString(object[key]) }); + } + }, + parseQuery: function (query) { + if (query) { + var attributes = split(query, '&'); + var index = 0; + var attribute, entry; + while (index < attributes.length) { + attribute = attributes[index++]; + if (attribute.length) { + entry = split(attribute, '='); + push(this.entries, { + key: deserialize(shift(entry)), + value: deserialize(join(entry, '=')) + }); } - } else for (key in init) if (hasOwn(init, key)) push(entries, { key: key, value: $toString(init[key]) }); - } else { - parseSearchParams( - entries, - typeof init == 'string' ? charAt(init, 0) === '?' ? stringSlice(init, 1) : init : $toString(init) - ); + } } + }, + toString: function () { + var entries = this.entries; + var result = []; + var index = 0; + var entry; + while (index < entries.length) { + entry = entries[index++]; + push(result, serialize(entry.key) + '=' + serialize(entry.value)); + } return join(result, '&'); + }, + update: function () { + this.entries.length = 0; + this.parseQuery(this.url.query); + }, + updateURL: function () { + if (this.url) this.url.update(); } }; +// `URLSearchParams` constructor +// https://url.spec.whatwg.org/#interface-urlsearchparams +var URLSearchParamsConstructor = function URLSearchParams(/* init */) { + anInstance(this, URLSearchParamsPrototype); + var init = arguments.length > 0 ? arguments[0] : undefined; + setInternalState(this, new URLSearchParamsState(init)); +}; + var URLSearchParamsPrototype = URLSearchParamsConstructor.prototype; redefineAll(URLSearchParamsPrototype, { @@ -310,14 +325,7 @@ redefine(URLSearchParamsPrototype, ITERATOR, URLSearchParamsPrototype.entries, { // `URLSearchParams.prototype.toString` method // https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior redefine(URLSearchParamsPrototype, 'toString', function toString() { - var entries = getInternalParamsState(this).entries; - var result = []; - var index = 0; - var entry; - while (index < entries.length) { - entry = entries[index++]; - push(result, serialize(entry.key) + '=' + serialize(entry.value)); - } return join(result, '&'); + return getInternalParamsState(this).toString(); }, { enumerable: true }); setToStringTag(URLSearchParamsConstructor, URL_SEARCH_PARAMS); diff --git a/packages/core-js/modules/web.url.js b/packages/core-js/modules/web.url.js index 86ca3429f5c0..20dbef3176c0 100644 --- a/packages/core-js/modules/web.url.js +++ b/packages/core-js/modules/web.url.js @@ -750,30 +750,39 @@ var parseURL = function (url, input, stateOverride, base) { } }; +var URLState = function () { + var searchParams = getInternalSearchParamsState(new URLSearchParams()); + this.searchParams = searchParams; + searchParams.bindURL(this); +}; + +URLState.prototype = { + type: 'URL', + update: function () { + this.query = this.searchParams.toString() || null; + } +}; + // `URL` constructor // https://url.spec.whatwg.org/#url-class var URLConstructor = function URL(url /* , base */) { var that = anInstance(this, URLPrototype); var base = arguments.length > 1 ? arguments[1] : undefined; var urlString = $toString(url); - var state = setInternalState(that, { type: 'URL' }); + var state = setInternalState(that, new URLState()); var baseState, failure; if (base !== undefined) { try { baseState = getInternalURLState(base); } catch (error) { - failure = parseURL(baseState = {}, $toString(base)); + baseState = {}; + failure = parseURL(baseState, $toString(base)); if (failure) throw TypeError(failure); } } failure = parseURL(state, urlString, null, baseState); if (failure) throw TypeError(failure); - var searchParams = state.searchParams = new URLSearchParams(); - var searchParamsState = getInternalSearchParamsState(searchParams); - searchParamsState.updateSearchParams(state.query); - searchParamsState.updateURL = function () { - state.query = $toString(searchParams) || null; - }; + state.searchParams.update(); if (!DESCRIPTORS) { that.href = call(serializeURL, that); that.origin = call(getOrigin, that); @@ -873,7 +882,7 @@ var getSearch = function () { }; var getSearchParams = function () { - return getInternalURLState(this).searchParams; + return getInternalURLState(this).searchParams.facade; }; var getHash = function () { @@ -894,7 +903,7 @@ if (DESCRIPTORS) { var urlString = $toString(href); var failure = parseURL(url, urlString); if (failure) throw TypeError(failure); - getInternalSearchParamsState(url.searchParams).updateSearchParams(url.query); + url.searchParams.update(); }), // `URL.prototype.origin` getter // https://url.spec.whatwg.org/#dom-url-origin @@ -970,7 +979,7 @@ if (DESCRIPTORS) { url.query = ''; parseURL(url, search, QUERY); } - getInternalSearchParamsState(url.searchParams).updateSearchParams(url.query); + url.searchParams.update(); }), // `URL.prototype.searchParams` getter // https://url.spec.whatwg.org/#dom-url-searchparams