Skip to content

Truly independent web browser

LibWeb: Import select attribute selectedness tests

Andreas Klingcf1bdb103bd9
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.txt
new file mode 100644
index 00000000000..d2cab1bf5c2
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-select-element/option-selectedness-script-mutation.txt
@@ -0,0 +1,11 @@
+Harness status: OK
+
+Found 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
+Pass multi-select keeps multiple selected options when selected attribute changes from script
+Pass single-select responds to setting option.selected from script
+Pass multi-select responds to option.selected without selected attribute reflection
\ No newline at end of file
diff --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.txt
new file mode 100644
index 00000000000..96596a532a2
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-select-element/select-attribute-change-reset.txt
@@ -0,0 +1,14 @@
+Harness status: OK
+
+Found 8 tests
+
+2 Pass
+6 Fail
+Fail 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 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
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/forms/the-select-element/option-selectedness-script-mutation.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/forms/the-select-element/option-selectedness-script-mutation.html
new file mode 100644
index 00000000000..f682642badc
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/forms/the-select-element/option-selectedness-script-mutation.html
@@ -0,0 +1,101 @@
+<!doctype html>
+<meta charset="utf-8">
+<title>Select reacts to option selectedness updates from selected attribute mutations</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/#concept-option-selectedness">
+<link rel="help" href="https://html.spec.whatwg.org/multipage/#reset-a-select's-selectedness">
+<link rel="help" href="https://html.spec.whatwg.org/multipage/#dom-select-selectedoptions">
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+<div id="log"></div>
+
+<script>
+"use strict";
+
+test(() => {
+ const select = document.createElement("select");
+ const first = new Option("123");
+ const second = new Option("234");
+ select.append(first, second);
+
+ const selectedOptions = select.selectedOptions;
+ assert_equals(select.selectedIndex, 0, "precondition: first option starts selected");
+ assert_equals(selectedOptions.length, 1, "precondition: exactly one selected option");
+ assert_equals(selectedOptions[0], first, "precondition: first selected option");
+
+ second.setAttribute("selected", "");
+
+ assert_equals(select.selectedIndex, 1, "setting selected content attribute updates select owner state");
+ assert_equals(selectedOptions.length, 1, "selectedOptions stays coherent for single select");
+ assert_equals(selectedOptions[0], second, "new option becomes the selected one");
+}, "single-select responds to setting selected attribute from script");
+
+test(() => {
+ const select = document.createElement("select");
+ const first = new Option("123");
+ const second = new Option("234");
+ second.defaultSelected = true;
+ select.append(first, second);
+
+ const selectedOptions = select.selectedOptions;
+ assert_equals(select.selectedIndex, 1, "precondition: second option starts selected");
+
+ second.removeAttribute("selected");
+
+ assert_equals(select.selectedIndex, 0, "removing selected content attribute runs reset behavior");
+ assert_equals(selectedOptions.length, 1, "selectedOptions remains coherent after removal");
+ assert_equals(selectedOptions[0], first, "selection falls back to first enabled option");
+}, "single-select responds to removing selected attribute from script");
+
+test(() => {
+ const select = document.createElement("select");
+ select.multiple = true;
+ const first = new Option("123");
+ const second = new Option("234");
+ select.append(first, second);
+
+ const selectedOptions = select.selectedOptions;
+ assert_equals(selectedOptions.length, 0, "precondition: no selected options in multi-select");
+
+ first.setAttribute("selected", "");
+ second.setAttribute("selected", "");
+
+ assert_equals(selectedOptions.length, 2, "multi-select retains both selected options");
+ assert_equals(selectedOptions[0], first, "first selected option should be present");
+ assert_equals(selectedOptions[1], second, "second selected option should be present");
+}, "multi-select keeps multiple selected options when selected attribute changes from script");
+
+test(() => {
+ const select = document.createElement("select");
+ const first = new Option("123");
+ const second = new Option("234");
+ select.append(first, second);
+
+ const selectedOptions = select.selectedOptions;
+ assert_equals(select.selectedIndex, 0, "precondition: first option starts selected");
+
+ second.selected = true;
+
+ assert_equals(select.selectedIndex, 1, "setting option.selected updates owner select state");
+ assert_equals(selectedOptions.length, 1, "selectedOptions stays coherent for single select");
+ assert_equals(selectedOptions[0], second, "new option becomes selected");
+ assert_false(second.hasAttribute("selected"), "IDL selected does not reflect to content attribute");
+}, "single-select responds to setting option.selected from script");
+
+test(() => {
+ const select = document.createElement("select");
+ select.multiple = true;
+ const first = new Option("123");
+ const second = new Option("234");
+ select.append(first, second);
+
+ const selectedOptions = select.selectedOptions;
+ first.selected = true;
+ second.selected = true;
+
+ assert_equals(selectedOptions.length, 2, "multi-select keeps both selections with option.selected");
+ assert_equals(selectedOptions[0], first, "first selected option should be present");
+ assert_equals(selectedOptions[1], second, "second selected option should be present");
+ assert_false(first.hasAttribute("selected"), "IDL selected does not reflect to content attribute");
+ assert_false(second.hasAttribute("selected"), "IDL selected does not reflect to content attribute");
+}, "multi-select responds to option.selected without selected attribute reflection");
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/forms/the-select-element/select-attribute-change-reset.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/forms/the-select-element/select-attribute-change-reset.html
new file mode 100644
index 00000000000..7b6e901f451
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/forms/the-select-element/select-attribute-change-reset.html
@@ -0,0 +1,205 @@
+<!DOCTYPE html>
+<meta charset=utf-8>
+<title>Changing size, multiple, disabled, or selected resets a select's selectedness</title>
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+<div id=log></div>
+
+<!-- A non-multiple select whose display size is 1 and which has an enabled
+ option must always have a selected option. Every attribute that could
+ otherwise leave it without one resets a select's selectedness. -->
+
+<select id=select1 size=2>
+ <option id=option1>A</option>
+</select>
+
+<script>
+test(() => {
+ const select = document.getElementById('select1');
+ const option = document.getElementById('option1');
+
+ assert_equals(select.value, '',
+ 'No option should be selected while the display size is greater than 1.');
+
+ select.size = 1;
+
+ assert_true(option.selected,
+ 'Reducing the display size to 1 should select the first enabled option.');
+ assert_equals(select.value, 'A',
+ 'select.value should reflect the newly selected option.');
+}, 'Reducing a select\'s size to 1 selects the first enabled option.');
+</script>
+
+<select id=select2 multiple size=1>
+ <option id=option2>B</option>
+</select>
+
+<script>
+test(() => {
+ const select = document.getElementById('select2');
+ const option = document.getElementById('option2');
+
+ assert_equals(select.value, '',
+ 'No option should be selected while the select is multiple.');
+
+ select.removeAttribute('multiple');
+
+ assert_true(option.selected,
+ 'Removing the multiple attribute should select the first enabled option.');
+ assert_equals(select.value, 'B',
+ 'select.value should reflect the newly selected option.');
+}, 'Removing a select\'s multiple attribute selects the first enabled option.');
+</script>
+
+<select id=select3>
+ <option id=option3a disabled>C</option>
+ <option id=option3b disabled>D</option>
+</select>
+
+<script>
+test(() => {
+ const select = document.getElementById('select3');
+ const optionA = document.getElementById('option3a');
+
+ assert_equals(select.value, '',
+ 'No option should be selected while every option is disabled.');
+
+ optionA.disabled = false;
+
+ assert_true(optionA.selected,
+ 'Enabling an option should select it when nothing else is selected.');
+ assert_equals(select.value, 'C',
+ 'select.value should reflect the newly selected option.');
+}, 'Removing an option\'s disabled attribute selects it.');
+</script>
+
+<select id=select4>
+ <optgroup id=optgroup4 disabled>
+ <option id=option4>E</option>
+ </optgroup>
+</select>
+
+<script>
+// An option's disabledness also depends on its nearest ancestor optgroup, so
+// the optgroup's disabled attribute needs the same treatment.
+test(() => {
+ const select = document.getElementById('select4');
+ const optgroup = document.getElementById('optgroup4');
+ const option = document.getElementById('option4');
+
+ assert_equals(select.value, '',
+ 'No option should be selected while the optgroup is disabled.');
+
+ optgroup.disabled = false;
+
+ assert_true(option.selected,
+ 'Enabling an optgroup should select the option inside it.');
+ assert_equals(select.value, 'E',
+ 'select.value should reflect the newly selected option.');
+}, 'Removing an optgroup\'s disabled attribute selects the option inside it.');
+</script>
+
+<select id=select5>
+ <option id=option5a>F</option>
+ <option id=option5b>G</option>
+</select>
+
+<script>
+// Setting the selected content attribute sets selectedness without going
+// through the selected setter, so it can leave two options selected in a
+// non-multiple select unless it also resets the select's selectedness.
+test(() => {
+ const select = document.getElementById('select5');
+ const optionA = document.getElementById('option5a');
+ const optionB = document.getElementById('option5b');
+
+ assert_true(optionA.selected,
+ 'The first option should be selected at the start of the test.');
+
+ optionB.setAttribute('selected', '');
+
+ assert_true(optionB.selected,
+ 'Adding the selected attribute should select the option.');
+ assert_false(optionA.selected,
+ 'The previously selected option should be deselected.');
+ assert_equals(select.value, 'G',
+ 'select.value should reflect the newly selected option.');
+}, 'Adding an option\'s selected attribute deselects the other options.');
+</script>
+
+<select id=select6>
+ <option id=option6a>H</option>
+ <option id=option6b selected>I</option>
+</select>
+
+<script>
+test(() => {
+ const select = document.getElementById('select6');
+ const optionA = document.getElementById('option6a');
+ const optionB = document.getElementById('option6b');
+
+ assert_true(optionB.selected,
+ 'The option with the selected attribute should be selected.');
+
+ optionB.removeAttribute('selected');
+
+ assert_false(optionB.selected,
+ 'Removing the selected attribute should deselect the option.');
+ assert_true(optionA.selected,
+ 'The first enabled option should be selected instead.');
+ assert_equals(select.value, 'H',
+ 'select.value should reflect the newly selected option.');
+}, 'Removing an option\'s selected attribute falls back to the first enabled option.');
+</script>
+
+<select id=select7>
+ <option id=option7a selected>J</option>
+ <option id=option7b>K</option>
+</select>
+
+<script>
+// Dirtiness makes the content attribute stop affecting selectedness. The reset
+// still runs, but finds nothing to change.
+test(() => {
+ const select = document.getElementById('select7');
+ const optionA = document.getElementById('option7a');
+ const optionB = document.getElementById('option7b');
+
+ // Setting the IDL attribute sets dirtiness, without changing selectedness.
+ optionA.selected = true;
+ assert_equals(select.value, 'J', 'Precondition: first option selected.');
+
+ optionA.removeAttribute('selected');
+
+ assert_true(optionA.selected,
+ 'A dirty option\'s selectedness is not changed by removing the content attribute.');
+ assert_false(optionB.selected,
+ 'The second option should not be selected.');
+ assert_equals(select.value, 'J',
+ 'select.value should be unchanged.');
+}, 'Removing the selected attribute from a dirty option does not change selectedness.');
+</script>
+
+<select id=select8 multiple>
+ <option id=option8a selected>L</option>
+ <option id=option8b selected>M</option>
+</select>
+
+<script>
+// Once the select is no longer multiple only one option can stay selected, and
+// it is the first one in tree order.
+test(() => {
+ const select = document.getElementById('select8');
+ const optionA = document.getElementById('option8a');
+ const optionB = document.getElementById('option8b');
+
+ select.removeAttribute('multiple');
+
+ assert_true(optionA.selected,
+ 'The first selected option should stay selected.');
+ assert_false(optionB.selected,
+ 'The other selected option should be deselected.');
+ assert_equals(select.value, 'L',
+ 'select.value should reflect the first selected option.');
+}, 'Removing a select\'s multiple attribute keeps the first selected option.');
+</script>