From 8e6727773d5da351bc6eb887dc491cc599c9e6d7 Mon Sep 17 00:00:00 2001 From: Raphael Kubo da Costa Date: Thu, 22 Jun 2017 14:21:28 +0200 Subject: [PATCH] url: Update URLSearchParams' constructor arguments. Adapt to https://github.com/whatwg/url/pull/175, which dropped URLSearchParams itself from the union accepted by the constructor. The constructor now takes an optional (sequence> or record or USVString) instead. Existing uses of `new URLSearchParams()` continue to work, as per WebIDL these objects are converted to sequences because URLSearchParams is iterable. Intent to ship/implement thread: https://groups.google.com/a/chromium.org/d/msg/blink-dev/AGyufsf5XU4/CBeVLmT7BgAJ Bug: 680531, 697378 Change-Id: Ie82d1151acc4334628be0cc258bc20e5a0dc1d15 Reviewed-on: https://chromium-review.googlesource.com/544919 Commit-Queue: Raphael Kubo da Costa (rakuco) Reviewed-by: Kentaro Hara Reviewed-by: Mike West Cr-Commit-Position: refs/heads/master@{#482588} --- .../urlsearchparams-constructor-expected.txt | 10 ++++----- .../domurl/urlsearchparams-constructor.html | 16 +++++++++++--- .../WebKit/Source/bindings/core/v8/BUILD.gn | 4 ++-- .../Source/core/dom/URLSearchParams.cpp | 22 ++++++++++++------- .../WebKit/Source/core/dom/URLSearchParams.h | 7 +++--- .../Source/core/dom/URLSearchParams.idl | 2 +- 6 files changed, 39 insertions(+), 22 deletions(-) diff --git a/third_party/WebKit/LayoutTests/external/wpt/url/urlsearchparams-constructor-expected.txt b/third_party/WebKit/LayoutTests/external/wpt/url/urlsearchparams-constructor-expected.txt index 9cc74ec4a462c4..31edfdf6a5480b 100644 --- a/third_party/WebKit/LayoutTests/external/wpt/url/urlsearchparams-constructor-expected.txt +++ b/third_party/WebKit/LayoutTests/external/wpt/url/urlsearchparams-constructor-expected.txt @@ -3,7 +3,7 @@ PASS Basic URLSearchParams construction PASS URLSearchParams constructor, no arguments FAIL URLSearchParams constructor, DOMException.prototype as argument Illegal invocation PASS URLSearchParams constructor, empty string as argument -FAIL URLSearchParams constructor, {} as argument assert_equals: expected "" but got "%5Bobject+Object%5D=" +PASS URLSearchParams constructor, {} as argument PASS URLSearchParams constructor, string. PASS URLSearchParams constructor, object. PASS Parse + @@ -17,10 +17,10 @@ PASS Parse %e2%8e%84 PASS Parse 💩 PASS Parse %f0%9f%92%a9 PASS Constructor with sequence of sequences of strings -FAIL Construct with object with + assert_array_equals: property 0, expected "+" but got "[object Object]" -FAIL Construct with object with two keys assert_array_equals: property 0, expected "c" but got "[object Object]" +PASS Construct with object with + +PASS Construct with object with two keys PASS Construct with array with two keys -FAIL Construct with object with NULL, non-ASCII, and surrogate keys assert_array_equals: property 0, expected "a\0b" but got "[object Object]" -FAIL Custom [Symbol.iterator] assert_equals: expected (string) "b" but got (object) null +PASS Construct with object with NULL, non-ASCII, and surrogate keys +PASS Custom [Symbol.iterator] Harness: the test ran to completion. diff --git a/third_party/WebKit/LayoutTests/fast/domurl/urlsearchparams-constructor.html b/third_party/WebKit/LayoutTests/fast/domurl/urlsearchparams-constructor.html index 5ced91c5e65ff5..f262947d533134 100644 --- a/third_party/WebKit/LayoutTests/fast/domurl/urlsearchparams-constructor.html +++ b/third_party/WebKit/LayoutTests/fast/domurl/urlsearchparams-constructor.html @@ -55,8 +55,6 @@ var params = new URLSearchParams(''); assert_not_equals(params, null, 'constructor returned non-null value.'); assert_equals(params.__proto__, URLSearchParams.prototype, 'expected URLSearchParams.prototype as prototype.'); - params = new URLSearchParams({}); - assert_equals(params + '', '%5Bobject+Object%5D='); }, 'URLSearchParams constructor, empty.'); test(function() { @@ -91,7 +89,7 @@ assert_false(params.has('e')); params.append('g', 'h'); assert_false(seed.has('g')); -}, 'URLSearchParams constructor, object.'); +}, 'sequence initializer, object coerced to iterable'); test(function() { var params = new URLSearchParams('a=b+c'); @@ -180,6 +178,18 @@ "Sequence elements must be pairs"); }, 'sequence initializer'); +test(function() { + let params = new URLSearchParams({}); + assert_true(params !== null, 'Empty record'); + assert_equals(params.toString(), ''); + + params = new URLSearchParams({1: 2, 'a': 'b'}); + assert_equals(params.toString(), '1=2&a=b'); + + params = new URLSearchParams({false: true, 0: 'foo'}); + assert_equals(params.toString(), '0=foo&false=true'); +}, 'record initializer'); + diff --git a/third_party/WebKit/Source/bindings/core/v8/BUILD.gn b/third_party/WebKit/Source/bindings/core/v8/BUILD.gn index c272330490e2af..4d57a5e94a319b 100644 --- a/third_party/WebKit/Source/bindings/core/v8/BUILD.gn +++ b/third_party/WebKit/Source/bindings/core/v8/BUILD.gn @@ -73,8 +73,8 @@ bindings_core_generated_union_type_files = [ "$bindings_core_v8_output_dir/StringOrFloat.h", "$bindings_core_v8_output_dir/StringOrUnrestrictedDoubleSequence.cpp", "$bindings_core_v8_output_dir/StringOrUnrestrictedDoubleSequence.h", - "$bindings_core_v8_output_dir/USVStringSequenceSequenceOrUSVStringOrURLSearchParams.cpp", - "$bindings_core_v8_output_dir/USVStringSequenceSequenceOrUSVStringOrURLSearchParams.h", + "$bindings_core_v8_output_dir/USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString.cpp", + "$bindings_core_v8_output_dir/USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString.h", "$bindings_core_v8_output_dir/UnrestrictedDoubleOrKeyframeAnimationOptions.cpp", "$bindings_core_v8_output_dir/UnrestrictedDoubleOrKeyframeAnimationOptions.h", "$bindings_core_v8_output_dir/UnrestrictedDoubleOrKeyframeEffectOptions.cpp", diff --git a/third_party/WebKit/Source/core/dom/URLSearchParams.cpp b/third_party/WebKit/Source/core/dom/URLSearchParams.cpp index e6ce11d5d10bb4..e0199bb0c28e4d 100644 --- a/third_party/WebKit/Source/core/dom/URLSearchParams.cpp +++ b/third_party/WebKit/Source/core/dom/URLSearchParams.cpp @@ -48,11 +48,10 @@ URLSearchParams* URLSearchParams::Create(const URLSearchParamsInit& init, return new URLSearchParams(query_string.Substring(1)); return new URLSearchParams(query_string); } - // TODO(sof): copy constructor no longer in the spec, - // consider removing. - if (init.isURLSearchParams()) - return new URLSearchParams(init.getAsURLSearchParams()); - + if (init.isUSVStringUSVStringRecord()) { + return URLSearchParams::Create(init.getAsUSVStringUSVStringRecord(), + exception_state); + } if (init.isUSVStringSequenceSequence()) { return URLSearchParams::Create(init.getAsUSVStringSequenceSequence(), exception_state); @@ -87,9 +86,16 @@ URLSearchParams::URLSearchParams(const String& query_string, DOMURL* url_object) SetInput(query_string); } -URLSearchParams::URLSearchParams(URLSearchParams* search_params) { - DCHECK(search_params); - params_ = search_params->params_; +URLSearchParams* URLSearchParams::Create( + const Vector>& init, + ExceptionState& exception_state) { + URLSearchParams* instance = new URLSearchParams(String()); + if (init.IsEmpty()) + return instance; + for (const auto& item : init) + instance->AppendWithoutUpdate(item.first, item.second); + instance->RunUpdateSteps(); + return instance; } URLSearchParams::~URLSearchParams() {} diff --git a/third_party/WebKit/Source/core/dom/URLSearchParams.h b/third_party/WebKit/Source/core/dom/URLSearchParams.h index 8ff91cd5791919..9dcd45513f2adf 100644 --- a/third_party/WebKit/Source/core/dom/URLSearchParams.h +++ b/third_party/WebKit/Source/core/dom/URLSearchParams.h @@ -8,7 +8,7 @@ #include #include #include "bindings/core/v8/Iterable.h" -#include "bindings/core/v8/USVStringSequenceSequenceOrUSVStringOrURLSearchParams.h" +#include "bindings/core/v8/USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString.h" #include "platform/bindings/ScriptWrappable.h" #include "platform/heap/Handle.h" #include "platform/network/EncodedFormData.h" @@ -20,7 +20,7 @@ namespace blink { class ExceptionState; class DOMURL; -typedef USVStringSequenceSequenceOrUSVStringOrURLSearchParams +typedef USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString URLSearchParamsInit; class CORE_EXPORT URLSearchParams final @@ -31,6 +31,8 @@ class CORE_EXPORT URLSearchParams final public: static URLSearchParams* Create(const URLSearchParamsInit&, ExceptionState&); + static URLSearchParams* Create(const Vector>&, + ExceptionState&); static URLSearchParams* Create(const Vector>&, ExceptionState&); @@ -65,7 +67,6 @@ class CORE_EXPORT URLSearchParams final FRIEND_TEST_ALL_PREFIXES(URLSearchParamsTest, EncodedFormData); explicit URLSearchParams(const String&, DOMURL* = nullptr); - explicit URLSearchParams(URLSearchParams*); void RunUpdateSteps(); IterationSource* StartIteration(ScriptState*, ExceptionState&) override; diff --git a/third_party/WebKit/Source/core/dom/URLSearchParams.idl b/third_party/WebKit/Source/core/dom/URLSearchParams.idl index 810b4dc9261f75..0d7571ce4ae564 100644 --- a/third_party/WebKit/Source/core/dom/URLSearchParams.idl +++ b/third_party/WebKit/Source/core/dom/URLSearchParams.idl @@ -5,7 +5,7 @@ // https://url.spec.whatwg.org/#interface-urlsearchparams [ - Constructor(optional (sequence> or USVString or URLSearchParams) init = ""), + Constructor(optional (sequence> or record or USVString) init = ""), Exposed=(Window,Worker), RaisesException=Constructor ] interface URLSearchParams {