Skip to content

Truly independent web browser

LibWeb: Add test for select width with options of different widths

Andreas Kling4e12d7454844
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
new file mode 100644
index 00000000000..81b67e52224
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/HTMLSelectElement-width-fits-widest-option.txt
@@ -0,0 +1,7 @@
+short selected option: false
+short selected option, appearance none: false
+option in optgroup: false
+after inserting wider option: false
+after shortening its text: true
+after setting a wider label: false
+after removing it: true
diff --git a/Tests/LibWeb/Text/input/HTML/HTMLSelectElement-width-fits-widest-option.html b/Tests/LibWeb/Text/input/HTML/HTMLSelectElement-width-fits-widest-option.html
new file mode 100644
index 00000000000..32a1f3a9346
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/HTMLSelectElement-width-fits-widest-option.html
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<style>
+ select {
+ font: 16px SerenitySans;
+ padding: 0 6px;
+ }
+ .none {
+ appearance: none;
+ }
+</style>
+<select id="reference"><option>Português (pt-BR)</option></select>
+<select id="reference-none" class="none"><option>Português (pt-BR)</option></select>
+<select id="short-selected"><option>English</option><option>Português (pt-BR)</option></select>
+<select id="short-selected-none" class="none"><option>English</option><option>Português (pt-BR)</option></select>
+<select id="nested" class="none"><option>English</option><optgroup label="Group"><option>Português (pt-BR)</option></optgroup></select>
+<select id="dynamic" class="none"><option>English</option></select>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const width = id => document.getElementById(id).getBoundingClientRect().width;
+ const reference = width("reference");
+ const referenceNone = width("reference-none");
+
+ println(`short selected option: ${width("short-selected") === reference}`);
+ println(`short selected option, appearance none: ${width("short-selected-none") === referenceNone}`);
+ println(`option in optgroup: ${width("nested") === referenceNone}`);
+
+ const dynamic = document.getElementById("dynamic");
+ const option = document.createElement("option");
+ option.textContent = "Português (pt-BR)";
+ dynamic.appendChild(option);
+ println(`after inserting wider option: ${width("dynamic") === referenceNone}`);
+
+ option.textContent = "E";
+ println(`after shortening its text: ${width("dynamic") < referenceNone}`);
+
+ option.label = "Português (pt-BR)";
+ println(`after setting a wider label: ${width("dynamic") === referenceNone}`);
+
+ option.remove();
+ println(`after removing it: ${width("dynamic") < referenceNone}`);
+ });
+</script>