LibWeb/HTML: Implement focus restoration in HTMLDialogElement

When a dialog is closed, restore focus to the previously focused
element if focus is within the dialog or if the dialog was modal.
This commit is contained in:
Feng Yu 2025-09-11 11:17:37 -07:00 committed by Jelle Raaijmakers
commit fd3c69227f
Notes: github-actions[bot] 2025-10-03 06:56:57 +00:00
4 changed files with 117 additions and 7 deletions

View file

@ -43,6 +43,7 @@ void HTMLDialogElement::visit_edges(JS::Cell::Visitor& visitor)
visitor.visit(m_close_watcher); visitor.visit(m_close_watcher);
visitor.visit(m_request_close_source_element); visitor.visit(m_request_close_source_element);
visitor.visit(m_previously_focused_element);
} }
void HTMLDialogElement::removed_from(Node* old_parent, Node& old_root) void HTMLDialogElement::removed_from(Node* old_parent, Node& old_root)
@ -142,7 +143,9 @@ WebIDL::ExceptionOr<void> HTMLDialogElement::show()
// 9. Set the dialog close watcher with this. // 9. Set the dialog close watcher with this.
set_close_watcher(); set_close_watcher();
// FIXME: 10. Set this's previously focused element to the focused element.
// 10. Set this's previously focused element to the focused element.
m_previously_focused_element = document().focused_area();
// 11. Let document be this's node document. // 11. Let document be this's node document.
auto document = m_document; auto document = m_document;
@ -358,7 +361,8 @@ void HTMLDialogElement::close_the_dialog(Optional<String> result, GC::Ptr<DOM::E
if (m_is_modal) if (m_is_modal)
document().request_an_element_to_be_remove_from_the_top_layer(*this); document().request_an_element_to_be_remove_from_the_top_layer(*this);
// FIXME: 7. Let wasModal be the value of subject's is modal flag. // 7. Let wasModal be the value of subject's is modal flag.
auto was_modal = m_is_modal;
// 8. Set the is modal flag of subject to false. // 8. Set the is modal flag of subject to false.
set_is_modal(false); set_is_modal(false);
@ -376,11 +380,21 @@ void HTMLDialogElement::close_the_dialog(Optional<String> result, GC::Ptr<DOM::E
// 12. Set subject's request close source element to null. // 12. Set subject's request close source element to null.
m_request_close_source_element = nullptr; m_request_close_source_element = nullptr;
// FIXME: 13. If subject's previously focused element is not null, then: // 13. If subject's previously focused element is not null, then:
if (m_previously_focused_element) {
// 1. Let element be subject's previously focused element. // 1. Let element be subject's previously focused element.
auto element = m_previously_focused_element;
// 2. Set subject's previously focused element to null. // 2. Set subject's previously focused element to null.
m_previously_focused_element = nullptr;
// 3. If subject's node document's focused area of the document's DOM anchor is a shadow-including inclusive descendant of subject, // 3. If subject's node document's focused area of the document's DOM anchor is a shadow-including inclusive descendant of subject,
// or wasModal is true, then run the focusing steps for element; the viewport should not be scrolled by doing this step. // or wasModal is true, then run the focusing steps for element; the viewport should not be scrolled by doing this step.
auto focused_element = document().focused_area();
auto is_focus_within_dialog = focused_element && focused_element->is_shadow_including_inclusive_descendant_of(*this);
if (is_focus_within_dialog || was_modal)
run_focusing_steps(element);
}
// 14. Queue an element task on the user interaction task source given the subject element to fire an event named close at subject. // 14. Queue an element task on the user interaction task source given the subject element to fire an event named close at subject.
queue_an_element_task(HTML::Task::Source::UserInteraction, [this] { queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {

View file

@ -72,6 +72,9 @@ private:
// https://html.spec.whatwg.org/multipage/interactive-elements.html#dialog-toggle-task-tracker // https://html.spec.whatwg.org/multipage/interactive-elements.html#dialog-toggle-task-tracker
Optional<ToggleTaskTracker> m_dialog_toggle_task_tracker; Optional<ToggleTaskTracker> m_dialog_toggle_task_tracker;
// https://html.spec.whatwg.org/multipage/interactive-elements.html#previously-focused-element
GC::Ptr<Node> m_previously_focused_element;
}; };
} }

View file

@ -0,0 +1,11 @@
Harness status: OK
Found 5 tests
3 Pass
2 Fail
Pass Focus should not be restored if the currently focused element is not inside the dialog.
Pass Focus restore should not occur when the focused element is in a shadowroot outside of the dialog.
Pass Focus restore should occur when the focused element is in a shadowroot inside the dialog.
Fail Focus restore should occur when the focused element is slotted into a dialog.
Fail Focus restore should always occur for modal dialogs.

View file

@ -0,0 +1,82 @@
<!DOCTYPE html>
<link rel=author href="mailto:jarhar@chromium.org">
<link rel=help href="https://github.com/whatwg/html/issues/8904">
<script src="../../../../resources/testharness.js"></script>
<script src="../../../../resources/testharnessreport.js"></script>
<button id=b1>button 1</button>
<button id=b2>button 2</button>
<div id=host>
<template shadowrootmode=open>
<button>button in shadowroot outside dialog</button>
</template>
</div>
<dialog id=mydialog>
<button id=b3>button in dialog</button>
<div id=dialoghost>
<template shadowrootmode=open>
<button>button in shadowroot in dialog</button>
</template>
</div>
</dialog>
<div id=host2>
<template shadowrootmode=open>
<dialog>
<slot></slot>
</dialog>
</template>
<button id=host2button>button</button>
</div>
<dialog id=mydialog2>hello world</dialog>
<script>
test(() => {
b1.focus();
mydialog.show();
b2.focus();
mydialog.close();
assert_equals(document.activeElement, b2);
}, 'Focus should not be restored if the currently focused element is not inside the dialog.');
test(() => {
const shadowbutton = host.shadowRoot.querySelector('button');
b2.focus();
mydialog.show();
shadowbutton.focus();
mydialog.close();
assert_equals(document.activeElement, host, 'document.activeElement should point at the shadow host.');
assert_equals(host.shadowRoot.activeElement, shadowbutton, 'The button in the shadowroot should remain focused.');
}, 'Focus restore should not occur when the focused element is in a shadowroot outside of the dialog.');
test(() => {
const shadowbutton = dialoghost.shadowRoot.querySelector('button');
b2.focus();
mydialog.show();
shadowbutton.focus();
mydialog.close();
assert_equals(document.activeElement, b2);
}, 'Focus restore should occur when the focused element is in a shadowroot inside the dialog.');
test(() => {
const dialog = host2.shadowRoot.querySelector('dialog');
b2.focus();
dialog.show();
host2button.focus();
dialog.close();
assert_equals(document.activeElement, b2);
}, 'Focus restore should occur when the focused element is slotted into a dialog.');
test(() => {
b1.focus();
const dialog = document.getElementById('mydialog2');
dialog.showModal();
dialog.blur();
assert_equals(document.activeElement, document.body,
'Focus should return to the body when calling dialog.blur().');
dialog.close();
assert_equals(document.activeElement, b1,
'Focus should be restored to the previously focused element.');
}, 'Focus restore should always occur for modal dialogs.');
</script>