Bug 590554 - maxlength in textarea does not prevent newline characters from being inserted; r,a=roc

--HG--
extra : rebase_source : 7156090a56ed7818db3539dbc8e6f9f7ae86d6e5
This commit is contained in:
Ehsan Akhgari 2010-08-27 01:36:09 -04:00
parent fad8a096c1
commit 8c8c515368
5 changed files with 80 additions and 8 deletions

View File

@ -623,7 +623,12 @@ nsPlaintextEditor::GetTextSelectionOffsets(nsISelection *aSelection,
}
}
#ifdef NS_DEBUG
++nodeCount;
// The post content iterator might return the parent node (which is the
// editor's root node) as the last item. Don't count the root node itself
// as one of its children!
if (!SameCOMIdentity(currentNode, rootNode)) {
++nodeCount;
}
#endif
}
@ -862,6 +867,7 @@ NS_IMETHODIMP nsPlaintextEditor::InsertLineBreak()
shell->MaybeInvalidateCaretPosition();
nsTextRulesInfo ruleInfo(nsTextEditRules::kInsertBreak);
ruleInfo.maxLength = mMaxTextLength;
PRBool cancel, handled;
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
NS_ENSURE_SUCCESS(res, res);

View File

@ -312,7 +312,7 @@ nsTextEditRules::WillDoAction(nsISelection *aSelection,
switch (info->action)
{
case kInsertBreak:
return WillInsertBreak(aSelection, aCancel, aHandled);
return WillInsertBreak(aSelection, aCancel, aHandled, info->maxLength);
case kInsertText:
case kInsertTextIME:
return WillInsertText(info->action,
@ -424,7 +424,10 @@ nsTextEditRules::DidInsert(nsISelection *aSelection, nsresult aResult)
}
nsresult
nsTextEditRules::WillInsertBreak(nsISelection *aSelection, PRBool *aCancel, PRBool *aHandled)
nsTextEditRules::WillInsertBreak(nsISelection *aSelection,
PRBool *aCancel,
PRBool *aHandled,
PRInt32 aMaxLength)
{
if (!aSelection || !aCancel || !aHandled) { return NS_ERROR_NULL_POINTER; }
CANCEL_OPERATION_IF_READONLY_OR_DISABLED
@ -434,11 +437,24 @@ nsTextEditRules::WillInsertBreak(nsISelection *aSelection, PRBool *aCancel, PRBo
}
else
{
// handle docs with a max length
// NOTE, this function copies inString into outString for us.
NS_NAMED_LITERAL_STRING(inString, "\n");
nsAutoString outString;
PRBool didTruncate;
nsresult res = TruncateInsertionIfNeeded(aSelection, &inString, &outString,
aMaxLength, &didTruncate);
NS_ENSURE_SUCCESS(res, res);
if (didTruncate) {
*aCancel = PR_TRUE;
return NS_OK;
}
*aCancel = PR_FALSE;
// if the selection isn't collapsed, delete it.
PRBool bCollapsed;
nsresult res = aSelection->GetIsCollapsed(&bCollapsed);
res = aSelection->GetIsCollapsed(&bCollapsed);
NS_ENSURE_SUCCESS(res, res);
if (!bCollapsed)
{
@ -642,7 +658,7 @@ nsTextEditRules::WillInsertText(PRInt32 aAction,
// handle docs with a max length
// NOTE, this function copies inString into outString for us.
nsresult res = TruncateInsertionIfNeeded(aSelection, inString, outString, aMaxLength);
nsresult res = TruncateInsertionIfNeeded(aSelection, inString, outString, aMaxLength, nsnull);
NS_ENSURE_SUCCESS(res, res);
PRUint32 start = 0;
@ -1256,12 +1272,16 @@ nsresult
nsTextEditRules::TruncateInsertionIfNeeded(nsISelection *aSelection,
const nsAString *aInString,
nsAString *aOutString,
PRInt32 aMaxLength)
PRInt32 aMaxLength,
PRBool *aTruncated)
{
if (!aSelection || !aInString || !aOutString) {return NS_ERROR_NULL_POINTER;}
nsresult res = NS_OK;
*aOutString = *aInString;
if (aTruncated) {
*aTruncated = PR_FALSE;
}
if ((-1 != aMaxLength) && IsPlaintextEditor() && !mEditor->IsIMEComposing() )
{
@ -1294,6 +1314,9 @@ nsTextEditRules::TruncateInsertionIfNeeded(nsISelection *aSelection,
if (resultingDocLength >= aMaxLength)
{
aOutString->Truncate();
if (aTruncated) {
*aTruncated = PR_TRUE;
}
}
else
{
@ -1301,6 +1324,9 @@ nsTextEditRules::TruncateInsertionIfNeeded(nsISelection *aSelection,
if (inCount + resultingDocLength > aMaxLength)
{
aOutString->Truncate(aMaxLength - resultingDocLength);
if (aTruncated) {
*aTruncated = PR_TRUE;
}
}
}
}

View File

@ -160,7 +160,8 @@ protected:
nsresult DidInsertText(nsISelection *aSelection, nsresult aResult);
nsresult GetTopEnclosingPre(nsIDOMNode *aNode, nsIDOMNode** aOutPreNode);
nsresult WillInsertBreak(nsISelection *aSelection, PRBool *aCancel, PRBool *aHandled);
nsresult WillInsertBreak(nsISelection *aSelection, PRBool *aCancel,
PRBool *aHandled, PRInt32 aMaxLength);
nsresult DidInsertBreak(nsISelection *aSelection, nsresult aResult);
nsresult WillInsert(nsISelection *aSelection, PRBool *aCancel);
@ -218,7 +219,8 @@ protected:
nsresult TruncateInsertionIfNeeded(nsISelection *aSelection,
const nsAString *aInString,
nsAString *aOutString,
PRInt32 aMaxLength);
PRInt32 aMaxLength,
PRBool *aTruncated);
/** Remove IME composition text from password buffer */
nsresult RemoveIMETextFromPWBuf(PRUint32 &aStart, nsAString *aIMEString);

View File

@ -47,6 +47,7 @@ include $(topsrcdir)/config/rules.mk
_TEST_FILES = \
test_bug471722.html \
test_bug569988.html \
test_bug590554.html \
$(NULL)
# disables the key handling test on gtk2 because gtk2 overrides some key events

View File

@ -0,0 +1,37 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=590554
-->
<head>
<title>Test for Bug 590554</title>
<script type="application/javascript" src="/MochiKit/packed.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
</head>
<body>
<script type="application/javascript">
/** Test for Bug 590554 **/
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(function() {
var t = document.querySelector("textarea");
t.focus();
synthesizeKey("VK_ENTER", {});
is(t.value, "\n", "Pressing enter should work the first time");
synthesizeKey("VK_ENTER", {});
is(t.value, "\n", "Pressing enter should not work the second time");
SimpleTest.finish();
});
</script>
<textarea maxlength="1"></textarea>
</body>
</html>