Bug 625452 - Make sure all plaintext editor commands generate trusted input events; r=roc a=blocking-final+

This commit is contained in:
Ehsan Akhgari 2011-01-15 23:16:45 -05:00
Родитель 4c368eaf5a
Коммит 79f95f5299
5 изменённых файлов: 98 добавлений и 6 удалений

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

@ -204,9 +204,29 @@ public:
nsIPrivateTextRangeList *aTextRange)=0;
nsresult EndIMEComposition();
void BeginKeypressHandling() { mLastKeypressEventWasTrusted = eTriTrue; }
void BeginKeypressHandling(nsIDOMNSEvent* aEvent);
void EndKeypressHandling() { mLastKeypressEventWasTrusted = eTriUnset; }
class FireTrustedInputEvent {
public:
explicit FireTrustedInputEvent(nsEditor* aSelf, PRBool aActive = PR_TRUE)
: mEditor(aSelf)
, mShouldAct(aActive && mEditor->mLastKeypressEventWasTrusted == eTriUnset) {
if (mShouldAct) {
mEditor->BeginKeypressHandling();
}
}
~FireTrustedInputEvent() {
if (mShouldAct) {
mEditor->EndKeypressHandling();
}
}
private:
nsEditor* mEditor;
PRBool mShouldAct;
};
protected:
nsCString mContentMIMEType; // MIME type of the doc we are editing.

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

@ -119,6 +119,8 @@ NS_IMETHODIMP nsPlaintextEditor::InsertTextFromTransferable(nsITransferable *aTr
PRInt32 aDestOffset,
PRBool aDoDeleteSelection)
{
FireTrustedInputEvent trusted(this);
nsresult rv = NS_OK;
char* bestFlavor = nsnull;
nsCOMPtr<nsISupports> genericDataObj;
@ -430,15 +432,9 @@ NS_IMETHODIMP nsPlaintextEditor::Paste(PRInt32 aSelectionType)
if (!nsEditorHookUtils::DoInsertionHook(domdoc, nsnull, trans))
return NS_OK;
// Make sure to issue an input event for paste operations
NS_ASSERTION(mLastKeypressEventWasTrusted == eTriUnset, "How come our status is not clear?");
mLastKeypressEventWasTrusted = eTriTrue;
// Beware! This may flush notifications via synchronous
// ScrollSelectionIntoView.
rv = InsertTextFromTransferable(trans, nsnull, nsnull, PR_TRUE);
mLastKeypressEventWasTrusted = eTriUnset;
}
}

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

@ -747,6 +747,8 @@ NS_IMETHODIMP nsPlaintextEditor::DeleteSelection(nsIEditor::EDirection aAction)
nsAutoPlaceHolderBatch batch(this, nsGkAtoms::DeleteTxnName);
nsAutoRules beginRulesSniffing(this, kOpDeleteSelection, aAction);
FireTrustedInputEvent trusted(this, aAction != eNone);
// pre-process
nsCOMPtr<nsISelection> selection;
result = GetSelection(getter_AddRefs(selection));
@ -1215,6 +1217,8 @@ nsPlaintextEditor::Undo(PRUint32 aCount)
nsAutoUpdateViewBatch beginViewBatching(this);
FireTrustedInputEvent trusted(this);
ForceCompositionEnd();
nsAutoRules beginRulesSniffing(this, kOpUndo, nsIEditor::eNone);
@ -1242,6 +1246,8 @@ nsPlaintextEditor::Redo(PRUint32 aCount)
nsAutoUpdateViewBatch beginViewBatching(this);
FireTrustedInputEvent trusted(this);
ForceCompositionEnd();
nsAutoRules beginRulesSniffing(this, kOpRedo, nsIEditor::eNone);
@ -1296,6 +1302,8 @@ nsPlaintextEditor::FireClipboardEvent(PRInt32 aType)
NS_IMETHODIMP nsPlaintextEditor::Cut()
{
FireTrustedInputEvent trusted(this);
if (FireClipboardEvent(NS_CUT))
return DeleteSelection(eNone);
return NS_OK;

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

@ -57,6 +57,7 @@ _TEST_FILES = \
test_bug602130.html \
test_bug603556.html \
test_bug604532.html \
test_bug625452.html \
$(NULL)
# disables the key handling test on gtk2 because gtk2 overrides some key events

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

@ -0,0 +1,67 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=625452
-->
<head>
<title>Test for Bug 625452</title>
<script type="application/javascript" src="/MochiKit/packed.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=625452">Mozilla Bug 625452</a>
<p id="display"></p>
<div id="content">
<input>
</div>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 625452 **/
SimpleTest.waitForExplicitFinish();
addLoadEvent(function() {
var i = document.querySelector("input");
var inputCount = 0;
i.addEventListener("input", function() inputCount++, false);
// test cut
i.focus();
i.value = "foo bar";
i.selectionStart = 0;
i.selectionEnd = 4;
synthesizeKey("X", {accelKey: true});
is(i.value, "bar", "Cut should work correctly");
is(inputCount, 1, "input event should be raised correctly");
// test undo
synthesizeKey("Z", {accelKey: true});
is(i.value, "foo bar", "Undo should work correctly");
is(inputCount, 2, "input event should be raised correctly");
// test redo
synthesizeKey("Z", {accelKey: true, shiftKey: true});
is(i.value, "bar", "Redo should work correctly");
is(inputCount, 3, "input event should be raised correctly");
// test delete
i.selectionStart = 0;
i.selectionEnd = 2;
synthesizeKey("VK_DELETE", {});
is(i.value, "r", "Delete should work correctly");
is(inputCount, 4, "input event should be raised correctly");
// test DeleteSelection(eNone)
i.value = "retest"; // the "r" common prefix is crucial here
is(inputCount, 4, "input event should not have been raised");
// paste is tested in test_bug596001.html
SimpleTest.finish();
});
</script>
</pre>
</body>
</html>