| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2022, the SerenityOS developers. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-06 17:54:21 +02:00
										 |  |  | #include <LibJS/Runtime/ValueInlines.h>
 | 
					
						
							| 
									
										
										
										
											2023-02-19 16:22:24 +01:00
										 |  |  | #include <LibWeb/Bindings/ExceptionOrUtils.h>
 | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  | #include <LibWeb/Bindings/HTMLOptionElementPrototype.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/OptionConstructor.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/ElementFactory.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | #include <LibWeb/DOM/Text.h>
 | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  | #include <LibWeb/HTML/HTMLOptionElement.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | #include <LibWeb/HTML/Scripting/Environments.h>
 | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  | #include <LibWeb/HTML/Window.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Namespace.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::Bindings { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-16 00:20:49 +01:00
										 |  |  | OptionConstructor::OptionConstructor(JS::Realm& realm) | 
					
						
							| 
									
										
										
										
											2023-04-13 00:47:15 +02:00
										 |  |  |     : NativeFunction(realm.intrinsics().function_prototype()) | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  | void OptionConstructor::initialize(JS::Realm& realm) | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  | { | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-10 07:01:57 -05:00
										 |  |  |     define_direct_property(vm.names.prototype, &ensure_web_prototype<Bindings::HTMLOptionElementPrototype>(realm, "HTMLOptionElement"), 0); | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  |     define_direct_property(vm.names.length, JS::Value(0), JS::Attribute::Configurable); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | JS::ThrowCompletionOr<JS::Value> OptionConstructor::call() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-16 20:33:17 +01:00
										 |  |  |     return vm().throw_completion<JS::TypeError>(JS::ErrorType::ConstructorWithoutNew, "Option"); | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option
 | 
					
						
							| 
									
										
										
										
											2022-12-14 19:18:10 +00:00
										 |  |  | JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Object>> OptionConstructor::construct(FunctionObject&) | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-21 14:00:56 +01:00
										 |  |  |     auto& vm = this->vm(); | 
					
						
							| 
									
										
										
										
											2022-08-22 18:31:08 +01:00
										 |  |  |     auto& realm = *vm.current_realm(); | 
					
						
							| 
									
										
										
										
											2022-08-21 14:00:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  |     // 1. Let document be the current global object's associated Document.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     auto& window = verify_cast<HTML::Window>(HTML::current_global_object()); | 
					
						
							|  |  |  |     auto& document = window.associated_document(); | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. Let option be the result of creating an element given document, option, and the HTML namespace.
 | 
					
						
							| 
									
										
										
										
											2023-02-19 16:22:24 +01:00
										 |  |  |     auto element = TRY(Bindings::throw_dom_exception_if_needed(vm, [&]() { return DOM::create_element(document, HTML::TagNames::option, Namespace::HTML); })); | 
					
						
							|  |  |  |     JS::NonnullGCPtr<HTML::HTMLOptionElement> option_element = verify_cast<HTML::HTMLOptionElement>(*element); | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 3. If text is not the empty string, then append to option a new Text node whose data is text.
 | 
					
						
							| 
									
										
										
										
											2022-08-21 14:00:56 +01:00
										 |  |  |     if (vm.argument_count() > 0) { | 
					
						
							| 
									
										
										
										
											2023-09-06 15:17:20 +12:00
										 |  |  |         auto text = TRY(vm.argument(0).to_string(vm)); | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  |         if (!text.is_empty()) { | 
					
						
							| 
									
										
										
										
											2023-08-13 13:05:26 +02:00
										 |  |  |             auto new_text_node = vm.heap().allocate<DOM::Text>(realm, document, text); | 
					
						
							| 
									
										
										
										
											2022-10-30 17:50:04 +00:00
										 |  |  |             MUST(option_element->append_child(*new_text_node)); | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 4. If value is given, then set an attribute value for option using "value" and value.
 | 
					
						
							| 
									
										
										
										
											2022-08-21 14:00:56 +01:00
										 |  |  |     if (vm.argument_count() > 1) { | 
					
						
							| 
									
										
										
										
											2023-01-13 10:29:02 -05:00
										 |  |  |         auto value = TRY(vm.argument(1).to_deprecated_string(vm)); | 
					
						
							| 
									
										
										
										
											2022-10-30 17:50:04 +00:00
										 |  |  |         MUST(option_element->set_attribute(HTML::AttributeNames::value, value)); | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 5. If defaultSelected is true, then set an attribute value for option using "selected" and the empty string.
 | 
					
						
							| 
									
										
										
										
											2022-08-21 14:00:56 +01:00
										 |  |  |     if (vm.argument_count() > 2) { | 
					
						
							|  |  |  |         auto default_selected = vm.argument(2).to_boolean(); | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  |         if (default_selected) { | 
					
						
							| 
									
										
										
										
											2022-10-30 17:50:04 +00:00
										 |  |  |             MUST(option_element->set_attribute(HTML::AttributeNames::selected, "")); | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 6. If selected is true, then set option's selectedness to true; otherwise set its selectedness to false (even if defaultSelected is true).
 | 
					
						
							| 
									
										
										
										
											2022-08-21 14:00:56 +01:00
										 |  |  |     option_element->m_selected = vm.argument(3).to_boolean(); | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 7. Return option.
 | 
					
						
							| 
									
										
										
										
											2022-12-14 19:18:10 +00:00
										 |  |  |     return option_element; | 
					
						
							| 
									
										
										
										
											2022-04-03 22:07:03 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |