/src/serenity/Userland/Libraries/LibWeb/Bindings/OptionConstructor.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022, the SerenityOS developers. |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibJS/Runtime/ValueInlines.h> |
8 | | #include <LibWeb/Bindings/ExceptionOrUtils.h> |
9 | | #include <LibWeb/Bindings/HTMLOptionElementPrototype.h> |
10 | | #include <LibWeb/Bindings/OptionConstructor.h> |
11 | | #include <LibWeb/DOM/ElementFactory.h> |
12 | | #include <LibWeb/DOM/Text.h> |
13 | | #include <LibWeb/HTML/HTMLOptionElement.h> |
14 | | #include <LibWeb/HTML/Scripting/Environments.h> |
15 | | #include <LibWeb/HTML/Window.h> |
16 | | #include <LibWeb/Namespace.h> |
17 | | |
18 | | namespace Web::Bindings { |
19 | | |
20 | | JS_DEFINE_ALLOCATOR(OptionConstructor); |
21 | | |
22 | | OptionConstructor::OptionConstructor(JS::Realm& realm) |
23 | 0 | : NativeFunction(realm.intrinsics().function_prototype()) |
24 | 0 | { |
25 | 0 | } |
26 | | |
27 | | void OptionConstructor::initialize(JS::Realm& realm) |
28 | 0 | { |
29 | 0 | auto& vm = this->vm(); |
30 | 0 | Base::initialize(realm); |
31 | |
|
32 | 0 | define_direct_property(vm.names.prototype, &ensure_web_prototype<Bindings::HTMLOptionElementPrototype>(realm, "HTMLOptionElement"_fly_string), 0); |
33 | 0 | define_direct_property(vm.names.length, JS::Value(0), JS::Attribute::Configurable); |
34 | 0 | } |
35 | | |
36 | | JS::ThrowCompletionOr<JS::Value> OptionConstructor::call() |
37 | 0 | { |
38 | 0 | return vm().throw_completion<JS::TypeError>(JS::ErrorType::ConstructorWithoutNew, "Option"); |
39 | 0 | } |
40 | | |
41 | | // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option |
42 | | JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Object>> OptionConstructor::construct(FunctionObject&) |
43 | 0 | { |
44 | 0 | auto& vm = this->vm(); |
45 | 0 | auto& realm = *vm.current_realm(); |
46 | | |
47 | | // 1. Let document be the current global object's associated Document. |
48 | 0 | auto& window = verify_cast<HTML::Window>(HTML::current_global_object()); |
49 | 0 | auto& document = window.associated_document(); |
50 | | |
51 | | // 2. Let option be the result of creating an element given document, option, and the HTML namespace. |
52 | 0 | auto element = TRY(Bindings::throw_dom_exception_if_needed(vm, [&]() { return DOM::create_element(document, HTML::TagNames::option, Namespace::HTML); })); |
53 | 0 | JS::NonnullGCPtr<HTML::HTMLOptionElement> option_element = verify_cast<HTML::HTMLOptionElement>(*element); |
54 | | |
55 | | // 3. If text is not the empty string, then append to option a new Text node whose data is text. |
56 | 0 | if (vm.argument_count() > 0) { |
57 | 0 | auto text = TRY(vm.argument(0).to_string(vm)); |
58 | 0 | if (!text.is_empty()) { |
59 | 0 | auto new_text_node = vm.heap().allocate<DOM::Text>(realm, document, text); |
60 | 0 | MUST(option_element->append_child(*new_text_node)); |
61 | 0 | } |
62 | 0 | } |
63 | | |
64 | | // 4. If value is given, then set an attribute value for option using "value" and value. |
65 | 0 | if (vm.argument_count() > 1) { |
66 | 0 | auto value = TRY(vm.argument(1).to_string(vm)); |
67 | 0 | MUST(option_element->set_attribute(HTML::AttributeNames::value, value)); |
68 | 0 | } |
69 | | |
70 | | // 5. If defaultSelected is true, then set an attribute value for option using "selected" and the empty string. |
71 | 0 | if (vm.argument_count() > 2) { |
72 | 0 | auto default_selected = vm.argument(2).to_boolean(); |
73 | 0 | if (default_selected) { |
74 | 0 | MUST(option_element->set_attribute(HTML::AttributeNames::selected, String {})); |
75 | 0 | } |
76 | 0 | } |
77 | | |
78 | | // 6. If selected is true, then set option's selectedness to true; otherwise set its selectedness to false (even if defaultSelected is true). |
79 | 0 | option_element->m_selected = vm.argument(3).to_boolean(); |
80 | | |
81 | | // 7. Return option. |
82 | 0 | return option_element; |
83 | 0 | } |
84 | | |
85 | | } |