Skip to content

Commit e1614bd

Browse files
committed
Implement URL.parse()
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
1 parent 926a356 commit e1614bd

File tree

9 files changed

+96
-10
lines changed

9 files changed

+96
-10
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
PASS URL.parse(undefined, undefined)
3+
PASS URL.parse(aaa:b, undefined)
4+
PASS URL.parse(undefined, aaa:b)
5+
PASS URL.parse(aaa:/b, undefined)
6+
PASS URL.parse(undefined, aaa:/b)
7+
PASS URL.parse(https://test:test, undefined)
8+
PASS URL.parse(a, https://b/)
9+
PASS URL.parse() should return a unique object
10+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- This file is required for WebKit test infrastructure to run the templated test -->
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// This intentionally does not use resources/urltestdata.json to preserve resources.
2+
[
3+
{
4+
"url": undefined,
5+
"base": undefined,
6+
"expected": false
7+
},
8+
{
9+
"url": "aaa:b",
10+
"base": undefined,
11+
"expected": true
12+
},
13+
{
14+
"url": undefined,
15+
"base": "aaa:b",
16+
"expected": false
17+
},
18+
{
19+
"url": "aaa:/b",
20+
"base": undefined,
21+
"expected": true
22+
},
23+
{
24+
"url": undefined,
25+
"base": "aaa:/b",
26+
"expected": true
27+
},
28+
{
29+
"url": "https://test:test",
30+
"base": undefined,
31+
"expected": false
32+
},
33+
{
34+
"url": "a",
35+
"base": "https://b/",
36+
"expected": true
37+
}
38+
].forEach(({ url, base, expected }) => {
39+
test(() => {
40+
if (expected == false) {
41+
assert_equals(URL.parse(url, base), null);
42+
} else {
43+
assert_equals(URL.parse(url, base).href, new URL(url, base).href);
44+
}
45+
}, `URL.parse(${url}, ${base})`);
46+
});
47+
48+
test(() => {
49+
assert_not_equals(URL.parse("https://example/"), URL.parse("https://example/"));
50+
}, `URL.parse() should return a unique object`);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
PASS URL.parse(undefined, undefined)
3+
PASS URL.parse(aaa:b, undefined)
4+
PASS URL.parse(undefined, aaa:b)
5+
PASS URL.parse(aaa:/b, undefined)
6+
PASS URL.parse(undefined, aaa:/b)
7+
PASS URL.parse(https://test:test, undefined)
8+
PASS URL.parse(a, https://b/)
9+
PASS URL.parse() should return a unique object
10+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- This file is required for WebKit test infrastructure to run the templated test -->

‎LayoutTests/imported/w3c/web-platform-tests/url/w3c-import.log‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ List of files:
3636
/LayoutTests/imported/w3c/web-platform-tests/url/url-setters-stripping.any.js
3737
/LayoutTests/imported/w3c/web-platform-tests/url/url-setters.any.js
3838
/LayoutTests/imported/w3c/web-platform-tests/url/url-statics-canparse.any.js
39+
/LayoutTests/imported/w3c/web-platform-tests/url/url-statics-parse.any.js
3940
/LayoutTests/imported/w3c/web-platform-tests/url/url-tojson.any.js
4041
/LayoutTests/imported/w3c/web-platform-tests/url/urlencoded-parser.any.js
4142
/LayoutTests/imported/w3c/web-platform-tests/url/urlsearchparams-append.any.js

‎Source/WebCore/html/DOMURL.cpp‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939

4040
namespace WebCore {
4141

42-
inline DOMURL::DOMURL(URL&& completeURL, const URL& baseURL)
43-
: m_baseURL(baseURL)
44-
, m_url(WTFMove(completeURL))
42+
inline DOMURL::DOMURL(URL&& completeURL)
43+
: m_url(WTFMove(completeURL))
4544
{
45+
ASSERT(m_url.isValid());
4646
}
4747

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

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

6565
DOMURL::~DOMURL() = default;
6666

67-
bool DOMURL::canParse(const String& url, const String& base)
67+
static URL parseInternal(const String& url, const String& base)
6868
{
6969
URL baseURL { base };
7070
if (!base.isNull() && !baseURL.isValid())
71-
return false;
72-
URL completeURL { baseURL, url };
73-
return completeURL.isValid();
71+
return { };
72+
return { baseURL, url };
73+
}
74+
75+
RefPtr<DOMURL> DOMURL::parse(const String& url, const String& base)
76+
{
77+
auto completeURL = parseInternal(url, base);
78+
if (!completeURL.isValid())
79+
return { };
80+
return adoptRef(*new DOMURL(WTFMove(completeURL)));
81+
}
82+
83+
bool DOMURL::canParse(const String& url, const String& base)
84+
{
85+
return parseInternal(url, base).isValid();
7486
}
7587

7688
ExceptionOr<void> DOMURL::setHref(const String& url)

‎Source/WebCore/html/DOMURL.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class DOMURL final : public RefCounted<DOMURL>, public CanMakeWeakPtr<DOMURL>, p
4343
static ExceptionOr<Ref<DOMURL>> create(const String& url, const String& base);
4444
WEBCORE_EXPORT ~DOMURL();
4545

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

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

6061
private:
6162
static ExceptionOr<Ref<DOMURL>> create(const String& url, const URL& base);
62-
DOMURL(URL&& completeURL, const URL& baseURL);
63+
DOMURL(URL&& completeURL);
6364

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

67-
URL m_baseURL;
6868
URL m_url;
6969
RefPtr<URLSearchParams> m_searchParams;
7070
};

‎Source/WebCore/html/DOMURL.idl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
] interface DOMURL {
3535
constructor(USVString url, optional USVString base);
3636

37+
static DOMURL? parse(USVString url, optional USVString base);
3738
static boolean canParse(USVString url, optional USVString base);
3839

3940
[URL] stringifier attribute USVString href;

0 commit comments

Comments
 (0)