Skip to content

Truly independent web browser

LibWeb: Match link pseudo-classes on MathML `<a>` elements

Tim Ledbetter0c983f97f9b1
diff --git a/Libraries/LibWeb/CSS/SelectorMatching.cpp b/Libraries/LibWeb/CSS/SelectorMatching.cpp
index 176e6539a5c..09ac1247b84 100644
--- a/Libraries/LibWeb/CSS/SelectorMatching.cpp
+++ b/Libraries/LibWeb/CSS/SelectorMatching.cpp
@@ -26,6 +26,7 @@
#include <LibWeb/HTML/HTMLProgressElement.h>
#include <LibWeb/HTML/HTMLSelectElement.h>
#include <LibWeb/HTML/HTMLTextAreaElement.h>
+#include <LibWeb/MathML/MathMLAnchorElement.h>
#include <LibWeb/SVG/SVGAElement.h>
namespace Web::SelectorMatching {
@@ -200,7 +201,7 @@ ElementStateQuestions element_state_questions(DOM::Element const& element)
|| is<HTML::HTMLDialogElement>(element)) {
questions |= ElementStateQuestions::Control;
}
- if (is<HTML::HTMLAnchorElement>(element) || is<HTML::HTMLAreaElement>(element) || is<SVG::SVGAElement>(element))
+ if (is<HTML::HTMLAnchorElement>(element) || is<HTML::HTMLAreaElement>(element) || is<SVG::SVGAElement>(element) || is<MathML::MathMLAnchorElement>(element))
questions |= ElementStateQuestions::Link;
if (is<HTML::HTMLMediaElement>(element))
questions |= ElementStateQuestions::Media;
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index b061c41ab29..15be1d6bbdf 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -149,6 +149,7 @@
#include <LibWeb/Layout/TreeBuilder.h>
#include <LibWeb/Layout/Viewport.h>
#include <LibWeb/Loader/ContentBlocker.h>
+#include <LibWeb/MathML/MathMLAnchorElement.h>
#include <LibWeb/MathML/MathMLElement.h>
#include <LibWeb/MathML/TagNames.h>
#include <LibWeb/Namespace.h>
@@ -3371,7 +3372,7 @@ bool Element::matches_placeholder_shown_pseudo_class() const
bool Element::matches_link_pseudo_class() const
{
// All a elements that have an href attribute, and all area elements that have an href attribute, must match one of :link and :visited.
- if (!is<HTML::HTMLAnchorElement>(*this) && !is<HTML::HTMLAreaElement>(*this) && !is<SVG::SVGAElement>(*this))
+ if (!is<HTML::HTMLAnchorElement>(*this) && !is<HTML::HTMLAreaElement>(*this) && !is<SVG::SVGAElement>(*this) && !is<MathML::MathMLAnchorElement>(*this))
return false;
return has_attribute(HTML::AttributeNames::href);
}
diff --git a/Libraries/LibWeb/DOM/Node.h b/Libraries/LibWeb/DOM/Node.h
index 8d035a498ea..ce994f098b5 100644
--- a/Libraries/LibWeb/DOM/Node.h
+++ b/Libraries/LibWeb/DOM/Node.h
@@ -215,6 +215,7 @@ public:
virtual bool is_svg_text_path_element() const { return false; }
virtual bool is_svg_geometry_element() const { return false; }
virtual bool is_svg_filter_element() const { return false; }
+ virtual bool is_mathml_anchor_element() const { return false; }
bool in_a_document_tree() const;
diff --git a/Libraries/LibWeb/MathML/Default.css b/Libraries/LibWeb/MathML/Default.css
index 75a654be5a6..1672d7f7da6 100644
--- a/Libraries/LibWeb/MathML/Default.css
+++ b/Libraries/LibWeb/MathML/Default.css
@@ -33,6 +33,11 @@ math[display="inline" i] {
math-style: compact;
}
+/* AD-HOC: Do not underline links, matching other engines. */
+a:any-link {
+ text-decoration: none;
+}
+
/* <mrow>-like elements */
semantics > :not(:first-child) {
display: none;
diff --git a/Libraries/LibWeb/MathML/MathMLAnchorElement.cpp b/Libraries/LibWeb/MathML/MathMLAnchorElement.cpp
index 97736dbb0d7..410260adf65 100644
--- a/Libraries/LibWeb/MathML/MathMLAnchorElement.cpp
+++ b/Libraries/LibWeb/MathML/MathMLAnchorElement.cpp
@@ -5,6 +5,7 @@
*/
#include <LibWeb/Bindings/MathMLAnchorElement.h>
+#include <LibWeb/MathML/AttributeNames.h>
#include <LibWeb/MathML/MathMLAnchorElement.h>
namespace Web::MathML {
@@ -18,4 +19,12 @@ MathMLAnchorElement::MathMLAnchorElement(DOM::Document& document, DOM::Qualified
MathMLAnchorElement::~MathMLAnchorElement() = default;
+void MathMLAnchorElement::attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_)
+{
+ Base::attribute_changed(name, old_value, value, namespace_);
+
+ if (name == AttributeNames::href && !namespace_.has_value())
+ href_content_attribute_changed();
+}
+
}
diff --git a/Libraries/LibWeb/MathML/MathMLAnchorElement.h b/Libraries/LibWeb/MathML/MathMLAnchorElement.h
index 9df30d624e4..c1b219fa3ec 100644
--- a/Libraries/LibWeb/MathML/MathMLAnchorElement.h
+++ b/Libraries/LibWeb/MathML/MathMLAnchorElement.h
@@ -22,12 +22,24 @@ class MathMLAnchorElement final
public:
virtual ~MathMLAnchorElement() override;
+ virtual bool is_mathml_anchor_element() const override { return true; }
+
private:
MathMLAnchorElement(DOM::Document&, DOM::QualifiedName);
+ // ^DOM::Element
+ virtual void attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_) override;
+
// ^HTML::HTMLHyperlinkElementUtils
virtual DOM::Element& hyperlink_element_utils_element() override { return *this; }
virtual DOM::Element const& hyperlink_element_utils_element() const override { return *this; }
};
}
+
+namespace Web::DOM {
+
+template<>
+inline bool Node::fast_is<MathML::MathMLAnchorElement>() const { return is_mathml_anchor_element(); }
+
+}
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/mathml/relations/html5-tree/link-color-001-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/mathml/relations/html5-tree/link-color-001-ref.html
new file mode 100644
index 00000000000..772fda7002f
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/mathml/relations/html5-tree/link-color-001-ref.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Test link on mtext (reference)</title>
+</head>
+<body>
+ <math xmlns="http://www.w3.org/1998/Math/MathML">
+ <mtext>MathML Link</mtext>
+ </math>
+</body>
+</html>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/mathml/relations/html5-tree/link-color-001.html b/Tests/LibWeb/Ref/input/wpt-import/mathml/relations/html5-tree/link-color-001.html
new file mode 100644
index 00000000000..f81eac23f85
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/mathml/relations/html5-tree/link-color-001.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Test link on a</title>
+ <link rel="mismatch" href="../../../../../expected/wpt-import/mathml/relations/html5-tree/link-color-001-ref.html"/>
+</head>
+<body>
+ <math xmlns="http://www.w3.org/1998/Math/MathML">
+ <a href="www.mozilla.org"><mtext>MathML Link</mtext></a>
+ </math>
+</body>
+</html>
diff --git a/Tests/LibWeb/Text/expected/wpt-import/mathml/relations/html5-tree/anchor-visited-privacy.txt b/Tests/LibWeb/Text/expected/wpt-import/mathml/relations/html5-tree/anchor-visited-privacy.txt
new file mode 100644
index 00000000000..7bb15b9b1ef
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/mathml/relations/html5-tree/anchor-visited-privacy.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass MathML 'a' element with :visited selector must return the unvisited (:any-link) style to JavaScript API.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/mathml/relations/html5-tree/canvas-visited-privacy.txt b/Tests/LibWeb/Text/expected/wpt-import/mathml/relations/html5-tree/canvas-visited-privacy.txt
index 77057d34479..cd511d3cb17 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/mathml/relations/html5-tree/canvas-visited-privacy.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/mathml/relations/html5-tree/canvas-visited-privacy.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Ensure MathML 'a' elements inside SVG drawn to canvas do not leak :visited styles via getImageData
\ No newline at end of file
+1 Pass
+Pass Ensure MathML 'a' elements inside SVG drawn to canvas do not leak :visited styles via getImageData
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/mathml/relations/html5-tree/anchor-visited-privacy.html b/Tests/LibWeb/Text/input/wpt-import/mathml/relations/html5-tree/anchor-visited-privacy.html
new file mode 100644
index 00000000000..744fdef7eb1
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/mathml/relations/html5-tree/anchor-visited-privacy.html
@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+
+<meta charset="utf-8">
+<title>MathML 'a' element: :visited privacy security restrictions</title>
+<link rel="help" href="https://drafts.csswg.org/selectors-4/#link">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<style>
+ #mathLink:any-link {
+ background-color: rgb(0, 255, 0);
+ }
+
+ #mathLink:visited {
+ color: rgb(255, 0, 0);
+ }
+</style>
+
+<math>
+ <!-- The empty string means the current page, so it's :visited -->
+ <a id="mathLink" href="">
+ <mtext>x</mtext>
+ </a>
+</math>
+
+<script>
+ test(function () {
+ const mathLink = document.getElementById('mathLink');
+ const style = window.getComputedStyle(mathLink);
+
+ assert_equals(style.backgroundColor, "rgb(0, 255, 0)");
+
+ assert_not_equals(style.color, "rgb(255, 0, 0)")
+
+ }, "MathML 'a' element with :visited selector must return the unvisited (:any-link) style to JavaScript API.");
+</script>
\ No newline at end of file