ladybird/Tests/LibWeb/Text/input/UIEvents/click-event-on-input-control-associated-with-label-element.html
Aliaksandr Kalenik c0e90a2a68 LibWeb: Dispatch "click" event on input control associated with <label>
For example, in the following HTML:
```html
<label>
    <input type="radio" name="fruit" value="apple" id="radio1">
    <span class="box"></span>
</label>
```

When any descendant of a <label> element is clicked, a "click" event
must be dispatched on the <input> element nested within the <label>, in
addition to the "click" event dispatched on the clicked descendant.

Previously, this behavior was implemented only for text node descendants
by "overriding" the mouse event target using `mouse_event_target()` in
the TextPaintable. This approach was incorrect because it was limited to
text nodes, whereas the behavior should apply to any box. Moreover, the
"click" event for the input control must be dispatched *in addition* to
the event on the clicked element, rather than redirecting it.
2024-11-21 16:11:03 +01:00

75 lines
1.9 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<style>
* {
margin: 0;
}
.box {
display: inline-block;
width: 100px;
height: 100px;
background-color: purple;
}
input {
display: none;
}
label {
display: block;
width: 200px;
height: 200px;
border: 1px solid black;
}
</style>
<script src="../include.js"></script>
<body>
<form>
<label>
<input type="radio" name="fruit" value="apple" id="radio1">
<span class="box"></span>
</label>
<label>
<input type="radio" name="fruit" value="banana" id="radio2">
hey
</label>
</form>
</body>
<script>
asyncTest(done => {
let eventCount = 0;
function endTestIfGotAllEvents() {
eventCount++;
if (eventCount == 4) {
done();
}
}
document.querySelectorAll('input').forEach(function (element) {
element.addEventListener('click', function (event) {
println(`click event on input#${event.target.id}`);
endTestIfGotAllEvents();
});
element.addEventListener('mouseup', function (event) {
println(`mouseup event on input#${event.target.id}`);
endTestIfGotAllEvents();
});
});
document.querySelectorAll('.box').forEach(function (element) {
element.addEventListener('click', function (event) {
println('click event on box');
endTestIfGotAllEvents();
});
element.addEventListener('mouseup', function (event) {
println('mouseup event on box');
endTestIfGotAllEvents();
});
});
internals.click(50, 50);
internals.click(50, 250);
});
</script>
</html>