OptionConstructor.cpp 3.2 KB

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