HTMLDialogElement.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/DOM/Event.h>
  8. #include <LibWeb/HTML/Focus.h>
  9. #include <LibWeb/HTML/HTMLDialogElement.h>
  10. namespace Web::HTML {
  11. JS_DEFINE_ALLOCATOR(HTMLDialogElement);
  12. HTMLDialogElement::HTMLDialogElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  13. : HTMLElement(document, move(qualified_name))
  14. {
  15. }
  16. HTMLDialogElement::~HTMLDialogElement() = default;
  17. void HTMLDialogElement::initialize(JS::Realm& realm)
  18. {
  19. Base::initialize(realm);
  20. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLDialogElement);
  21. }
  22. // https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-show
  23. WebIDL::ExceptionOr<void> HTMLDialogElement::show()
  24. {
  25. // 1. If this has an open attribute and the is modal flag of this is false, then return.
  26. // FIXME: Add modal flag check here when modal dialog support is added
  27. if (has_attribute(AttributeNames::open))
  28. return {};
  29. // FIXME: 2. If this has an open attribute, then throw an "InvalidStateError" DOMException.
  30. // 3. Add an open attribute to this, whose value is the empty string.
  31. TRY(set_attribute(AttributeNames::open, {}));
  32. // FIXME 4. Set this's previously focused element to the focused element.
  33. // FIXME 5. Run hide all popovers given this's node document.
  34. // 6. Run the dialog focusing steps given this.
  35. run_dialog_focusing_steps();
  36. return {};
  37. }
  38. // https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-showmodal
  39. void HTMLDialogElement::show_modal()
  40. {
  41. dbgln("(STUBBED) HTMLDialogElement::show_modal()");
  42. }
  43. // https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-close
  44. void HTMLDialogElement::close(Optional<String> return_value)
  45. {
  46. // 1. If returnValue is not given, then set it to null.
  47. // 2. Close the dialog this with returnValue.
  48. close_the_dialog(move(return_value));
  49. }
  50. // https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-returnvalue
  51. String HTMLDialogElement::return_value() const
  52. {
  53. return m_return_value;
  54. }
  55. // https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-returnvalue
  56. void HTMLDialogElement::set_return_value(String return_value)
  57. {
  58. m_return_value = move(return_value);
  59. }
  60. // https://html.spec.whatwg.org/multipage/interactive-elements.html#close-the-dialog
  61. void HTMLDialogElement::close_the_dialog(Optional<String> result)
  62. {
  63. // 1. If subject does not have an open attribute, then return.
  64. if (!has_attribute(AttributeNames::open))
  65. return;
  66. // 2. Remove subject's open attribute.
  67. remove_attribute(AttributeNames::open);
  68. // FIXME: 3. If the is modal flag of subject is true, then request an element to be removed from the top layer given subject.
  69. // FIXME: 4. Let wasModal be the value of subject's is modal flag.
  70. // FIXME: 5. Set the is modal flag of subject to false.
  71. // 6. If result is not null, then set the returnValue attribute to result.
  72. if (result.has_value())
  73. set_return_value(result.release_value());
  74. // FIXME: 7. If subject's previously focused element is not null, then:
  75. // 1. Let element be subject's previously focused element.
  76. // 2. Set subject's previously focused element to null.
  77. // 3. If subject's node document's focused area of the document's DOM anchor is a shadow-including inclusive descendant of element,
  78. // or wasModal is true, then run the focusing steps for element; the viewport should not be scrolled by doing this step.
  79. // 8. Queue an element task on the user interaction task source given the subject element to fire an event named close at subject.
  80. queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
  81. auto close_event = DOM::Event::create(realm(), HTML::EventNames::close);
  82. dispatch_event(close_event);
  83. });
  84. // FIXME: 9. If subject's close watcher is not null, then:
  85. // 1. Destroy subject's close watcher.
  86. // 2. Set subject's close watcher to null.
  87. }
  88. // https://html.spec.whatwg.org/multipage/interactive-elements.html#dialog-focusing-steps
  89. void HTMLDialogElement::run_dialog_focusing_steps()
  90. {
  91. // 1. Let control be null
  92. JS::GCPtr<Element> control = nullptr;
  93. // FIXME 2. If subject has the autofocus attribute, then set control to subject.
  94. // FIXME 3. If control is null, then set control to the focus delegate of subject.
  95. // 4. If control is null, then set control to subject.
  96. if (!control)
  97. control = this;
  98. // 5. Run the focusing steps for control.
  99. run_focusing_steps(control);
  100. }
  101. }