Bug 542912 - Refactor the newline handling behavior in nsTextEditRules; r=bzbarsky sr=roc

--HG--
extra : rebase_source : 92fb4649c72293253317cc5eb6fc0f10d8aa6dfb
This commit is contained in:
Ehsan Akhgari 2010-02-01 13:12:31 -05:00
parent 350ddc321e
commit 1000016308
4 changed files with 133 additions and 68 deletions

View File

@ -161,20 +161,36 @@ NS_IMETHODIMP nsPlaintextEditor::Init(nsIDOMDocument *aDoc,
// check the "single line editor newline handling"
// and "caret behaviour in selection" prefs
GetDefaultEditorPrefs(mNewlineHandling, mCaretStyle);
if (NS_FAILED(rulesRes)) return rulesRes;
return res;
}
// static
void
nsPlaintextEditor::GetDefaultEditorPrefs(PRInt32 &aNewlineHandling,
PRInt32 &aCaretStyle)
{
// set default values
aNewlineHandling = nsIPlaintextEditor::eNewlinesPasteToFirst;
#ifdef XP_WIN
aCaretStyle = 1;
#else
aCaretStyle = 0;
#endif
nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID));
if (prefBranch)
{
prefBranch->GetIntPref("editor.singleLine.pasteNewlines",
&mNewlineHandling);
prefBranch->GetIntPref("layout.selection.caret_style", &mCaretStyle);
&aNewlineHandling);
prefBranch->GetIntPref("layout.selection.caret_style", &aCaretStyle);
#ifdef XP_WIN
if (mCaretStyle == 0)
mCaretStyle = 1;
if (aCaretStyle == 0)
aCaretStyle = 1;
#endif
}
if (NS_FAILED(rulesRes)) return rulesRes;
return res;
}
void

View File

@ -176,6 +176,9 @@ public:
nsresult ExtendSelectionForDelete(nsISelection* aSelection,
nsIEditor::EDirection *aAction);
static void GetDefaultEditorPrefs(PRInt32 &aNewLineHandling,
PRInt32 &aCaretStyle);
protected:
NS_IMETHOD InitRules();

View File

