Skip to content

Truly independent web browser

LibWeb: Compile event handler content attributes on SVG elements

sideshowbarker3c4116c5a3a4
diff --git a/Libraries/LibWeb/SVG/SVGElement.cpp b/Libraries/LibWeb/SVG/SVGElement.cpp
index 037d9314c3a..5075f913b77 100644
--- a/Libraries/LibWeb/SVG/SVGElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGElement.cpp
@@ -17,6 +17,7 @@
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/ElementRareData.h>
#include <LibWeb/DOM/ShadowRoot.h>
+#include <LibWeb/HTML/AttributeNames.h>
#include <LibWeb/Layout/LayoutRustBridge.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Painting/BoxViews.h>
@@ -473,6 +474,18 @@ void SVGElement::attribute_changed(Utf16FlyString const& local_name, Optional<Ut
Base::attribute_changed(local_name, old_value, value, namespace_);
HTMLOrSVGOrMathMLElement::attribute_changed(local_name, old_value, value, namespace_);
+ // https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-attributes:concept-element-attributes-change-ext
+ // 1. If namespace is not null, or localName is not the name of an event handler content attribute on element, then return.
+ if (!namespace_.has_value() && local_name.view().starts_with(u"on"sv)) {
+#undef __ENUMERATE
+#define __ENUMERATE(attribute_name, event_name) \
+ if (local_name == HTML::AttributeNames::attribute_name) { \
+ element_event_handler_attribute_changed(local_name, value); \
+ }
+ ENUMERATE_GLOBAL_EVENT_HANDLERS(__ENUMERATE)
+#undef __ENUMERATE
+ }
+
if (old_value != value) {
update_presentation_attribute_style(local_name, value, namespace_);
invalidate_element_instances(ElementInstancesToInvalidate::All);
diff --git a/Tests/LibWeb/Text/expected/SVG/event-handler-content-attributes.txt b/Tests/LibWeb/Text/expected/SVG/event-handler-content-attributes.txt
new file mode 100644
index 00000000000..4918a8ab5c0
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/event-handler-content-attributes.txt
@@ -0,0 +1,4 @@
+rect.onclick is a function: true
+rect.onclick after removing the attribute: null
+click at the rect
+click at the rect, after the attribute changed
diff --git a/Tests/LibWeb/Text/input/SVG/event-handler-content-attributes.html b/Tests/LibWeb/Text/input/SVG/event-handler-content-attributes.html
new file mode 100644
index 00000000000..d3e8340ea5d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/event-handler-content-attributes.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ const order = [];
+ function log(entry) {
+ order.push(entry);
+ }
+</script>
+<svg>
+ <rect id="rect" width="10" height="10" onclick="log('click at the rect')"></rect>
+</svg>
+<script>
+ test(() => {
+ const rect = document.getElementById("rect");
+ println(`rect.onclick is a function: ${typeof rect.onclick === "function"}`);
+ rect.dispatchEvent(new MouseEvent("click"));
+
+ rect.setAttribute("onclick", "log('click at the rect, after the attribute changed')");
+ rect.dispatchEvent(new MouseEvent("click"));
+ rect.removeAttribute("onclick");
+ println(`rect.onclick after removing the attribute: ${rect.onclick}`);
+ rect.dispatchEvent(new MouseEvent("click"));
+
+ for (const entry of order)
+ println(entry);
+ });
+</script>