Skip to content

Commit 0a3d5fa

Browse files
committed
Bug 1811129 - Implement the new dialog initial focus algorithm r=emilio
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
1 parent f34cf84 commit 0a3d5fa

File tree

9 files changed

+31
-54
lines changed

9 files changed

+31
-54
lines changed

dom/base/FragmentOrElement.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,9 +1036,14 @@ Element* nsIContent::GetFocusDelegate(bool aWithMouse,
10361036
whereToLook = root;
10371037
}
10381038

1039-
auto IsFocusable = [&](Element* aElement) {
1039+
auto IsFocusable = [&](Element* aElement) -> nsIFrame::Focusable {
10401040
nsIFrame* frame = aElement->GetPrimaryFrame();
1041-
return frame && frame->IsFocusable(aWithMouse);
1041+
1042+
if (!frame) {
1043+
return {};
1044+
}
1045+
1046+
return frame->IsFocusable(aWithMouse);
10421047
};
10431048

10441049
Element* potentialFocus = nullptr;
@@ -1059,10 +1064,20 @@ Element* nsIContent::GetFocusDelegate(bool aWithMouse,
10591064
// Found an autofocus candidate.
10601065
return el;
10611066
}
1062-
} else if (!potentialFocus && IsFocusable(el)) {
1063-
// This element could be the one if we can't find an
1064-
// autofocus candidate which has the precedence.
1065-
potentialFocus = el;
1067+
} else if (!potentialFocus) {
1068+
if (nsIFrame::Focusable focusable = IsFocusable(el)) {
1069+
if (IsHTMLElement(nsGkAtoms::dialog)) {
1070+
if (focusable.mTabIndex >= 0) {
1071+
// If focusTarget is a dialog element and descendant is sequentially
1072+
// focusable, then set focusableArea to descendant.
1073+
potentialFocus = el;
1074+
}
1075+
} else {
1076+
// This element could be the one if we can't find an
1077+
// autofocus candidate which has the precedence.
1078+
potentialFocus = el;
1079+
}
1080+
}
10661081
}
10671082

10681083
if (!autofocus && potentialFocus) {

dom/html/HTMLDialogElement.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ void HTMLDialogElement::FocusDialog() {
140140
doc->FlushPendingNotifications(FlushType::Frames);
141141
}
142142

143-
RefPtr<Element> control = GetFocusDelegate(false /* aWithMouse */);
143+
RefPtr<Element> control = HasAttr(nsGkAtoms::autofocus)
144+
? this
145+
: GetFocusDelegate(false /* aWithMouse */);
144146

145147
// If there isn't one of those either, then let control be subject.
146148
if (!control) {
@@ -150,6 +152,8 @@ void HTMLDialogElement::FocusDialog() {
150152
FocusCandidate(*control, IsInTopLayer());
151153
}
152154

155+
int32_t HTMLDialogElement::TabIndexDefault() { return 0; }
156+
153157
void HTMLDialogElement::QueueCancelDialog() {
154158
// queues an element task on the user interaction task source
155159
OwnerDoc()

dom/html/HTMLDialogElement.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class HTMLDialogElement final : public nsGenericHTMLElement {
4747

4848
MOZ_CAN_RUN_SCRIPT_BOUNDARY void FocusDialog();
4949

50+
int32_t TabIndexDefault() override;
51+
5052
nsString mReturnValue;
5153

5254
protected:

testing/web-platform/meta/css/css-contain/content-visibility/content-visibility-with-top-layer-006.html.ini

Lines changed: 0 additions & 5 deletions
This file was deleted.

testing/web-platform/meta/html/semantics/interactive-elements/the-dialog-element/child-sequential-focus.html.ini

Lines changed: 0 additions & 12 deletions
This file was deleted.

testing/web-platform/meta/html/semantics/interactive-elements/the-dialog-element/dialog-focus-shadow.html.ini

Lines changed: 0 additions & 20 deletions
This file was deleted.

testing/web-platform/meta/html/semantics/interactive-elements/the-dialog-element/dialog-showModal.html.ini

Lines changed: 0 additions & 5 deletions
This file was deleted.

testing/web-platform/meta/html/semantics/interactive-elements/the-dialog-element/show-modal-focusing-steps.html.ini

Lines changed: 0 additions & 5 deletions
This file was deleted.

testing/web-platform/tests/html/semantics/interactive-elements/the-dialog-element/modal-dialog-backdrop-opacity.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
background-color: rgb(0, 128, 0);
88
opacity: 0.5;
99
}
10+
dialog:focus {
11+
outline: none;
12+
}
1013
</style>
1114
<body>
1215
<dialog>Test passes if you see a green backdrop at half opacity.</dialog>

0 commit comments

Comments
 (0)