Truly independent web browser
LibWeb: Follow hyperlinks created by MathML `<a>` elements
Tim Ledbetter086d7afac204
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cppindex 15be1d6bbdf..7b32f7d5f4b 100644--- a/Libraries/LibWeb/DOM/Element.cpp+++ b/Libraries/LibWeb/DOM/Element.cpp@@ -613,7 +613,7 @@ bool Element::cannot_navigate() constreturn true;// - element is not an a element and is not connected.- return !(is_html_anchor_element() || is_svg_a_element()) && !is_connected();+ return !(is_html_anchor_element() || is_svg_a_element() || is_mathml_anchor_element()) && !is_connected();}// https://html.spec.whatwg.org/multipage/links.html#links-created-by-a-and-area-elements@@ -622,6 +622,8 @@ HTML::HTMLHyperlinkElementUtils const* Element::created_hyperlink() constHTML::HTMLHyperlinkElementUtils const* hyperlink_element = as_if<HTML::HTMLAnchorElement>(*this);if (!hyperlink_element)hyperlink_element = as_if<HTML::HTMLAreaElement>(*this);+ if (!hyperlink_element)+ hyperlink_element = as_if<MathML::MathMLAnchorElement>(*this);if (!hyperlink_element)return nullptr;@@ -648,7 +650,7 @@ void Element::follow_the_hyperlink(Optional<Utf16String> hyperlink_suffix, HTML:Utf16String target_attribute_value;// 3. If subject is an a or area element, then set targetAttributeValue to the result of getting an element's target given subject.- if (is_html_anchor_element() || is_html_area_element() || is_svg_a_element())+ if (is_html_anchor_element() || is_html_area_element() || is_svg_a_element() || is_mathml_anchor_element())target_attribute_value = get_an_elements_target();// 4. Let urlRecord be the result of encoding-parsing a URL given subject's href attribute value, relative to subject's node document.diff --git a/Libraries/LibWeb/MathML/MathMLAnchorElement.cpp b/Libraries/LibWeb/MathML/MathMLAnchorElement.cppindex c6a62b15132..80bcc228550 100644--- a/Libraries/LibWeb/MathML/MathMLAnchorElement.cpp+++ b/Libraries/LibWeb/MathML/MathMLAnchorElement.cpp@@ -5,6 +5,7 @@*/#include <LibWeb/Bindings/MathMLAnchorElement.h>+#include <LibWeb/DOM/Event.h>#include <LibWeb/MathML/AttributeNames.h>#include <LibWeb/MathML/MathMLAnchorElement.h>@@ -27,6 +28,19 @@ void MathMLAnchorElement::attribute_changed(Utf16FlyString const& name, Optionalhref_content_attribute_changed();}+bool MathMLAnchorElement::has_activation_behavior() const+{+ return creates_a_hyperlink();+}++void MathMLAnchorElement::activation_behavior(DOM::Event const& event)+{+ if (event.is_trusted() && is_editable_or_editing_host())+ return;++ activate_the_hyperlink(event);+}+bool MathMLAnchorElement::is_focusable() const{return (Base::is_focusable() || has_attribute(AttributeNames::href)) && meets_focusable_area_rendering_requirements();diff --git a/Libraries/LibWeb/MathML/MathMLAnchorElement.h b/Libraries/LibWeb/MathML/MathMLAnchorElement.hindex c2f6a650dcd..9cf5e89a3a3 100644--- a/Libraries/LibWeb/MathML/MathMLAnchorElement.h+++ b/Libraries/LibWeb/MathML/MathMLAnchorElement.h@@ -30,6 +30,10 @@ public:private:MathMLAnchorElement(DOM::Document&, DOM::QualifiedName);+ // ^DOM::EventTarget+ virtual bool has_activation_behavior() const override;+ virtual void activation_behavior(DOM::Event const&) override;+// ^DOM::Elementvirtual void attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_) override;virtual i32 default_tab_index_value() const override;diff --git a/Tests/LibWeb/Ref/expected/wpt-import/mathml/relations/html5-tree/href-click-001-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/mathml/relations/html5-tree/href-click-001-ref.htmlnew file mode 100644index 00000000000..86952567c76--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/mathml/relations/html5-tree/href-click-001-ref.html@@ -0,0 +1,15 @@+<!DOCTYPE html>+<html>+<head>+<meta charset="utf-8"/>+<title>href click (reference)</title>+</head>+<body>++ <p>This test passes if you see a green square.</p>++ <div style="width: 150px; height: 150px; background: green">+ </div>++</body>+</html>diff --git a/Tests/LibWeb/Ref/expected/wpt-import/mathml/relations/html5-tree/href-click-002-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/mathml/relations/html5-tree/href-click-002-ref.htmlnew file mode 100644index 00000000000..86952567c76--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/mathml/relations/html5-tree/href-click-002-ref.html@@ -0,0 +1,15 @@+<!DOCTYPE html>+<html>+<head>+<meta charset="utf-8"/>+<title>href click (reference)</title>+</head>+<body>++ <p>This test passes if you see a green square.</p>++ <div style="width: 150px; height: 150px; background: green">+ </div>++</body>+</html>diff --git a/Tests/LibWeb/Ref/input/wpt-import/mathml/relations/html5-tree/href-click-001.html b/Tests/LibWeb/Ref/input/wpt-import/mathml/relations/html5-tree/href-click-001.htmlnew file mode 100644index 00000000000..9849aa49cfe--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/mathml/relations/html5-tree/href-click-001.html@@ -0,0 +1,47 @@+<!DOCTYPE html>+<html class="reftest-wait">+<head>+<meta charset="utf-8"/>+<title>href click</title>+<link rel="help" href="https://w3c.github.io/mathml-core/#attributes-common-to-html-and-mathml-elements">+<link rel="help" href="https://w3c.github.io/mathml-core/#dom-and-javascript">+<link rel="match" href="../../../../../expected/wpt-import/mathml/relations/html5-tree/href-click-001-ref.html"/>+<meta name="assert" content="Verify that a click on a link moves to the target.">+<script type="text/javascript">+ function test()+ {+ // Click the <a href=> tag, it should scroll to the green target.+ var event = new MouseEvent('click', {bubbles: true, cancelable: true});+ document.getElementById('link').dispatchEvent(event);++ // Click the <mrow href=> tag, it should not scroll to the target.+ var event2 = new MouseEvent('click', {bubbles: true, cancelable: true});+ document.getElementById('notALink').dispatchEvent(event2);++ document.documentElement.className = "";+ }+</script>+</head>+<body onload="test()">++ <p>This test passes if you see a green square.</p>++ <div style="width: 150px; height: 150px; overflow: hidden">+ <math>+ <a id="link" href="#target">+ <mspace id="space" width="150px" height="150px" style="background: red"/>+ </a>+ <mrow id="notALink" href="#badTarget"></mrow>+ </math>+ <div style="height: 500px;"></div>+ <math id="target">+ <mspace width="150px" height="154px" style="background: green"/>+ </math>+ <div style="height: 500px;"></div>+ <math id="badTarget">+ <mspace width="150px" height="154px" style="background: red"/>+ </math>+ </div>++</body>+</html>diff --git a/Tests/LibWeb/Ref/input/wpt-import/mathml/relations/html5-tree/href-click-002.html b/Tests/LibWeb/Ref/input/wpt-import/mathml/relations/html5-tree/href-click-002.htmlnew file mode 100644index 00000000000..a7dbe398d4e--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/mathml/relations/html5-tree/href-click-002.html@@ -0,0 +1,46 @@+<!DOCTYPE html>+<html class="reftest-wait">+<head>+<meta charset="utf-8"/>+<title>href click</title>+<link rel="help" href="https://w3c.github.io/mathml-core/#attributes-common-to-html-and-mathml-elements">+<link rel="help" href="https://w3c.github.io/mathml-core/#dom-and-javascript">+<link rel="match" href="../../../../../expected/wpt-import/mathml/relations/html5-tree/href-click-002-ref.html"/>+<meta name="assert" content="Verify that a click on an element bubbles to an ancestor link.">+<script type="text/javascript">+ function test()+ {+ var event = new MouseEvent('click', {bubbles: true, cancelable: true});+ document.getElementById('space').dispatchEvent(event);+ document.documentElement.className = "";+ }+</script>+</head>+<body onload="test()">++ <p>This test passes if you see a green square.</p>++ <div style="width: 150px; height: 150px; overflow: hidden">+ <math>+ <a href="#target">+ <mrow>+ <mrow href="#badTarget">+ <mrow>+ <mspace id="space" width="150px" height="150px" style="background: red"/>+ </mrow>+ </mrow>+ </mrow>+ </a>+ </math>+ <div style="height: 500px;"></div>+ <math id="target">+ <mspace width="150px" height="154px" style="background: green"/>+ </math>+ <div style="height: 500px;"></div>+ <math id="badTarget">+ <mspace width="150px" height="154px" style="background: red"/>+ </math>+ </div>++</body>+</html>diff --git a/Tests/LibWeb/Text/expected/wpt-import/mathml/relations/html5-tree/dynamic-href-002.txt b/Tests/LibWeb/Text/expected/wpt-import/mathml/relations/html5-tree/dynamic-href-002.txtnew file mode 100644index 00000000000..01de780c6da--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/mathml/relations/html5-tree/dynamic-href-002.txt@@ -0,0 +1,6 @@+Harness status: OK++Found 1 tests++1 Pass+Pass Click element with href\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/mathml/relations/html5-tree/href-click-003.txt b/Tests/LibWeb/Text/expected/wpt-import/mathml/relations/html5-tree/href-click-003.txtnew file mode 100644index 00000000000..01de780c6da--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/mathml/relations/html5-tree/href-click-003.txt@@ -0,0 +1,6 @@+Harness status: OK++Found 1 tests++1 Pass+Pass Click element with href\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/wpt-import/mathml/relations/html5-tree/dynamic-href-002.html b/Tests/LibWeb/Text/input/wpt-import/mathml/relations/html5-tree/dynamic-href-002.htmlnew file mode 100644index 00000000000..13b8b5e0d39--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/mathml/relations/html5-tree/dynamic-href-002.html@@ -0,0 +1,46 @@+<!DOCTYPE html>+<html>+<head>+<meta charset="utf-8">+<title>Test dynamic change of the href attribute</title>+<link rel="help" href="https://w3c.github.io/mathml-core/#attributes-common-to-html-and-mathml-elements">+<link rel="help" href="https://w3c.github.io/mathml-core/#dom-and-javascript">+<link rel="help" href="https://github.com/w3c/mathml-core/issues/142">+<script src="../../../resources/testharness.js"></script>+<script src="../../../resources/testharnessreport.js"></script>+<script src="../../../resources/testdriver.js"></script>+<script src="../../../resources/testdriver-vendor.js"></script>+</head>+<body>+ <p>To test manually, click the blue rectangle.</p>+ <p>+ <math>+ <mspace width="50px" height="10px" style="background: gray"></mspace>+ <a id="target" href="javascript:badHandler()" style="background: blue">+ <mspace width="50px" height="10px"></mspace>+ </a>+ <mspace width="50px" height="10px" style="background: gray"></mspace>+ </math>+ </p>+ <a id="badTarget" href="javascript:badHandler()">DON'T CLICK ME</a>+ <script>+ var t = async_test("Click element with href");+ function handler() { t.done(); }+ function badHandler() {+ t.step(() => { assert_unreached("Bad handler executed"); });+ t.done();+ }++ // Modify the href attribute to execute the 'good' handler.+ let target = document.getElementById("target");+ target.setAttribute("href", "javascript:handler()");++ test_driver.click(target).then(() => {+ return test_driver.click(document.getElementById("badTarget"));+ }).catch(() => {+ t.step(() => { assert_unreached("Click failed"); });+ t.done();+ });+ </script>+</body>+</html>diff --git a/Tests/LibWeb/Text/input/wpt-import/mathml/relations/html5-tree/href-click-003.html b/Tests/LibWeb/Text/input/wpt-import/mathml/relations/html5-tree/href-click-003.htmlnew file mode 100644index 00000000000..711dfc88819--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/mathml/relations/html5-tree/href-click-003.html@@ -0,0 +1,38 @@+<!DOCTYPE html>+<html>+<head>+<meta charset="utf-8">+<title>href click</title>+<link rel="help" href="https://w3c.github.io/mathml-core/#attributes-common-to-html-and-mathml-elements">+<link rel="help" href="https://w3c.github.io/mathml-core/#dom-and-javascript">+<script src="../../../resources/testharness.js"></script>+<script src="../../../resources/testharnessreport.js"></script>+<script src="../../../resources/testdriver.js"></script>+<script src="../../../resources/testdriver-vendor.js"></script>+</head>+<body>+ <p>To test manually, click the blue rectangle.</p>+ <p>+ <math>+ <mspace width="50px" height="10px" style="background: gray"></mspace>+ <a id="target" href="javascript:handler()"><mspace width="50px" height="10px" style="background: blue"></mspace></a>+ <mspace width="50px" height="10px" style="background: gray"></mspace>+ </math>+ </p>+ <a id="badTarget" href="javascript:badHandler()">DON'T CLICK ME</a>+ <script>+ var t = async_test("Click element with href");+ function handler() { t.done(); }+ function badHandler() {+ t.step(() => { assert_unreached("Bad handler executed"); });+ t.done();+ }+ test_driver.click(document.getElementById("target")).then(() => {+ return test_driver.click(document.getElementById("badTarget"));+ }).catch(() => {+ t.step(() => { assert_unreached("Click failed"); });+ t.done();+ });+ </script>+</body>+</html>