LibWeb/HTML: Signal a type change

Type changes are now signaled to radio buttons. This causes other radio
buttons in the group to be unchecked if the input element is a checked
radio button after the type change.
This commit is contained in:
Glenn Skrzypczak 2025-04-20 19:48:47 +02:00 committed by Tim Ledbetter
commit d4076ec9fa
Notes: github-actions[bot] 2025-04-23 06:21:06 +00:00
4 changed files with 44 additions and 1 deletions

View file

@ -1456,7 +1456,8 @@ void HTMLInputElement::type_attribute_changed(TypeAttributeState old_state, Type
set_shadow_root(nullptr); set_shadow_root(nullptr);
create_shadow_tree_if_needed(); create_shadow_tree_if_needed();
// FIXME: 5. Signal a type change for the element. (The Radio Button state uses this, in particular.) // 5. Signal a type change for the element. (The Radio Button state uses this, in particular.)
signal_a_type_change();
// 6. Invoke the value sanitization algorithm, if one is defined for the type attribute's new state. // 6. Invoke the value sanitization algorithm, if one is defined for the type attribute's new state.
m_value = value_sanitization_algorithm(m_value); m_value = value_sanitization_algorithm(m_value);
@ -1475,6 +1476,23 @@ void HTMLInputElement::type_attribute_changed(TypeAttributeState old_state, Type
} }
} }
// https://html.spec.whatwg.org/multipage/input.html#radio-button-state-(type=radio):signal-a-type-change
void HTMLInputElement::signal_a_type_change()
{
// https://html.spec.whatwg.org/multipage/input.html#radio-button-state-(type=radio)
// When any of the following phenomena occur, if the element's checkedness state is true after the occurrence,
// the checkedness state of all the other elements in the same radio button group must be set to false:
// ...
// - A type change is signalled for the element.
if (type_state() == TypeAttributeState::RadioButton && checked()) {
root().for_each_in_inclusive_subtree_of_type<HTMLInputElement>([&](auto& element) {
if (element.checked() && &element != this && is_in_same_radio_button_group(*this, element))
element.set_checked(false);
return TraversalDecision::Continue;
});
}
}
// https://html.spec.whatwg.org/multipage/input.html#attr-input-src // https://html.spec.whatwg.org/multipage/input.html#attr-input-src
WebIDL::ExceptionOr<void> HTMLInputElement::handle_src_attribute(String const& value) WebIDL::ExceptionOr<void> HTMLInputElement::handle_src_attribute(String const& value)
{ {

View file

@ -389,6 +389,8 @@ private:
bool m_has_uncommitted_changes { false }; bool m_has_uncommitted_changes { false };
bool m_is_open { false }; bool m_is_open { false };
void signal_a_type_change();
}; };
} }

View file

@ -0,0 +1,6 @@
Harness status: OK
Found 1 tests
1 Pass
Pass Setting type attribute must unset checkedness of other elements

View file

@ -0,0 +1,17 @@
<!DOCTYPE html>
<title>Morphed radio input</title>
<link rel="author" title="Kagami Sascha Rosylight" href="mailto:krosylight@mozilla.com">
<link rel="help" href="https://html.spec.whatwg.org/#radio-button-state-(type=radio)">
<script src="../../../../resources/testharness.js"></script>
<script src="../../../../resources/testharnessreport.js"></script>
<input id="radio" type="radio" name="name_7" checked>
<input id="text" name="name_7" checked>
<script>
"use strict";
test(() => {
text.type = 'radio';
assert_false(radio.checked);
}, "Setting type attribute must unset checkedness of other elements");
</script>