Skip to content

Truly independent web browser

LibWeb: Let click() run on elements in a disabled fieldset

sideshowbarker918768a83c86
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index e860e5eadf3..f36c56a8401 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -3814,6 +3814,15 @@ bool Element::is_actually_disabled() const
return false;
}
+// A button, input, select, or textarea element, or a form-associated custom element, that's disabled. Unlike a disabled
+// fieldset, such a control stops the mouse presses aimed at it from being dispatched.
+bool Element::is_disabled_form_control() const
+{
+ if (is<HTML::HTMLOptGroupElement>(this) || is<HTML::HTMLOptionElement>(this) || is<HTML::HTMLFieldSetElement>(this))
+ return false;
+ return is_actually_disabled();
+}
+
// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#fragment-parsing-algorithm-steps
WebIDL::ExceptionOr<GC::Ref<DOM::DocumentFragment>> Element::parse_fragment(Variant<GC::Ref<Element>, GC::Ref<DocumentFragment>> target, Utf16View markup, HTML::ParserScriptingMode scripting_mode, HTML::FragmentFastPath fast_path, GC::Ptr<TrustedTypes::TrustedHTMLParserOptions> trusted_options)
{
diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.h
index e158b4d3870..28085db0673 100644
--- a/Libraries/LibWeb/DOM/Element.h
+++ b/Libraries/LibWeb/DOM/Element.h
@@ -742,6 +742,7 @@ public:
int scroll_height();
bool is_actually_disabled() const;
+ bool is_disabled_form_control() const;
WebIDL::ExceptionOr<GC::Ptr<Element>> insert_adjacent_element(Utf16View where, GC::Ref<Element> element);
WebIDL::ExceptionOr<void> insert_adjacent_text(Utf16View where, Utf16View data);
diff --git a/Libraries/LibWeb/HTML/HTMLElement.cpp b/Libraries/LibWeb/HTML/HTMLElement.cpp
index 7b9101e3504..4be518d0b1c 100644
--- a/Libraries/LibWeb/HTML/HTMLElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLElement.cpp
@@ -1421,10 +1421,8 @@ void HTMLElement::set_hidden(Variant<bool, double, Utf16String, Empty> const& gi
void HTMLElement::click()
{
// 1. If this element is a form control that is disabled, then return.
- if (auto* form_control = as_if<FormAssociatedElement>(this)) {
- if (!form_control->enabled())
- return;
- }
+ if (is_disabled_form_control())
+ return;
// 2. If this element's click in progress flag is set, then return.
if (m_click_in_progress)
diff --git a/Tests/LibWeb/Text/expected/HTML/click-in-disabled-fieldset.txt b/Tests/LibWeb/Text/expected/HTML/click-in-disabled-fieldset.txt
new file mode 100644
index 00000000000..17640f43108
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/click-in-disabled-fieldset.txt
@@ -0,0 +1,6 @@
+click() on #div: div, fieldset, parent
+click() on #span: span, fieldset, parent
+click() on #fieldset: fieldset, parent
+click() on #button: no click
+click() on #button-in-legend: button-in-legend, fieldset, parent
+click() on #disabled-button: no click
diff --git a/Tests/LibWeb/Text/input/HTML/click-in-disabled-fieldset.html b/Tests/LibWeb/Text/input/HTML/click-in-disabled-fieldset.html
new file mode 100644
index 00000000000..c49de3a3576
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/click-in-disabled-fieldset.html
@@ -0,0 +1,24 @@
+<!doctype html>
+<script src="../include.js"></script>
+<div id="parent">
+ <fieldset disabled id="fieldset">
+ <legend><button id="button-in-legend">in the legend</button></legend>
+ <div id="div">div</div>
+ <button id="button">button</button>
+ <span id="span">span</span>
+ </fieldset>
+</div>
+<button id="disabled-button" disabled>disabled</button>
+<script>
+ // click() only does nothing on a form control that's disabled: An element that a disabled fieldset contains, but
+ // isn't a form control, still gets its click.
+ test(() => {
+ for (const id of ["div", "span", "fieldset", "button", "button-in-legend", "disabled-button"]) {
+ const clicked = [];
+ for (const element of document.querySelectorAll("[id]"))
+ element.onclick = () => clicked.push(element.id);
+ document.getElementById(id).click();
+ println(`click() on #${id}: ${clicked.length ? clicked.join(", ") : "no click"}`);
+ }
+ });
+</script>