Skip to content

Truly independent web browser

LibWeb: Extract origins from SVG links

Andreas Kling9e7dd3872dc8
diff --git a/Libraries/LibWeb/SVG/SVGAElement.cpp b/Libraries/LibWeb/SVG/SVGAElement.cpp
index 4b7e1c4eafc..7ba94491c8f 100644
--- a/Libraries/LibWeb/SVG/SVGAElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGAElement.cpp
@@ -119,7 +119,7 @@ void SVGAElement::set_the_url()
m_url = {};
// 2. If this element's href content attribute is absent, then return.
- auto href = attribute(AttributeNames::href);
+ auto href = get_attribute_ns({}, AttributeNames::href);
if (!href.has_value())
href = get_attribute_ns(Namespace::XLink, AttributeNames::href);
if (!href.has_value())
diff --git a/Libraries/LibWeb/SVG/SVGAElement.h b/Libraries/LibWeb/SVG/SVGAElement.h
index b32834534bd..122f182b49f 100644
--- a/Libraries/LibWeb/SVG/SVGAElement.h
+++ b/Libraries/LibWeb/SVG/SVGAElement.h
@@ -27,6 +27,8 @@ public:
GC::Ref<DOM::DOMTokenList> rel_list();
+ virtual Optional<URL::Origin> extract_an_origin() const override { return hyperlink_element_utils_extract_an_origin(); }
+
virtual CSS::ElementBoxKind box_kind() const override;
private:
diff --git a/Tests/LibWeb/Text/expected/SVG/svg-link-origin-namespaces.txt b/Tests/LibWeb/Text/expected/SVG/svg-link-origin-namespaces.txt
new file mode 100644
index 00000000000..eef24a37226
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/svg-link-origin-namespaces.txt
@@ -0,0 +1,8 @@
+modern href only: true
+legacy href only: true
+foreign href only: TypeError
+foreign before legacy href: true
+foreign before modern href: true
+foreign after modern href: true
+empty modern href before legacy: true
+modern href overrides legacy: true
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/origin/api/origin-from-hyperlinkelementutils.window.txt b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/origin/api/origin-from-hyperlinkelementutils.window.txt
index 97bcf3fa719..27ac69377dd 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/origin/api/origin-from-hyperlinkelementutils.window.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/origin/api/origin-from-hyperlinkelementutils.window.txt
@@ -2,134 +2,134 @@ Harness status: OK
Found 129 tests
-54 Pass
-75 Fail
+104 Pass
+25 Fail
Pass Origin.from(<a>) throws.
Pass Origin.from(<area>) throws.
Pass Origin.from(SVG <a>) throws.
Pass Origin.from(MathML <a>) throws.
Pass Origin.from(<a href="about:blank">) returns an opaque origin.
Pass Origin.from(<area href="about:blank">) returns an opaque origin.
-Fail Origin.from(SVG <a href="about:blank">) returns an opaque origin.
-Fail Origin.from(SVG <a xlink:href="about:blank">) returns an opaque origin.
+Pass Origin.from(SVG <a href="about:blank">) returns an opaque origin.
+Pass Origin.from(SVG <a xlink:href="about:blank">) returns an opaque origin.
Fail Origin.from(MathML <a href="about:blank">) returns an opaque origin.
Pass Origin.from(<a href="data:text/plain,opaque">) returns an opaque origin.
Pass Origin.from(<area href="data:text/plain,opaque">) returns an opaque origin.
-Fail Origin.from(SVG <a href="data:text/plain,opaque">) returns an opaque origin.
-Fail Origin.from(SVG <a xlink:href="data:text/plain,opaque">) returns an opaque origin.
+Pass Origin.from(SVG <a href="data:text/plain,opaque">) returns an opaque origin.
+Pass Origin.from(SVG <a xlink:href="data:text/plain,opaque">) returns an opaque origin.
Fail Origin.from(MathML <a href="data:text/plain,opaque">) returns an opaque origin.
Pass Origin.from(<a href="weird-protocol:whatever">) returns an opaque origin.
Pass Origin.from(<area href="weird-protocol:whatever">) returns an opaque origin.
-Fail Origin.from(SVG <a href="weird-protocol:whatever">) returns an opaque origin.
-Fail Origin.from(SVG <a xlink:href="weird-protocol:whatever">) returns an opaque origin.
+Pass Origin.from(SVG <a href="weird-protocol:whatever">) returns an opaque origin.
+Pass Origin.from(SVG <a xlink:href="weird-protocol:whatever">) returns an opaque origin.
Fail Origin.from(MathML <a href="weird-protocol:whatever">) returns an opaque origin.
Pass Origin.from(<a href="weird-hierarchical-protocol://host/path?etc">) returns an opaque origin.
Pass Origin.from(<area href="weird-hierarchical-protocol://host/path?etc">) returns an opaque origin.
-Fail Origin.from(SVG <a href="weird-hierarchical-protocol://host/path?etc">) returns an opaque origin.
-Fail Origin.from(SVG <a xlink:href="weird-hierarchical-protocol://host/path?etc">) returns an opaque origin.
+Pass Origin.from(SVG <a href="weird-hierarchical-protocol://host/path?etc">) returns an opaque origin.
+Pass Origin.from(SVG <a xlink:href="weird-hierarchical-protocol://host/path?etc">) returns an opaque origin.
Fail Origin.from(MathML <a href="weird-hierarchical-protocol://host/path?etc">) returns an opaque origin.
Pass Origin.from(<a href="blob:weird-protocol:whatever">) returns an opaque origin.
Pass Origin.from(<area href="blob:weird-protocol:whatever">) returns an opaque origin.
-Fail Origin.from(SVG <a href="blob:weird-protocol:whatever">) returns an opaque origin.
-Fail Origin.from(SVG <a xlink:href="blob:weird-protocol:whatever">) returns an opaque origin.
+Pass Origin.from(SVG <a href="blob:weird-protocol:whatever">) returns an opaque origin.
+Pass Origin.from(SVG <a xlink:href="blob:weird-protocol:whatever">) returns an opaque origin.
Fail Origin.from(MathML <a href="blob:weird-protocol:whatever">) returns an opaque origin.
Pass Origin.from(<a href="blob:weird-hierarchical-protocol://host/path?etc">) returns an opaque origin.
Pass Origin.from(<area href="blob:weird-hierarchical-protocol://host/path?etc">) returns an opaque origin.
-Fail Origin.from(SVG <a href="blob:weird-hierarchical-protocol://host/path?etc">) returns an opaque origin.
-Fail Origin.from(SVG <a xlink:href="blob:weird-hierarchical-protocol://host/path?etc">) returns an opaque origin.
+Pass Origin.from(SVG <a href="blob:weird-hierarchical-protocol://host/path?etc">) returns an opaque origin.
+Pass Origin.from(SVG <a xlink:href="blob:weird-hierarchical-protocol://host/path?etc">) returns an opaque origin.
Fail Origin.from(MathML <a href="blob:weird-hierarchical-protocol://host/path?etc">) returns an opaque origin.
Pass Origin.from(<a href="http://site.example">) returns a tuple origin.
Pass Origin.from(<area href="http://site.example">) returns a tuple origin.
-Fail Origin.from(SVG <a href="http://site.example">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="http://site.example">) returns a tuple origin.
+Pass Origin.from(SVG <a href="http://site.example">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="http://site.example">) returns a tuple origin.
Fail Origin.from(MathML <a href="http://site.example">) returns a tuple origin.
Pass Origin.from(<a href="https://site.example">) returns a tuple origin.
Pass Origin.from(<area href="https://site.example">) returns a tuple origin.
-Fail Origin.from(SVG <a href="https://site.example">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="https://site.example">) returns a tuple origin.
+Pass Origin.from(SVG <a href="https://site.example">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="https://site.example">) returns a tuple origin.
Fail Origin.from(MathML <a href="https://site.example">) returns a tuple origin.
Pass Origin.from(<a href="https://site.example:123">) returns a tuple origin.
Pass Origin.from(<area href="https://site.example:123">) returns a tuple origin.
-Fail Origin.from(SVG <a href="https://site.example:123">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="https://site.example:123">) returns a tuple origin.
+Pass Origin.from(SVG <a href="https://site.example:123">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="https://site.example:123">) returns a tuple origin.
Fail Origin.from(MathML <a href="https://site.example:123">) returns a tuple origin.
Pass Origin.from(<a href="http://sub.site.example">) returns a tuple origin.
Pass Origin.from(<area href="http://sub.site.example">) returns a tuple origin.
-Fail Origin.from(SVG <a href="http://sub.site.example">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="http://sub.site.example">) returns a tuple origin.
+Pass Origin.from(SVG <a href="http://sub.site.example">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="http://sub.site.example">) returns a tuple origin.
Fail Origin.from(MathML <a href="http://sub.site.example">) returns a tuple origin.
Pass Origin.from(<a href="https://sub.site.example">) returns a tuple origin.
Pass Origin.from(<area href="https://sub.site.example">) returns a tuple origin.
-Fail Origin.from(SVG <a href="https://sub.site.example">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="https://sub.site.example">) returns a tuple origin.
+Pass Origin.from(SVG <a href="https://sub.site.example">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="https://sub.site.example">) returns a tuple origin.
Fail Origin.from(MathML <a href="https://sub.site.example">) returns a tuple origin.
Pass Origin.from(<a href="https://sub.site.example:123">) returns a tuple origin.
Pass Origin.from(<area href="https://sub.site.example:123">) returns a tuple origin.
-Fail Origin.from(SVG <a href="https://sub.site.example:123">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="https://sub.site.example:123">) returns a tuple origin.
+Pass Origin.from(SVG <a href="https://sub.site.example:123">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="https://sub.site.example:123">) returns a tuple origin.
Fail Origin.from(MathML <a href="https://sub.site.example:123">) returns a tuple origin.
Pass Origin.from(<a href="https://xn--mlauted-m2a.example">) returns a tuple origin.
Pass Origin.from(<area href="https://xn--mlauted-m2a.example">) returns a tuple origin.
-Fail Origin.from(SVG <a href="https://xn--mlauted-m2a.example">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="https://xn--mlauted-m2a.example">) returns a tuple origin.
+Pass Origin.from(SVG <a href="https://xn--mlauted-m2a.example">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="https://xn--mlauted-m2a.example">) returns a tuple origin.
Fail Origin.from(MathML <a href="https://xn--mlauted-m2a.example">) returns a tuple origin.
Pass Origin.from(<a href="ftp://ftp.example">) returns a tuple origin.
Pass Origin.from(<area href="ftp://ftp.example">) returns a tuple origin.
-Fail Origin.from(SVG <a href="ftp://ftp.example">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="ftp://ftp.example">) returns a tuple origin.
+Pass Origin.from(SVG <a href="ftp://ftp.example">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="ftp://ftp.example">) returns a tuple origin.
Fail Origin.from(MathML <a href="ftp://ftp.example">) returns a tuple origin.
Pass Origin.from(<a href="ws://ws.example">) returns a tuple origin.
Pass Origin.from(<area href="ws://ws.example">) returns a tuple origin.
-Fail Origin.from(SVG <a href="ws://ws.example">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="ws://ws.example">) returns a tuple origin.
+Pass Origin.from(SVG <a href="ws://ws.example">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="ws://ws.example">) returns a tuple origin.
Fail Origin.from(MathML <a href="ws://ws.example">) returns a tuple origin.
Pass Origin.from(<a href="wss://wss.example">) returns a tuple origin.
Pass Origin.from(<area href="wss://wss.example">) returns a tuple origin.
-Fail Origin.from(SVG <a href="wss://wss.example">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="wss://wss.example">) returns a tuple origin.
+Pass Origin.from(SVG <a href="wss://wss.example">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="wss://wss.example">) returns a tuple origin.
Fail Origin.from(MathML <a href="wss://wss.example">) returns a tuple origin.
Pass Origin.from(<a href="https://trailing.slash/">) returns a tuple origin.
Pass Origin.from(<area href="https://trailing.slash/">) returns a tuple origin.
-Fail Origin.from(SVG <a href="https://trailing.slash/">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="https://trailing.slash/">) returns a tuple origin.
+Pass Origin.from(SVG <a href="https://trailing.slash/">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="https://trailing.slash/">) returns a tuple origin.
Fail Origin.from(MathML <a href="https://trailing.slash/">) returns a tuple origin.
Pass Origin.from(<a href="https://user:[email protected]">) returns a tuple origin.
Pass Origin.from(<area href="https://user:[email protected]">) returns a tuple origin.
-Fail Origin.from(SVG <a href="https://user:[email protected]">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="https://user:[email protected]">) returns a tuple origin.
+Pass Origin.from(SVG <a href="https://user:[email protected]">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="https://user:[email protected]">) returns a tuple origin.
Fail Origin.from(MathML <a href="https://user:[email protected]">) returns a tuple origin.
Pass Origin.from(<a href="https://has.a.port:1234/and/path">) returns a tuple origin.
Pass Origin.from(<area href="https://has.a.port:1234/and/path">) returns a tuple origin.
-Fail Origin.from(SVG <a href="https://has.a.port:1234/and/path">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="https://has.a.port:1234/and/path">) returns a tuple origin.
+Pass Origin.from(SVG <a href="https://has.a.port:1234/and/path">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="https://has.a.port:1234/and/path">) returns a tuple origin.
Fail Origin.from(MathML <a href="https://has.a.port:1234/and/path">) returns a tuple origin.
Pass Origin.from(<a href="https://ümlauted.example">) returns a tuple origin.
Pass Origin.from(<area href="https://ümlauted.example">) returns a tuple origin.
-Fail Origin.from(SVG <a href="https://ümlauted.example">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="https://ümlauted.example">) returns a tuple origin.
+Pass Origin.from(SVG <a href="https://ümlauted.example">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="https://ümlauted.example">) returns a tuple origin.
Fail Origin.from(MathML <a href="https://ümlauted.example">) returns a tuple origin.
Pass Origin.from(<a href="blob:https://example.com/some-guid">) returns a tuple origin.
Pass Origin.from(<area href="blob:https://example.com/some-guid">) returns a tuple origin.
-Fail Origin.from(SVG <a href="blob:https://example.com/some-guid">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="blob:https://example.com/some-guid">) returns a tuple origin.
+Pass Origin.from(SVG <a href="blob:https://example.com/some-guid">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="blob:https://example.com/some-guid">) returns a tuple origin.
Fail Origin.from(MathML <a href="blob:https://example.com/some-guid">) returns a tuple origin.
Pass Origin.from(<a href="ftp://example.com/">) returns a tuple origin.
Pass Origin.from(<area href="ftp://example.com/">) returns a tuple origin.
-Fail Origin.from(SVG <a href="ftp://example.com/">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="ftp://example.com/">) returns a tuple origin.
+Pass Origin.from(SVG <a href="ftp://example.com/">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="ftp://example.com/">) returns a tuple origin.
Fail Origin.from(MathML <a href="ftp://example.com/">) returns a tuple origin.
Pass Origin.from(<a href="https://example.com/path?query#fragment">) returns a tuple origin.
Pass Origin.from(<area href="https://example.com/path?query#fragment">) returns a tuple origin.
-Fail Origin.from(SVG <a href="https://example.com/path?query#fragment">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="https://example.com/path?query#fragment">) returns a tuple origin.
+Pass Origin.from(SVG <a href="https://example.com/path?query#fragment">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="https://example.com/path?query#fragment">) returns a tuple origin.
Fail Origin.from(MathML <a href="https://example.com/path?query#fragment">) returns a tuple origin.
Pass Origin.from(<a href="https://127.0.0.1/">) returns a tuple origin.
Pass Origin.from(<area href="https://127.0.0.1/">) returns a tuple origin.
-Fail Origin.from(SVG <a href="https://127.0.0.1/">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="https://127.0.0.1/">) returns a tuple origin.
+Pass Origin.from(SVG <a href="https://127.0.0.1/">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="https://127.0.0.1/">) returns a tuple origin.
Fail Origin.from(MathML <a href="https://127.0.0.1/">) returns a tuple origin.
Pass Origin.from(<a href="https://[::1]/">) returns a tuple origin.
Pass Origin.from(<area href="https://[::1]/">) returns a tuple origin.
-Fail Origin.from(SVG <a href="https://[::1]/">) returns a tuple origin.
-Fail Origin.from(SVG <a xlink:href="https://[::1]/">) returns a tuple origin.
+Pass Origin.from(SVG <a href="https://[::1]/">) returns a tuple origin.
+Pass Origin.from(SVG <a xlink:href="https://[::1]/">) returns a tuple origin.
Fail Origin.from(MathML <a href="https://[::1]/">) returns a tuple origin.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/SVG/svg-link-origin-namespaces.html b/Tests/LibWeb/Text/input/SVG/svg-link-origin-namespaces.html
new file mode 100644
index 00000000000..0feac26acb2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/svg-link-origin-namespaces.html
@@ -0,0 +1,70 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const base = document.createElement("base");
+ base.href = "https://base.example/path/";
+ document.head.appendChild(base);
+ const foreign = "urn:foreign";
+ const xlink = "http://www.w3.org/1999/xlink";
+ const cases = [
+ ["modern href only", [[null, "href", "https://modern.example/"]], "https://modern.example/"],
+ ["legacy href only", [[xlink, "xlink:href", "https://legacy.example/"]], "https://legacy.example/"],
+ ["foreign href only", [[foreign, "href", "https://foreign.example/"]], null],
+ [
+ "foreign before legacy href",
+ [
+ [foreign, "href", "https://foreign.example/"],
+ [xlink, "xlink:href", "https://legacy.example/"],
+ ],
+ "https://legacy.example/",
+ ],
+ [
+ "foreign before modern href",
+ [
+ [foreign, "href", "https://foreign.example/"],
+ [null, "href", "https://modern.example/"],
+ ],
+ "https://modern.example/",
+ ],
+ [
+ "foreign after modern href",
+ [
+ [null, "href", "https://modern.example/"],
+ [foreign, "href", "https://foreign.example/"],
+ ],
+ "https://modern.example/",
+ ],
+ [
+ "empty modern href before legacy",
+ [
+ [foreign, "href", "https://foreign.example/"],
+ [null, "href", ""],
+ [xlink, "xlink:href", "https://legacy.example/"],
+ ],
+ "https://base.example/",
+ ],
+ [
+ "modern href overrides legacy",
+ [
+ [xlink, "xlink:href", "https://legacy.example/"],
+ [null, "href", "https://modern.example/"],
+ ],
+ "https://modern.example/",
+ ],
+ ];
+ for (const [label, attributes, expectedURL] of cases) {
+ const link = document.createElementNS("http://www.w3.org/2000/svg", "a");
+ for (const [namespace, name, value] of attributes) link.setAttributeNS(namespace, name, value);
+ try {
+ const origin = Origin.from(link);
+ println(
+ `${label}: ${expectedURL === null ? "accepted" : origin.isSameOrigin(Origin.from(expectedURL))}`
+ );
+ } catch (error) {
+ println(`${label}: ${error.name}`);
+ }
+ }
+ base.remove();
+ });
+</script>