Skip to content

Commit b4cda0c

Browse files
committed
fix(runtime): URL toJSON and value-aware searchParams.delete
WHATWG URL defines toJSON as the href serialization, so JSON.stringify(url) yields the href string. The runtime's URL lacked it, which also kept a module URL from being assignable to lib.dom's URL in typed code. It is registered next to toString and shares its callback, so both return the same serialization. types/ns-url.d.ts declares it; tsc against the DOM and the non-DOM program stays clean, and the DOM program now assigns pathToFileURL()'s result to the global URL type. The searchParams wrapper that keeps a URL's query in sync forwarded only the name to the native delete, so url.searchParams.delete(name, value) removed every pair with that name. It now forwards the value whenever the caller passed one, by arity, so an explicit undefined still reaches the native coercion.
1 parent 40887a2 commit b4cda0c

5 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎NativeScript/runtime/URLImpl.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ v8::Local<v8::FunctionTemplate> URLImpl::GetCtor(v8::Isolate* isolate) {
9393
tmpl->Set(ToV8String(isolate, "toString"),
9494
v8::FunctionTemplate::New(isolate, &ToString));
9595

96+
// WHATWG defines toJSON as the href serialization, the same as toString.
97+
tmpl->Set(ToV8String(isolate, "toJSON"),
98+
v8::FunctionTemplate::New(isolate, &ToString));
99+
96100
ctorTmpl->Set(ToV8String(isolate, "canParse"),
97101
v8::FunctionTemplate::New(isolate, &CanParse));
98102

‎NativeScript/runtime/js/blob-url.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,15 @@ ObjectDefineProperty(URL.prototype, 'searchParams', {
105105
writeBack(this);
106106
};
107107
params._delete = params.delete;
108-
params.delete = function (name) {
108+
params.delete = function (name, value) {
109109
resync(this);
110-
this._delete(name);
110+
// An explicitly passed undefined must still reach the native
111+
// coercion, so forward by arity rather than by value.
112+
if (arguments.length > 1) {
113+
this._delete(name, value);
114+
} else {
115+
this._delete(name);
116+
}
111117
writeBack(this);
112118
};
113119
params._set = params.set;

‎TestRunner/app/tests/URL.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,11 @@ describe("URL", function () {
5858
expect(url.searchParams.get("q")).toBe("hello");
5959
expect(url.pathname).toBe("/some/path");
6060
});
61+
62+
it("serializes to JSON as its href", function () {
63+
const url = new URL("https://x.test/a?b#c");
64+
expect(url.toJSON()).toBe(url.href);
65+
expect(JSON.stringify(url)).toBe(JSON.stringify(url.href));
66+
expect(JSON.stringify({ u: url })).toBe(JSON.stringify({ u: url.href }));
67+
});
6168
});

‎TestRunner/app/tests/UrlSearchParamsTests.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,18 @@ describe("URL.searchParams caching", function () {
146146
expect(key).not.toBe("_searchParamsSource");
147147
}
148148
});
149+
150+
it("deletes only the matching pair when given a value", function () {
151+
const url = new URL("https://x.test/?a=1&a=2");
152+
url.searchParams.delete("a", "1");
153+
expect(url.search).toBe("?a=2");
154+
expect(url.searchParams.getAll("a")).toEqual(["2"]);
155+
});
156+
157+
it("deletes every pair of the name when given no value", function () {
158+
const url = new URL("https://x.test/?a=1&a=2");
159+
url.searchParams.delete("a");
160+
expect(url.search).toBe("");
161+
expect(url.searchParams.getAll("a")).toEqual([]);
162+
});
149163
});

‎types/ns-url.d.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ declare module "ns:url" {
2222
username: string;
2323
/** Returns `href`. */
2424
toString(): string;
25+
/** Returns `href`, so `JSON.stringify` serializes a URL as its href. */
26+
toJSON(): string;
2527
}
2628

2729
/** The WHATWG `URLSearchParams` surface the runtime implements. */

0 commit comments

Comments
 (0)