HTMLDialogElement.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/NativeFunction.h>
  8. #include <LibWeb/Bindings/HTMLDialogElementPrototype.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/Bindings/PrincipalHostDefined.h>
  11. #include <LibWeb/DOM/Document.h>
  12. #include <LibWeb/DOM/Event.h>
  13. #include <LibWeb/DOM/IDLEventListener.h>
  14. #include <LibWeb/HTML/CloseWatcher.h>
  15. #include <LibWeb/HTML/Focus.h>
  16. #include <LibWeb/HTML/HTMLDialogElement.h>
  17. #include <LibWeb/HTML/ToggleEvent.h>
  18. namespace Web::HTML {
  19. GC_DEFINE_ALLOCATOR(HTMLDialogElement);
  20. HTMLDialogElement::HTMLDialogElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  21. : HTMLElement(document, move(qualified_name))
  22. {
  23. }
  24. HTMLDialogElement::~HTMLDialogElement() = default;
  25. void HTMLDialogElement::initialize(JS::Realm& realm)
  26. {
  27. Base::initialize(realm);
  28. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLDialogElement);
  29. }
  30. void HTMLDialogElement::visit_edges(JS::Cell::Visitor& visitor)
  31. {
  32. Base::visit_edges(visitor);
  33. visitor.visit(m_close_watcher);
  34. }
  35. void HTMLDialogElement::removed_from(Node* old_parent)
  36. {
  37. HTMLElement::removed_from(old_parent);
  38. // 1. If removedNode's close watcher is not null, then:
  39. if (m_close_watcher) {
  40. // 1.1. Destroy removedNode's close watcher.
  41. m_close_watcher->destroy();
  42. // 1.2. Set removedNode's close watcher to null.
  43. m_close_watcher = nullptr;
  44. }
  45. // 2. If removedNode's node document's top layer contains removedNode, then remove an element from the top layer
  46. // immediately given removedNode.
  47. if (document().top_layer_elements().contains(*this))
  48. document().remove_an_element_from_the_top_layer_immediately(*this);
  49. }
  50. // https://html.spec.whatwg.org/multipage/interactive-elements.html#queue-a-dialog-toggle-event-task
  51. void HTMLDialogElement::queue_a_dialog_toggle_event_task(AK::String old_state, AK::String new_state)
  52. {
  53. // 1. If element's dialog toggle task tracker is not null, then:
  54. if (m_dialog_toggle_task_tracker.has_value()) {
  55. // 1. Set oldState to element's dialog toggle task tracker's old state.
  56. old_state = m_dialog_toggle_task_tracker->old_state;
  57. // 2. Remove element's dialog toggle task tracker's task from its task queue.
  58. HTML::main_thread_event_loop().task_queue().remove_tasks_matching([&](auto const& task) {
  59. return task.id() == m_dialog_toggle_task_tracker->task_id;
  60. });
  61. // 3. Set element's dialog toggle task tracker to null.
  62. m_dialog_toggle_task_tracker = {};
  63. }
  64. // 2. Queue an element task given the DOM manipulation task source and element to run the following steps:
  65. auto task_id = queue_an_element_task(Task::Source::DOMManipulation, [this, old_state, new_state = move(new_state)]() {
  66. // 1. Fire an event named toggle at element, using ToggleEvent, with the oldState attribute initialized to
  67. // oldState and the newState attribute initialized to newState.
  68. ToggleEventInit event_init {};
  69. event_init.old_state = move(old_state);
  70. event_init.new_state = move(new_state);
  71. dispatch_event(ToggleEvent::create(realm(), HTML::EventNames::toggle, move(event_init)));
  72. // 2. Set element's dialog toggle task tracker to null.
  73. m_dialog_toggle_task_tracker = {};
  74. });
  75. // 3. Set element's dialog toggle task tracker to a struct with task set to the just-queued task and old state set to oldState.
  76. m_dialog_toggle_task_tracker = ToggleTaskTracker {
  77. .task_id = task_id,
  78. .old_state = move(old_state),
  79. };
  80. }
  81. // https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-show
  82. WebIDL::ExceptionOr<void> HTMLDialogElement::show()
  83. {
  84. // 1. If this has an open attribute and the is modal flag of this is false, then return.
  85. if (has_attribute(AttributeNames::open) && !m_is_modal)
  86. return {};
  87. // 2. If this has an open attribute, then throw an "InvalidStateError" DOMException.
  88. if (has_attribute(AttributeNames::open))
  89. return WebIDL::InvalidStateError::create(realm(), "Dialog already open"_string);
  90. // 3. If the result of firing an event named beforetoggle, using ToggleEvent,
  91. // with the cancelable attribute initialized to true, the oldState attribute initialized to "closed",
  92. // and the newState attribute initialized to "open" at this is false, then return.
  93. ToggleEventInit event_init {};
  94. event_init.cancelable = true;
  95. event_init.old_state = "closed"_string;
  96. event_init.new_state = "open"_string;
  97. auto beforetoggle_result = dispatch_event(ToggleEvent::create(realm(), HTML::EventNames::beforetoggle, move(event_init)));
  98. if (!beforetoggle_result)
  99. return {};
  100. // 4. If this has an open attribute, then return.
  101. if (has_attribute(AttributeNames::open))
  102. return {};
  103. // 5. Queue a dialog toggle event task given subject, "closed", and "open".
  104. queue_a_dialog_toggle_event_task("closed"_string, "open"_string);
  105. // 6. Add an open attribute to this, whose value is the empty string.
  106. TRY(set_attribute(AttributeNames::open, {}));
  107. // FIXME: 7. Set this's previously focused element to the focused element.
  108. // FIXME: 8. Let hideUntil be the result of running topmost popover ancestor given this, null, and false.
  109. // FIXME: 9. If hideUntil is null, then set hideUntil to this's node document.
  110. // FIXME: 10. Run hide all popovers given this's node document.
  111. // 11. Run the dialog focusing steps given this.
  112. run_dialog_focusing_steps();
  113. return {};
  114. }
  115. // https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-showmodal
  116. WebIDL::ExceptionOr<void> HTMLDialogElement::show_modal()
  117. {
  118. // 1. If this has an open attribute and the is modal flag of this is true, then return.
  119. if (has_attribute(AttributeNames::open) && m_is_modal)
  120. return {};
  121. // 2. If this has an open attribute, then throw an "InvalidStateError" DOMException.
  122. if (has_attribute(AttributeNames::open))
  123. return WebIDL::InvalidStateError::create(realm(), "Dialog already open"_string);
  124. // 3. If this's node document is not fully active, then throw an "InvalidStateError" DOMException.
  125. if (!document().is_fully_active())
  126. return WebIDL::InvalidStateError::create(realm(), "Document is not fully active"_string);
  127. // 4. If this is not connected, then throw an "InvalidStateError" DOMException.
  128. if (!is_connected())
  129. return WebIDL::InvalidStateError::create(realm(), "Dialog not connected"_string);
  130. // FIXME: 5. If this is in the popover showing state, then throw an "InvalidStateError" DOMException.
  131. // 6. If the result of firing an event named beforetoggle, using ToggleEvent,
  132. // with the cancelable attribute initialized to true, the oldState attribute initialized to "closed",
  133. // and the newState attribute initialized to "open" at this is false, then return.
  134. ToggleEventInit event_init {};
  135. event_init.cancelable = true;
  136. event_init.old_state = "closed"_string;
  137. event_init.new_state = "open"_string;
  138. auto beforetoggle_result = dispatch_event(ToggleEvent::create(realm(), HTML::EventNames::beforetoggle, move(event_init)));
  139. if (!beforetoggle_result)
  140. return {};
  141. // 7. If this has an open attribute, then return.
  142. if (has_attribute(AttributeNames::open))
  143. return {};
  144. // 8. If this is not connected, then return.
  145. if (!is_connected())
  146. return {};
  147. // FIXME: 9. If this is in the popover showing state, then return.
  148. // 10. Queue a dialog toggle event task given subject, "closed", and "open".
  149. queue_a_dialog_toggle_event_task("closed"_string, "open"_string);
  150. // 11. Add an open attribute to this, whose value is the empty string.
  151. TRY(set_attribute(AttributeNames::open, {}));
  152. // 12. Set the is modal flag of this to true.
  153. m_is_modal = true;
  154. // FIXME: 13. Let this's node document be blocked by the modal dialog this.
  155. // 14. If this's node document's top layer does not already contain this, then add an element to the top layer given this.
  156. if (!document().top_layer_elements().contains(*this))
  157. document().add_an_element_to_the_top_layer(*this);
  158. // 15. Set this's close watcher to the result of establishing a close watcher given this's relevant global object, with:
  159. m_close_watcher = CloseWatcher::establish(*document().window());
  160. // - cancelAction given canPreventClose being to return the result of firing an event named cancel at this, with the cancelable attribute initialized to canPreventClose.
  161. auto cancel_callback_function = JS::NativeFunction::create(
  162. realm(), [this](JS::VM& vm) {
  163. auto& event = verify_cast<DOM::Event>(vm.argument(0).as_object());
  164. bool can_prevent_close = event.cancelable();
  165. auto should_continue = dispatch_event(DOM::Event::create(realm(), HTML::EventNames::cancel, { .cancelable = can_prevent_close }));
  166. if (!should_continue)
  167. event.prevent_default();
  168. return JS::js_undefined();
  169. },
  170. 0, "", &realm());
  171. auto cancel_callback = realm().heap().allocate<WebIDL::CallbackType>(*cancel_callback_function, realm());
  172. m_close_watcher->add_event_listener_without_options(HTML::EventNames::cancel, DOM::IDLEventListener::create(realm(), cancel_callback));
  173. // - closeAction being to close the dialog given this and null.
  174. auto close_callback_function = JS::NativeFunction::create(
  175. realm(), [this](JS::VM&) {
  176. close_the_dialog({});
  177. return JS::js_undefined();
  178. },
  179. 0, "", &realm());
  180. auto close_callback = realm().heap().allocate<WebIDL::CallbackType>(*close_callback_function, realm());
  181. m_close_watcher->add_event_listener_without_options(HTML::EventNames::close, DOM::IDLEventListener::create(realm(), close_callback));
  182. // FIXME: 16. Set this's previously focused element to the focused element.
  183. // FIXME: 17. Let hideUntil be the result of running topmost popover ancestor given this, null, and false.
  184. // FIXME: 18. If hideUntil is null, then set hideUntil to this's node document.
  185. // FIXME: 19. Run hide all popovers until given hideUntil, false, and true.
  186. // 20. Run the dialog focusing steps given this.
  187. run_dialog_focusing_steps();
  188. return {};
  189. }
  190. // https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-close
  191. void HTMLDialogElement::close(Optional<String> return_value)
  192. {
  193. // 1. If returnValue is not given, then set it to null.
  194. // 2. Close the dialog this with returnValue.
  195. close_the_dialog(move(return_value));
  196. }
  197. // https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-returnvalue
  198. String HTMLDialogElement::return_value() const
  199. {
  200. return m_return_value;
  201. }
  202. // https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-returnvalue
  203. void HTMLDialogElement::set_return_value(String return_value)
  204. {
  205. m_return_value = move(return_value);
  206. }
  207. // https://html.spec.whatwg.org/multipage/interactive-elements.html#close-the-dialog
  208. void HTMLDialogElement::close_the_dialog(Optional<String> result)
  209. {
  210. // 1. If subject does not have an open attribute, then return.
  211. if (!has_attribute(AttributeNames::open))
  212. return;
  213. // 2. Fire an event named beforetoggle, using ToggleEvent, with the oldState attribute initialized to "open" and the newState attribute initialized to "closed" at subject.
  214. ToggleEventInit event_init {};
  215. event_init.old_state = "open"_string;
  216. event_init.new_state = "closed"_string;
  217. dispatch_event(ToggleEvent::create(realm(), HTML::EventNames::beforetoggle, move(event_init)));
  218. // 3. If subject does not have an open attribute, then return.
  219. if (!has_attribute(AttributeNames::open))
  220. return;
  221. // 4. Queue a dialog toggle event task given subject, "open", and "closed".
  222. queue_a_dialog_toggle_event_task("open"_string, "closed"_string);
  223. // 5. Remove subject's open attribute.
  224. remove_attribute(AttributeNames::open);
  225. // 6. If the is modal flag of subject is true, then request an element to be removed from the top layer given subject.
  226. if (m_is_modal)
  227. document().request_an_element_to_be_remove_from_the_top_layer(*this);
  228. // FIXME: 7. Let wasModal be the value of subject's is modal flag.
  229. // 8. Set the is modal flag of subject to false.
  230. m_is_modal = false;
  231. // 9. If result is not null, then set the returnValue attribute to result.
  232. if (result.has_value())
  233. set_return_value(result.release_value());
  234. // FIXME: 10. If subject's previously focused element is not null, then:
  235. // 1. Let element be subject's previously focused element.
  236. // 2. Set subject's previously focused element to null.
  237. // 3. If subject's node document's focused area of the document's DOM anchor is a shadow-including inclusive descendant of element,
  238. // or wasModal is true, then run the focusing steps for element; the viewport should not be scrolled by doing this step.
  239. // 11. Queue an element task on the user interaction task source given the subject element to fire an event named close at subject.
  240. queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
  241. auto close_event = DOM::Event::create(realm(), HTML::EventNames::close);
  242. dispatch_event(close_event);
  243. });
  244. // 12. If subject's close watcher is not null, then:
  245. if (m_close_watcher) {
  246. // 9.1 Destroy subject's close watcher.
  247. m_close_watcher->destroy();
  248. // 9.2 Set subject's close watcher to null.
  249. m_close_watcher = nullptr;
  250. }
  251. }
  252. // https://html.spec.whatwg.org/multipage/interactive-elements.html#dialog-focusing-steps
  253. void HTMLDialogElement::run_dialog_focusing_steps()
  254. {
  255. // 1. Let control be null
  256. GC::Ptr<Element> control = nullptr;
  257. // FIXME 2. If subject has the autofocus attribute, then set control to subject.
  258. // FIXME 3. If control is null, then set control to the focus delegate of subject.
  259. // 4. If control is null, then set control to subject.
  260. if (!control)
  261. control = this;
  262. // 5. Run the focusing steps for control.
  263. run_focusing_steps(control);
  264. }
  265. }