Focus.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
  4. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/TypeCasts.h>
  9. #include <AK/Vector.h>
  10. #include <LibGC/Root.h>
  11. #include <LibWeb/DOM/Document.h>
  12. #include <LibWeb/DOM/Element.h>
  13. #include <LibWeb/DOM/ShadowRoot.h>
  14. #include <LibWeb/HTML/Focus.h>
  15. #include <LibWeb/HTML/HTMLInputElement.h>
  16. #include <LibWeb/HTML/TraversableNavigable.h>
  17. #include <LibWeb/UIEvents/FocusEvent.h>
  18. namespace Web::HTML {
  19. // https://html.spec.whatwg.org/multipage/interaction.html#fire-a-focus-event
  20. static void fire_a_focus_event(GC::Ptr<DOM::EventTarget> focus_event_target, GC::Ptr<DOM::EventTarget> related_focus_target, FlyString const& event_name, bool bubbles)
  21. {
  22. // To fire a focus event named e at an element t with a given related target r, fire an event named e at t, using FocusEvent,
  23. // with the relatedTarget attribute initialized to r, the view attribute initialized to t's node document's relevant global
  24. // object, and the composed flag set.
  25. UIEvents::FocusEventInit focus_event_init {};
  26. focus_event_init.related_target = related_focus_target;
  27. focus_event_init.view = verify_cast<HTML::Window>(focus_event_target->realm().global_object()).window();
  28. auto focus_event = UIEvents::FocusEvent::create(focus_event_target->realm(), event_name, focus_event_init);
  29. // AD-HOC: support bubbling focus events, used for focusin & focusout.
  30. // See: https://github.com/whatwg/html/issues/3514
  31. focus_event->set_bubbles(bubbles);
  32. focus_event->set_composed(true);
  33. focus_event_target->dispatch_event(focus_event);
  34. }
  35. // https://html.spec.whatwg.org/multipage/interaction.html#focus-update-steps
  36. static void run_focus_update_steps(Vector<GC::Root<DOM::Node>> old_chain, Vector<GC::Root<DOM::Node>> new_chain, DOM::Node* new_focus_target)
  37. {
  38. // 1. If the last entry in old chain and the last entry in new chain are the same,
  39. // pop the last entry from old chain and the last entry from new chain and redo this step.
  40. while (!old_chain.is_empty()
  41. && !new_chain.is_empty()
  42. && &old_chain.last() == &new_chain.last()) {
  43. (void)old_chain.take_last();
  44. (void)new_chain.take_last();
  45. }
  46. // 2. For each entry entry in old chain, in order, run these substeps:
  47. for (auto& entry : old_chain) {
  48. // 1. If entry is an input element, and the change event applies to the element, and the element does not have
  49. // a defined activation behavior, and the user has changed the element's value or its list of selected files
  50. // while the control was focused without committing that change (such that it is different to what it was
  51. // when the control was first focused), then fire an event named change at the element, with the bubbles
  52. // attribute initialized to true.
  53. if (is<HTMLInputElement>(*entry)) {
  54. auto& input_element = static_cast<HTMLInputElement&>(*entry);
  55. // FIXME: Spec issue: It doesn't make sense to check if the element has a defined activation behavior, as
  56. // that is always true. Instead, we check if it has an *input* activation behavior.
  57. // https://github.com/whatwg/html/issues/9973
  58. if (input_element.change_event_applies() && !input_element.has_input_activation_behavior()) {
  59. input_element.commit_pending_changes();
  60. }
  61. }
  62. GC::Ptr<DOM::EventTarget> blur_event_target;
  63. if (is<DOM::Element>(*entry)) {
  64. // 2. If entry is an element, let blur event target be entry.
  65. blur_event_target = entry.ptr();
  66. } else if (is<DOM::Document>(*entry)) {
  67. // If entry is a Document object, let blur event target be that Document object's relevant global object.
  68. blur_event_target = static_cast<DOM::Document&>(*entry).window();
  69. }
  70. // 3. If entry is the last entry in old chain, and entry is an Element,
  71. // and the last entry in new chain is also an Element,
  72. // then let related blur target be the last entry in new chain.
  73. // Otherwise, let related blur target be null.
  74. GC::Ptr<DOM::EventTarget> related_blur_target;
  75. if (!old_chain.is_empty()
  76. && &entry == &old_chain.last()
  77. && is<DOM::Element>(*entry)
  78. && !new_chain.is_empty()
  79. && is<DOM::Element>(*new_chain.last())) {
  80. related_blur_target = new_chain.last().ptr();
  81. }
  82. // 4. If blur event target is not null, fire a focus event named blur at blur event target,
  83. // with related blur target as the related target.
  84. if (blur_event_target) {
  85. fire_a_focus_event(blur_event_target, related_blur_target, HTML::EventNames::blur, false);
  86. // AD-HOC: dispatch focusout
  87. fire_a_focus_event(blur_event_target, related_blur_target, HTML::EventNames::focusout, true);
  88. }
  89. }
  90. // FIXME: 3. Apply any relevant platform-specific conventions for focusing new focus target.
  91. // (For example, some platforms select the contents of a text control when that control is focused.)
  92. (void)new_focus_target;
  93. // 4. For each entry entry in new chain, in reverse order, run these substeps:
  94. for (auto& entry : new_chain.in_reverse()) {
  95. // 1. If entry is a focusable area: designate entry as the focused area of the document.
  96. // FIXME: This isn't entirely right.
  97. if (is<DOM::Element>(*entry))
  98. entry->document().set_focused_element(&static_cast<DOM::Element&>(*entry));
  99. else if (is<DOM::Document>(*entry))
  100. entry->document().set_focused_element(static_cast<DOM::Document&>(*entry).document_element());
  101. GC::Ptr<DOM::EventTarget> focus_event_target;
  102. if (is<DOM::Element>(*entry)) {
  103. // 2. If entry is an element, let focus event target be entry.
  104. focus_event_target = entry.ptr();
  105. } else if (is<DOM::Document>(*entry)) {
  106. // If entry is a Document object, let focus event target be that Document object's relevant global object.
  107. focus_event_target = static_cast<DOM::Document&>(*entry).window();
  108. }
  109. // 3. If entry is the last entry in new chain, and entry is an Element,
  110. // and the last entry in old chain is also an Element,
  111. // then let related focus target be the last entry in old chain.
  112. // Otherwise, let related focus target be null.
  113. GC::Ptr<DOM::EventTarget> related_focus_target;
  114. if (!new_chain.is_empty()
  115. && &entry == &new_chain.last()
  116. && is<DOM::Element>(*entry)
  117. && !old_chain.is_empty()
  118. && is<DOM::Element>(*old_chain.last())) {
  119. related_focus_target = old_chain.last().ptr();
  120. }
  121. // 4. If focus event target is not null, fire a focus event named focus at focus event target,
  122. // with related focus target as the related target.
  123. if (focus_event_target) {
  124. fire_a_focus_event(focus_event_target, related_focus_target, HTML::EventNames::focus, false);
  125. // AD-HOC: dispatch focusin
  126. fire_a_focus_event(focus_event_target, related_focus_target, HTML::EventNames::focusin, true);
  127. }
  128. }
  129. }
  130. // https://html.spec.whatwg.org/multipage/interaction.html#focus-chain
  131. static Vector<GC::Root<DOM::Node>> focus_chain(DOM::Node* subject)
  132. {
  133. // FIXME: Move this somewhere more spec-friendly.
  134. if (!subject)
  135. return {};
  136. // 1. Let output be an empty list.
  137. Vector<GC::Root<DOM::Node>> output;
  138. // 2. Let currentObject be subject.
  139. auto* current_object = subject;
  140. // 3. While true:
  141. while (true) {
  142. // 1. Append currentObject to output.
  143. output.append(GC::make_root(*current_object));
  144. // FIXME: 2. If currentObject is an area element's shape, then append that area element to output.
  145. // FIXME: Otherwise, if currentObject's DOM anchor is an element that is not currentObject itself, then append currentObject's DOM anchor to output.
  146. // FIXME: Everything below needs work. The conditions are not entirely right.
  147. if (!is<DOM::Document>(*current_object)) {
  148. // 3. If currentObject is a focusable area, then set currentObject to currentObject's DOM anchor's node document.
  149. current_object = &current_object->document();
  150. } else if (is<DOM::Document>(*current_object)
  151. && current_object->navigable()
  152. && current_object->navigable()->parent()) {
  153. // Otherwise, if currentObject is a Document whose node navigable's parent is non-null, then set currentObject to currentObject's node navigable's parent.
  154. current_object = current_object->navigable()->container();
  155. } else {
  156. // Otherwise, break.
  157. break;
  158. }
  159. }
  160. // 4. Return output.
  161. return output;
  162. }
  163. // https://html.spec.whatwg.org/multipage/interaction.html#focusing-steps
  164. // FIXME: This should accept more types.
  165. void run_focusing_steps(DOM::Node* new_focus_target, DOM::Node* fallback_target, [[maybe_unused]] Optional<ByteString> focus_trigger)
  166. {
  167. // FIXME: 1. If new focus target is not a focusable area, then set new focus target
  168. // to the result of getting the focusable area for new focus target,
  169. // given focus trigger if it was passed.
  170. // 2. If new focus target is null, then:
  171. if (!new_focus_target) {
  172. // 1. If no fallback target was specified, then return.
  173. if (!fallback_target)
  174. return;
  175. // 2. Otherwise, set new focus target to the fallback target.
  176. new_focus_target = fallback_target;
  177. }
  178. // 3. If new focus target is a navigable container with non-null content navigable, then set new focus target to the content navigable's active document.
  179. if (is<HTML::NavigableContainer>(*new_focus_target)) {
  180. auto& navigable_container = static_cast<HTML::NavigableContainer&>(*new_focus_target);
  181. if (auto content_navigable = navigable_container.content_navigable())
  182. new_focus_target = content_navigable->active_document();
  183. }
  184. // FIXME: 4. If new focus target is a focusable area and its DOM anchor is inert, then return.
  185. // 5. If new focus target is the currently focused area of a top-level browsing context, then return.
  186. if (!new_focus_target->document().browsing_context())
  187. return;
  188. auto top_level_traversable = new_focus_target->document().browsing_context()->top_level_traversable();
  189. if (new_focus_target == top_level_traversable->currently_focused_area().ptr())
  190. return;
  191. // 6. Let old chain be the current focus chain of the top-level browsing context in which
  192. // new focus target finds itself.
  193. auto old_chain = focus_chain(top_level_traversable->currently_focused_area());
  194. // 7. Let new chain be the focus chain of new focus target.
  195. auto new_chain = focus_chain(new_focus_target);
  196. // 8. Run the focus update steps with old chain, new chain, and new focus target respectively.
  197. run_focus_update_steps(old_chain, new_chain, new_focus_target);
  198. }
  199. // https://html.spec.whatwg.org/multipage/interaction.html#unfocusing-steps
  200. void run_unfocusing_steps(DOM::Node* old_focus_target)
  201. {
  202. // NOTE: The unfocusing steps do not always result in the focus changing, even when applied to the currently focused
  203. // area of a top-level browsing context. For example, if the currently focused area of a top-level browsing context
  204. // is a viewport, then it will usually keep its focus regardless until another focusable area is explicitly focused
  205. // with the focusing steps.
  206. auto is_shadow_host = [](DOM::Node* node) {
  207. return is<DOM::Element>(node) && static_cast<DOM::Element*>(node)->is_shadow_host();
  208. };
  209. // 1. If old focus target is a shadow host whose shadow root's delegates focus is true, and old focus target's
  210. // shadow root is a shadow-including inclusive ancestor of the currently focused area of a top-level browsing
  211. // context's DOM anchor, then set old focus target to that currently focused area of a top-level browsing
  212. // context.
  213. if (is_shadow_host(old_focus_target)) {
  214. auto shadow_root = static_cast<DOM::Element*>(old_focus_target)->shadow_root();
  215. if (shadow_root->delegates_focus()) {
  216. auto top_level_traversable = old_focus_target->document().browsing_context()->top_level_traversable();
  217. if (auto currently_focused_area = top_level_traversable->currently_focused_area()) {
  218. if (shadow_root->is_shadow_including_ancestor_of(*currently_focused_area)) {
  219. old_focus_target = currently_focused_area;
  220. }
  221. }
  222. }
  223. }
  224. // FIXME: 2. If old focus target is inert, then return.
  225. // FIXME: 3. If old focus target is an area element and one of its shapes is the currently focused area of a
  226. // top-level browsing context, or, if old focus target is an element with one or more scrollable regions, and one
  227. // of them is the currently focused area of a top-level browsing context, then let old focus target be that
  228. // currently focused area of a top-level browsing context.
  229. // NOTE: HTMLAreaElement is currently missing the shapes property
  230. auto top_level_traversable = old_focus_target->document().browsing_context()->top_level_traversable();
  231. // 4. Let old chain be the current focus chain of the top-level browsing context in which old focus target finds itself.
  232. auto old_chain = focus_chain(top_level_traversable->currently_focused_area());
  233. // 5. If old focus target is not one of the entries in old chain, then return.
  234. auto it = old_chain.find_if([&](auto const& node) { return old_focus_target == node; });
  235. if (it == old_chain.end())
  236. return;
  237. // 6. If old focus target is not a focusable area, then return.
  238. if (!old_focus_target->is_focusable())
  239. return;
  240. // 7. Let topDocument be old chain's last entry.
  241. auto* top_document = verify_cast<DOM::Document>(old_chain.last().ptr());
  242. // 8. If topDocument's node navigable has system focus, then run the focusing steps for topDocument's viewport.
  243. if (top_document->navigable()->traversable_navigable()->system_visibility_state() == HTML::VisibilityState::Visible) {
  244. run_focusing_steps(top_document);
  245. } else {
  246. // FIXME: Otherwise, apply any relevant platform-specific conventions for removing system focus from
  247. // topDocument's browsing context, and run the focus update steps with old chain, an empty list, and null
  248. // respectively.
  249. // What? It already doesn't have system focus, what possible platform-specific conventions are there?
  250. run_focus_update_steps(old_chain, {}, nullptr);
  251. }
  252. // FIXME: When the currently focused area of a top-level browsing context is somehow unfocused without another
  253. // element being explicitly focused in its stead, the user agent must immediately run the unfocusing steps for that
  254. // object.
  255. // What? How are we supposed to detect when something is "somehow unfocused without another element being explicitly focused"?
  256. }
  257. }