Skip to content

Truly independent web browser

LibWeb: Add the URL decomposition attributes to SVGAElement

Andreas Klingf2207f8fb47a
diff --git a/Libraries/LibWeb/SVG/SVGAElement.cpp b/Libraries/LibWeb/SVG/SVGAElement.cpp
index a171d5fa111..4b7e1c4eafc 100644
--- a/Libraries/LibWeb/SVG/SVGAElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGAElement.cpp
@@ -5,10 +5,13 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/ScopeGuard.h>
#include <LibGC/Heap.h>
#include <LibWeb/CSS/ElementBoxKind.h>
#include <LibWeb/CSS/Invalidation/LinkInvalidator.h>
#include <LibWeb/DOM/DOMTokenList.h>
+#include <LibWeb/DOM/Document.h>
+#include <LibWeb/Infra/SerializedURL.h>
#include <LibWeb/Namespace.h>
#include <LibWeb/SVG/AttributeNames.h>
#include <LibWeb/SVG/SVGAElement.h>
@@ -36,8 +39,8 @@ void SVGAElement::attribute_changed(Utf16FlyString const& name, Optional<Utf16St
{
Base::attribute_changed(name, old_value, value, namespace_);
- if (name == SVG::AttributeNames::href)
- CSS::Invalidation::invalidate_style_after_hyperlink_state_change(*this);
+ if (name == SVG::AttributeNames::href && (!namespace_.has_value() || namespace_ == Namespace::XLink))
+ href_content_attribute_changed();
if (name == HTML::AttributeNames::rel) {
if (m_rel_list)
m_rel_list->associated_attribute_changed(value.has_value() ? value->utf16_view() : u""sv);
@@ -100,4 +103,48 @@ void SVGAElement::activation_behavior(DOM::Event const& event)
activate_the_hyperlink(event);
}
+// AD-HOC: The URL of an SVG a element comes from its href attribute, or from the deprecated xlink:href attribute if
+// href is absent, so that the HyperlinkElementUtils members agree with the link the element follows.
+// https://html.spec.whatwg.org/multipage/links.html#concept-hyperlink-url-set
+void SVGAElement::set_the_url()
+{
+ auto should_invalidate_style = m_should_invalidate_style_after_url_change;
+ m_should_invalidate_style_after_url_change = true;
+ ScopeGuard invalidate_style_if_needed = [old_url = m_url, should_invalidate_style, this] {
+ if (should_invalidate_style && m_url != old_url)
+ CSS::Invalidation::invalidate_style_after_hyperlink_state_change(*this);
+ };
+
+ // 1. Set this element's url to null.
+ m_url = {};
+
+ // 2. If this element's href content attribute is absent, then return.
+ auto href = attribute(AttributeNames::href);
+ if (!href.has_value())
+ href = get_attribute_ns(Namespace::XLink, AttributeNames::href);
+ if (!href.has_value())
+ return;
+
+ // 3. Let url be the result of encoding-parsing a URL given this element's href content attribute's value, relative
+ // to this element's node document.
+ auto url = document().encoding_parse_url(*href);
+
+ // 4. If url is not failure, then set this element's url to url.
+ if (url.has_value())
+ m_url = url.release_value();
+}
+
+// AD-HOC: Updating the href of an SVG a element writes to the href attribute, unless only the deprecated xlink:href
+// attribute is present, in which case that attribute is written instead.
+// https://html.spec.whatwg.org/multipage/links.html#update-href
+void SVGAElement::update_href()
+{
+ auto serialized_url = utf16_string_from_url_ascii(m_url->serialize());
+ if (!has_attribute(AttributeNames::href) && has_attribute_ns(Namespace::XLink, AttributeNames::href)) {
+ set_attribute_value(AttributeNames::href, move(serialized_url), "xlink"_utf16_fly_string, Namespace::XLink);
+ return;
+ }
+ set_attribute_value(AttributeNames::href, move(serialized_url));
+}
+
}
diff --git a/Libraries/LibWeb/SVG/SVGAElement.h b/Libraries/LibWeb/SVG/SVGAElement.h
index db3b8bab1ad..b32834534bd 100644
--- a/Libraries/LibWeb/SVG/SVGAElement.h
+++ b/Libraries/LibWeb/SVG/SVGAElement.h
@@ -7,6 +7,7 @@
#pragma once
+#include <LibWeb/HTML/HyperlinkElementUtils.h>
#include <LibWeb/SVG/SVGGraphicsElement.h>
#include <LibWeb/SVG/SVGURIReference.h>
@@ -14,7 +15,8 @@ namespace Web::SVG {
class SVGAElement final
: public SVGGraphicsElement
- , public SVGURIReferenceMixin<SupportsXLinkHref::Yes> {
+ , public SVGURIReferenceMixin<SupportsXLinkHref::Yes>
+ , public HTML::HyperlinkElementUtils {
WEB_WRAPPABLE(SVGAElement, SVGGraphicsElement);
GC_DECLARE_ALLOCATOR(SVGAElement);
@@ -41,6 +43,12 @@ private:
virtual bool has_activation_behavior() const override { return true; }
virtual void activation_behavior(DOM::Event const&) override;
+ // ^HTML::HyperlinkElementUtils
+ virtual DOM::Element& hyperlink_element_utils_element() override { return *this; }
+ virtual DOM::Element const& hyperlink_element_utils_element() const override { return *this; }
+ virtual void update_href() override;
+ virtual void set_the_url() override;
+
GC::Ptr<DOM::DOMTokenList> m_rel_list;
GC::Ptr<SVGAnimatedString> m_target;
diff --git a/Libraries/LibWeb/SVG/SVGAElement.idl b/Libraries/LibWeb/SVG/SVGAElement.idl
index 180b83a265c..20e633b7ce2 100644
--- a/Libraries/LibWeb/SVG/SVGAElement.idl
+++ b/Libraries/LibWeb/SVG/SVGAElement.idl
@@ -7,12 +7,14 @@ interface SVGAElement : SVGGraphicsElement {
[Reflect] attribute USVString ping;
[Reflect] attribute DOMString rel;
[SameObject, PutForwards=value] readonly attribute DOMTokenList relList;
- [Reflect] attribute DOMString hreflang;
- [Reflect] attribute DOMString type;
+ // NB: hreflang and type come from HyperlinkElementUtils.
[Reflect=referrerpolicy, Enumerated=ReferrerPolicy] attribute DOMString referrerPolicy;
};
SVGAElement includes SVGURIReference;
-// FIXME: SVGAElement includes HTMLHyperlinkElementUtils;
+// NB: The spec includes HTMLHyperlinkElementUtils here, from before HTML split the URL decomposition attributes out
+// into HyperlinkElementUtils. The href and target attributes of HTMLHyperlinkElementUtils would conflict with
+// SVGURIReference's href and SVGAElement's target.
+SVGAElement includes HyperlinkElementUtils;
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/idlharness.window.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/idlharness.window.txt
index 8d1b3d4f854..7902a116969 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/svg/idlharness.window.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/idlharness.window.txt
@@ -2,8 +2,8 @@ Harness status: OK
Found 1780 tests
-1470 Pass
-310 Fail
+1490 Pass
+290 Fail
Pass idl_test setup
Pass idl_test validation
Pass Partial interface Document: original interface defined
@@ -1567,16 +1567,16 @@ Pass SVGAElement interface: attribute relList
Pass SVGAElement interface: attribute hreflang
Pass SVGAElement interface: attribute type
Pass SVGAElement interface: attribute referrerPolicy
-Fail SVGAElement interface: attribute origin
-Fail SVGAElement interface: attribute protocol
-Fail SVGAElement interface: attribute username
-Fail SVGAElement interface: attribute password
-Fail SVGAElement interface: attribute host
-Fail SVGAElement interface: attribute hostname
-Fail SVGAElement interface: attribute port
-Fail SVGAElement interface: attribute pathname
-Fail SVGAElement interface: attribute search
-Fail SVGAElement interface: attribute hash
+Pass SVGAElement interface: attribute origin
+Pass SVGAElement interface: attribute protocol
+Pass SVGAElement interface: attribute username
+Pass SVGAElement interface: attribute password
+Pass SVGAElement interface: attribute host
+Pass SVGAElement interface: attribute hostname
+Pass SVGAElement interface: attribute port
+Pass SVGAElement interface: attribute pathname
+Pass SVGAElement interface: attribute search
+Pass SVGAElement interface: attribute hash
Pass SVGAElement interface: attribute href
Pass SVGAElement must be primary interface of objects.a
Pass Stringification of objects.a
@@ -1588,16 +1588,16 @@ Pass SVGAElement interface: objects.a must inherit property "relList" with the p
Pass SVGAElement interface: objects.a must inherit property "hreflang" with the proper type
Pass SVGAElement interface: objects.a must inherit property "type" with the proper type
Pass SVGAElement interface: objects.a must inherit property "referrerPolicy" with the proper type
-Fail SVGAElement interface: objects.a must inherit property "origin" with the proper type
-Fail SVGAElement interface: objects.a must inherit property "protocol" with the proper type
-Fail SVGAElement interface: objects.a must inherit property "username" with the proper type
-Fail SVGAElement interface: objects.a must inherit property "password" with the proper type
-Fail SVGAElement interface: objects.a must inherit property "host" with the proper type
-Fail SVGAElement interface: objects.a must inherit property "hostname" with the proper type
-Fail SVGAElement interface: objects.a must inherit property "port" with the proper type
-Fail SVGAElement interface: objects.a must inherit property "pathname" with the proper type
-Fail SVGAElement interface: objects.a must inherit property "search" with the proper type
-Fail SVGAElement interface: objects.a must inherit property "hash" with the proper type
+Pass SVGAElement interface: objects.a must inherit property "origin" with the proper type
+Pass SVGAElement interface: objects.a must inherit property "protocol" with the proper type
+Pass SVGAElement interface: objects.a must inherit property "username" with the proper type
+Pass SVGAElement interface: objects.a must inherit property "password" with the proper type
+Pass SVGAElement interface: objects.a must inherit property "host" with the proper type
+Pass SVGAElement interface: objects.a must inherit property "hostname" with the proper type
+Pass SVGAElement interface: objects.a must inherit property "port" with the proper type
+Pass SVGAElement interface: objects.a must inherit property "pathname" with the proper type
+Pass SVGAElement interface: objects.a must inherit property "search" with the proper type
+Pass SVGAElement interface: objects.a must inherit property "hash" with the proper type
Pass SVGAElement interface: objects.a must inherit property "href" with the proper type
Pass SVGGraphicsElement interface: objects.a must inherit property "transform" with the proper type
Pass SVGGraphicsElement interface: objects.a must inherit property "getBBox(optional SVGBoundingBoxOptions)" with the proper type
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/linking/scripted/a-hyperlinkutils-getter-setter.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/linking/scripted/a-hyperlinkutils-getter-setter.txt
index 48d01c5e163..6662a94a8dd 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/svg/linking/scripted/a-hyperlinkutils-getter-setter.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/linking/scripted/a-hyperlinkutils-getter-setter.txt
@@ -2,50 +2,49 @@ Harness status: OK
Found 45 tests
-3 Pass
-42 Fail
-Fail SVGAElement mutation [Mode: href] (0): hash
-Fail SVGAElement mutation [Mode: href] (1): hash
-Fail SVGAElement mutation [Mode: href] (2): host
-Fail SVGAElement mutation [Mode: href] (3): hostname
+45 Pass
+Pass SVGAElement mutation [Mode: href] (0): hash
+Pass SVGAElement mutation [Mode: href] (1): hash
+Pass SVGAElement mutation [Mode: href] (2): host
+Pass SVGAElement mutation [Mode: href] (3): hostname
Pass SVGAElement mutation [Mode: href] (4): href
-Fail SVGAElement mutation [Mode: href] (5): password
-Fail SVGAElement mutation [Mode: href] (6): pathname
-Fail SVGAElement mutation [Mode: href] (7): port
-Fail SVGAElement mutation [Mode: href] (8): protocol
-Fail SVGAElement mutation [Mode: href] (9): protocol
-Fail SVGAElement mutation [Mode: href] (10): search
-Fail SVGAElement mutation [Mode: href] (11): search
-Fail SVGAElement mutation [Mode: href] (12): search
-Fail SVGAElement mutation [Mode: href] (13): search
-Fail SVGAElement mutation [Mode: href] (14): username
-Fail SVGAElement mutation [Mode: xlink] (0): hash
-Fail SVGAElement mutation [Mode: xlink] (1): hash
-Fail SVGAElement mutation [Mode: xlink] (2): host
-Fail SVGAElement mutation [Mode: xlink] (3): hostname
+Pass SVGAElement mutation [Mode: href] (5): password
+Pass SVGAElement mutation [Mode: href] (6): pathname
+Pass SVGAElement mutation [Mode: href] (7): port
+Pass SVGAElement mutation [Mode: href] (8): protocol
+Pass SVGAElement mutation [Mode: href] (9): protocol
+Pass SVGAElement mutation [Mode: href] (10): search
+Pass SVGAElement mutation [Mode: href] (11): search
+Pass SVGAElement mutation [Mode: href] (12): search
+Pass SVGAElement mutation [Mode: href] (13): search
+Pass SVGAElement mutation [Mode: href] (14): username
+Pass SVGAElement mutation [Mode: xlink] (0): hash
+Pass SVGAElement mutation [Mode: xlink] (1): hash
+Pass SVGAElement mutation [Mode: xlink] (2): host
+Pass SVGAElement mutation [Mode: xlink] (3): hostname
Pass SVGAElement mutation [Mode: xlink] (4): href
-Fail SVGAElement mutation [Mode: xlink] (5): password
-Fail SVGAElement mutation [Mode: xlink] (6): pathname
-Fail SVGAElement mutation [Mode: xlink] (7): port
-Fail SVGAElement mutation [Mode: xlink] (8): protocol
-Fail SVGAElement mutation [Mode: xlink] (9): protocol
-Fail SVGAElement mutation [Mode: xlink] (10): search
-Fail SVGAElement mutation [Mode: xlink] (11): search
-Fail SVGAElement mutation [Mode: xlink] (12): search
-Fail SVGAElement mutation [Mode: xlink] (13): search
-Fail SVGAElement mutation [Mode: xlink] (14): username
-Fail SVGAElement mutation [Mode: both] (0): hash
-Fail SVGAElement mutation [Mode: both] (1): hash
-Fail SVGAElement mutation [Mode: both] (2): host
-Fail SVGAElement mutation [Mode: both] (3): hostname
+Pass SVGAElement mutation [Mode: xlink] (5): password
+Pass SVGAElement mutation [Mode: xlink] (6): pathname
+Pass SVGAElement mutation [Mode: xlink] (7): port
+Pass SVGAElement mutation [Mode: xlink] (8): protocol
+Pass SVGAElement mutation [Mode: xlink] (9): protocol
+Pass SVGAElement mutation [Mode: xlink] (10): search
+Pass SVGAElement mutation [Mode: xlink] (11): search
+Pass SVGAElement mutation [Mode: xlink] (12): search
+Pass SVGAElement mutation [Mode: xlink] (13): search
+Pass SVGAElement mutation [Mode: xlink] (14): username
+Pass SVGAElement mutation [Mode: both] (0): hash
+Pass SVGAElement mutation [Mode: both] (1): hash
+Pass SVGAElement mutation [Mode: both] (2): host
+Pass SVGAElement mutation [Mode: both] (3): hostname
Pass SVGAElement mutation [Mode: both] (4): href
-Fail SVGAElement mutation [Mode: both] (5): password
-Fail SVGAElement mutation [Mode: both] (6): pathname
-Fail SVGAElement mutation [Mode: both] (7): port
-Fail SVGAElement mutation [Mode: both] (8): protocol
-Fail SVGAElement mutation [Mode: both] (9): protocol
-Fail SVGAElement mutation [Mode: both] (10): search
-Fail SVGAElement mutation [Mode: both] (11): search
-Fail SVGAElement mutation [Mode: both] (12): search
-Fail SVGAElement mutation [Mode: both] (13): search
-Fail SVGAElement mutation [Mode: both] (14): username
\ No newline at end of file
+Pass SVGAElement mutation [Mode: both] (5): password
+Pass SVGAElement mutation [Mode: both] (6): pathname
+Pass SVGAElement mutation [Mode: both] (7): port
+Pass SVGAElement mutation [Mode: both] (8): protocol
+Pass SVGAElement mutation [Mode: both] (9): protocol
+Pass SVGAElement mutation [Mode: both] (10): search
+Pass SVGAElement mutation [Mode: both] (11): search
+Pass SVGAElement mutation [Mode: both] (12): search
+Pass SVGAElement mutation [Mode: both] (13): search
+Pass SVGAElement mutation [Mode: both] (14): username
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/linking/scripted/a-hyperlinkutils-getter.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/linking/scripted/a-hyperlinkutils-getter.txt
index 348aed5359c..fbefd59fbbb 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/svg/linking/scripted/a-hyperlinkutils-getter.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/linking/scripted/a-hyperlinkutils-getter.txt
@@ -2,34 +2,34 @@ Harness status: OK
Found 30 tests
-30 Fail
-Fail SVGAElement HyperlinkElementUtils getter [Mode: href] (0): origin
-Fail SVGAElement HyperlinkElementUtils getter [Mode: href] (1): protocol
-Fail SVGAElement HyperlinkElementUtils getter [Mode: href] (2): username
-Fail SVGAElement HyperlinkElementUtils getter [Mode: href] (3): password
-Fail SVGAElement HyperlinkElementUtils getter [Mode: href] (4): host
-Fail SVGAElement HyperlinkElementUtils getter [Mode: href] (5): hostname
-Fail SVGAElement HyperlinkElementUtils getter [Mode: href] (6): port
-Fail SVGAElement HyperlinkElementUtils getter [Mode: href] (7): pathname
-Fail SVGAElement HyperlinkElementUtils getter [Mode: href] (8): search
-Fail SVGAElement HyperlinkElementUtils getter [Mode: href] (9): hash
-Fail SVGAElement HyperlinkElementUtils getter [Mode: xlink] (0): origin
-Fail SVGAElement HyperlinkElementUtils getter [Mode: xlink] (1): protocol
-Fail SVGAElement HyperlinkElementUtils getter [Mode: xlink] (2): username
-Fail SVGAElement HyperlinkElementUtils getter [Mode: xlink] (3): password
-Fail SVGAElement HyperlinkElementUtils getter [Mode: xlink] (4): host
-Fail SVGAElement HyperlinkElementUtils getter [Mode: xlink] (5): hostname
-Fail SVGAElement HyperlinkElementUtils getter [Mode: xlink] (6): port
-Fail SVGAElement HyperlinkElementUtils getter [Mode: xlink] (7): pathname
-Fail SVGAElement HyperlinkElementUtils getter [Mode: xlink] (8): search
-Fail SVGAElement HyperlinkElementUtils getter [Mode: xlink] (9): hash
-Fail SVGAElement HyperlinkElementUtils getter [Mode: both] (0): origin
-Fail SVGAElement HyperlinkElementUtils getter [Mode: both] (1): protocol
-Fail SVGAElement HyperlinkElementUtils getter [Mode: both] (2): username
-Fail SVGAElement HyperlinkElementUtils getter [Mode: both] (3): password
-Fail SVGAElement HyperlinkElementUtils getter [Mode: both] (4): host
-Fail SVGAElement HyperlinkElementUtils getter [Mode: both] (5): hostname
-Fail SVGAElement HyperlinkElementUtils getter [Mode: both] (6): port
-Fail SVGAElement HyperlinkElementUtils getter [Mode: both] (7): pathname
-Fail SVGAElement HyperlinkElementUtils getter [Mode: both] (8): search
-Fail SVGAElement HyperlinkElementUtils getter [Mode: both] (9): hash
\ No newline at end of file
+30 Pass
+Pass SVGAElement HyperlinkElementUtils getter [Mode: href] (0): origin
+Pass SVGAElement HyperlinkElementUtils getter [Mode: href] (1): protocol
+Pass SVGAElement HyperlinkElementUtils getter [Mode: href] (2): username
+Pass SVGAElement HyperlinkElementUtils getter [Mode: href] (3): password
+Pass SVGAElement HyperlinkElementUtils getter [Mode: href] (4): host
+Pass SVGAElement HyperlinkElementUtils getter [Mode: href] (5): hostname
+Pass SVGAElement HyperlinkElementUtils getter [Mode: href] (6): port
+Pass SVGAElement HyperlinkElementUtils getter [Mode: href] (7): pathname
+Pass SVGAElement HyperlinkElementUtils getter [Mode: href] (8): search
+Pass SVGAElement HyperlinkElementUtils getter [Mode: href] (9): hash
+Pass SVGAElement HyperlinkElementUtils getter [Mode: xlink] (0): origin
+Pass SVGAElement HyperlinkElementUtils getter [Mode: xlink] (1): protocol
+Pass SVGAElement HyperlinkElementUtils getter [Mode: xlink] (2): username
+Pass SVGAElement HyperlinkElementUtils getter [Mode: xlink] (3): password
+Pass SVGAElement HyperlinkElementUtils getter [Mode: xlink] (4): host
+Pass SVGAElement HyperlinkElementUtils getter [Mode: xlink] (5): hostname
+Pass SVGAElement HyperlinkElementUtils getter [Mode: xlink] (6): port
+Pass SVGAElement HyperlinkElementUtils getter [Mode: xlink] (7): pathname
+Pass SVGAElement HyperlinkElementUtils getter [Mode: xlink] (8): search
+Pass SVGAElement HyperlinkElementUtils getter [Mode: xlink] (9): hash
+Pass SVGAElement HyperlinkElementUtils getter [Mode: both] (0): origin
+Pass SVGAElement HyperlinkElementUtils getter [Mode: both] (1): protocol
+Pass SVGAElement HyperlinkElementUtils getter [Mode: both] (2): username
+Pass SVGAElement HyperlinkElementUtils getter [Mode: both] (3): password
+Pass SVGAElement HyperlinkElementUtils getter [Mode: both] (4): host
+Pass SVGAElement HyperlinkElementUtils getter [Mode: both] (5): hostname
+Pass SVGAElement HyperlinkElementUtils getter [Mode: both] (6): port
+Pass SVGAElement HyperlinkElementUtils getter [Mode: both] (7): pathname
+Pass SVGAElement HyperlinkElementUtils getter [Mode: both] (8): search
+Pass SVGAElement HyperlinkElementUtils getter [Mode: both] (9): hash
\ No newline at end of file