Skip to content

Commit

Permalink
<a>/<area>'s origin getter returns "null" rather than "" when URL can…
Browse files Browse the repository at this point in the history
…not be parsed

https://bugs.webkit.org/show_bug.cgi?id=128020
rdar://114078288

Reviewed by Brent Fulgham.

Check if the URL cannot be parsed first (isValid() in our code) and if
so, return the empty string. This aligns us with the HTML Standard and
other browsers.

This is not implemented in URLDecomposition as URL, Location, and
WorkerLocation always represent parsable URLs.

* LayoutTests/fast/dom/HTMLAnchorElement/anchor-element-href-parsing-expected.txt:
* LayoutTests/fast/dom/HTMLAnchorElement/anchor-element-href-parsing.html:

Add coverage for username and password getters while here.

* Source/WebCore/html/HTMLAnchorElement.cpp:
(WebCore::HTMLAnchorElement::origin const):

Canonical link: https://commits.webkit.org/267222@main
  • Loading branch information
annevk committed Aug 24, 2023
1 parent 5e54c0b commit 0e09f4e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
PASS breakDownURL('about:blank') is 'protocol=about:, pathname=blank, origin=null'
PASS breakDownURL('http://example.com/') is 'protocol=http:, host=example.com, pathname=/, origin=http://example.com'
PASS breakDownURL('http://@example.com/') is 'protocol=http:, host=example.com, pathname=/, origin=http://example.com, toString=http://example.com/'
PASS breakDownURL('http://a@example.com/') is 'protocol=http:, host=example.com, pathname=/, origin=http://example.com'
PASS breakDownURL('http://a:@example.com/') is 'protocol=http:, host=example.com, pathname=/, origin=http://example.com, toString=http://a@example.com/'
PASS breakDownURL('http://joebob1:abc123@example.com/') is 'protocol=http:, host=example.com, pathname=/, origin=http://example.com'
PASS breakDownURL('http://:def456@example.com/') is 'protocol=http:, host=example.com, pathname=/, origin=http://example.com'
PASS breakDownURL('http://a@example.com/') is 'protocol=http:, username=a, host=example.com, pathname=/, origin=http://example.com'
PASS breakDownURL('http://a:@example.com/') is 'protocol=http:, username=a, host=example.com, pathname=/, origin=http://example.com, toString=http://a@example.com/'
PASS breakDownURL('http://joebob1:abc123@example.com/') is 'protocol=http:, username=joebob1, password=abc123, host=example.com, pathname=/, origin=http://example.com'
PASS breakDownURL('http://:def456@example.com/') is 'protocol=http:, password=def456, host=example.com, pathname=/, origin=http://example.com'
PASS breakDownURL('http://example.com/foo/bar') is 'protocol=http:, host=example.com, pathname=/foo/bar, origin=http://example.com'
PASS breakDownURL('HTTP://example.com/foo/bar') is 'protocol=http:, host=example.com, pathname=/foo/bar, origin=http://example.com, toString=http://example.com/foo/bar'
PASS breakDownURL('https://example.com/ttt?ggg') is 'protocol=https:, host=example.com, pathname=/ttt, search=?ggg, origin=https://example.com'
PASS breakDownURL('ftp://example.com/ttt?ggg') is 'protocol=ftp:, host=example.com, pathname=/ttt, search=?ggg, origin=ftp://example.com'
PASS breakDownURL('file:///Users/darin') is 'protocol=file:, pathname=/Users/darin, origin=file://'
PASS breakDownURL('data:text/html,<b>foo</b>') is 'protocol=data:, pathname=text/html,<b>foo</b>, origin=null'
PASS breakDownURL('http://a:b@c:1/e/f?g%h') is 'protocol=http:, hostname=c, host=c:1, port=1, pathname=/e/f, search=?g%h, origin=http://c:1'
PASS breakDownURL('http://a:b@c:1/e/f?g%h') is 'protocol=http:, username=a, password=b, hostname=c, host=c:1, port=1, pathname=/e/f, search=?g%h, origin=http://c:1'
PASS breakDownURL('http://ex%61mple.com/') is 'protocol=http:, host=example.com, pathname=/, origin=http://example.com, toString=http://example.com/'
PASS breakDownURL('http://ex%2fmple.com/') is 'protocol=:, origin=null'
PASS breakDownURL('http://ex%2fmple.com/') is 'protocol=:'
PASS successfullyParsed is true

