Skip to content

Commit

Permalink
Implement URL.parse()
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=271636

Reviewed by Alex Christensen.

This was standardized in whatwg/url#825 and test
coverage was added here:
web-platform-tests/wpt#45248

As a drive-by fix we remove m_baseURL from DOMURL as it does not need it.

* LayoutTests/imported/w3c/web-platform-tests/url/url-statics-parse.any-expected.txt: Added.
* LayoutTests/imported/w3c/web-platform-tests/url/url-statics-parse.any.html: Added.
* LayoutTests/imported/w3c/web-platform-tests/url/url-statics-parse.any.js: Added.
(forEach):
(test):
* LayoutTests/imported/w3c/web-platform-tests/url/url-statics-parse.any.worker-expected.txt: Added.
* LayoutTests/imported/w3c/web-platform-tests/url/url-statics-parse.any.worker.html: Added.
* LayoutTests/imported/w3c/web-platform-tests/url/w3c-import.log:
* Source/WebCore/html/DOMURL.cpp:
(WebCore::DOMURL::DOMURL):
(WebCore::DOMURL::create):
(WebCore::parseInternal):
(WebCore::DOMURL::parse):
(WebCore::DOMURL::canParse):
* Source/WebCore/html/DOMURL.h:
* Source/WebCore/html/DOMURL.idl:

Canonical link: https://commits.webkit.org/276656@main
  • Loading branch information
annevk committed Mar 25, 2024
1 parent 926a356 commit e1614bd
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

PASS URL.parse(undefined, undefined)
PASS URL.parse(aaa:b, undefined)
PASS URL.parse(undefined, aaa:b)
PASS URL.parse(aaa:/b, undefined)
PASS URL.parse(undefined, aaa:/b)
PASS URL.parse(https://test:test, undefined)
PASS URL.parse(a, https://b/)
PASS URL.parse() should return a unique object

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- This file is required for WebKit test infrastructure to run the templated test -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// This intentionally does not use resources/urltestdata.json to preserve resources.
[
{
"url": undefined,
"base": undefined,
"expected": false
},
{
"url": "aaa:b",
"base": undefined,
"expected": true
},
{
"url": undefined,
"base": "aaa:b",
"expected": false
},
{
"url": "aaa:/b",
"base": undefined,
"expected": true
},
{
"url": undefined,
"base": "aaa:/b",
"expected": true
},
{
"url": "https://test:test",
"base": undefined,
"expected": false
},
{
"url": "a",
"base": "https://b/",
"expected": true
}
].forEach(({ url, base, expected }) => {
test(() => {
if (expected == false) {
assert_equals(URL.parse(url, base), null);
} else {
assert_equals(URL.parse(url, base).href, new URL(url, base).href);
}
}, `URL.parse(${url}, ${base})`);
});

test(() => {
assert_not_equals(URL.parse("https://example/"), URL.parse("https://example/"));
}, `URL.parse() should return a unique object`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

PASS URL.parse(undefined, undefined)
PASS URL.parse(aaa:b, undefined)
PASS URL.parse(undefined, aaa:b)
PASS URL.parse(aaa:/b, undefined)
PASS URL.parse(undefined, aaa:/b)
PASS URL.parse(https://test:test, undefined)
PASS URL.parse(a, https://b/)
PASS URL.parse() should return a unique object

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- This file is required for WebKit test infrastructure to run the templated test -->
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ List of files:
/LayoutTests/imported/w3c/web-platform-tests/url/url-setters-stripping.any.js
/LayoutTests/imported/w3c/web-platform-tests/url/url-setters.any.js
/LayoutTests/imported/w3c/web-platform-tests/url/url-statics-canparse.any.js
/LayoutTests/imported/w3c/web-platform-tests/url/url-statics-parse.any.js
/LayoutTests/imported/w3c/web-platform-tests/url/url-tojson.any.js
/LayoutTests/imported/w3c/web-platform-tests/url/urlencoded-parser.any.js
/LayoutTests/imported/w3c/web-platform-tests/url/urlsearchparams-append.any.js
Expand Down
28 changes: 20 additions & 8 deletions Source/WebCore/html/DOMURL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@

namespace WebCore {

inline DOMURL::DOMURL(URL&& completeURL, const URL& baseURL)
: m_baseURL(baseURL)
, m_url(WTFMove(completeURL))
inline DOMURL::DOMURL(URL&& completeURL)
: m_url(WTFMove(completeURL))
{
ASSERT(m_url.isValid());
}

ExceptionOr<Ref<DOMURL>> DOMURL::create(const String& url, const URL& base)
Expand All @@ -51,7 +51,7 @@ ExceptionOr<Ref<DOMURL>> DOMURL::create(const String& url, const URL& base)
URL completeURL { base, url };
if (!completeURL.isValid())
return Exception { ExceptionCode::TypeError, makeString("\"", url, "\" cannot be parsed as a URL.") };
return adoptRef(*new DOMURL(WTFMove(completeURL), base));
return adoptRef(*new DOMURL(WTFMove(completeURL)));
}

ExceptionOr<Ref<DOMURL>> DOMURL::create(const String& url, const String& base)
Expand All @@ -64,13 +64,25 @@ ExceptionOr<Ref<DOMURL>> DOMURL::create(const String& url, const String& base)

DOMURL::~DOMURL() = default;

bool DOMURL::canParse(const String& url, const String& base)
static URL parseInternal(const String& url, const String& base)
{
URL baseURL { base };
if (!base.isNull() && !baseURL.isValid())
return false;
URL completeURL { baseURL, url };
return completeURL.isValid();
return { };
return { baseURL, url };
}

RefPtr<DOMURL> DOMURL::parse(const String& url, const String& base)
{
auto completeURL = parseInternal(url, base);
if (!completeURL.isValid())
return { };
return adoptRef(*new DOMURL(WTFMove(completeURL)));
}

bool DOMURL::canParse(const String& url, const String& base)
{
return parseInternal(url, base).isValid();
}

ExceptionOr<void> DOMURL::setHref(const String& url)
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/html/DOMURL.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class DOMURL final : public RefCounted<DOMURL>, public CanMakeWeakPtr<DOMURL>, p
static ExceptionOr<Ref<DOMURL>> create(const String& url, const String& base);
WEBCORE_EXPORT ~DOMURL();

static RefPtr<DOMURL> parse(const String& url, const String& base);
static bool canParse(const String& url, const String& base);

const URL& href() const { return m_url; }
Expand All @@ -59,12 +60,11 @@ class DOMURL final : public RefCounted<DOMURL>, public CanMakeWeakPtr<DOMURL>, p

private:
static ExceptionOr<Ref<DOMURL>> create(const String& url, const URL& base);
DOMURL(URL&& completeURL, const URL& baseURL);
DOMURL(URL&& completeURL);

URL fullURL() const final { return m_url; }
void setFullURL(const URL& fullURL) final { setHref(fullURL.string()); }

URL m_baseURL;
URL m_url;
RefPtr<URLSearchParams> m_searchParams;
};
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/html/DOMURL.idl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
] interface DOMURL {
constructor(USVString url, optional USVString base);

static DOMURL? parse(USVString url, optional USVString base);
static boolean canParse(USVString url, optional USVString base);

[URL] stringifier attribute USVString href;
Expand Down

0 comments on commit e1614bd

Please sign in to comment.