Skip to content

Truly independent web browser

LibWeb: Follow the SVG rules for which elements are focusable

Andreas Kling2197f755140d
diff --git a/Libraries/LibWeb/SVG/SVGAElement.cpp b/Libraries/LibWeb/SVG/SVGAElement.cpp
index ec0c125a548..a171d5fa111 100644
--- a/Libraries/LibWeb/SVG/SVGAElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGAElement.cpp
@@ -9,6 +9,7 @@
#include <LibWeb/CSS/ElementBoxKind.h>
#include <LibWeb/CSS/Invalidation/LinkInvalidator.h>
#include <LibWeb/DOM/DOMTokenList.h>
+#include <LibWeb/Namespace.h>
#include <LibWeb/SVG/AttributeNames.h>
#include <LibWeb/SVG/SVGAElement.h>
@@ -50,6 +51,28 @@ i32 SVGAElement::default_tab_index_value() const
return 0;
}
+// https://svgwg.org/svg2-draft/interact.html#Focus
+bool SVGAElement::is_focusable() const
+{
+ // In addition, all ‘a’ elements that are valid links are focusable, and their tabindex focus flag must be set
+ // unless the user agent normally provides an alternative method of keyboard traversal of links.
+ auto forms_link = [](DOM::Element const& element) {
+ if (element.is_svg_a_element())
+ return element.has_attribute(SVG::AttributeNames::href) || element.has_attribute_ns(Namespace::XLink, SVG::AttributeNames::href);
+ return (element.is_html_anchor_element() || element.is_html_area_element()) && element.has_attribute(HTML::AttributeNames::href);
+ };
+
+ // https://svgwg.org/svg2-draft/linking.html#AElement
+ // Links may not be nested; if an ‘a’ element is a descendent of another hyperlink element (whether in the SVG
+ // namespace or another namespace), user agents must ignore its href attribute and treat it as inactive.
+ auto is_valid_link = forms_link(*this);
+ for (auto ancestor = parent_element(); is_valid_link && ancestor; ancestor = ancestor->parent_element()) {
+ if (forms_link(*ancestor))
+ is_valid_link = false;
+ }
+ return Base::is_focusable() || (is_valid_link && !is_in_never_rendered_subtree() && meets_focusable_area_rendering_requirements());
+}
+
// https://svgwg.org/svg2-draft/linking.html#__svg__SVGAElement__target
GC::Ref<SVGAnimatedString> SVGAElement::target()
{
diff --git a/Libraries/LibWeb/SVG/SVGAElement.h b/Libraries/LibWeb/SVG/SVGAElement.h
index 9f262884203..db3b8bab1ad 100644
--- a/Libraries/LibWeb/SVG/SVGAElement.h
+++ b/Libraries/LibWeb/SVG/SVGAElement.h
@@ -36,6 +36,7 @@ private:
// ^DOM::Element
virtual 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;
+ virtual bool is_focusable() const override;
virtual bool has_activation_behavior() const override { return true; }
virtual void activation_behavior(DOM::Event const&) override;
diff --git a/Libraries/LibWeb/SVG/SVGElement.cpp b/Libraries/LibWeb/SVG/SVGElement.cpp
index 3e27920d056..24f7acabce9 100644
--- a/Libraries/LibWeb/SVG/SVGElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGElement.cpp
@@ -47,6 +47,50 @@ CSS::ElementBoxKind SVGElement::box_kind() const
return CSS::ElementBoxKind::NoBox;
}
+// https://svgwg.org/svg2-draft/render.html#TermNeverRenderedElement
+static bool is_never_rendered_element(SVGElement const& element)
+{
+ // Any element type that is never directly rendered, regardless of context or display style value. It includes the
+ // following elements: ‘clipPath’, ‘defs’, ‘desc’, ‘linearGradient’, ‘marker’, ‘mask’, ‘metadata’, ‘pattern’,
+ // ‘radialGradient’, ‘script’, ‘style’ and ‘title’; it also includes a ‘symbol’ element that is not the instance root
+ // of a use-element shadow tree.
+ if (is<SVGSymbolElement>(element))
+ return element.box_kind() == CSS::ElementBoxKind::NoBox;
+
+ // https://drafts.csswg.org/filter-effects-1/#FilterElement
+ // filter elements are never rendered directly; their only usage is as something that can be referenced using the
+ // filter property.
+ return element.local_name().is_one_of(
+ TagNames::clipPath, TagNames::defs, TagNames::desc, TagNames::filter, TagNames::linearGradient,
+ TagNames::marker, TagNames::mask, TagNames::metadata, TagNames::pattern, TagNames::radialGradient,
+ TagNames::script, TagNames::style, TagNames::title);
+}
+
+// https://svgwg.org/svg2-draft/interact.html#Focus
+bool SVGElement::is_focusable() const
+{
+ // The following SVG elements are focusable in an interactive document. [...] For the purpose of the HTML focus
+ // model, interactive user agents must treat them as focusable areas whose tabindex focus flag should be set:
+ // - the document root element
+ if (document().document_element() == this)
+ return meets_focusable_area_rendering_requirements();
+
+ // - any directly rendered element with a valid integer ‘tabindex’ attribute
+ // NB: A never-rendered element and its descendants are never directly rendered.
+ if (is_in_never_rendered_subtree())
+ return false;
+ return Base::is_focusable();
+}
+
+bool SVGElement::is_in_never_rendered_subtree() const
+{
+ for (auto element = this; element; element = as_if<SVGElement>(element->parent_element().ptr())) {
+ if (is_never_rendered_element(*element))
+ return true;
+ }
+ return false;
+}
+
struct NamedPropertyID {
NamedPropertyID(CSS::PropertyID property_id, Utf16FlyString name, std::initializer_list<Utf16FlyString> supported_elements = {})
: id(property_id)
diff --git a/Libraries/LibWeb/SVG/SVGElement.h b/Libraries/LibWeb/SVG/SVGElement.h
index 24f92aa6d25..459b3448b1b 100644
--- a/Libraries/LibWeb/SVG/SVGElement.h
+++ b/Libraries/LibWeb/SVG/SVGElement.h
@@ -35,6 +35,11 @@ public:
virtual CSS::ElementBoxKind box_kind() const override;
+ // ^EventTarget
+ virtual bool is_focusable() const override;
+
+ bool is_in_never_rendered_subtree() const;
+
GC::Ref<SVGAnimatedString> class_name();
GC::Ptr<SVGSVGElement> owner_svg_element();
GC::Ptr<SVGSVGElement> outermost_svg_element();
diff --git a/Libraries/LibWeb/SVG/TagNames.h b/Libraries/LibWeb/SVG/TagNames.h
index 15a613495dd..31c5482fa6f 100644
--- a/Libraries/LibWeb/SVG/TagNames.h
+++ b/Libraries/LibWeb/SVG/TagNames.h
@@ -42,6 +42,7 @@ namespace Web::SVG::TagNames {
__ENUMERATE_SVG_TAG(image, "image") \
__ENUMERATE_SVG_TAG(line, "line") \
__ENUMERATE_SVG_TAG(linearGradient, "linearGradient") \
+ __ENUMERATE_SVG_TAG(marker, "marker") \
__ENUMERATE_SVG_TAG(mask, "mask") \
__ENUMERATE_SVG_TAG(metadata, "metadata") \
__ENUMERATE_SVG_TAG(mpath, "mpath") \
diff --git a/Tests/LibWeb/Text/expected/SVG/focusable-elements.txt b/Tests/LibWeb/Text/expected/SVG/focusable-elements.txt
new file mode 100644
index 00000000000..88e44531c8e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/focusable-elements.txt
@@ -0,0 +1,7 @@
+outer: true
+inner: false
+inner-html: false
+alone: true
+in-defs: false
+rect-in-defs: false
+filter: false
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/interaction/focus/tabindex-focus-flag.txt b/Tests/LibWeb/Text/expected/wpt-import/html/interaction/focus/tabindex-focus-flag.txt
index 43073df830c..d40a1de7532 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/interaction/focus/tabindex-focus-flag.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/interaction/focus/tabindex-focus-flag.txt
@@ -2,8 +2,7 @@ Harness status: OK
Found 35 tests
-34 Pass
-1 Fail
+35 Pass
Pass a should not be focusable by default.
Pass a[href] should be focusable by default.
Pass button should be focusable by default.
@@ -34,7 +33,7 @@ Pass IMG with tabindex=-1 should be focusable.
Pass A with tabindex=invalid should not be focusable.
Pass A#with-href with tabindex=invalid should be focusable.
Pass a with tabindex=invalid should not be focusable.
-Fail a#with-href with tabindex=invalid should be focusable.
+Pass a#with-href with tabindex=invalid should be focusable.
Pass text with tabindex=invalid should not be focusable.
Pass DIV with tabindex=invalid should not be focusable.
Pass SUMMARY#summary-out-tabindex-invalid with tabindex=invalid should not be focusable.
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/interact/focus-inside-hidden-svg-containers-04.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/interact/focus-inside-hidden-svg-containers-04.txt
index 37e6bb0e3b6..5e396c6f71c 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/svg/interact/focus-inside-hidden-svg-containers-04.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/interact/focus-inside-hidden-svg-containers-04.txt
@@ -2,12 +2,12 @@ Harness status: OK
Found 8 tests
-8 Fail
-Fail Non-rendered <defs> is not focusable
-Fail Non-rendered <symbol> is not focusable
-Fail Non-rendered <clipPath> is not focusable
-Fail Non-rendered <mask> is not focusable
-Fail Non-rendered <marker> is not focusable
-Fail Non-rendered <pattern> is not focusable
-Fail Non-rendered <linearGradient> is not focusable
-Fail Non-rendered <radialGradient> is not focusable
\ No newline at end of file
+8 Pass
+Pass Non-rendered <defs> is not focusable
+Pass Non-rendered <symbol> is not focusable
+Pass Non-rendered <clipPath> is not focusable
+Pass Non-rendered <mask> is not focusable
+Pass Non-rendered <marker> is not focusable
+Pass Non-rendered <pattern> is not focusable
+Pass Non-rendered <linearGradient> is not focusable
+Pass Non-rendered <radialGradient> is not focusable
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/interact/scripted/tabindex-focus-flag.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/interact/scripted/tabindex-focus-flag.txt
index ea01cdd9d06..4dc3969e99b 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/svg/interact/scripted/tabindex-focus-flag.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/interact/scripted/tabindex-focus-flag.txt
@@ -2,13 +2,12 @@ Harness status: OK
Found 39 tests
-27 Pass
-12 Fail
-Fail The document root element should be focusable.
+39 Pass
+Pass The document root element should be focusable.
Pass rect should not be focusable.
Pass svg should not be focusable.
Pass a should not be focusable.
-Fail a[href] should be focusable.
+Pass a[href] should be focusable.
Pass iframe should be focusable.
Pass audio[controls] should be focusable.
Pass video[controls] should be focusable.
@@ -30,16 +29,16 @@ Pass textPath[tabindex] should be focusable.
Pass tspan[tabindex] should be focusable.
Pass unknown[tabindex] should be focusable.
Pass use[tabindex] should be focusable.
-Fail clipPath[tabindex] should not be focusable.
-Fail defs[tabindex] should not be focusable.
-Fail desc[tabindex] should not be focusable.
-Fail linearGradient[tabindex] should not be focusable.
-Fail marker[tabindex] should not be focusable.
-Fail mask[tabindex] should not be focusable.
-Fail metadata[tabindex] should not be focusable.
-Fail pattern[tabindex] should not be focusable.
-Fail radialGradient[tabindex] should not be focusable.
+Pass clipPath[tabindex] should not be focusable.
+Pass defs[tabindex] should not be focusable.
+Pass desc[tabindex] should not be focusable.
+Pass linearGradient[tabindex] should not be focusable.
+Pass marker[tabindex] should not be focusable.
+Pass mask[tabindex] should not be focusable.
+Pass metadata[tabindex] should not be focusable.
+Pass pattern[tabindex] should not be focusable.
+Pass radialGradient[tabindex] should not be focusable.
Pass script[tabindex] should not be focusable.
Pass style[tabindex] should not be focusable.
Pass title[tabindex] should not be focusable.
-Fail symbol[tabindex] should not be focusable.
\ No newline at end of file
+Pass symbol[tabindex] should not be focusable.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/SVG/focusable-elements.html b/Tests/LibWeb/Text/input/SVG/focusable-elements.html
new file mode 100644
index 00000000000..2fcf2c431a2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/focusable-elements.html
@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<svg>
+ <a id="outer" href="#outer">
+ <a id="inner" href="#inner"><rect width="10" height="10" /></a>
+ </a>
+</svg>
+<a href="#html">
+ <svg><a id="inner-html" href="#inner-html"><rect width="10" height="10" /></a></svg>
+</a>
+<svg><a id="alone" href="#alone"><rect width="10" height="10" /></a></svg>
+<svg>
+ <defs><a id="in-defs" href="#in-defs"><rect width="10" height="10" /></a></defs>
+ <defs><rect id="rect-in-defs" tabindex="0" width="10" height="10" /></defs>
+ <filter id="filter" tabindex="0"></filter>
+</svg>
+<script>
+ test(() => {
+ for (const id of ["outer", "inner", "inner-html", "alone", "in-defs", "rect-in-defs", "filter"]) {
+ const element = document.getElementById(id);
+ element.focus();
+ println(`${id}: ${document.activeElement === element}`);
+ }
+ });
+</script>