Skip to content

Commit 27d1d5d

Browse files
authored
feat(url): implement URLSearchParams.size property (#296)
Adds the readonly .size property to URLSearchParams as per the WHATWG URL Standard, returning the number of name-value pairs.
1 parent faa9114 commit 27d1d5d

5 files changed

Lines changed: 22 additions & 7 deletions

File tree

builtins/web/url.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ const JSFunctionSpec URLSearchParams::methods[] = {
174174
};
175175

176176
const JSPropertySpec URLSearchParams::properties[] = {
177+
JS_PSG("size", URLSearchParams::size_get, JSPROP_ENUMERATE),
177178
JS_PS_END,
178179
};
179180

@@ -347,6 +348,14 @@ bool URLSearchParams::set(JSContext *cx, unsigned argc, JS::Value *vp) {
347348
return true;
348349
}
349350

351+
bool URLSearchParams::size_get(JSContext *cx, unsigned argc, JS::Value *vp) {
352+
METHOD_HEADER(0)
353+
auto *params =
354+
static_cast<jsurl::JSUrlSearchParams *>(JS::GetReservedSlot(self, Slots::Params).toPrivate());
355+
args.rval().setNumber(jsurl::params_size(params));
356+
return true;
357+
}
358+
350359
bool URLSearchParams::sort(JSContext *cx, unsigned argc, JS::Value *vp) {
351360
METHOD_HEADER(0)
352361
auto *params =
@@ -831,5 +840,3 @@ bool install(api::Engine *engine) {
831840
}
832841

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

builtins/web/url.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class URLSearchParams : public BuiltinNoConstructor<URLSearchParams> {
3232
static bool has(JSContext *cx, unsigned argc, JS::Value *vp);
3333
static bool getAll(JSContext *cx, unsigned argc, JS::Value *vp);
3434
static bool sort(JSContext *cx, unsigned argc, JS::Value *vp);
35+
static bool size_get(JSContext *cx, unsigned argc, JS::Value *vp);
3536
static bool forEach(JSContext *cx, unsigned argc, JS::Value *vp);
3637
static bool delete_(JSContext *cx, unsigned argc, JS::Value *vp);
3738
static bool get(JSContext *cx, unsigned argc, JS::Value *vp);

crates/rust-url/rust-url.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ CVec<SpecSlice> params_get_all(const JSUrlSearchParams *params, const SpecString
185185

186186
void params_set(JSUrlSearchParams *params, SpecString name, SpecString value);
187187

188+
size_t params_size(JSUrlSearchParams *params);
189+
188190
void params_sort(JSUrlSearchParams *params);
189191

190192
SpecSlice params_to_string(const JSUrlSearchParams *params);

crates/rust-url/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,11 @@ pub extern "C" fn params_set(params: &mut JSUrlSearchParams, name: SpecString, v
408408
params.update_url_or_str();
409409
}
410410

411+
#[no_mangle]
412+
pub extern "C" fn params_size(params: &JSUrlSearchParams) -> usize {
413+
params.list.len()
414+
}
415+
411416
#[no_mangle]
412417
pub extern "C" fn params_sort(params: &mut JSUrlSearchParams) {
413418
params
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"URLSearchParams's size and deletion": {
3-
"status": "FAIL"
3+
"status": "PASS"
44
},
55
"URLSearchParams's size and addition": {
6-
"status": "FAIL"
6+
"status": "PASS"
77
},
88
"URLSearchParams's size when obtained from a URL": {
9-
"status": "FAIL"
9+
"status": "PASS"
1010
},
1111
"URLSearchParams's size when obtained from a URL and using .search": {
12-
"status": "FAIL"
12+
"status": "PASS"
1313
}
14-
}
14+
}

0 commit comments

Comments
 (0)