Skip to content

Truly independent web browser

LibWeb: Size select elements to fit their widest option

Andreas Kling6039117cb40a
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 8de51ba82dd..6e8477aa9d9 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -3438,6 +3438,12 @@ void Element::children_changed(ChildrenChangedMetadata const& metadata)
document().style_computer().style_engine().note_children_changed_beside_flown_transaction(style_node_id());
if (child_style_uses_tree_counting_function())
document().style_computer().style_engine().restyle_children_reading_sibling_position(*this);
+
+ // NB: Option labels include descendant text, so mutations below a wrapper must update their cached labels too.
+ for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
+ if (auto* option = as_if<HTML::HTMLOptionElement>(ancestor))
+ option->descendant_text_content_changed({});
+ }
}
void Element::set_style_node_id(CSS::StyleNodeID style_node_id)
diff --git a/Libraries/LibWeb/HTML/HTMLOptionElement.cpp b/Libraries/LibWeb/HTML/HTMLOptionElement.cpp
index 8b69d9bcedf..3244debd119 100644
--- a/Libraries/LibWeb/HTML/HTMLOptionElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLOptionElement.cpp
@@ -10,6 +10,7 @@
#include <LibWeb/CSS/Invalidation/ElementStateInvalidator.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/DocumentFragment.h>
+#include <LibWeb/DOM/ElementFactory.h>
#include <LibWeb/DOM/Node.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/HTML/HTMLDataListElement.h>
@@ -19,7 +20,9 @@
#include <LibWeb/HTML/HTMLScriptElement.h>
#include <LibWeb/HTML/HTMLSelectElement.h>
#include <LibWeb/HTML/HTMLSelectedContentElement.h>
+#include <LibWeb/HTML/TagNames.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
+#include <LibWeb/Namespace.h>
#include <LibWeb/SVG/SVGScriptElement.h>
#include <LibWebCommon/Infra/Strings.h>
@@ -40,9 +43,9 @@ void HTMLOptionElement::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_cached_nearest_select_element);
+ visitor.visit(m_label_sizer_text);
}
-// FIXME: This needs to be called any time a descendant's text is modified.
void HTMLOptionElement::update_selection_label()
{
if (selected()) {
@@ -50,6 +53,34 @@ void HTMLOptionElement::update_selection_label()
select_element->clone_selected_option_into_select_button();
}
}
+ update_label_sizer_text();
+}
+
+void HTMLOptionElement::update_label_sizer_text()
+{
+ GC::Ptr<DOM::Element> label_sizer;
+ if (m_cached_nearest_select_element)
+ label_sizer = m_cached_nearest_select_element->m_label_sizer_element;
+
+ if (m_label_sizer_text && (!label_sizer || !m_label_sizer_text->parent() || m_label_sizer_text->parent()->parent() != label_sizer.ptr())) {
+ if (auto* row = m_label_sizer_text->parent())
+ row->remove();
+ m_label_sizer_text = nullptr;
+ }
+ if (!label_sizer)
+ return;
+
+ // NB: Each option has a block row, with the same inherited white-space as the selected label.
+ auto text = label();
+ if (m_label_sizer_text) {
+ if (m_label_sizer_text->data() != text)
+ m_label_sizer_text->set_data(text);
+ return;
+ }
+ auto row = DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
+ m_label_sizer_text = document().create_text_node(move(text));
+ MUST(row->append_child(*m_label_sizer_text));
+ MUST(label_sizer->append_child(*row));
}
void HTMLOptionElement::attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_)
@@ -282,6 +313,7 @@ void HTMLOptionElement::update_nearest_select_element()
++new_select->m_number_of_selected_options;
}
m_cached_nearest_select_element = new_select;
+ update_label_sizer_text();
// 1. If oldSelect is not null, then run the selectedness setting algorithm given oldSelect.
if (old_select)
diff --git a/Libraries/LibWeb/HTML/HTMLOptionElement.h b/Libraries/LibWeb/HTML/HTMLOptionElement.h
index e82b97b8a9b..052314e44e0 100644
--- a/Libraries/LibWeb/HTML/HTMLOptionElement.h
+++ b/Libraries/LibWeb/HTML/HTMLOptionElement.h
@@ -7,6 +7,7 @@
#pragma once
+#include <AK/Badge.h>
#include <AK/Utf16View.h>
#include <LibWeb/Export.h>
#include <LibWeb/HTML/HTMLElement.h>
@@ -35,6 +36,8 @@ public:
[[nodiscard]] Utf16String label() const;
void set_label(Utf16View);
+ void descendant_text_content_changed(Badge<DOM::Element>) { update_selection_label(); }
+
int index() const;
bool disabled() const;
@@ -68,6 +71,7 @@ private:
void ask_for_a_reset();
void update_selection_label();
+ void update_label_sizer_text();
// https://html.spec.whatwg.org/multipage/form-elements.html#update-an-option's-nearest-ancestor-select
void update_nearest_select_element();
@@ -82,6 +86,9 @@ private:
// https://html.spec.whatwg.org/multipage/form-elements.html#cached-nearest-ancestor-select-element
GC::Ptr<HTMLSelectElement> m_cached_nearest_select_element;
+
+ // The text in this option's row in its select's label sizer, which sizes the select to fit its widest option.
+ GC::Ptr<DOM::Text> m_label_sizer_text;
};
}
diff --git a/Libraries/LibWeb/HTML/HTMLSelectElement.cpp b/Libraries/LibWeb/HTML/HTMLSelectElement.cpp
index 07d70e08844..713e4af86ad 100644
--- a/Libraries/LibWeb/HTML/HTMLSelectElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLSelectElement.cpp
@@ -63,6 +63,8 @@ void HTMLSelectElement::visit_edges(Cell::Visitor& visitor)
visitor.visit(m_options);
visitor.visit(m_selected_options);
visitor.visit(m_inner_text_element);
+ visitor.visit(m_selected_label_element);
+ visitor.visit(m_label_sizer_element);
visitor.visit(m_chevron_icon_element);
visitor.visit(m_cached_list_of_options);
visitor.visit(m_select_item_option_elements);
@@ -725,6 +727,28 @@ void HTMLSelectElement::create_shadow_tree_if_needed()
)~~~"_utf16);
MUST(border->append_child(*m_inner_text_element));
+ m_selected_label_element = DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
+ m_selected_label_element->set_attribute_value(HTML::AttributeNames::style, R"~~~(
+ overflow: clip;
+ text-overflow: inherit;
+ )~~~"_utf16);
+ MUST(m_inner_text_element->append_child(*m_selected_label_element));
+
+ // NB: The label sizer holds the label of every option in its own block row, without taking up any block size or
+ // being painted. It gives the select enough inline size for its widest option, so choosing another option
+ // does not resize it. It floats so that its rows never give the select a baseline, even when no option is selected.
+ m_label_sizer_element = DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
+ m_label_sizer_element->set_attribute_value(HTML::AttributeNames::style, R"~~~(
+ float: inline-start;
+ block-size: 0;
+ overflow: clip;
+ visibility: hidden;
+ )~~~"_utf16);
+ MUST(m_inner_text_element->append_child(*m_label_sizer_element));
+ update_cached_list_of_options();
+ for (auto const& option : m_cached_list_of_options)
+ option->update_label_sizer_text();
+
// NB: The chevron icon is positioned out of flow so that the label is the wrapper's only flex item, keeping the
// baseline of the select derived from the label text. The label's margin-inline-end reserves its space.
m_chevron_icon_element = DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
@@ -757,7 +781,7 @@ void HTMLSelectElement::clone_selected_option_into_select_button()
{
// To clone selected option into select button, given a select element select:
- if (!m_inner_text_element)
+ if (!m_selected_label_element)
return;
update_cached_list_of_options();
@@ -774,7 +798,7 @@ void HTMLSelectElement::clone_selected_option_into_select_button()
text = (*option)->label();
// 4. Set select's select fallback button text to text.
- m_inner_text_element->string_replace_all(move(text));
+ m_selected_label_element->string_replace_all(move(text));
}
// https://html.spec.whatwg.org/multipage/form-elements.html#selectedness-setting-algorithm
diff --git a/Libraries/LibWeb/HTML/HTMLSelectElement.h b/Libraries/LibWeb/HTML/HTMLSelectElement.h
index ed31df89c6d..f44e1c3a139 100644
--- a/Libraries/LibWeb/HTML/HTMLSelectElement.h
+++ b/Libraries/LibWeb/HTML/HTMLSelectElement.h
@@ -167,6 +167,8 @@ private:
bool m_is_open { false };
Vector<GC::Ref<HTMLOptionElement>> m_select_item_option_elements;
GC::Ptr<DOM::Element> m_inner_text_element;
+ GC::Ptr<DOM::Element> m_selected_label_element;
+ GC::Ptr<DOM::Element> m_label_sizer_element;
GC::Ptr<DOM::Element> m_chevron_icon_element;
bool m_chevron_icon_hidden { false };
diff --git a/Tests/LibWeb/Layout/expected/HTMLSelectElement-selected-index.txt b/Tests/LibWeb/Layout/expected/HTMLSelectElement-selected-index.txt
index d2b1f1c3fbe..017cc35f7f3 100644
--- a/Tests/LibWeb/Layout/expected/HTMLSelectElement-selected-index.txt
+++ b/Tests/LibWeb/Layout/expected/HTMLSelectElement-selected-index.txt
@@ -1,16 +1,26 @@
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 36 0+0+0] [BFC] children: not-inline
BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 20 0+0+8] children: inline
- frag 0 from BlockContainer start: 0, length: 0, rect: [13,10 78.390625x16] baseline: 14.796875
- BlockContainer <select#select> at [13,10] inline-block [0+1+4 78.390625 4+1+0] [0+1+1 16 1+1+0] [BFC] children: not-inline
- Box <div> at [13,10] positioned flex-container(row) [0+0+0 78.390625 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline
- BlockContainer <div> at [13,10] flex-item [0+0+0 58.390625 0+0+20] [0+0+0 16 0+0+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 7, rect: [13,10 58.390625x16] baseline: 12.796875
- "Value 1"
- TextNode <#text> (not painted)
- BlockContainer <div> at [75.390625,10] positioned [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline
- frag 0 from SVGSVGBox start: 0, length: 0, rect: [75.390625,10 16x16] baseline: 16
- SVGSVGBox <svg> at [75.390625,10] [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] viewport-transform=[0.6666667 0 0 0.6666667 0 0] [SVG] children: not-inline
+ frag 0 from BlockContainer start: 0, length: 0, rect: [13,10 81.640625x16] baseline: 14.796875
+ BlockContainer <select#select> at [13,10] inline-block [0+1+4 81.640625 4+1+0] [0+1+1 16 1+1+0] [BFC] children: not-inline
+ Box <div> at [13,10] positioned flex-container(row) [0+0+0 81.640625 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline
+ BlockContainer <div> at [13,10] flex-item [0+0+0 61.640625 0+0+20] [0+0+0 16 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [13,10] [0+0+0 61.640625 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 7, rect: [13,10 58.390625x16] baseline: 12.796875
+ "Value 1"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [13,26] floating [0+0+0 61.640625 0+0+0] [0+0+0 0 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [13,26] [0+0+0 61.640625 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 7, rect: [13,26 61.640625x16] baseline: 12.796875
+ "Value 0"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [13,42] [0+0+0 61.640625 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 7, rect: [13,42 58.390625x16] baseline: 12.796875
+ "Value 1"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [78.640625,10] positioned [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline
+ frag 0 from SVGSVGBox start: 0, length: 0, rect: [78.640625,10 16x16] baseline: 16
+ SVGSVGBox <svg> at [78.640625,10] [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] viewport-transform=[0.6666667 0 0 0.6666667 0 0] [SVG] children: not-inline
SVGGeometryBox <path> at [6,8.578125] [0+0+0 12 0+0+0] [0+0+0 7.421875 0+0+0] children: not-inline
TextNode <#text> (not painted)
TextNode <#text> (not painted)
diff --git a/Tests/LibWeb/Layout/expected/pdf-viewer.txt b/Tests/LibWeb/Layout/expected/pdf-viewer.txt
index a5a5fedc4f1..6c16cfae061 100644
--- a/Tests/LibWeb/Layout/expected/pdf-viewer.txt
+++ b/Tests/LibWeb/Layout/expected/pdf-viewer.txt
@@ -167,10 +167,64 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
TextNode <#text> (not painted)
BlockContainer <select#scaleSelect> at [379.9375,3] flex-item [0+0+6 102.484375 38+0+0] [0+0+1 25 2+0+0] [BFC] children: not-inline
Box <div> at [379.9375,3] positioned flex-container(row) [0+0+0 102.484375 0+0+0] [0+0+0 25 0+0+0] [FFC] children: not-inline
- BlockContainer <div> at [379.9375,9.5] flex-item [0+0+0 102.484375 0+0+0] [0+0+0 12 0+0+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 14, rect: [379.9375,9.5 102.484375x12] baseline: 9.59375
- "Automatic Zoom"
- TextNode <#text> (not painted)
+ BlockContainer <div> at [379.9375,9.5] flex-item [0+0+0 102.484375 0+0+0] [0+0+0 12 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [379.9375,9.5] [0+0+0 102.484375 0+0+0] [0+0+0 12 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 14, rect: [379.9375,9.5 102.484375x12] baseline: 9.59375
+ "Automatic Zoom"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [379.9375,21.5] floating [0+0+0 102.484375 0+0+0] [0+0+0 0 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [379.9375,21.5] [0+0+0 102.484375 0+0+0] [0+0+0 12 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 14, rect: [379.9375,21.5 102.484375x12] baseline: 9.59375
+ "Automatic Zoom"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [379.9375,33.5] [0+0+0 102.484375 0+0+0] [0+0+0 12 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 11, rect: [379.9375,33.5 71.28125x12] baseline: 9.59375
+ "Actual Size"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [379.9375,45.5] [0+0+0 102.484375 0+0+0] [0+0+0 12 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 8, rect: [379.9375,45.5 51.859375x12] baseline: 9.59375
+ "Page Fit"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [379.9375,57.5] [0+0+0 102.484375 0+0+0] [0+0+0 12 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 10, rect: [379.9375,57.5 64.984375x12] baseline: 9.59375
+ "Page Width"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [379.9375,69.5] [0+0+0 102.484375 0+0+0] [0+0+0 12 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 4, rect: [379.9375,69.5 16.421875x12] baseline: 9.59375
+ "⁨0⁩%"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [379.9375,81.5] [0+0+0 102.484375 0+0+0] [0+0+0 12 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 5, rect: [379.9375,81.5 22.765625x12] baseline: 9.59375
+ "⁨50⁩%"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [379.9375,93.5] [0+0+0 102.484375 0+0+0] [0+0+0 12 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 5, rect: [379.9375,93.5 22.109375x12] baseline: 9.59375
+ "⁨75⁩%"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [379.9375,105.5] [0+0+0 102.484375 0+0+0] [0+0+0 12 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 6, rect: [379.9375,105.5 28.390625x12] baseline: 9.59375
+ "⁨100⁩%"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [379.9375,117.5] [0+0+0 102.484375 0+0+0] [0+0+0 12 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 6, rect: [379.9375,117.5 26.9375x12] baseline: 9.59375
+ "⁨125⁩%"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [379.9375,129.5] [0+0+0 102.484375 0+0+0] [0+0+0 12 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 6, rect: [379.9375,129.5 27.53125x12] baseline: 9.59375
+ "⁨150⁩%"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [379.9375,141.5] [0+0+0 102.484375 0+0+0] [0+0+0 12 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 6, rect: [379.9375,141.5 30.234375x12] baseline: 9.59375
+ "⁨200⁩%"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [379.9375,153.5] [0+0+0 102.484375 0+0+0] [0+0+0 12 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 6, rect: [379.9375,153.5 30.453125x12] baseline: 9.59375
+ "⁨300⁩%"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [379.9375,165.5] [0+0+0 102.484375 0+0+0] [0+0+0 12 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 6, rect: [379.9375,165.5 29.4375x12] baseline: 9.59375
+ "⁨400⁩%"
+ TextNode <#text> (not painted)
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
BlockContainer <(anonymous)> at [500.421875,8] positioned [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] [BFC] children: not-inline
diff --git a/Tests/LibWeb/Layout/expected/select-baseline-from-label-text.txt b/Tests/LibWeb/Layout/expected/select-baseline-from-label-text.txt
index 02cbe418357..4a02bfc75d2 100644
--- a/Tests/LibWeb/Layout/expected/select-baseline-from-label-text.txt
+++ b/Tests/LibWeb/Layout/expected/select-baseline-from-label-text.txt
@@ -11,10 +11,16 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
TextNode <#text> (not painted)
BlockContainer <select> at [42.265625,10] inline-block [0+1+4 76.46875 4+1+0] [0+1+1 16 1+1+0] [BFC] children: not-inline
Box <div> at [42.265625,10] positioned flex-container(row) [0+0+0 76.46875 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline
- BlockContainer <div> at [42.265625,10] flex-item [0+0+0 56.46875 0+0+20] [0+0+0 16 0+0+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 7, rect: [42.265625,10 56.46875x16] baseline: 12.796875
- "English"
- TextNode <#text> (not painted)
+ BlockContainer <div> at [42.265625,10] flex-item [0+0+0 56.46875 0+0+20] [0+0+0 16 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [42.265625,10] [0+0+0 56.46875 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 7, rect: [42.265625,10 56.46875x16] baseline: 12.796875
+ "English"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [42.265625,26] floating [0+0+0 56.46875 0+0+0] [0+0+0 0 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [42.265625,26] [0+0+0 56.46875 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 7, rect: [42.265625,26 56.46875x16] baseline: 12.796875
+ "English"
+ TextNode <#text> (not painted)
BlockContainer <div> at [102.734375,10] positioned [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline
frag 0 from SVGSVGBox start: 0, length: 0, rect: [102.734375,10 16x16] baseline: 16
SVGSVGBox <svg> at [102.734375,10] [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] viewport-transform=[0.6666667 0 0 0.6666667 0 0] [SVG] children: not-inline
@@ -37,10 +43,16 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
TextNode <#text> (not painted)
BlockContainer <select> at [42.265625,30] inline-block [0+1+4 56.46875 4+1+0] [0+1+1 16 1+1+0] [BFC] children: not-inline
Box <div> at [42.265625,30] positioned flex-container(row) [0+0+0 56.46875 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline
- BlockContainer <div> at [42.265625,30] flex-item [0+0+0 56.46875 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 7, rect: [42.265625,30 56.46875x16] baseline: 12.796875
- "English"
- TextNode <#text> (not painted)
+ BlockContainer <div> at [42.265625,30] flex-item [0+0+0 56.46875 0+0+0] [0+0+0 16 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [42.265625,30] [0+0+0 56.46875 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 7, rect: [42.265625,30 56.46875x16] baseline: 12.796875
+ "English"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [42.265625,46] floating [0+0+0 56.46875 0+0+0] [0+0+0 0 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [42.265625,46] [0+0+0 56.46875 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 7, rect: [42.265625,46 56.46875x16] baseline: 12.796875
+ "English"
+ TextNode <#text> (not painted)
BlockContainer <(anonymous)> at [8,48] [0+0+0 784 0+0+0] [0+0+0 0 0+0+0] children: inline
TextNode <#text> (not painted)
diff --git a/Tests/LibWeb/Layout/expected/select-innerhtml-optgroup-selected-option.txt b/Tests/LibWeb/Layout/expected/select-innerhtml-optgroup-selected-option.txt
index a51a0c134e2..bafe03a8f5b 100644
--- a/Tests/LibWeb/Layout/expected/select-innerhtml-optgroup-selected-option.txt
+++ b/Tests/LibWeb/Layout/expected/select-innerhtml-optgroup-selected-option.txt
@@ -1,16 +1,30 @@
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 36 0+0+0] [BFC] children: not-inline
BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 20 0+0+8] children: inline
- frag 0 from BlockContainer start: 0, length: 0, rect: [13,10 89.109375x16] baseline: 14.796875
- BlockContainer <select#s> at [13,10] inline-block [0+1+4 89.109375 4+1+0] [0+1+1 16 1+1+0] [BFC] children: not-inline
- Box <div> at [13,10] positioned flex-container(row) [0+0+0 89.109375 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline
- BlockContainer <div> at [13,10] flex-item [0+0+0 69.109375 0+0+20] [0+0+0 16 0+0+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 8, rect: [13,10 69.109375x16] baseline: 12.796875
- "Option 2"
- TextNode <#text> (not painted)
- BlockContainer <div> at [86.109375,10] positioned [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline
- frag 0 from SVGSVGBox start: 0, length: 0, rect: [86.109375,10 16x16] baseline: 16
- SVGSVGBox <svg> at [86.109375,10] [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] viewport-transform=[0.6666667 0 0 0.6666667 0 0] [SVG] children: not-inline
+ frag 0 from BlockContainer start: 0, length: 0, rect: [13,10 89.390625x16] baseline: 14.796875
+ BlockContainer <select#s> at [13,10] inline-block [0+1+4 89.390625 4+1+0] [0+1+1 16 1+1+0] [BFC] children: not-inline
+ Box <div> at [13,10] positioned flex-container(row) [0+0+0 89.390625 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline
+ BlockContainer <div> at [13,10] flex-item [0+0+0 69.390625 0+0+20] [0+0+0 16 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [13,10] [0+0+0 69.390625 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 8, rect: [13,10 69.109375x16] baseline: 12.796875
+ "Option 2"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [13,26] floating [0+0+0 69.390625 0+0+0] [0+0+0 0 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [13,26] [0+0+0 69.390625 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 8, rect: [13,26 66.640625x16] baseline: 12.796875
+ "Option 1"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [13,42] [0+0+0 69.390625 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 8, rect: [13,42 69.109375x16] baseline: 12.796875
+ "Option 2"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [13,58] [0+0+0 69.390625 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 8, rect: [13,58 69.390625x16] baseline: 12.796875
+ "Option 3"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [86.390625,10] positioned [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline
+ frag 0 from SVGSVGBox start: 0, length: 0, rect: [86.390625,10 16x16] baseline: 16
+ SVGSVGBox <svg> at [86.390625,10] [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] viewport-transform=[0.6666667 0 0 0.6666667 0 0] [SVG] children: not-inline
SVGGeometryBox <path> at [6,8.578125] [0+0+0 12 0+0+0] [0+0+0 7.421875 0+0+0] children: not-inline
TextNode <#text> (not painted)
TextNode <#text> (not painted)
diff --git a/Tests/LibWeb/Layout/expected/select-with-option-selected.txt b/Tests/LibWeb/Layout/expected/select-with-option-selected.txt
index 2c89d348332..4404c05d38d 100644
--- a/Tests/LibWeb/Layout/expected/select-with-option-selected.txt
+++ b/Tests/LibWeb/Layout/expected/select-with-option-selected.txt
@@ -4,10 +4,20 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
frag 0 from BlockContainer start: 0, length: 0, rect: [13,10 89.109375x16] baseline: 14.796875
BlockContainer <select> at [13,10] inline-block [0+1+4 89.109375 4+1+0] [0+1+1 16 1+1+0] [BFC] children: not-inline
Box <div> at [13,10] positioned flex-container(row) [0+0+0 89.109375 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline
- BlockContainer <div> at [13,10] flex-item [0+0+0 69.109375 0+0+20] [0+0+0 16 0+0+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 8, rect: [13,10 69.109375x16] baseline: 12.796875
- "Option 2"
- TextNode <#text> (not painted)
+ BlockContainer <div> at [13,10] flex-item [0+0+0 69.109375 0+0+20] [0+0+0 16 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [13,10] [0+0+0 69.109375 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 8, rect: [13,10 69.109375x16] baseline: 12.796875
+ "Option 2"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [13,26] floating [0+0+0 69.109375 0+0+0] [0+0+0 0 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [13,26] [0+0+0 69.109375 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 8, rect: [13,26 66.640625x16] baseline: 12.796875
+ "Option 1"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [13,42] [0+0+0 69.109375 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 8, rect: [13,42 69.109375x16] baseline: 12.796875
+ "Option 2"
+ TextNode <#text> (not painted)
BlockContainer <div> at [86.109375,10] positioned [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline
frag 0 from SVGSVGBox start: 0, length: 0, rect: [86.109375,10 16x16] baseline: 16
SVGSVGBox <svg> at [86.109375,10] [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] viewport-transform=[0.6666667 0 0 0.6666667 0 0] [SVG] children: not-inline
diff --git a/Tests/LibWeb/Text/expected/HTML/HTMLSelectElement-label-sizer-updates.txt b/Tests/LibWeb/Text/expected/HTML/HTMLSelectElement-label-sizer-updates.txt
new file mode 100644
index 00000000000..c60e94352c0
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/HTMLSelectElement-label-sizer-updates.txt
@@ -0,0 +1,13 @@
+nested character data: true
+nested textContent shrink: true
+nested insertion: true
+nested removal: true
+nested option ancestor: true
+newline label, auto: true
+selection keeps width, auto: true
+newline label, none: true
+selection keeps width, none: true
+moved option sizes destination: true
+moved option shrinks source: true
+removed option shrinks destination: true
+adopted option sizes destination: true
diff --git a/Tests/LibWeb/Text/expected/HTML/HTMLSelectElement-label-sizer-writing-mode.txt b/Tests/LibWeb/Text/expected/HTML/HTMLSelectElement-label-sizer-writing-mode.txt
new file mode 100644
index 00000000000..0a22ac63874
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/HTMLSelectElement-label-sizer-writing-mode.txt
@@ -0,0 +1,54 @@
+horizontal-tb, auto, longer reference grows: true
+horizontal-tb, auto, widest option: true
+horizontal-tb, auto, block size matches: true
+horizontal-tb, auto, selection keeps inline size: true
+horizontal-tb, auto, selection keeps block size: true
+horizontal-tb, auto, unselected label mutation: true
+horizontal-tb, auto, no selection keeps inline size: true
+horizontal-tb, auto, no selection block size matches: true
+horizontal-tb, auto, author logical sizes: true
+horizontal-tb, none, longer reference grows: true
+horizontal-tb, none, widest option: true
+horizontal-tb, none, block size matches: true
+horizontal-tb, none, selection keeps inline size: true
+horizontal-tb, none, selection keeps block size: true
+horizontal-tb, none, unselected label mutation: true
+horizontal-tb, none, no selection keeps inline size: true
+horizontal-tb, none, no selection block size matches: true
+horizontal-tb, none, author logical sizes: true
+vertical-rl, auto, longer reference grows: true
+vertical-rl, auto, widest option: true
+vertical-rl, auto, block size matches: true
+vertical-rl, auto, selection keeps inline size: true
+vertical-rl, auto, selection keeps block size: true
+vertical-rl, auto, unselected label mutation: true
+vertical-rl, auto, no selection keeps inline size: true
+vertical-rl, auto, no selection block size matches: true
+vertical-rl, auto, author logical sizes: true
+vertical-rl, none, longer reference grows: true
+vertical-rl, none, widest option: true
+vertical-rl, none, block size matches: true
+vertical-rl, none, selection keeps inline size: true
+vertical-rl, none, selection keeps block size: true
+vertical-rl, none, unselected label mutation: true
+vertical-rl, none, no selection keeps inline size: true
+vertical-rl, none, no selection block size matches: true
+vertical-rl, none, author logical sizes: true
+vertical-lr, auto, longer reference grows: true
+vertical-lr, auto, widest option: true
+vertical-lr, auto, block size matches: true
+vertical-lr, auto, selection keeps inline size: true
+vertical-lr, auto, selection keeps block size: true
+vertical-lr, auto, unselected label mutation: true
+vertical-lr, auto, no selection keeps inline size: true
+vertical-lr, auto, no selection block size matches: true
+vertical-lr, auto, author logical sizes: true
+vertical-lr, none, longer reference grows: true
+vertical-lr, none, widest option: true
+vertical-lr, none, block size matches: true
+vertical-lr, none, selection keeps inline size: true
+vertical-lr, none, selection keeps block size: true
+vertical-lr, none, unselected label mutation: true
+vertical-lr, none, no selection keeps inline size: true
+vertical-lr, none, no selection block size matches: true
+vertical-lr, none, author logical sizes: true
diff --git a/Tests/LibWeb/Text/expected/HTML/HTMLSelectElement-width-fits-widest-option.txt b/Tests/LibWeb/Text/expected/HTML/HTMLSelectElement-width-fits-widest-option.txt
index 81b67e52224..ddc4fcda7fc 100644
--- a/Tests/LibWeb/Text/expected/HTML/HTMLSelectElement-width-fits-widest-option.txt
+++ b/Tests/LibWeb/Text/expected/HTML/HTMLSelectElement-width-fits-widest-option.txt
@@ -1,7 +1,7 @@
-short selected option: false
-short selected option, appearance none: false
-option in optgroup: false
-after inserting wider option: false
+short selected option: true
+short selected option, appearance none: true
+option in optgroup: true
+after inserting wider option: true
after shortening its text: true
-after setting a wider label: false
+after setting a wider label: true
after removing it: true
diff --git a/Tests/LibWeb/Text/input/HTML/HTMLSelectElement-label-sizer-updates.html b/Tests/LibWeb/Text/input/HTML/HTMLSelectElement-label-sizer-updates.html
new file mode 100644
index 00000000000..9cf30308119
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/HTMLSelectElement-label-sizer-updates.html
@@ -0,0 +1,68 @@
+<!doctype html>
+<style>
+ select {
+ font: 16px SerenitySans;
+ padding: 0 6px;
+ }
+</style>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const short = "E";
+ const long = "Português (pt-BR)";
+ const make = (labels, appearance = "none", whiteSpace = "pre") => {
+ const select = document.createElement("select");
+ select.style.appearance = appearance;
+ select.style.whiteSpace = whiteSpace;
+ for (const label of labels) {
+ const option = document.createElement("option");
+ option.label = label;
+ select.append(option);
+ }
+ document.body.append(select);
+ return select;
+ };
+ const width = select => select.getBoundingClientRect().width;
+ const reference = make([long]);
+ const select = make([short]);
+ const option = document.createElement("option");
+ const span = document.createElement("span");
+ span.textContent = short;
+ option.append(span);
+ select.append(option);
+ span.firstChild.data = long;
+ println(`nested character data: ${width(select) === width(reference)}`);
+ span.textContent = short;
+ println(`nested textContent shrink: ${width(select) < width(reference)}`);
+ span.append(document.createTextNode(long));
+ const combinedReference = make([short + long]);
+ println(`nested insertion: ${width(select) === width(combinedReference)}`);
+ span.lastChild.remove();
+ println(`nested removal: ${width(select) < width(reference)}`);
+ const innerOption = document.createElement("option");
+ innerOption.textContent = short;
+ option.replaceChildren(innerOption);
+ innerOption.firstChild.data = long;
+ println(`nested option ancestor: ${width(select) === width(reference)}`);
+
+ for (const appearance of ["auto", "none"]) {
+ const multiline = make([short, "English\nPortuguês"], appearance, "nowrap");
+ const multilineReference = make(["English\nPortuguês"], appearance, "nowrap");
+ const before = width(multiline);
+ println(`newline label, ${appearance}: ${before === width(multilineReference)}`);
+ multiline.selectedIndex = 1;
+ println(`selection keeps width, ${appearance}: ${width(multiline) === before}`);
+ }
+
+ const destination = make([short]);
+ destination.append(option);
+ println(`moved option sizes destination: ${width(destination) === width(reference)}`);
+ println(`moved option shrinks source: ${width(select) < width(reference)}`);
+ option.remove();
+ println(`removed option shrinks destination: ${width(destination) < width(reference)}`);
+ const otherDocument = document.implementation.createHTMLDocument();
+ otherDocument.adoptNode(option);
+ destination.append(option);
+ println(`adopted option sizes destination: ${width(destination) === width(reference)}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/HTMLSelectElement-label-sizer-writing-mode.html b/Tests/LibWeb/Text/input/HTML/HTMLSelectElement-label-sizer-writing-mode.html
new file mode 100644
index 00000000000..4dc2fed359b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/HTMLSelectElement-label-sizer-writing-mode.html
@@ -0,0 +1,73 @@
+<!doctype html>
+<style>
+ select {
+ font: 16px SerenitySans;
+ white-space: pre;
+ }
+</style>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const short = "E";
+ const long = "Português (pt-BR)";
+ const longest = "A much longer option label";
+ const make = (labels, mode, appearance) => {
+ const select = document.createElement("select");
+ select.style.writingMode = mode;
+ select.style.appearance = appearance;
+ for (const label of labels) {
+ const option = document.createElement("option");
+ option.label = label;
+ select.append(option);
+ }
+ document.body.append(select);
+ return select;
+ };
+ for (const mode of ["horizontal-tb", "vertical-rl", "vertical-lr"]) {
+ for (const appearance of ["auto", "none"]) {
+ const inlineSize = select => {
+ const rect = select.getBoundingClientRect();
+ return mode === "horizontal-tb" ? rect.width : rect.height;
+ };
+ const blockSize = select => {
+ const rect = select.getBoundingClientRect();
+ return mode === "horizontal-tb" ? rect.height : rect.width;
+ };
+ const reference = make([long], mode, appearance);
+ const shortReference = make([short], mode, appearance);
+ const select = make([short, long], mode, appearance);
+ const before = inlineSize(select);
+ const blockBefore = blockSize(select);
+ println(
+ `${mode}, ${appearance}, longer reference grows: ${inlineSize(reference) > inlineSize(shortReference)}`
+ );
+ println(`${mode}, ${appearance}, widest option: ${before === inlineSize(reference)}`);
+ println(`${mode}, ${appearance}, block size matches: ${blockBefore === blockSize(reference)}`);
+ select.selectedIndex = 1;
+ println(`${mode}, ${appearance}, selection keeps inline size: ${inlineSize(select) === before}`);
+ println(`${mode}, ${appearance}, selection keeps block size: ${blockSize(select) === blockBefore}`);
+ select.selectedIndex = 0;
+ select.options[1].label = longest;
+ const longestReference = make([longest], mode, appearance);
+ println(
+ `${mode}, ${appearance}, unselected label mutation: ${inlineSize(select) === inlineSize(longestReference)}`
+ );
+ const longestInlineSize = inlineSize(longestReference);
+ select.selectedIndex = -1;
+ longestReference.selectedIndex = -1;
+ println(
+ `${mode}, ${appearance}, no selection keeps inline size: ${inlineSize(select) === longestInlineSize}`
+ );
+ println(
+ `${mode}, ${appearance}, no selection block size matches: ${blockSize(select) === blockSize(longestReference)}`
+ );
+ select.style.boxSizing = "border-box";
+ select.style.inlineSize = "240px";
+ select.style.blockSize = "48px";
+ println(
+ `${mode}, ${appearance}, author logical sizes: ${inlineSize(select) === 240 && blockSize(select) === 48}`
+ );
+ }
+ }
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/select-label-overflow.html b/Tests/LibWeb/Text/input/select-label-overflow.html
index 7430c44c950..473d01dda05 100644
--- a/Tests/LibWeb/Text/input/select-label-overflow.html
+++ b/Tests/LibWeb/Text/input/select-label-overflow.html
@@ -9,7 +9,7 @@
<script>
test(() => {
for (const select of document.querySelectorAll("select")) {
- const label = internals.getShadowRoot(select).firstChild.firstChild;
+ const label = internals.getShadowRoot(select).firstChild.firstChild.firstChild;
const rect = label.getBoundingClientRect();
const range = document.createRange();
range.selectNodeContents(label);