Focus.cpp 14 KB

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