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

UltraBlame original commit: cbf5ea1b17d2b0fbea7c1849ab2a1902396ad291
  • Loading branch information
marco-c committed Jul 16, 2023
1 parent 685c14f commit fb8dfc9
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,

return el;
}
} else if (!potentialFocus && IsFocusable(el)) {


potentialFocus = el;
} else if (!potentialFocus) {
if (nsIFrame::Focusable focusable = IsFocusable(el)) {
if (IsHTMLElement(nsGkAtoms::dialog)) {
if (focusable.mTabIndex >= 0) {


potentialFocus = el;
}
} else {


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 );
RefPtr<Element> control = HasAttr(nsGkAtoms::autofocus)
? this
: GetFocusDelegate(false );


if (!control) {
Expand All @@ -169,6 +171,8 @@ void HTMLDialogElement::FocusDialog() {
FocusCandidate(*control, IsInTopLayer());
}

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

void HTMLDialogElement::QueueCancelDialog() {

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 fb8dfc9

Please sign in to comment.