Bug 1649278 - Don't do PreventDefault for escape key if <select> dropdown is not shown r=smaug

We always use PreventDefault for <select> element, which is
problematic if modal dialog is on as it prevents cancelling
the dialog.

We fix it by not blocking the default action if <select> is
not shown.

Differential Revision: https://phabricator.services.mozilla.com/D82590
This commit is contained in:
sefeng 2020-07-15 19:00:30 +00:00
parent da46069826
commit 7ba74df412
4 changed files with 61 additions and 3 deletions

View File

@ -2,6 +2,7 @@
prefs =
dom.dialog_element.enabled=true
[test_cancelDialogByEscape.html]
[test_dialog_cancel_with_select.html]
[test_dialog_cancel_events.html]
[test_dialog_cancel_preventDefault.html]
[test_dialog_keydown_preventDefault.html]

View File

@ -0,0 +1,48 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1322947
-->
<head>
<title>Test for Bug 1322947</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body onload="SimpleTest.waitForFocus(runTest)">
<a target="_blank"
href="https://bugzilla.mozilla.org/show_bug.cgi?id=1322947"> Test dialog modal is closed by escape key</a>
<p id="display"></p>
<dialog id="dialog">
<select>
<option value="one">one</option>
<option value="two">two</option>
</select>
</dialog>
<pre id="test">
<script type="application/javascript">
SimpleTest.waitForExplicitFinish();
function runTest() {
const dialog = document.getElementById("dialog");
const select = document.querySelector("select");
dialog.addEventListener("close", function() {
ok(dialog.close, "dialog with select is closed");
done();
});
dialog.showModal();
ok(select == document.activeElement, "select element should be focused");
synthesizeKey("VK_ESCAPE", {}, window);
}
function done() {
SimpleTest.finish();
}
</script>
</pre>
</body>
</html>

View File

@ -2061,11 +2061,20 @@ nsresult nsListControlFrame::KeyDown(dom::Event* aKeyEvent) {
return NS_OK;
}
// We don't want to preventDefault for escape key if the dropdown
// popup is not shown.
// Need to do the check before AboutToRollup because AboutToRollup
// may update the dropdown flags.
bool doPreventDefault =
!mComboboxFrame || mComboboxFrame->IsDroppedDownOrHasParentPopup();
AboutToRollup();
// If the select element is a dropdown style, Enter key should be
// If the select element is a dropdown style, Escape key should be
// consumed everytime since Escape key may be pressed accidentally after
// the dropdown is closed by Escepe key.
aKeyEvent->PreventDefault();
if (doPreventDefault) {
aKeyEvent->PreventDefault();
}
return NS_OK;
}
case NS_VK_PAGE_UP: {

View File

@ -343,7 +343,7 @@ function runTests()
reset()
synthesizeKey("KEY_Escape");
check(true, false, "'Escape' key on combobox");
check(false, false, "'Escape' key on combobox");
if (!kIsWin) {
reset()