Skip to content

Commit

Permalink
Bug 551434 - Store the keydown target and retarget the keypress/keyup…
Browse files Browse the repository at this point in the history
… events when focus changes from chrome to content. r=smaug
  • Loading branch information
Enndeakin committed Aug 17, 2010
1 parent acfc13f commit 838688f
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
2 changes: 2 additions & 0 deletions layout/base/nsIPresShell.h
Expand Up @@ -1159,6 +1159,8 @@ class nsIPresShell : public nsIPresShell_base
// Live pres shells, for memory and other tracking
typedef nsPtrHashKey<nsIPresShell> PresShellPtrKey;
static nsTHashtable<PresShellPtrKey> *sLiveShells;

static nsIContent* gKeyDownTarget;
};

/**
Expand Down
29 changes: 29 additions & 0 deletions layout/base/nsPresShell.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -1838,6 +1839,10 @@ PresShell::Destroy()

MaybeReleaseCapturingContent();

if (gKeyDownTarget && gKeyDownTarget->GetOwnerDoc() == mDocument) {
NS_RELEASE(gKeyDownTarget);
}

mContentToScrollTo = nsnull;

if (mPresContext) {
Expand Down Expand Up @@ -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() ||
Expand Down
2 changes: 2 additions & 0 deletions layout/base/tests/chrome/Makefile.in
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions layout/base/tests/chrome/bug551434_childframe.html
@@ -0,0 +1 @@
<input id='i4'>
79 changes: 79 additions & 0 deletions layout/base/tests/chrome/test_bug551434.html
@@ -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>

0 comments on commit 838688f

Please sign in to comment.