Skip to content

Truly independent web browser

LibWeb: Render select elements with multiple or size > 1 as list boxes

Andreas Klingcf4436a7e302
diff --git a/Libraries/LibWeb/CSS/Default.css b/Libraries/LibWeb/CSS/Default.css
index 7f8905da30d..200702378e7 100644
--- a/Libraries/LibWeb/CSS/Default.css
+++ b/Libraries/LibWeb/CSS/Default.css
@@ -85,6 +85,20 @@ button, input[type=submit], input[type=button], input[type=reset], input[type=co
}
}
+/* https://html.spec.whatwg.org/multipage/rendering.html#the-select-element-2
+ * NB: A select that renders as a list box looks like a text field holding its options, which have their own padding.
+ */
+select:is([multiple], [size]:not([size="0"], [size="1"])) {
+ padding: 1px 0;
+ vertical-align: text-bottom;
+ background-color: Field;
+ color: FieldText;
+
+ &:hover, &:active:hover {
+ background-color: Field;
+ }
+}
+
input[type=file]:enabled::file-selector-button:hover {
background-color: -libweb-ButtonFaceHover;
}
diff --git a/Libraries/LibWeb/HTML/HTMLOptGroupElement.cpp b/Libraries/LibWeb/HTML/HTMLOptGroupElement.cpp
index 8a8e712c80c..3e1bce25792 100644
--- a/Libraries/LibWeb/HTML/HTMLOptGroupElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLOptGroupElement.cpp
@@ -39,4 +39,18 @@ void HTMLOptGroupElement::removed_from(IsSubtreeRoot is_subtree_root, Node* old_
static_cast<HTMLSelectElement&>(*old_ancestor).update_selectedness();
}
+void HTMLOptGroupElement::attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_)
+{
+ Base::attribute_changed(name, old_value, value, namespace_);
+
+ // NB: The rows of a list box show the label of the group, leave out a hidden group, and gray out the options of a
+ // disabled one.
+ if (namespace_.has_value() || old_value == value)
+ return;
+ if (name == AttributeNames::label || name == AttributeNames::hidden || name == AttributeNames::disabled) {
+ if (auto* select = as_if<HTMLSelectElement>(parent()))
+ select->list_box_options_did_change();
+ }
+}
+
}
diff --git a/Libraries/LibWeb/HTML/HTMLOptGroupElement.h b/Libraries/LibWeb/HTML/HTMLOptGroupElement.h
index ce16d8a061a..67d8543eeca 100644
--- a/Libraries/LibWeb/HTML/HTMLOptGroupElement.h
+++ b/Libraries/LibWeb/HTML/HTMLOptGroupElement.h
@@ -28,6 +28,7 @@ private:
virtual bool is_html_optgroup_element() const final { return true; }
virtual void removed_from(IsSubtreeRoot, Node* old_ancestor, Node& old_root) override;
virtual void inserted() override;
+ virtual void attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_) override;
};
}
diff --git a/Libraries/LibWeb/HTML/HTMLOptionElement.cpp b/Libraries/LibWeb/HTML/HTMLOptionElement.cpp
index c770c56996b..05b622b11e4 100644
--- a/Libraries/LibWeb/HTML/HTMLOptionElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLOptionElement.cpp
@@ -48,6 +48,8 @@ void HTMLOptionElement::visit_edges(Cell::Visitor& visitor)
void HTMLOptionElement::update_selection_label()
{
+ if (m_cached_nearest_select_element)
+ m_cached_nearest_select_element->list_box_options_did_change();
if (selected()) {
if (auto* select_element = first_ancestor_of_type<HTMLSelectElement>()) {
select_element->clone_selected_option_into_select_button();
@@ -134,6 +136,9 @@ void HTMLOptionElement::attribute_changed(Utf16FlyString const& name, Optional<U
// 5. Reset a select's selectedness given select and hasChanged.
select->reset_selectedness(has_changed);
+ } else if (name == HTML::AttributeNames::disabled || name == HTML::AttributeNames::hidden) {
+ if (m_cached_nearest_select_element)
+ m_cached_nearest_select_element->list_box_options_did_change();
} else if (name == HTML::AttributeNames::label) {
update_selection_label();
}
@@ -161,7 +166,10 @@ void HTMLOptionElement::set_selected_internal(bool selected)
}
}
+ bool const changed = m_selected != selected;
m_selected = selected;
+ if (changed && m_cached_nearest_select_element)
+ m_cached_nearest_select_element->list_box_option_selectedness_did_change(*this);
if (selected)
m_selectedness_update_index = m_next_selectedness_update_index++;
}
@@ -323,6 +331,10 @@ void HTMLOptionElement::update_nearest_select_element()
}
m_cached_nearest_select_element = new_select;
update_label_sizer_text();
+ if (old_select)
+ old_select->list_box_options_did_change();
+ if (new_select)
+ new_select->list_box_options_did_change();
// 1. If oldSelect is not null, then run the selectedness setting algorithm given oldSelect.
if (old_select)
diff --git a/Libraries/LibWeb/HTML/HTMLSelectElement.cpp b/Libraries/LibWeb/HTML/HTMLSelectElement.cpp
index 713e4af86ad..9a22655e358 100644
--- a/Libraries/LibWeb/HTML/HTMLSelectElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLSelectElement.cpp
@@ -7,15 +7,19 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <LibJS/Runtime/NativeFunction.h>
+#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/CSS/CSSStyleProperties.h>
#include <LibWeb/CSS/Invalidation/ElementStateInvalidator.h>
#include <LibWeb/CSS/Invalidation/FormControlInvalidator.h>
#include <LibWeb/CSS/PropertyID.h>
#include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
#include <LibWeb/CSS/StyleValues/KeywordStyleValue.h>
+#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/ElementFactory.h>
#include <LibWeb/DOM/Event.h>
+#include <LibWeb/DOM/IDLEventListener.h>
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/HTMLDataListElement.h>
@@ -33,6 +37,9 @@
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Namespace.h>
#include <LibWeb/Page/Page.h>
+#include <LibWeb/UIEvents/EventNames.h>
+#include <LibWeb/UIEvents/MouseEvent.h>
+#include <LibWeb/WebIDL/CallbackType.h>
#include <LibWebCommon/Infra/Strings.h>
namespace Web::HTML {
@@ -68,6 +75,11 @@ void HTMLSelectElement::visit_edges(Cell::Visitor& visitor)
visitor.visit(m_chevron_icon_element);
visitor.visit(m_cached_list_of_options);
visitor.visit(m_select_item_option_elements);
+ visitor.visit(m_list_box_element);
+ for (auto const& row : m_list_box_rows) {
+ visitor.visit(row.element);
+ visitor.visit(row.option);
+ }
}
// https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-size
@@ -383,6 +395,7 @@ void HTMLSelectElement::children_changed(ChildrenChangedMetadata const& metadata
update_cached_list_of_options();
update_selectedness();
+ list_box_options_did_change();
}
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-type
@@ -615,6 +628,9 @@ WebIDL::ExceptionOr<void> HTMLSelectElement::show_picker()
void HTMLSelectElement::activation_behavior(DOM::Event const& event)
{
+ // NB: A list box shows its options in place, and is interacted with there.
+ if (renders_as_list_box())
+ return;
if (event.is_trusted())
show_the_picker_if_applicable();
}
@@ -671,6 +687,24 @@ void HTMLSelectElement::form_associated_element_attribute_changed(Utf16FlyString
reset_selectedness(HasChanged::Yes);
}
+ // NB: The size and multiple attributes decide whether the select renders as a drop-down box or a list box.
+ if ((name == HTML::AttributeNames::size || name == HTML::AttributeNames::multiple) && shadow_root()
+ && renders_as_list_box() != !!m_list_box_element) {
+ set_shadow_root(nullptr);
+ m_inner_text_element = nullptr;
+ m_selected_label_element = nullptr;
+ m_label_sizer_element = nullptr;
+ m_chevron_icon_element = nullptr;
+ m_chevron_icon_hidden = false;
+ m_list_box_element = nullptr;
+ m_list_box_rows.clear();
+ m_list_box_anchor_index = {};
+ m_list_box_focus_index = {};
+ create_shadow_tree_if_needed();
+ } else if (name == HTML::AttributeNames::size && m_list_box_element) {
+ update_list_box();
+ }
+
// AD-HOC: Changing the required or multiple attribute can change whether the select satisfies its constraints.
if (name == HTML::AttributeNames::required || name == HTML::AttributeNames::multiple)
CSS::Invalidation::invalidate_style_after_validity_change(*this);
@@ -708,6 +742,11 @@ void HTMLSelectElement::create_shadow_tree_if_needed()
shadow_root->set_user_agent_internal(true);
set_shadow_root(shadow_root);
+ if (renders_as_list_box()) {
+ create_list_box_shadow_tree(shadow_root);
+ return;
+ }
+
auto border = DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
border->set_attribute_value(HTML::AttributeNames::style, R"~~~(
display: flex;
@@ -1044,4 +1083,297 @@ GC::Ptr<HTMLSelectElement const> get_nearest_ancestor_select(DOM::Element const&
return get_nearest_ancestor_select(const_cast<DOM::Element&>(element));
}
+static constexpr double list_box_row_height_in_em = 1.2;
+
+// https://html.spec.whatwg.org/multipage/rendering.html#the-select-element-2
+bool HTMLSelectElement::renders_as_list_box() const
+{
+ // A select element whose multiple attribute is present, or whose display size is greater than 1, is expected to
+ // render as a multi-select list box.
+ return has_attribute(AttributeNames::multiple) || display_size() > 1;
+}
+
+bool HTMLSelectElement::is_presentational_hint(Utf16FlyString const& name) const
+{
+ if (Base::is_presentational_hint(name))
+ return true;
+ return first_is_one_of(name, HTML::AttributeNames::size, HTML::AttributeNames::multiple);
+}
+
+void HTMLSelectElement::apply_presentational_hints(Vector<CSS::StyleProperty>& properties) const
+{
+ Base::apply_presentational_hints(properties);
+
+ // AD-HOC: A list box is as tall as the number of rows its display size asks for, whatever options it holds. Its
+ // rows are its contents, so size containment keeps that height through its contain-intrinsic-block-size.
+ if (!renders_as_list_box())
+ return;
+ auto rows = max(display_size(), 1u);
+ properties.append({ .property_id = CSS::PropertyID::ContainIntrinsicBlockSize, .value = CSS::LengthStyleValue::create(CSS::Length(rows * list_box_row_height_in_em, CSS::LengthUnit::Em)) });
+}
+
+void HTMLSelectElement::create_list_box_shadow_tree(DOM::ShadowRoot& shadow_root)
+{
+ m_list_box_element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
+ MUST(shadow_root.append_child(*m_list_box_element));
+
+ auto mousedown_function = JS::NativeFunction::create(
+ HTML::relevant_realm(*this), [this](JS::VM& vm) {
+ auto event = vm.argument(0).is_object()
+ ? Bindings::impl_from<UIEvents::MouseEvent>(&vm.argument(0).as_object())
+ : nullptr;
+ if (event)
+ handle_list_box_mousedown(*event);
+ return JS::js_undefined();
+ },
+ 1, Utf16FlyString {}, &HTML::relevant_realm(*this));
+ auto mousedown_callback = GC::Heap::the().allocate<WebIDL::CallbackType>(*mousedown_function, HTML::relevant_realm(*this));
+ m_list_box_element->add_event_listener_without_options(UIEvents::EventNames::mousedown, DOM::IDLEventListener::create(mousedown_callback));
+
+ update_list_box();
+}
+
+void HTMLSelectElement::list_box_options_did_change()
+{
+ // NB: The rows are built again once for any number of changes made to the options before the next task, so that
+ // inserting the options of a long list one by one does not build its rows over and over.
+ if (!m_list_box_element || m_list_box_update_queued)
+ return;
+ m_list_box_update_queued = true;
+ queue_an_element_task(HTML::Task::Source::DOMManipulation, [this] {
+ m_list_box_update_queued = false;
+ if (m_list_box_element)
+ update_list_box();
+ });
+}
+
+void HTMLSelectElement::update_list_box_row_styles()
+{
+ for (auto const& row : m_list_box_rows)
+ update_list_box_row_style(row.element, row.option);
+}
+
+void HTMLSelectElement::did_receive_focus()
+{
+ m_list_box_is_focused = true;
+ update_list_box_row_styles();
+}
+
+void HTMLSelectElement::did_lose_focus()
+{
+ m_list_box_is_focused = false;
+ update_list_box_row_styles();
+}
+
+void HTMLSelectElement::list_box_option_selectedness_did_change(HTMLOptionElement const& option)
+{
+ if (!m_list_box_element)
+ return;
+ for (auto const& row : m_list_box_rows) {
+ if (row.option.ptr() == &option) {
+ update_list_box_row_style(row.element, option);
+ return;
+ }
+ }
+}
+
+void HTMLSelectElement::update_list_box_row_style(DOM::Element& row, HTMLOptionElement const& option) const
+{
+ StringBuilder style;
+ style.append("display: block; min-block-size: 1.2em; line-height: 1.2em; padding-inline: 4px; user-select: none;"sv);
+ if (as_if<HTMLOptGroupElement>(option.parent()))
+ style.append(" padding-inline-start: 1.25em;"sv);
+ // NB: The selection of a list box without focus is gray, so that it stands out less than one the keys move.
+ if (option.selected() && m_list_box_is_focused)
+ style.append(" background-color: AccentColor; color: AccentColorText;"sv);
+ else if (option.selected())
+ style.append(" background-color: color-mix(in srgb, currentcolor 22%, transparent);"sv);
+ else if (option.disabled())
+ style.append(" color: GrayText;"sv);
+ row.set_attribute_value(HTML::AttributeNames::style, Utf16String::from_utf8(style.string_view()));
+}
+
+void HTMLSelectElement::update_list_box()
+{
+ VERIFY(m_list_box_element);
+
+ auto rows = max(display_size(), 1u);
+ m_list_box_element->set_attribute_value(HTML::AttributeNames::style, Utf16String::formatted("block-size: {}em; overflow-x: hidden; overflow-y: auto;", rows * list_box_row_height_in_em));
+
+ m_list_box_rows.clear();
+ m_list_box_anchor_index = {};
+ m_list_box_focus_index = {};
+ m_list_box_element->replace_all(nullptr);
+
+ auto append_option_row = [&](HTMLOptionElement& option) {
+ if (option.has_attribute(AttributeNames::hidden))
+ return;
+ auto row = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
+ MUST(row->append_child(document().create_text_node(Infra::strip_and_collapse_whitespace(option.label()))));
+ update_list_box_row_style(row, option);
+ MUST(m_list_box_element->append_child(row));
+ m_list_box_rows.append({ row, option });
+ };
+
+ for (auto const& child : children_as_vector()) {
+ if (auto* option = as_if<HTMLOptionElement>(*child)) {
+ append_option_row(*option);
+ } else if (auto* group = as_if<HTMLOptGroupElement>(*child)) {
+ if (group->has_attribute(AttributeNames::hidden))
+ continue;
+ auto label_row = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
+ label_row->set_attribute_value(HTML::AttributeNames::style, "display: block; min-block-size: 1.2em; line-height: 1.2em; padding-inline: 4px; font-weight: bold; user-select: none;"_utf16);
+ MUST(label_row->append_child(document().create_text_node(group->get_attribute(AttributeNames::label).value_or({}))));
+ MUST(m_list_box_element->append_child(label_row));
+ for (auto const& group_child : group->children_as_vector()) {
+ if (auto* group_option = as_if<HTMLOptionElement>(*group_child))
+ append_option_row(*group_option);
+ }
+ }
+ }
+}
+
+void HTMLSelectElement::handle_list_box_mousedown(UIEvents::MouseEvent& event)
+{
+ if (event.button() != 0 || !is_mutable())
+ return;
+
+ // Find the row that was pressed, as the child of the list box that holds the event's target.
+ auto* node = as_if<DOM::Node>(event.target().ptr());
+ while (node && node->parent() != m_list_box_element.ptr())
+ node = node->parent();
+ if (!node)
+ return;
+
+ Optional<size_t> index;
+ for (size_t i = 0; i < m_list_box_rows.size(); ++i) {
+ if (m_list_box_rows[i].element.ptr() == node) {
+ index = i;
+ break;
+ }
+ }
+ if (!index.has_value() || m_list_box_rows[*index].option->disabled())
+ return;
+
+ // NB: Pressing an option chooses it. In a multiple select, a press with Ctrl or Cmd held toggles the option, and a
+ // press with Shift held chooses the options from the last one pressed up to this one.
+ bool const multiple = has_attribute(AttributeNames::multiple);
+ bool const toggle = multiple && (event.ctrl_key() || event.meta_key());
+ if (!toggle) {
+ choose_list_box_row(*index, multiple && event.shift_key());
+ return;
+ }
+
+ auto& option = m_list_box_rows[*index].option;
+ option->set_selected(!option->selected());
+ m_list_box_anchor_index = *index;
+ m_list_box_focus_index = *index;
+ send_select_update_notifications();
+}
+
+void HTMLSelectElement::choose_list_box_row(size_t index, bool extend)
+{
+ update_cached_list_of_options();
+ Vector<bool> was_selected;
+ for (auto const& option : m_cached_list_of_options)
+ was_selected.append(option->selected());
+
+ // NB: Set the selectedness and dirtiness of each option directly, then run the selectedness setting algorithm
+ // once, instead of letting every option ask for a reset.
+ auto set_selectedness = [](HTMLOptionElement& option, bool selected) {
+ option.set_selected_internal(selected);
+ option.m_dirty = true;
+ };
+ for (auto const& option : m_cached_list_of_options)
+ set_selectedness(*option, false);
+ if (extend && m_list_box_anchor_index.has_value()) {
+ auto first = min(*m_list_box_anchor_index, index);
+ auto last = max(*m_list_box_anchor_index, index);
+ for (size_t i = first; i <= last; ++i) {
+ if (!m_list_box_rows[i].option->disabled())
+ set_selectedness(*m_list_box_rows[i].option, true);
+ }
+ } else {
+ set_selectedness(*m_list_box_rows[index].option, true);
+ m_list_box_anchor_index = index;
+ }
+ update_selectedness();
+ m_list_box_focus_index = index;
+ Bindings::ScrollIntoViewOptions scroll_options;
+ scroll_options.block = Bindings::ScrollLogicalPosition::Nearest;
+ scroll_options.inline_ = Bindings::ScrollLogicalPosition::Nearest;
+ m_list_box_rows[index].element->scroll_into_view(scroll_options, nullptr);
+
+ bool changed = false;
+ for (size_t i = 0; i < m_cached_list_of_options.size(); ++i) {
+ if (m_cached_list_of_options[i]->selected() != was_selected[i])
+ changed = true;
+ }
+ if (changed)
+ send_select_update_notifications();
+}
+
+bool HTMLSelectElement::handle_list_box_key(UIEvents::KeyCode key, u32 modifiers)
+{
+ if (m_list_box_rows.is_empty() || !is_mutable())
+ return false;
+ if (modifiers != UIEvents::Mod_None && modifiers != UIEvents::Mod_Shift)
+ return false;
+
+ // NB: The keys move from the option last chosen, or from the first selected option.
+ Optional<size_t> current = m_list_box_focus_index;
+ if (!current.has_value()) {
+ for (size_t i = 0; i < m_list_box_rows.size(); ++i) {
+ if (m_list_box_rows[i].option->selected()) {
+ current = i;
+ break;
+ }
+ }
+ }
+
+ auto is_choosable = [&](size_t i) { return !m_list_box_rows[i].option->disabled(); };
+ Optional<size_t> target;
+ switch (key) {
+ case UIEvents::KeyCode::Key_Down:
+ for (size_t i = current.has_value() ? *current + 1 : 0; i < m_list_box_rows.size(); ++i) {
+ if (is_choosable(i)) {
+ target = i;
+ break;
+ }
+ }
+ break;
+ case UIEvents::KeyCode::Key_Up:
+ // NB: Without a current option, Up starts from the end of the list.
+ for (size_t i = current.value_or(m_list_box_rows.size()); i-- > 0;) {
+ if (is_choosable(i)) {
+ target = i;
+ break;
+ }
+ }
+ break;
+ case UIEvents::KeyCode::Key_Home:
+ for (size_t i = 0; i < m_list_box_rows.size(); ++i) {
+ if (is_choosable(i)) {
+ target = i;
+ break;
+ }
+ }
+ break;
+ case UIEvents::KeyCode::Key_End:
+ for (size_t i = m_list_box_rows.size(); i-- > 0;) {
+ if (is_choosable(i)) {
+ target = i;
+ break;
+ }
+ }
+ break;
+ default:
+ return false;
+ }
+
+ if (target.has_value())
+ choose_list_box_row(*target, has_attribute(AttributeNames::multiple) && modifiers == UIEvents::Mod_Shift);
+ return true;
+}
+
}
diff --git a/Libraries/LibWeb/HTML/HTMLSelectElement.h b/Libraries/LibWeb/HTML/HTMLSelectElement.h
index ff0550486bd..a9cf736a681 100644
--- a/Libraries/LibWeb/HTML/HTMLSelectElement.h
+++ b/Libraries/LibWeb/HTML/HTMLSelectElement.h
@@ -15,6 +15,7 @@
#include <LibWeb/HTML/HTMLElement.h>
#include <LibWeb/HTML/HTMLOptionsCollection.h>
#include <LibWebCommon/HTML/SelectItem.h>
+#include <LibWebCommon/UIEvents/KeyCode.h>
#include <LibWebCommon/WebIDL/Types.h>
namespace Web::HTML {
@@ -103,6 +104,9 @@ public:
virtual void activation_behavior(DOM::Event const&) override;
virtual void form_associated_element_was_inserted() override;
+
+ virtual bool is_presentational_hint(Utf16FlyString const&) const override;
+ virtual void apply_presentational_hints(Vector<CSS::StyleProperty>&) const override;
virtual void form_associated_element_attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_) override;
void did_select_item(Optional<u32> const& id);
@@ -119,6 +123,12 @@ public:
void clone_selected_option_into_select_button();
+ // https://html.spec.whatwg.org/multipage/rendering.html#the-select-element-2
+ bool renders_as_list_box() const;
+ void list_box_options_did_change();
+ void list_box_option_selectedness_did_change(HTMLOptionElement const&);
+ bool handle_list_box_key(UIEvents::KeyCode, u32 modifiers);
+
bool can_skip_selectedness_update_for_inserted_option(HTMLOptionElement const&) const;
bool user_validity() const { return m_user_validity; }
@@ -177,6 +187,26 @@ private:
GC::Ptr<DOM::Element> m_chevron_icon_element;
bool m_chevron_icon_hidden { false };
+ void create_list_box_shadow_tree(DOM::ShadowRoot&);
+ void update_list_box();
+ void update_list_box_row_style(DOM::Element& row, HTMLOptionElement const&) const;
+ void update_list_box_row_styles();
+ virtual void did_receive_focus() override;
+ virtual void did_lose_focus() override;
+ bool m_list_box_is_focused { false };
+ void handle_list_box_mousedown(UIEvents::MouseEvent&);
+ void choose_list_box_row(size_t index, bool extend);
+
+ GC::Ptr<DOM::Element> m_list_box_element;
+ struct ListBoxRow {
+ GC::Ref<DOM::Element> element;
+ GC::Ref<HTMLOptionElement> option;
+ };
+ Vector<ListBoxRow> m_list_box_rows;
+ Optional<size_t> m_list_box_anchor_index;
+ Optional<size_t> m_list_box_focus_index;
+ bool m_list_box_update_queued { false };
+
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#user-validity
bool m_user_validity { false };
};
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index 224aa815e7a..2ef8704815b 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -2014,6 +2014,12 @@ EventResult EventHandler::handle_keydown(UIEvents::KeyCode key, u32 modifiers, u
GC::Ref<DOM::Document> document = *m_navigable->active_document();
+ // NB: A focused list box moves its selection with the arrow, Home and End keys instead of scrolling.
+ if (auto* select = as_if<HTML::HTMLSelectElement>(document->focused_area().ptr()); select && select->renders_as_list_box()) {
+ if (select->handle_list_box_key(key, modifiers & ~UIEvents::Mod_Keypad))
+ return EventResult::Handled;
+ }
+
if (!(modifiers & UIEvents::KeyModifier::Mod_Ctrl)) {
if (key == UIEvents::KeyCode::Key_Tab)
return focus_sequentially(modifiers & UIEvents::KeyModifier::Mod_Shift ? HTML::SequentialFocusDirection::Backward : HTML::SequentialFocusDirection::Forward);
diff --git a/Tests/LibWeb/Layout/expected/select-list-box-optgroup-attribute-changes.txt b/Tests/LibWeb/Layout/expected/select-list-box-optgroup-attribute-changes.txt
new file mode 100644
index 00000000000..d84f56a4e77
--- /dev/null
+++ b/Tests/LibWeb/Layout/expected/select-list-box-optgroup-attribute-changes.txt
@@ -0,0 +1,27 @@
+Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline
+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 147.953125 0+0+0] [BFC] children: not-inline
+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 131.953125 0+0+8] children: inline
+ frag 0 from BlockContainer start: 0, length: 0, rect: [9,10 69.84375x127.953125] baseline: 128.75
+ BlockContainer <select> at [9,10] inline-block [0+1+0 69.84375 0+1+0] [0+1+1 127.953125 1+1+0] [BFC] children: not-inline
+ BlockContainer <div> at [9,10] [0+0+0 69.84375 0+0+0] [0+0+0 127.953125 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [13,10] [0+0+4 61.84375 4+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 9, rect: [13,11 61.84375x14] baseline: 10.984375
+ "New label"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [25.65625,26] [0+0+16.65625 49.1875 4+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [25.65625,27 7.78125x14] baseline: 10.984375
+ "B"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [13,42] [0+0+4 61.84375 4+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 8, rect: [13,43 53.875x14] baseline: 10.984375
+ "Disabled"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [25.65625,58] [0+0+16.65625 49.1875 4+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [25.65625,59 8.59375x14] baseline: 10.984375
+ "C"
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+
+SC for Viewport<#document> [0,0 800x600] (z-index: auto)
+ SC for BlockContainer<HTML> [0,0 800x147.953125] (z-index: auto)
diff --git a/Tests/LibWeb/Layout/input/select-list-box-optgroup-attribute-changes.html b/Tests/LibWeb/Layout/input/select-list-box-optgroup-attribute-changes.html
new file mode 100644
index 00000000000..4f78d955dbf
--- /dev/null
+++ b/Tests/LibWeb/Layout/input/select-list-box-optgroup-attribute-changes.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<select size="8">
+ <optgroup id="hidden" label="Hidden"><option>A</option></optgroup>
+ <optgroup id="relabeled" label="Old label"><option>B</option></optgroup>
+ <optgroup id="disabled" label="Disabled"><option>C</option></optgroup>
+</select>
+<script>
+ document.body.offsetWidth;
+ document.getElementById("hidden").hidden = true;
+ document.getElementById("relabeled").label = "New label";
+ document.getElementById("disabled").disabled = true;
+</script>
diff --git a/Tests/LibWeb/Text/expected/select-list-box-interaction.txt b/Tests/LibWeb/Text/expected/select-list-box-interaction.txt
new file mode 100644
index 00000000000..f1ba4e77d1a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/select-list-box-interaction.txt
@@ -0,0 +1,15 @@
+change on single: One
+single after pressing a disabled option: One
+change on single: Four
+change on multi: A
+change on multi: A,C
+change on multi: C
+change on multi: A,B,C,D
+change on multi: B
+change on single: Two
+change on single: One
+single after ArrowUp past a disabled option: One
+change on single: Four
+change on multi: B,C
+change on multi: D
+empty after ArrowUp: Y
diff --git a/Tests/LibWeb/Text/input/select-list-box-interaction.html b/Tests/LibWeb/Text/input/select-list-box-interaction.html
new file mode 100644
index 00000000000..e7cddbd132b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/select-list-box-interaction.html
@@ -0,0 +1,58 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<style>
+ body {
+ margin: 0;
+ font: 16px SerenitySans;
+ }
+</style>
+<select id="single" size="4"><option>One</option><option selected>Two</option><option disabled>Three</option><option>Four</option></select>
+<select id="multi" multiple><option>A</option><option>B</option><option>C</option><option>D</option></select>
+<select id="empty" multiple size="4"><option>W</option><option>X</option><option>Y</option><option disabled>Z</option></select>
+<script>
+ function rowPoint(select, index) {
+ const rect = select.getBoundingClientRect();
+ const rowHeight = (rect.height - 4) / select.size;
+ return [rect.left + 10, rect.top + 2 + rowHeight * (index + 0.5)];
+ }
+
+ function press(select, index, modifiers = 0) {
+ const [x, y] = rowPoint(select, index);
+ internals.mouseDown(x, y, 1, 0, modifiers);
+ internals.mouseUp(x, y, 0, modifiers);
+ }
+
+ function selectedLabels(select) {
+ return [...select.selectedOptions].map(option => option.label).join(",");
+ }
+
+ test(() => {
+ multi.size = 4;
+ for (const select of [single, multi])
+ select.addEventListener("change", () => println(`change on ${select.id}: ${selectedLabels(select)}`));
+
+ press(single, 0);
+ press(single, 2);
+ println(`single after pressing a disabled option: ${selectedLabels(single)}`);
+ press(single, 3);
+
+ press(multi, 0);
+ press(multi, 2, internals.MOD_SUPER);
+ press(multi, 0, internals.MOD_SUPER);
+ press(multi, 3, internals.MOD_SHIFT);
+ press(multi, 1);
+
+ single.focus();
+ internals.sendKey(single, "Up");
+ internals.sendKey(single, "Up");
+ println(`single after ArrowUp past a disabled option: ${selectedLabels(single)}`);
+ internals.sendKey(single, "Home");
+ internals.sendKey(single, "End");
+ multi.focus();
+ internals.sendKey(multi, "Down", internals.MOD_SHIFT);
+ internals.sendKey(multi, "Down");
+ empty.focus();
+ internals.sendKey(empty, "Up");
+ println(`empty after ArrowUp: ${selectedLabels(empty)}`);
+ });
+</script>