HTMLDialogElement.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/Document.h>
  8. #include <LibWeb/DOM/Event.h>
  9. #include <LibWeb/HTML/Focus.h>
  10. #include <LibWeb/HTML/HTMLDialogElement.h>
  11. namespace Web::HTML {
  12. JS_DEFINE_ALLOCATOR(HTMLDialogElement);
  13. HTMLDialogElement::HTMLDialogElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  14. : HTMLElement(document, move(qualified_name))
  15. {
  16. }
  17. HTMLDialogElement::~HTMLDialogElement() = default;
  18. void HTMLDialogElement::initialize(JS::Realm& realm)
  19. {
  20. Base::initialize(realm);
  21. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLDialogElement);
  22. }
  23. void HTMLDialogElement::removed_from(Node* old_parent)
  24. {
  25. HTMLElement::removed_from(old_parent);
  26. // FIXME: 1. If removedNode's close watcher is not null, then:
  27. // 1. Destroy removedNode's close watcher.
  28. // 2. Set removedNode's close watcher to null.
  29. // 2. If removedNode's node document's top layer contains removedNode, then remove an element from the top layer
  30. // immediately given removedNode.
  31. if (document().top_layer_elements().contains(*this))
  32. document().remove_an_element_from_the_top_layer_immediately(*this);
  33. }
  34. // https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-show
  35. WebIDL::ExceptionOr<void> HTMLDialogElement::show()
  36. {
  37. // 1. If this has an open attribute and the is modal flag of this is false, then return.
  38. // FIXME: Add modal flag check here when modal dialog support is added
  39. if (has_attribute(AttributeNames::open))
  40. return {};
  41. // FIXME: 2. If this has an open attribute, then throw an "InvalidStateError" DOMException.
  42. // 3. Add an open attribute to this, whose value is the empty string.
  43. TRY(set_attribute(AttributeNames::open, {}));
  44. // FIXME 4. Set this's previously focused element to the focused element.
  45. // FIXME 5. Run hide all popovers given this's node document.
  46. // 6. Run the dialog focusing steps given this.
  47. run_dialog_focusing_steps();
  48. return {};
  49. }
  50. // https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-showmodal
  51. WebIDL::ExceptionOr<void> HTMLDialogElement::show_modal()
  52. {
  53. // 1. If this has an open attribute and the is modal flag of this is true, then return.
  54. if (has_attribute(AttributeNames::open) && m_is_modal)
  55. return {};
  56. // 2. If this has an open attribute, then throw an "InvalidStateError" DOMException.
  57. if (has_attribute(AttributeNames::open))
  58. return WebIDL::InvalidStateError::create(realm(), "Dialog already open"_fly_string);
  59. // 3. If this is not connected, then throw an "InvalidStateError" DOMException.
  60. if (!is_connected())
  61. return WebIDL::InvalidStateError::create(realm(), "Dialog not connected"_fly_string);
  62. // FIXME: 4. If this is in the popover showing state, then throw an "InvalidStateError" DOMException.
  63. // 5. Add an open attribute to this, whose value is the empty string.
  64. TRY(set_attribute(AttributeNames::open, {}));
  65. // 6. Set the is modal flag of this to true.
  66. m_is_modal = true;
  67. // FIXME: 7. Let this's node document be blocked by the modal dialog this.
  68. // 8. If this's node document's top layer does not already contain this, then add an element to the top layer given this.
  69. if (!document().top_layer_elements().contains(*this))
  70. document().add_an_element_to_the_top_layer(*this);
  71. // FIXME: 9. Set this's close watcher to the result of establishing a close watcher given this's relevant global object, with:
  72. // - cancelAction being to return the result of firing an event named cancel at this, with the cancelable
  73. // attribute initialized to true.
  74. // - closeAction being to close the dialog given this and null.
  75. // FIXME: 10. Set this's previously focused element to the focused element.
  76. // FIXME: 11. Let hideUntil be the result of running topmost popover ancestor given this, null, and false.
  77. // FIXME: 12. If hideUntil is null, then set hideUntil to this's node document.
  78. // FIXME: 13. Run hide all popovers until given hideUntil, false, and true.
  79. // FIXME: 14. Run the dialog focusing steps given this.
  80. return {};
  81. }
  82. // https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-close
  83. void HTMLDialogElement::close(Optional<String> return_value)
  84. {
  85. // 1. If returnValue is not given, then set it to null.
  86. // 2. Close the dialog this with returnValue.
  87. close_the_dialog(move(return_value));
  88. }
  89. // https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-returnvalue
  90. String HTMLDialogElement::return_value() const
  91. {
  92. return m_return_value;
  93. }
  94. // https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-returnvalue
  95. void HTMLDialogElement::set_return_value(String return_value)
  96. {
  97. m_return_value = move(return_value);
  98. }
  99. // https://html.spec.whatwg.org/multipage/interactive-elements.html#close-the-dialog
  100. void HTMLDialogElement::close_the_dialog(Optional<String> result)
  101. {
  102. // 1. If subject does not have an open attribute, then return.
  103. if (!has_attribute(AttributeNames::open))
  104. return;
  105. // 2. Remove subject's open attribute.
  106. remove_attribute(AttributeNames::open);
  107. // 3. If the is modal flag of subject is true, then request an element to be removed from the top layer given subject.
  108. if (m_is_modal)
  109. document().request_an_element_to_be_remove_from_the_top_layer(*this);
  110. // FIXME: 4. Let wasModal be the value of subject's is modal flag.
  111. // 5. Set the is modal flag of subject to false.
  112. m_is_modal = false;
  113. // 6. If result is not null, then set the returnValue attribute to result.
  114. if (result.has_value())
  115. set_return_value(result.release_value());
  116. // FIXME: 7. If subject's previously focused element is not null, then:
  117. // 1. Let element be subject's previously focused element.
  118. // 2. Set subject's previously focused element to null.
  119. // 3. If subject's node document's focused area of the document's DOM anchor is a shadow-including inclusive descendant of element,
  120. // or wasModal is true, then run the focusing steps for element; the viewport should not be scrolled by doing this step.
  121. // 8. Queue an element task on the user interaction task source given the subject element to fire an event named close at subject.
  122. queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
  123. auto close_event = DOM::Event::create(realm(), HTML::EventNames::close);
  124. dispatch_event(close_event);
  125. });
  126. // FIXME: 9. If subject's close watcher is not null, then:
  127. // 1. Destroy subject's close watcher.
  128. // 2. Set subject's close watcher to null.
  129. }
  130. // https://html.spec.whatwg.org/multipage/interactive-elements.html#dialog-focusing-steps
  131. void HTMLDialogElement::run_dialog_focusing_steps()
  132. {
  133. // 1. Let control be null
  134. JS::GCPtr<Element> control = nullptr;
  135. // FIXME 2. If subject has the autofocus attribute, then set control to subject.
  136. // FIXME 3. If control is null, then set control to the focus delegate of subject.
  137. // 4. If control is null, then set control to subject.
  138. if (!control)
  139. control = this;
  140. // 5. Run the focusing steps for control.
  141. run_focusing_steps(control);
  142. }
  143. }