Skip to content

Commit ad8af2c

Browse files
committed
fix(url): make URLSearchParams record constructor spec compliant
1 parent faa9114 commit ad8af2c

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

builtins/web/fetch/headers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ bool Headers::init_entries(JSContext *cx, HandleObject self, HandleValue initv)
650650
// TODO: But note: forbidden headers have to be applied correctly.
651651
bool consumed = false;
652652
if (!core::maybe_consume_sequence_or_record<host_api::HostString, validate_header_name,
653-
append_valid_header>(cx, initv, self, &consumed,
653+
append_valid_header, append_valid_header>(cx, initv, self, &consumed,
654654
"Headers")) {
655655
return false;
656656
}

builtins/web/url.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ jsurl::JSUrlSearchParams *URLSearchParams::get_params(JSObject *self) {
183183
}
184184

185185
namespace {
186-
jsurl::SpecString append_impl_validate(JSContext *cx, JS::HandleValue key, const char *_) {
186+
jsurl::SpecString validate(JSContext *cx, JS::HandleValue key, const char *_) {
187187
return core::encode_spec_string(cx, key);
188188
}
189189
bool append_impl(JSContext *cx, JS::HandleObject self, jsurl::SpecString key, JS::HandleValue val,
@@ -198,6 +198,18 @@ bool append_impl(JSContext *cx, JS::HandleObject self, jsurl::SpecString key, JS
198198
jsurl::params_append(params, key, value);
199199
return true;
200200
}
201+
bool set_impl(JSContext *cx, JS::HandleObject self, jsurl::SpecString key,
202+
JS::HandleValue val, const char *_) {
203+
auto *const params = URLSearchParams::get_params(self);
204+
205+
auto value = core::encode_spec_string(cx, val);
206+
if (!value.data) {
207+
return false;
208+
}
209+
210+
jsurl::params_set(params, key, value);
211+
return true;
212+
}
201213
} // namespace
202214

203215
jsurl::SpecSlice URLSearchParams::serialize(JSContext *cx, JS::HandleObject self) {
@@ -206,7 +218,7 @@ jsurl::SpecSlice URLSearchParams::serialize(JSContext *cx, JS::HandleObject self
206218

207219
bool URLSearchParams::append(JSContext *cx, unsigned argc, JS::Value *vp) {
208220
METHOD_HEADER(2)
209-
auto value = append_impl_validate(cx, args[0], "append");
221+
auto value = validate(cx, args[0], "append");
210222
if (!append_impl(cx, self, value, args[1], "append")) {
211223
return false;
212224
}
@@ -407,7 +419,7 @@ JSObject *URLSearchParams::create(JSContext *cx, JS::HandleObject self,
407419

408420
bool consumed = false;
409421
const char *alt_text = ", or a value that can be stringified";
410-
if (!core::maybe_consume_sequence_or_record<jsurl::SpecString, append_impl_validate, append_impl>(
422+
if (!core::maybe_consume_sequence_or_record<jsurl::SpecString, validate, append_impl, set_impl>(
411423
cx, params_val, self, &consumed, "URLSearchParams", alt_text)) {
412424
return nullptr;
413425
}
@@ -831,5 +843,3 @@ bool install(api::Engine *engine) {
831843
}
832844

833845
} // namespace builtins::web::url
834-
835-

runtime/sequence.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace core {
1313
* Extract <key,value> pairs from the given value if it is either a
1414
* sequence<sequence<Value> or a record<Value, Value>.
1515
*/
16-
template <typename T, auto validate, auto apply>
16+
template <typename T, auto validate, auto apply_sequence, auto apply_record>
1717
bool maybe_consume_sequence_or_record(JSContext *cx, JS::HandleValue initv, JS::HandleObject target,
1818
bool *consumed, const char *ctor_name,
1919
const char *alt_text = "") {
@@ -92,7 +92,7 @@ bool maybe_consume_sequence_or_record(JSContext *cx, JS::HandleValue initv, JS::
9292
return api::throw_error(cx, api::Errors::InvalidSequence, ctor_name, alt_text);
9393
}
9494

95-
if (!apply(cx, target, std::move(validated_key), value, ctor_name)) {
95+
if (!apply_sequence(cx, target, std::move(validated_key), value, ctor_name)) {
9696
return false;
9797
}
9898
}
@@ -128,7 +128,7 @@ bool maybe_consume_sequence_or_record(JSContext *cx, JS::HandleValue initv, JS::
128128
if (!JS_GetPropertyById(cx, init, curId, &value)) {
129129
return false;
130130
}
131-
if (!apply(cx, target, std::move(validated_key), value, ctor_name)) {
131+
if (!apply_record(cx, target, std::move(validated_key), value, ctor_name)) {
132132
return false;
133133
}
134134
}

tests/wpt-harness/expectations/url/urlsearchparams-constructor.any.js.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@
6969
"status": "PASS"
7070
},
7171
"Construct with 2 unpaired surrogates (no trailing)": {
72-
"status": "FAIL"
72+
"status": "PASS"
7373
},
7474
"Construct with 3 unpaired surrogates (no leading)": {
75-
"status": "FAIL"
75+
"status": "PASS"
7676
},
7777
"Construct with object with NULL, non-ASCII, and surrogate keys": {
7878
"status": "PASS"
7979
},
8080
"Custom [Symbol.iterator]": {
8181
"status": "PASS"
8282
}
83-
}
83+
}

0 commit comments

Comments
 (0)