Skip to content

Commit

Permalink
url: fix url spec compliance issues
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Feb 7, 2023
1 parent bed28f2 commit 0d8b53a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
9 changes: 8 additions & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class URLContext {
hash = '';
hasHost = false;
hasOpaquePath = false;
hasHash = false;
}

function isURLSearchParams(self) {
Expand Down Expand Up @@ -628,7 +629,7 @@ class URL {

#onParseComplete = (href, origin, protocol, host, hostname, pathname,
search, username, password, port, hash, hasHost,
hasOpaquePath) => {
hasOpaquePath, hasHash) => {
const ctx = this[context];
ctx.href = href;
ctx.origin = origin;
Expand All @@ -644,6 +645,7 @@ class URL {
// TODO(@anonrig): Remove hasHost and hasOpaquePath when kFormat is removed.
ctx.hasHost = hasHost;
ctx.hasOpaquePath = hasOpaquePath;
ctx.hasHash = hasHash;
if (!this[searchParams]) { // Invoked from URL constructor
this[searchParams] = new URLSearchParams();
this[searchParams][context] = this;
Expand Down Expand Up @@ -864,6 +866,11 @@ function update(url, params) {
ctx.search = '?' + serializedParams;
} else {
ctx.search = '';

// Potentially strip trailing spaces from an opaque path
if (ctx.hasOpaquePath && !ctx.hasHash) {
ctx.pathname = ctx.pathname.trimEnd();
}
}
ctx.href = constructHref(ctx);
}
Expand Down
3 changes: 3 additions & 0 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void SetArgs(Environment* env, Local<Value> argv[12], const ada::result& url) {
argv[10] = Utf8String(isolate, url->get_hash());
argv[11] = Boolean::New(isolate, url->host.has_value());
argv[12] = Boolean::New(isolate, url->has_opaque_path);
argv[13] = Boolean::New(isolate, url->fragment.has_value());
}

void Parse(const FunctionCallbackInfo<Value>& args) {
Expand Down Expand Up @@ -109,6 +110,7 @@ void Parse(const FunctionCallbackInfo<Value>& args) {
undef,
undef,
undef,
undef,
};
SetArgs(env, argv, out);
USE(success_callback_->Call(
Expand Down Expand Up @@ -260,6 +262,7 @@ void UpdateUrl(const FunctionCallbackInfo<Value>& args) {
undef,
undef,
undef,
undef,
};
SetArgs(env, argv, out);
USE(success_callback_->Call(
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-whatwg-url-custom-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ assert.strictEqual(
port: '8080',
hash: '#hash',
hasHost: true,
hasOpaquePath: false
hasOpaquePath: false,
hasHash: true
}
}`);

Expand Down
8 changes: 7 additions & 1 deletion test/wpt/status/url.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
"skip": "TODO: port from .window.js"
},
"historical.any.js": {
"requires": ["small-icu"]
"requires": ["small-icu"],
"fail": {
"expected": [
"URL: no structured serialize/deserialize support",
"URLSearchParams: no structured serialize/deserialize support"
]
}
},
"urlencoded-parser.any.js": {
"requires": ["small-icu"]
Expand Down

0 comments on commit 0d8b53a

Please sign in to comment.