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