Skip to content

Commit

Permalink
Only restore dialog focus if focus is in dialog
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=256717
rdar://109572320

Reviewed by NOBODY (OOPS!).

Implement whatwg/html#9178 to avoid unexpected focus shifting when focus is already outside of the dialog.

* LayoutTests/imported/w3c/web-platform-tests/html/semantics/interactive-elements/the-dialog-element/dialog-focus-previous-outside-expected.txt:
* Source/WebCore/html/HTMLDialogElement.cpp:
(WebCore::HTMLDialogElement::close):
  • Loading branch information
nt1m committed Jan 3, 2025
1 parent 4890053 commit b69e934
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
button 1 button 2

FAIL Focus should not be restored if the currently focused element is not inside the dialog. assert_equals: expected Element node <button id="b2">button 2</button> but got Element node <button id="b1">button 1</button>
FAIL Focus restore should not occur when the focused element is in a shadowroot outside of the dialog. assert_equals: document.activeElement should point at the shadow host. expected Element node <div id="host">

</div> but got Element node <button id="b2">button 2</button>
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.
PASS Focus restore should occur when the focused element is slotted into a dialog.
FAIL Focus restore should occur when the focused element is slotted into a dialog. assert_equals: expected Element node <button id="b2">button 2</button> but got Element node <button id="host2button">button</button>
PASS Focus restore should always occur for modal dialogs.

13 changes: 9 additions & 4 deletions Source/WebCore/html/HTMLDialogElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ void HTMLDialogElement::close(const String& result)

setBooleanAttribute(openAttr, false);

if (isModal())
bool wasModal = isModal();

if (wasModal)
removeFromTopLayer();

setIsModal(false);
Expand All @@ -135,9 +137,12 @@ void HTMLDialogElement::close(const String& result)
m_returnValue = result;

if (RefPtr element = std::exchange(m_previouslyFocusedElement, nullptr).get()) {
FocusOptions options;
options.preventScroll = true;
element->focus(options);
RefPtr focusedElement = document().focusedElement();
if ((focusedElement && containsIncludingShadowDOM(focusedElement)) || wasModal) {
FocusOptions options;
options.preventScroll = true;
element->focus(options);
}
}

queueTaskToDispatchEvent(TaskSource::UserInteraction, Event::create(eventNames().closeEvent, Event::CanBubble::No, Event::IsCancelable::No));
Expand Down

0 comments on commit b69e934

Please sign in to comment.