From 1d597a4e1933c5c159799761a8210bf9bde532cd Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Tue, 7 Feb 2023 14:17:58 -0500 Subject: [PATCH] url: fix url spec compliance issues Co-authored-by: Daniel Lemire PR-URL: https://github.com/nodejs/node/pull/46547 Reviewed-By: James M Snell Reviewed-By: Filip Skokan Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Rich Trott --- lib/internal/url.js | 13 +++++++++++++ test/wpt/status/url.json | 8 +++++++- test/wpt/test-url.js | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index dbec51826e18d6..f723bd646f900b 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -872,6 +872,19 @@ function update(url, params) { ctx.search = '?' + serializedParams; } else { ctx.search = ''; + + // Potentially strip trailing spaces from an opaque path + if (ctx.hasOpaquePath && ctx.hash.length === 0) { + let length = ctx.pathname.length; + while (length > 0 && ctx.pathname.charCodeAt(length - 1) === 32) { + length--; + } + + // No need to copy the whole string if there is no space at the end + if (length !== ctx.pathname.length) { + ctx.pathname = ctx.pathname.slice(0, length); + } + } } ctx.href = constructHref(ctx); } diff --git a/test/wpt/status/url.json b/test/wpt/status/url.json index 1c60db0fa7e715..a0957dccb53c73 100644 --- a/test/wpt/status/url.json +++ b/test/wpt/status/url.json @@ -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"] diff --git a/test/wpt/test-url.js b/test/wpt/test-url.js index 880153fd8a2d63..1998ea5bf43798 100644 --- a/test/wpt/test-url.js +++ b/test/wpt/test-url.js @@ -15,6 +15,6 @@ runner.setScriptModifier((obj) => { }); runner.pretendGlobalThisAs('Window'); runner.setInitScript(` - globalThis.location = {}; + globalThis.location ||= {}; `); runner.runJsTests();