TEST COMPLETE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

var propertyNames = [
"protocol",
"username",
"password",
"hostname",
"host",
"port",
Expand Down Expand Up @@ -42,19 +44,19 @@
shouldBe("breakDownURL('about:blank')", "'protocol=about:, pathname=blank, origin=null'");
shouldBe("breakDownURL('http://example.com/')", "'protocol=http:, host=example.com, pathname=/, origin=http://example.com'");
shouldBe("breakDownURL('http://@example.com/')", "'protocol=http:, host=example.com, pathname=/, origin=http://example.com, toString=http://example.com/'");
shouldBe("breakDownURL('http://a@example.com/')", "'protocol=http:, host=example.com, pathname=/, origin=http://example.com'");
shouldBe("breakDownURL('http://a:@example.com/')", "'protocol=http:, host=example.com, pathname=/, origin=http://example.com, toString=http://a@example.com/'");
shouldBe("breakDownURL('http://joebob1:abc123@example.com/')", "'protocol=http:, host=example.com, pathname=/, origin=http://example.com'");
shouldBe("breakDownURL('http://:def456@example.com/')", "'protocol=http:, host=example.com, pathname=/, origin=http://example.com'");
shouldBe("breakDownURL('http://a@example.com/')", "'protocol=http:, username=a, host=example.com, pathname=/, origin=http://example.com'");
shouldBe("breakDownURL('http://a:@example.com/')", "'protocol=http:, username=a, host=example.com, pathname=/, origin=http://example.com, toString=http://a@example.com/'");
shouldBe("breakDownURL('http://joebob1:abc123@example.com/')", "'protocol=http:, username=joebob1, password=abc123, host=example.com, pathname=/, origin=http://example.com'");
shouldBe("breakDownURL('http://:def456@example.com/')", "'protocol=http:, password=def456, host=example.com, pathname=/, origin=http://example.com'");
shouldBe("breakDownURL('http://example.com/foo/bar')", "'protocol=http:, host=example.com, pathname=/foo/bar, origin=http://example.com'");
shouldBe("breakDownURL('HTTP://example.com/foo/bar')", "'protocol=http:, host=example.com, pathname=/foo/bar, origin=http://example.com, toString=http://example.com/foo/bar'");
shouldBe("breakDownURL('https://example.com/ttt?ggg')", "'protocol=https:, host=example.com, pathname=/ttt, search=?ggg, origin=https://example.com'");
shouldBe("breakDownURL('ftp://example.com/ttt?ggg')", "'protocol=ftp:, host=example.com, pathname=/ttt, search=?ggg, origin=ftp://example.com'");
shouldBe("breakDownURL('file:///Users/darin')", "'protocol=file:, pathname=/Users/darin, origin=file://'");
shouldBe("breakDownURL('data:text/html,<b>foo</b>')", "'protocol=data:, pathname=text/html,<b>foo</b>, origin=null'");
shouldBe("breakDownURL('http://a:b@c:1/e/f?g%h')", "'protocol=http:, hostname=c, host=c:1, port=1, pathname=/e/f, search=?g%h, origin=http://c:1'");
shouldBe("breakDownURL('http://a:b@c:1/e/f?g%h')", "'protocol=http:, username=a, password=b, hostname=c, host=c:1, port=1, pathname=/e/f, search=?g%h, origin=http://c:1'");

shouldBe("breakDownURL('http://ex%61mple.com/')", "'protocol=http:, host=example.com, pathname=/, origin=http://example.com, toString=http://example.com/'");
shouldBe("breakDownURL('http://ex%2fmple.com/')", "'protocol=:, origin=null'");
shouldBe("breakDownURL('http://ex%2fmple.com/')", "'protocol=:'");

</script>
5 changes: 4 additions & 1 deletion Source/WebCore/html/HTMLAnchorElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,10 @@ AtomString HTMLAnchorElement::target() const

String HTMLAnchorElement::origin() const
{
return SecurityOrigin::create(href()).get().toString();
auto url = href();
if (!url.isValid())
return emptyString();
return SecurityOrigin::create(url)->toString();
}

String HTMLAnchorElement::text()
Expand Down

0 comments on commit 0e09f4e

Please sign in to comment.