Truly independent web browser
LibWeb: Reset select selectedness after attribute changes
Andreas Klinge923d82dc2c5
diff --git a/Libraries/LibWeb/HTML/HTMLOptionElement.cpp b/Libraries/LibWeb/HTML/HTMLOptionElement.cppindex c4893c0667d..8b69d9bcedf 100644--- a/Libraries/LibWeb/HTML/HTMLOptionElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLOptionElement.cpp@@ -60,17 +60,40 @@ void HTMLOptionElement::attribute_changed(Utf16FlyString const& name, Optional<Ureturn;if (name == HTML::AttributeNames::selected) {- if (!value.has_value()) {- // Whenever an option element's selected attribute is removed, if its dirtiness is false, its selectedness must be set to false.- if (!m_dirty)- set_selected_internal(false);- } else {- // Except where otherwise specified, when the element is created, its selectedness must be set to true- // if the element has a selected attribute. Whenever an option element's selected attribute is added,- // if its dirtiness is false, its selectedness must be set to true.- if (!m_dirty)+ // https://html.spec.whatwg.org/multipage/form-elements.html#attr-option-selected+ // Whenever an option element option's selected attribute is added or removed:+ if (old_value.has_value() == value.has_value())+ return;++ // 1. Let hasChanged be false.+ auto has_changed = HTMLSelectElement::HasChanged::No;++ // 2. If option's dirtiness is false:+ if (!m_dirty) {+ // 1. If the selected attribute was added and option's selectedness is false, then set option's selectedness+ // to true and set hasChanged to true.+ if (value.has_value() && !m_selected) {set_selected_internal(true);+ has_changed = HTMLSelectElement::HasChanged::Yes;+ }++ // 2. If the selected attribute was removed and option's selectedness is true, then set option's+ // selectedness to false and set hasChanged to true.+ if (!value.has_value() && m_selected) {+ set_selected_internal(false);+ has_changed = HTMLSelectElement::HasChanged::Yes;+ }}++ // 3. Let select be option's cached nearest ancestor select element.+ auto select = m_cached_nearest_select_element;++ // 4. If select is null, then return.+ if (!select)+ return;++ // 5. Reset a select's selectedness given select and hasChanged.+ select->reset_selectedness(has_changed);} else if (name == HTML::AttributeNames::label) {update_selection_label();}diff --git a/Libraries/LibWeb/HTML/HTMLSelectElement.cpp b/Libraries/LibWeb/HTML/HTMLSelectElement.cppindex 56c7088e9a8..07d70e08844 100644--- a/Libraries/LibWeb/HTML/HTMLSelectElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLSelectElement.cpp@@ -640,13 +640,33 @@ void HTMLSelectElement::form_associated_element_was_inserted()create_shadow_tree_if_needed();}-void HTMLSelectElement::form_associated_element_attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const&, Optional<Utf16String> const& value, Optional<Utf16FlyString> const&)+void HTMLSelectElement::form_associated_element_attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_){- if (name == HTML::AttributeNames::multiple) {- // If the multiple attribute is absent then update the selectedness of the option elements.+ if (namespace_.has_value())+ return;++ // https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element+ if (name == HTML::AttributeNames::size) {+ // Whenever a select element's size attribute is set, changed, or removed, reset a select's selectedness given the select element and false.+ reset_selectedness(HasChanged::No);+ } else if (name == HTML::AttributeNames::multiple && old_value.has_value() != value.has_value()) {+ // Whenever a select element select's multiple attribute is added or removed, run these steps:+ // 1. If the multiple attribute was removed, then set the selectedness of all but the first option element+ // with its selectedness set to true in select's list of options to false.if (!value.has_value()) {- update_selectedness();+ update_cached_list_of_options();+ bool found_selected_option = false;+ for (auto const& option : m_cached_list_of_options) {+ if (!option->selected())+ continue;+ if (found_selected_option)+ option->set_selected_internal(false);+ found_selected_option = true;+ }}++ // 2. Reset a select's selectedness given select and true.+ reset_selectedness(HasChanged::Yes);}// AD-HOC: Changing the required or multiple attribute can change whether the select satisfies its constraints.@@ -759,7 +779,7 @@ void HTMLSelectElement::clone_selected_option_into_select_button()// https://html.spec.whatwg.org/multipage/form-elements.html#selectedness-setting-algorithm// https://whatpr.org/html/11890/form-elements.html#selectedness-setting-algorithm-void HTMLSelectElement::update_selectedness()+bool HTMLSelectElement::update_selectedness(){// The selectedness setting algorithm, given a select element element, is to run the following steps:update_cached_list_of_options();@@ -820,6 +840,24 @@ void HTMLSelectElement::update_selectedness()// AD-HOC: A change to the selected option can change whether a required select satisfies its constraints.CSS::Invalidation::invalidate_style_after_validity_change(*this);++ return should_update_selectedcontent;+}++// https://html.spec.whatwg.org/multipage/form-elements.html#reset-a-select%27s-selectedness+void HTMLSelectElement::reset_selectedness(HasChanged has_changed)+{+ // 1. If the result of running the selectedness setting algorithm given select is true, then set hasChanged to true.+ // NB: Our selectedness setting algorithm already updated the selectedcontent elements if it changed anything.+ if (update_selectedness())+ return;++ // 2. If hasChanged is false, then return.+ if (has_changed == HasChanged::No)+ return;++ // 3. Update a select's descendant selectedcontent elements given select.+ MUST(update_selectedcontent());}bool HTMLSelectElement::is_focusable() constdiff --git a/Libraries/LibWeb/HTML/HTMLSelectElement.h b/Libraries/LibWeb/HTML/HTMLSelectElement.hindex d9e4eada053..ed31df89c6d 100644--- a/Libraries/LibWeb/HTML/HTMLSelectElement.h+++ b/Libraries/LibWeb/HTML/HTMLSelectElement.h@@ -102,7 +102,15 @@ public:void did_select_item(Optional<u32> const& id);- void update_selectedness();+ // NB: Returns whether the selectedness changed, in which case the select's selectedcontent was also updated.+ bool update_selectedness();++ // https://html.spec.whatwg.org/multipage/form-elements.html#reset-a-select%27s-selectedness+ enum class HasChanged : u8 {+ No,+ Yes,+ };+ void reset_selectedness(HasChanged);void clone_selected_option_into_select_button();diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-select-element/option-selectedness-script-mutation.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-select-element/option-selectedness-script-mutation.txtindex d2cab1bf5c2..240f3da2bf6 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-select-element/option-selectedness-script-mutation.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-select-element/option-selectedness-script-mutation.txt@@ -2,10 +2,9 @@ Harness status: OKFound 5 tests-3 Pass-2 Fail-Fail single-select responds to setting selected attribute from script-Fail single-select responds to removing selected attribute from script+5 Pass+Pass single-select responds to setting selected attribute from script+Pass single-select responds to removing selected attribute from scriptPass multi-select keeps multiple selected options when selected attribute changes from scriptPass single-select responds to setting option.selected from scriptPass multi-select responds to option.selected without selected attribute reflection\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-select-element/select-attribute-change-reset.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-select-element/select-attribute-change-reset.txtindex 96596a532a2..0cdbacf9bbd 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-select-element/select-attribute-change-reset.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-select-element/select-attribute-change-reset.txt@@ -2,13 +2,13 @@ Harness status: OKFound 8 tests-2 Pass-6 Fail-Fail Reducing a select's size to 1 selects the first enabled option.+6 Pass+2 Fail+Pass Reducing a select's size to 1 selects the first enabled option.Pass Removing a select's multiple attribute selects the first enabled option.Fail Removing an option's disabled attribute selects it.Fail Removing an optgroup's disabled attribute selects the option inside it.-Fail Adding an option's selected attribute deselects the other options.-Fail Removing an option's selected attribute falls back to the first enabled option.+Pass Adding an option's selected attribute deselects the other options.+Pass Removing an option's selected attribute falls back to the first enabled option.Pass Removing the selected attribute from a dirty option does not change selectedness.-Fail Removing a select's multiple attribute keeps the first selected option.\ No newline at end of file+Pass Removing a select's multiple attribute keeps the first selected option.\ No newline at end of file