OptionConstructor.cpp 3.2 KB

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