Bug 551434 - Store the keydown target and retarget the keypress/keyup events when focus changes from chrome to content. r=smaug

This commit is contained in:
Neil Deakin 2010-08-17 14:37:50 +02:00
Родитель b3ebb64497
Коммит 0381acd0c0
5 изменённых файлов: 113 добавлений и 0 удалений

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

@ -1159,6 +1159,8 @@ protected:
// Live pres shells, for memory and other tracking
typedef nsPtrHashKey<nsIPresShell> PresShellPtrKey;
static nsTHashtable<PresShellPtrKey> *sLiveShells;
static nsIContent* gKeyDownTarget;
};
/**

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

@ -237,6 +237,7 @@ using namespace mozilla::layers;
PRBool nsIPresShell::gIsAccessibilityActive = PR_FALSE;
CapturingContentInfo nsIPresShell::gCaptureInfo;
nsIContent* nsIPresShell::gKeyDownTarget;
// convert a color value to a string, in the CSS format #RRGGBB
// * - initially created for bugs 31816, 20760, 22963
@ -1838,6 +1839,10 @@ PresShell::Destroy()
MaybeReleaseCapturingContent();
if (gKeyDownTarget && gKeyDownTarget->GetOwnerDoc() == mDocument) {
NS_RELEASE(gKeyDownTarget);
}
mContentToScrollTo = nsnull;
if (mPresContext) {
@ -6460,6 +6465,30 @@ PresShell::HandleEvent(nsIView *aView,
// frame goes away while it is focused.
if (!mCurrentEventContent || !GetCurrentEventFrame())
mCurrentEventContent = mDocument->GetRootElement();
if (aEvent->message == NS_KEY_DOWN) {
NS_IF_RELEASE(gKeyDownTarget);
NS_IF_ADDREF(gKeyDownTarget = mCurrentEventContent);
}
else if ((aEvent->message == NS_KEY_PRESS || aEvent->message == NS_KEY_UP) &&
gKeyDownTarget) {
// If a different element is now focused for the keypress/keyup event
// than what was focused during the keydown event, check if the new
// focused element is not in a chrome document any more, and if so,
// retarget the event back at the keydown target. This prevents a
// content area from grabbing the focus from chrome in-between key
// events.
if (mCurrentEventContent &&
nsContentUtils::IsChromeDoc(gKeyDownTarget->GetCurrentDoc()) &&
!nsContentUtils::IsChromeDoc(mCurrentEventContent->GetCurrentDoc())) {
mCurrentEventContent = gKeyDownTarget;
}
if (aEvent->message == NS_KEY_UP) {
NS_RELEASE(gKeyDownTarget);
}
}
mCurrentEventFrame = nsnull;
if (!mCurrentEventContent || !GetCurrentEventFrame() ||

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

@ -49,6 +49,8 @@ _CHROME_FILES = \
test_bug504311.xul \
test_bug514660.xul \
test_bug533845.xul \
test_bug551434.html \
bug551434_childframe.html \
test_chrome_content_integration.xul \
chrome_content_integration_window.xul \
test_printpreview.xul \

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

@ -0,0 +1 @@
<input id='i4'>

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

@ -0,0 +1,79 @@
<html>
<head>
<title>Test for Bug 551434</title>
<script type="application/javascript"
src="chrome://mochikit/content/MochiKit/packed.js"></script>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
</head>
<body>
</div>
<pre id="test">
<input id="i1" onkeydown="gKeyDown1++; $('i2').focus();" onkeypress="gKeyPress1++;" onkeyup="gKeyUp1++;"/>
<input id="i2" onkeydown="gKeyDown2++;" onkeypress="gKeyPress2++;" onkeyup="gKeyUp2++;"/>
<input id="i3" onkeydown="gKeyDown3++; frames[0].document.getElementById('i4').focus();"
onkeypress="gKeyPress3++;" onkeyup="gKeyUp3++;"/>
<iframe id="iframe" src="http://example.org/chrome/layout/base/test/chrome/bug551434_childframe.html"></iframe>
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
var gKeyDown1 = 0, gKeyPress1 = 0, gKeyUp1 = 0;
var gKeyDown2 = 0, gKeyPress2 = 0, gKeyUp2 = 0;
var gKeyDown3 = 0, gKeyPress3 = 0, gKeyUp3 = 0;
function runTest()
{
$("i1").focus();
// key events should not be retargeted when the focus changes to an
// element in the same document.
synthesizeKey("a", { type: "keydown" });
is(document.activeElement, $("i2"), "input 2 in focused");
synthesizeKey("a", { type: "keypress" });
synthesizeKey("a", { type: "keyup" });
is(gKeyDown1, 1, "keydown on input 1");
is(gKeyPress1, 0, "keypress on input 1");
is(gKeyUp1, 0, "keyup on input 1");
is(gKeyDown2, 0, "keydown on input 2");
is(gKeyPress2, 1, "keypress on input 2");
is(gKeyUp2, 1, "keyup on input 2");
is($("i1").value, "", "input 1 value");
is($("i2").value, "a", "input 2 value");
// key events should however be retargeted when the focus changes to an
// element in the a content document from a chrome document.
$("i3").focus();
synthesizeKey("b", { type: "keydown" });
synthesizeKey("b", { type: "keypress" });
synthesizeKey("b", { type: "keyup" });
is(gKeyDown3, 1, "keydown on input 3");
is(gKeyPress3, 1, "keypress on input 3");
is(gKeyUp3, 1, "keyup on input 3");
var i4 = frames[0].document.getElementById("i4");
is($("i3").value, "b", "input 3 value");
is(i4.value, "", "input 4 value");
is(document.activeElement, $("iframe"), "parent focus");
is(frames[0].document.activeElement, i4, "child focus");
SimpleTest.finish();
}
SimpleTest.waitForFocus(runTest);
</script>
</pre>
</body>
</html>