Skip to content

Commit

Permalink
url: Update URLSearchParams' constructor arguments.
Browse files Browse the repository at this point in the history
Adapt to whatwg/url#175, which dropped
URLSearchParams itself from the union accepted by the constructor. The
constructor now takes an optional

  (sequence<sequence<USVString>> or record<USVString, USVString> or USVString)

instead. Existing uses of `new URLSearchParams(<existing URLSearchParam>)`
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) <raphael.kubo.da.costa@intel.com>
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Reviewed-by: Mike West <mkwst@chromium.org>
Cr-Commit-Position: refs/heads/master@{#482588}
  • Loading branch information
Raphael Kubo da Costa authored and Commit Bot committed Jun 27, 2017
1 parent 6b4ec7b commit 8e67277
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
Expand All @@ -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.

Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');

</script>
</head>
</html>
4 changes: 2 additions & 2 deletions third_party/WebKit/Source/bindings/core/v8/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 14 additions & 8 deletions third_party/WebKit/Source/core/dom/URLSearchParams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<std::pair<String, String>>& 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() {}
Expand Down
7 changes: 4 additions & 3 deletions third_party/WebKit/Source/core/dom/URLSearchParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <base/gtest_prod_util.h>
#include <utility>
#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"
Expand All @@ -20,7 +20,7 @@ namespace blink {
class ExceptionState;
class DOMURL;

typedef USVStringSequenceSequenceOrUSVStringOrURLSearchParams
typedef USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString
URLSearchParamsInit;

class CORE_EXPORT URLSearchParams final
Expand All @@ -31,6 +31,8 @@ class CORE_EXPORT URLSearchParams final

public:
static URLSearchParams* Create(const URLSearchParamsInit&, ExceptionState&);
static URLSearchParams* Create(const Vector<std::pair<String, String>>&,
ExceptionState&);
static URLSearchParams* Create(const Vector<Vector<String>>&,
ExceptionState&);

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion third_party/WebKit/Source/core/dom/URLSearchParams.idl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// https://url.spec.whatwg.org/#interface-urlsearchparams

[
Constructor(optional (sequence<sequence<USVString>> or USVString or URLSearchParams) init = ""),
Constructor(optional (sequence<sequence<USVString>> or record<USVString, USVString> or USVString) init = ""),
Exposed=(Window,Worker),
RaisesException=Constructor
] interface URLSearchParams {
Expand Down

0 comments on commit 8e67277

Please sign in to comment.