OptionConstructor.cpp 3.0 KB

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