OptionConstructor.cpp 3.1 KB

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