@ -555,6 +555,75 @@ GetTextNode(nsISelection *selection, nsEditor *editor) {
#define ASSERT_PASSWORD_LENGTHS_EQUAL()
#endif
// static
void
nsTextEditRules::HandleNewLines(nsString &aString,
PRInt32 aNewlineHandling)
{
if (aNewlineHandling < 0) {
PRInt32 caretStyle;
nsPlaintextEditor::GetDefaultEditorPrefs(aNewlineHandling, caretStyle);
}
switch(aNewlineHandling)
{
case nsIPlaintextEditor::eNewlinesReplaceWithSpaces:
// Strip trailing newlines first so we don't wind up with trailing spaces
aString.Trim(CRLF, PR_FALSE, PR_TRUE);
aString.ReplaceChar(CRLF, ' ');
break;
case nsIPlaintextEditor::eNewlinesStrip:
aString.StripChars(CRLF);
break;
case nsIPlaintextEditor::eNewlinesPasteToFirst:
default:
{
PRInt32 firstCRLF = aString.FindCharInSet(CRLF);
// we get first *non-empty* line.
PRInt32 offset = 0;
while (firstCRLF == offset)
{
offset++;
firstCRLF = aString.FindCharInSet(CRLF, offset);
}
if (firstCRLF > 0)
aString.Truncate(firstCRLF);
if (offset > 0)
aString.Cut(0, offset);
}
break;
case nsIPlaintextEditor::eNewlinesReplaceWithCommas:
aString.Trim(CRLF, PR_TRUE, PR_TRUE);
aString.ReplaceChar(CRLF, ',');
break;
case nsIPlaintextEditor::eNewlinesStripSurroundingWhitespace:
{
// find each newline, and strip all the whitespace before
// and after it
PRInt32 firstCRLF = aString.FindCharInSet(CRLF);
while (firstCRLF >= 0)
{
PRUint32 wsBegin = firstCRLF, wsEnd = firstCRLF + 1;
// look backwards for the first non-whitespace char
while (wsBegin > 0 && NS_IS_SPACE(aString[wsBegin - 1]))
--wsBegin;
while (wsEnd < aString.Length() && NS_IS_SPACE(aString[wsEnd]))
++wsEnd;
// now cut this range out of the string
aString.Cut(wsBegin, wsEnd - wsBegin);
// look for another CR or LF
firstCRLF = aString.FindCharInSet(CRLF);
}
}
break;
case nsIPlaintextEditor::eNewlinesPasteIntact:
// even if we're pasting newlines, don't paste leading/trailing ones
aString.Trim(CRLF, PR_TRUE, PR_TRUE);
break;
}
}
nsresult
nsTextEditRules::WillInsertText(PRInt32 aAction,
nsISelection *aSelection,
@ -639,63 +708,7 @@ nsTextEditRules::WillInsertText(PRInt32 aAction,
{
nsAutoString tString(*outString);
switch(mEditor->mNewlineHandling)
{
case nsIPlaintextEditor::eNewlinesReplaceWithSpaces:
// Strip trailing newlines first so we don't wind up with trailing spaces
tString.Trim(CRLF, PR_FALSE, PR_TRUE);
tString.ReplaceChar(CRLF, ' ');
break;
case nsIPlaintextEditor::eNewlinesStrip:
tString.StripChars(CRLF);
break;
case nsIPlaintextEditor::eNewlinesPasteToFirst:
default:
{
PRInt32 firstCRLF = tString.FindCharInSet(CRLF);
// we get first *non-empty* line.
PRInt32 offset = 0;
while (firstCRLF == offset)
{
offset++;
firstCRLF = tString.FindCharInSet(CRLF, offset);
}
if (firstCRLF > 0)
tString.Truncate(firstCRLF);
if (offset > 0)
tString.Cut(0, offset);
}
break;
case nsIPlaintextEditor::eNewlinesReplaceWithCommas:
tString.Trim(CRLF, PR_TRUE, PR_TRUE);
tString.ReplaceChar(CRLF, ',');
break;
case nsIPlaintextEditor::eNewlinesStripSurroundingWhitespace:
{
// find each newline, and strip all the whitespace before
// and after it
PRInt32 firstCRLF = tString.FindCharInSet(CRLF);
while (firstCRLF >= 0)
{
PRUint32 wsBegin = firstCRLF, wsEnd = firstCRLF + 1;
// look backwards for the first non-whitespace char
while (wsBegin > 0 && NS_IS_SPACE(tString[wsBegin - 1]))
--wsBegin;
while (wsEnd < tString.Length() && NS_IS_SPACE(tString[wsEnd]))
++wsEnd;
// now cut this range out of the string
tString.Cut(wsBegin, wsEnd - wsBegin);
// look for another CR or LF
firstCRLF = tString.FindCharInSet(CRLF);
}
}
break;
case nsIPlaintextEditor::eNewlinesPasteIntact:
// even if we're pasting newlines, don't paste leading/trailing ones
tString.Trim(CRLF, PR_TRUE, PR_TRUE);
break;
}
HandleNewLines(tString, mEditor->mNewlineHandling);
outString->Assign(tString);
}
@ -1485,6 +1498,7 @@ nsresult nsTextEditRules::HideLastPWInput() {
return NS_OK;
}
// static
nsresult
nsTextEditRules::FillBufWithPWChars(nsAString *aOutString, PRInt32 aLength)
{

View File

@ -113,6 +113,42 @@ public:
public:
nsresult ResetIMETextPWBuf();
/**
* Handles the newline characters either according to aNewLineHandling
* or to the default system prefs if aNewLineHandling is negative.
*
* @param aString the string to be modified in place.
* @param aNewLineHandling determine the desired type of newline handling:
* * negative values:
* handle newlines according to platform defaults.
* * nsIPlaintextEditor::eNewlinesReplaceWithSpaces:
* replace newlines with spaces.
* * nsIPlaintextEditor::eNewlinesStrip:
* remove newlines from the string.
* * nsIPlaintextEditor::eNewlinesReplaceWithCommas:
* replace newlines with commas.
* * nsIPlaintextEditor::eNewlinesStripSurroundingWhitespace:
* collapse newlines and surrounding whitespace characters and
* remove them from the string.
* * nsIPlaintextEditor::eNewlinesPasteIntact:
* only remove the leading and trailing newlines.
* * nsIPlaintextEditor::eNewlinesPasteToFirst or any other value:
* remove the first newline and all characters following it.
*/
static void HandleNewLines(nsString &aString, PRInt32 aNewLineHandling);
/**
* Prepare a string buffer for being displayed as the contents of a password
* field. This function uses the platform-specific character for representing
* characters entered into password fields.
*
* @param aOutString the output string. When this function returns,
* aOutString will contain aLength password characters.
* @param aLength the number of password characters that aOutString should
* contain.
*/
static nsresult FillBufWithPWChars(nsAString *aOutString, PRInt32 aLength);
protected:
// nsTextEditRules implementation methods
@ -185,10 +221,6 @@ protected:
const nsAString *aInString,
nsAString *aOutString,
PRInt32 aMaxLength);
/** Echo's the insertion text into the password buffer, and converts
insertion text to '*'s */
nsresult FillBufWithPWChars(nsAString *aOutString, PRInt32 aLength);
/** Remove IME composition text from password buffer */
nsresult RemoveIMETextFromPWBuf(PRUint32 &aStart, nsAString *aIMEString);