Skip to content

Commit

Permalink
Bug 1811129 - Implement the new dialog initial focus algorithm r=emilio
Browse files Browse the repository at this point in the history
The main changes of the new algorithm are

* Make the dialog focusing steps look at sequentially focusable elements instead of any focusable element.
* Make the dialog element itself get focus if it has the autofocus attribute set.
* Make the dialog element itself get focus as a fallback instead of focus being "reset" to the body element.

Spec PR (merged): whatwg/html#8199

Differential Revision: https://phabricator.services.mozilla.com/D181263
  • Loading branch information
sefeng211 committed Jul 6, 2023
1 parent c760df2 commit 185848e
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 49 deletions.
27 changes: 21 additions & 6 deletions dom/base/FragmentOrElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1036,9 +1036,14 @@ Element* nsIContent::GetFocusDelegate(bool aWithMouse,
whereToLook = root;
}

auto IsFocusable = [&](Element* aElement) {
auto IsFocusable = [&](Element* aElement) -> nsIFrame::Focusable {
nsIFrame* frame = aElement->GetPrimaryFrame();
return frame && frame->IsFocusable(aWithMouse);

if (!frame) {
return {};
}

return frame->IsFocusable(aWithMouse);
};

Element* potentialFocus = nullptr;
Expand All @@ -1059,10 +1064,20 @@ Element* nsIContent::GetFocusDelegate(bool aWithMouse,
// Found an autofocus candidate.
return el;
}
} else if (!potentialFocus && IsFocusable(el)) {
// This element could be the one if we can't find an
// autofocus candidate which has the precedence.
potentialFocus = el;
} else if (!potentialFocus) {
if (nsIFrame::Focusable focusable = IsFocusable(el)) {
if (IsHTMLElement(nsGkAtoms::dialog)) {
if (focusable.mTabIndex >= 0) {
// If focusTarget is a dialog element and descendant is sequentially
// focusable, then set focusableArea to descendant.
potentialFocus = el;
}
} else {
// This element could be the one if we can't find an
// autofocus candidate which has the precedence.
potentialFocus = el;
}
}
}

if (!autofocus && potentialFocus) {
Expand Down
6 changes: 5 additions & 1 deletion dom/html/HTMLDialogElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ void HTMLDialogElement::FocusDialog() {
doc->FlushPendingNotifications(FlushType::Frames);
}

RefPtr<Element> control = GetFocusDelegate(false /* aWithMouse */);
RefPtr<Element> control = HasAttr(nsGkAtoms::autofocus)
? this
: GetFocusDelegate(false /* aWithMouse */);

// If there isn't one of those either, then let control be subject.
if (!control) {
Expand All @@ -169,6 +171,8 @@ void HTMLDialogElement::FocusDialog() {
FocusCandidate(*control, IsInTopLayer());
}

int32_t HTMLDialogElement::TabIndexDefault() { return 0; }

void HTMLDialogElement::QueueCancelDialog() {
// queues an element task on the user interaction task source
OwnerDoc()
Expand Down
2 changes: 2 additions & 0 deletions dom/html/HTMLDialogElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class HTMLDialogElement final : public nsGenericHTMLElement {

MOZ_CAN_RUN_SCRIPT_BOUNDARY void FocusDialog();

int32_t TabIndexDefault() override;

nsString mReturnValue;

protected:
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 185848e

Please sign in to comment.