Bug 1781434 - Clean-up dialog initial focus code. r=pbz,Gijs

Make it a bit easier to read and less prone to race conditions. Remove a
setTimeout referencing bug 103197 which I'm pretty sure it's not an
issue.

Differential Revision: https://phabricator.services.mozilla.com/D156543
This commit is contained in:
Emilio Cobos Álvarez 2022-09-19 21:57:12 +00:00
Родитель 33d42fbb13
Коммит 93d2bb3408
5 изменённых файлов: 116 добавлений и 105 удалений

Просмотреть файл

@ -6,19 +6,19 @@
<script src="chrome://mochikit/content/tests/SimpleTest/WindowSnapshot.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
</head>
<body style="height: 100%" onload="setTimeout(onBodyLoad, 0);">
<iframe id="test" src="398289-resource.xhtml" width="100%" height="100%">
</iframe>
<body style="height: 100%; margin: 0; overflow: hidden;" onload="setTimeout(onBodyLoad, 0);">
<iframe id="test" style="border: 0; width: 100%; height: 100%" scrolling="no" src="398289-resource.xhtml"></iframe>
<script class="testbody" type="text/javascript">
<script class="testbody">
var snap1, snap2;
SimpleTest.waitForExplicitFinish();
async function onBodyLoad() {
window.frames[0].document.getElementById("test").selectedIndex = 0;
window.frames[0].document.getElementById("test").selectedIndex = 1;
frames[0].scrollTo(0, 0);
scrollTo(0, 0);
snap1 = await snapshotWindow(window);
document.getElementById("test").onload = onFrameLoad;
@ -26,13 +26,14 @@
}
async function onFrameLoad() {
frames[0].scrollTo(0, 0);
scrollTo(0, 0);
snap2 = await snapshotWindow(window);
var equal, str1, str2;
[equal, str1, str2] = compareSnapshots(snap1, snap2, true);
ok(equal, "persistent attribute in tab box broken, expected: "+str1+" got: "+str2);
SimpleTest.finish();
}
</script>

Просмотреть файл

@ -7,6 +7,5 @@ support-files =
skip-if =
toolkit == 'android'
os == 'linux' && (debug || asan) #android: TIMED_OUT (For Linux : 950636)
win10_2004 # Bug 1781434
[test_modal_select.html]
skip-if = toolkit == 'android' #android: TIMED_OUT

Просмотреть файл

@ -1063,13 +1063,19 @@ async function runTests(util) {
passValue: "",
checkMsg: "",
checked: false,
focused: "infoBody",
focused: null, // Nothing focused until the delay triggers.
defButton: "button0",
butt0Label: "OK",
butt1Label: "Cancel",
butt0Disabled: true,
};
if (isOSX) {
// OS X doesn't initially focus the button, but rather the infoBody.
// The focus stays there even after the button-enable delay has fired.
state.focused = "infoBody";
}
action = {
buttonClick: "pollOK",
};

Просмотреть файл

@ -1,7 +1,6 @@
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<window onload="loaded()"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
xmlns:html="http://www.w3.org/1999/xhtml">
<dialog id="dialog-focus"
buttons="extra2,accept,cancel">
@ -24,9 +23,7 @@
<button id="two" label="Two" hidden="true"/>
<script>
function loaded()
{
if (window.arguments) {
if (window.arguments) {
var step = window.arguments[0];
switch (step) {
case 2:
@ -45,9 +42,11 @@ function loaded()
document.getElementById("tabbox").hidden = false;
break;
case 7:
window.addEventListener("load", function() {
var two = document.getElementById("two");
two.hidden = false;
two.focus();
});
break;
case 8:
document.getElementById("textbox-yes").hidden = false;
@ -56,7 +55,6 @@ function loaded()
document.getElementById("textbox-no").hidden = false;
break;
}
}
}
</script>

Просмотреть файл

@ -240,13 +240,14 @@
window.moveTo(xOffset, yOffset);
}
postLoadInit(aEvent) {
let focusInit = () => {
const defaultButton = this.getButton(this.defaultButton);
// give focus to the first focusable element in the dialog
// Give focus to the first focusable element in the dialog
_setInitialFocusIfNeeded() {
let focusedElt = document.commandDispatcher.focusedElement;
if (!focusedElt) {
if (focusedElt) {
return;
}
const defaultButton = this.getButton(this.defaultButton);
Services.focus.moveFocus(
window,
null,
@ -255,8 +256,11 @@
);
focusedElt = document.commandDispatcher.focusedElement;
if (focusedElt) {
var initialFocusedElt = focusedElt;
if (!focusedElt) {
return; // No focusable element?
}
let firstFocusedElt = focusedElt;
while (
focusedElt.localName == "tab" ||
focusedElt.getAttribute("noinitialfocus") == "true"
@ -268,45 +272,48 @@
Services.focus.FLAG_NOPARENTFRAME
);
focusedElt = document.commandDispatcher.focusedElement;
if (focusedElt) {
if (focusedElt == initialFocusedElt) {
if (focusedElt == firstFocusedElt) {
if (focusedElt.getAttribute("noinitialfocus") == "true") {
focusedElt.blur();
}
break;
}
// Didn't find anything else to focus, we're done.
return;
}
}
if (initialFocusedElt.localName == "tab") {
if (firstFocusedElt.localName == "tab") {
if (focusedElt.hasAttribute("dlgtype")) {
// We don't want to focus on anonymous OK, Cancel, etc. buttons,
// so return focus to the tab itself
initialFocusedElt.focus();
firstFocusedElt.focus();
}
} else if (
AppConstants.platform != "macosx" &&
focusedElt.hasAttribute("dlgtype") &&
focusedElt != defaultButton
) {
// If the default button is not focusable, then return focus.
defaultButton.focus();
if (document.commandDispatcher.focusedElement != defaultButton) {
initialFocusedElt.focus();
// If the default button is not focusable, then return focus to the
// initial element if possible, or blur otherwise.
if (firstFocusedElt.getAttribute("noinitialfocus") == "true") {
focusedElt.blur();
} else {
firstFocusedElt.focus();
}
}
}
}
postLoadInit(aEvent) {
this._setInitialFocusIfNeeded();
try {
const defaultButton = this.getButton(this.defaultButton);
if (defaultButton) {
window.notifyDefaultButtonLoaded(defaultButton);
}
} catch (e) {}
};
// Give focus after onload completes, see bug 103197.
setTimeout(focusInit, 0);
if (this._l10nButtons.length) {
document.l10n.translateElements(this._l10nButtons).then(() => {