HTMLDialogElement.cpp 7.4 KB

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