LibWeb: Handle undefined arguments correctly in the Option constructor
Some checks are pending
CI / Lagom (false, FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (true, NO_FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Push notes / build (push) Waiting to run

The hand-rolled factory function wasn't handling undefined values
entirely correctly.
This commit is contained in:
Andreas Kling 2024-11-15 11:38:51 +01:00 committed by Andreas Kling
parent 77d30a0cb7
commit 3ecc843cff
Notes: github-actions[bot] 2024-11-15 11:55:33 +00:00
2 changed files with 12 additions and 10 deletions

View file

@ -45,6 +45,11 @@ JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Object>> OptionConstructor::construct
auto& vm = this->vm();
auto& realm = *vm.current_realm();
// NOTE: This implements the default value for the `text` parameter (the empty string "").
auto text_value = vm.argument(0);
if (text_value.is_undefined())
text_value = &vm.empty_string();
// 1. Let document be the current principal global object's associated Document.
auto& window = verify_cast<HTML::Window>(HTML::current_principal_global_object());
auto& document = window.associated_document();
@ -54,16 +59,14 @@ JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Object>> OptionConstructor::construct
JS::NonnullGCPtr<HTML::HTMLOptionElement> option_element = verify_cast<HTML::HTMLOptionElement>(*element);
// 3. If text is not the empty string, then append to option a new Text node whose data is text.
if (vm.argument_count() > 0) {
auto text = TRY(vm.argument(0).to_string(vm));
if (!text.is_empty()) {
auto new_text_node = realm.create<DOM::Text>(document, text);
MUST(option_element->append_child(*new_text_node));
}
auto text = TRY(text_value.to_string(vm));
if (!text.is_empty()) {
auto new_text_node = realm.create<DOM::Text>(document, text);
MUST(option_element->append_child(*new_text_node));
}
// 4. If value is given, then set an attribute value for option using "value" and value.
if (vm.argument_count() > 1) {
if (!vm.argument(1).is_undefined()) {
auto value = TRY(vm.argument(1).to_string(vm));
MUST(option_element->set_attribute(HTML::AttributeNames::value, value));
}

View file

@ -6,15 +6,14 @@ Rerun
Found 11 tests
10 Pass
1 Fail
11 Pass
Details
Result Test Name MessagePass Option constructor with no arguments
Pass Option constructor with falsy arguments
Pass Option constructor creates HTMLOptionElement with specified text and value
Pass Option constructor handles selectedness correctly when specified with defaultSelected only
Pass Option constructor handles selectedness correctly, even when incongruous with defaultSelected
Fail Option constructor treats undefined text and value correctly
Pass Option constructor treats undefined text and value correctly
Pass Option constructor treats empty text and value correctly
Pass Option constructor treats falsy selected and defaultSelected correctly
Pass Option constructor treats truthy selected and defaultSelected correctly