OptionConstructor.cpp 3.2 KB

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