Skip to content

Truly independent web browser

LibWeb: Skip empty option label sizing rows

Andreas Kling9868c4ccc78d
diff --git a/Libraries/LibWeb/HTML/HTMLOptionElement.cpp b/Libraries/LibWeb/HTML/HTMLOptionElement.cpp
index 3244debd119..c770c56996b 100644
--- a/Libraries/LibWeb/HTML/HTMLOptionElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLOptionElement.cpp
@@ -70,8 +70,17 @@ void HTMLOptionElement::update_label_sizer_text()
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 (text.is_empty()) {
+ if (m_label_sizer_text) {
+ if (auto* row = m_label_sizer_text->parent())
+ row->remove();
+ m_label_sizer_text = nullptr;
+ }
+ return;
+ }
+
+ // NB: Nonempty labels have a block row, with the same inherited white-space as the selected label.
if (m_label_sizer_text) {
if (m_label_sizer_text->data() != text)
m_label_sizer_text->set_data(text);
diff --git a/Tests/LibWeb/Text/expected/select-empty-label-sizing-rows.txt b/Tests/LibWeb/Text/expected/select-empty-label-sizing-rows.txt
new file mode 100644
index 00000000000..27571899d21
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/select-empty-label-sizing-rows.txt
@@ -0,0 +1,10 @@
+initial empty rows: 0
+grown empty rows: 0
+nonempty row: true, wider: true
+cleared rows: 0, width restored: true
+spaces row: true, wider: true
+text fallback row: true, wider: true
+cleared text rows: 0, width restored: true
+moved row removed: true
+moved label retained: true
+moved empty row removed: true
diff --git a/Tests/LibWeb/Text/input/select-empty-label-sizing-rows.html b/Tests/LibWeb/Text/input/select-empty-label-sizing-rows.html
new file mode 100644
index 00000000000..3b695196b51
--- /dev/null
+++ b/Tests/LibWeb/Text/input/select-empty-label-sizing-rows.html
@@ -0,0 +1,52 @@
+<!doctype html>
+<script src="include.js"></script>
+<style>
+ select {
+ font: 16px monospace;
+ white-space: pre;
+ appearance: none;
+ padding: 0;
+ border: 0;
+ }
+</style>
+<script>
+ test(() => {
+ const select = document.createElement("select");
+ select.innerHTML = "<option></option><option></option><option></option>";
+ document.body.append(select);
+ const width = () => select.getBoundingClientRect().width;
+ const rows = () => {
+ width();
+ const shadow = internals.getShadowRoot(select);
+ return Array.from(shadow.querySelectorAll("div")).find(div => div.style.visibility === "hidden").children;
+ };
+ const emptyWidth = width();
+ println(`initial empty rows: ${rows().length}`);
+ select.options.length = 2000;
+ println(`grown empty rows: ${rows().length}`);
+ const option = select.options[1];
+ option.label = "A deliberately long option label";
+ println(`nonempty row: ${rows().length === 1}, wider: ${width() > emptyWidth}`);
+ option.label = "";
+ println(`cleared rows: ${rows().length}, width restored: ${width() === emptyWidth}`);
+ option.label = " ";
+ println(`spaces row: ${rows().length === 1}, wider: ${width() > emptyWidth}`);
+ option.removeAttribute("label");
+ option.textContent = "Text label";
+ println(`text fallback row: ${rows().length === 1}, wider: ${width() > emptyWidth}`);
+ option.textContent = "";
+ println(`cleared text rows: ${rows().length}, width restored: ${width() === emptyWidth}`);
+ option.label = "Moved label";
+ const other = document.createElement("select");
+ document.body.append(other);
+ other.getBoundingClientRect();
+ other.append(option);
+ println(`moved row removed: ${rows().length === 0}`);
+ const otherSizer = Array.from(internals.getShadowRoot(other).querySelectorAll("div")).find(
+ div => div.style.visibility === "hidden"
+ );
+ println(`moved label retained: ${otherSizer.children.length === 1}`);
+ option.label = "";
+ println(`moved empty row removed: ${otherSizer.children.length === 0}`);
+ });
+</script>