Bug 1965052 - Only check top-most dialog for closedby in HandleEsc r=edgar,dom-core

We do not need to iterate over all open dialogs, as the top-most dialog
must respond _somehow_ to the escape key press, it either closes, or it
blocks the escape key-press, disallowing outer dialogs to handle the
key-press.

Differential Revision: https://phabricator.services.mozilla.com/D248309
This commit is contained in:
Keith Cirkel
2025-05-09 16:17:06 +00:00
committed by mozilla@keithcirkel.co.uk
parent fdf96a1cd2
commit c71aafeaae
3 changed files with 136 additions and 5 deletions

View File

@@ -15029,14 +15029,17 @@ void Document::HandleEscKey() {
}
}
// Not all dialogs exist in the top layer, so despite already iterating
// through all top layer elements we also need to iterate over non-modal
// dialogs, as they may have a specified `closedby` value which may allow them
// to be closed via Escape key.
for (RefPtr<HTMLDialogElement> dialog : Reversed(mOpenDialogs)) {
// through all top layer elements we also need to check open dialogs that are
// _not_ open via the top-layer (showModal).
// The top-most dialog in mOpenDialogs may need to be closed.
if (RefPtr<HTMLDialogElement> dialog =
mOpenDialogs.SafeLastElement(nullptr)) {
if (dialog->GetClosedBy() != HTMLDialogElement::ClosedBy::None) {
MOZ_ASSERT(StaticPrefs::dom_dialog_light_dismiss_enabled(),
"Light Dismiss must have been enabled for GetClosedBy() "
"returns != ClosedBy::None");
const mozilla::dom::Optional<nsAString> returnValue;
dialog->RequestClose(returnValue);
return;
}
}
}