LibWeb/DOM: Prefix inline event handler source text with "on"

EventTarget stores event handler entries under bare event names like
click and error, because those keys are also used in places that need
the event type rather than the IDL/content-attribute spelling.

That means we cannot only reuse the stored key when synthesizing the
function source for inline handlers. To handle this, prefix the name
with "on".
This commit is contained in:
Shannon Booth 2026-05-08 14:22:47 +02:00 committed by Sam Atkins
parent b639b8e3c9
commit 29033939f3
3 changed files with 52 additions and 2 deletions

View file

@ -448,12 +448,12 @@ WebIDL::CallbackType* EventTarget::get_current_value_of_event_handler(FlyString
if (name == HTML::EventNames::error && is<HTML::Window>(this)) {
// -> If name is onerror and eventTarget is a Window object
// Let the function have five arguments, named event, source, lineno, colno, and error.
source_builder.appendff("function {}(event, source, lineno, colno, error) {{\n{}\n}}", name, body);
source_builder.appendff("function on{}(event, source, lineno, colno, error) {{\n{}\n}}", name, body);
parameters_string = "event, source, lineno, colno, error"sv;
} else {
// -> Otherwise
// Let the function have a single argument called event.
source_builder.appendff("function {}(event) {{\n{}\n}}", name, body);
source_builder.appendff("function on{}(event) {{\n{}\n}}", name, body);
parameters_string = "event"sv;
}

View file

@ -0,0 +1,10 @@
Harness status: OK
Found 5 tests
5 Pass
Pass non-error event handler
Pass error event handler not on body
Pass error event handler on disconnected body
Pass error event handler on disconnected frameset
Pass error event handler on connected body, reflected to Window

View file

@ -0,0 +1,40 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Test the sourceText of event handlers</title>
<link rel="help" href="https://github.com/whatwg/html/issues/5500">
<script src="../../../../resources/testharness.js"></script>
<script src="../../../../resources/testharnessreport.js"></script>
<body>
<script>
"use strict";
test(() => {
const el = document.createElement("div");
el.setAttribute("onclick", "foo");
assert_equals(el.onclick.toString(), "function onclick(event) {\nfoo\n}");
}, "non-error event handler");
test(() => {
const el = document.createElement("div");
el.setAttribute("onerror", "foo");
assert_equals(el.onerror.toString(), "function onerror(event) {\nfoo\n}");
}, "error event handler not on body");
test(() => {
const el = document.createElement("body");
el.setAttribute("onerror", "foo");
assert_equals(el.onerror.toString(), "function onerror(event, source, lineno, colno, error) {\nfoo\n}");
}, "error event handler on disconnected body");
test(() => {
const el = document.createElement("frameset");
el.setAttribute("onerror", "foo");
assert_equals(el.onerror.toString(), "function onerror(event, source, lineno, colno, error) {\nfoo\n}");
}, "error event handler on disconnected frameset");
test(() => {
document.body.setAttribute("onerror", "foo");
assert_equals(window.onerror.toString(), "function onerror(event, source, lineno, colno, error) {\nfoo\n}");
}, "error event handler on connected body, reflected to Window");
</script>