Bug 253481 xul:textbox attribute to specify what happens to pasted line breaks

patch by ted.mielczarek@gmail.com r=brade/neil sr=jag
This commit is contained in:
timeless%mozdev.org 2005-12-20 20:12:54 +00:00
parent 1dac917585
commit f675dea5f2
5 changed files with 103 additions and 47 deletions

View File

@ -37,13 +37,9 @@
#include "nsISupports.idl"
%{C++
#include "nsIDOMKeyEvent.h"
%}
interface nsIDOMKeyEvent;
[ptr] native nsIDOMKeyEvent(nsIDOMKeyEvent);
[scriptable, uuid(b5f39ed4-1dd1-11b2-9d00-fd54d6f54962)]
[scriptable, uuid(28dbb4d0-5fea-43f4-aca7-344fc2ecc4f9)]
interface nsIPlaintextEditor : nsISupports
{
@ -72,6 +68,18 @@ interface nsIPlaintextEditor : nsISupports
const long eEditorWidgetMask = 512;
const long eEditorNoCSSMask = 1024;
/*
* The valid values for newlines handling.
* Can't change the values unless we remove
* use of the pref.
*/
const long eNewlinesPasteIntact = 0;
const long eNewlinesPasteToFirst = 1;
const long eNewlinesReplaceWithSpaces = 2;
const long eNewlinesStrip = 3;
const long eNewlinesReplaceWithCommas = 4;
const long eNewlinesStripSurroundingWhitespace = 5;
/**
* The length of the contents in characters.
* XXX change this type to 'unsigned long'
@ -92,11 +100,17 @@ interface nsIPlaintextEditor : nsISupports
*/
attribute long wrapWidth;
/** Get and set newline handling.
*
* Values are the constants defined above.
*/
attribute long newlineHandling;
/**
* EditorKeyPress consumes a keyevent.
* @param aKeyEvent key event to consume
*/
[noscript]void handleKeyPress(in nsIDOMKeyEvent aKeyEvent);
void handleKeyPress(in nsIDOMKeyEvent aKeyEvent);
/**
* Inserts a string at the current location,

View File

@ -94,6 +94,7 @@ nsPlaintextEditor::nsPlaintextEditor()
, mWrapColumn(0)
, mMaxTextLength(-1)
, mInitTriggerCounter(0)
, mNewlineHandling(nsIPlaintextEditor::eNewlinesPasteToFirst)
{
}
@ -136,6 +137,12 @@ NS_IMETHODIMP nsPlaintextEditor::Init(nsIDOMDocument *aDoc,
res = nsEditor::Init(aDoc, aPresShell, aRoot, aSelCon, aFlags);
}
// check the "single line editor newline handling" pref
nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID));
if (prefBranch)
prefBranch->GetIntPref("editor.singleLine.pasteNewlines",
&mNewlineHandling);
if (NS_FAILED(rulesRes)) return rulesRes;
return res;
}
@ -1014,7 +1021,28 @@ nsPlaintextEditor::SetWrapWidth(PRInt32 aWrapColumn)
return rootElement->SetAttribute(styleName, styleValue);
}
//
// Get the newline handling for this editor
//
NS_IMETHODIMP
nsPlaintextEditor::GetNewlineHandling(PRInt32 *aNewlineHandling)
{
NS_ENSURE_ARG_POINTER(aNewlineHandling);
*aNewlineHandling = mNewlineHandling;
return NS_OK;
}
//
// Change the newline handling for this editor
//
NS_IMETHODIMP
nsPlaintextEditor::SetNewlineHandling(PRInt32 aNewlineHandling)
{
mNewlineHandling = aNewlineHandling;
return NS_OK;
}
#ifdef XP_MAC
#pragma mark -

View File

@ -223,6 +223,7 @@ protected:
PRInt32 mWrapColumn;
PRInt32 mMaxTextLength;
PRInt32 mInitTriggerCounter;
PRInt32 mNewlineHandling;
// friends
friend class nsHTMLEditRules;

View File

@ -555,39 +555,28 @@ nsTextEditRules::WillInsertText(PRInt32 aAction,
// People have lots of different ideas about what text fields
// should do with multiline pastes. See bugs 21032, 23485, 23485, 50935.
// The four possible options are:
// The six possible options are:
// 0. paste newlines intact
// 1. paste up to the first newline
// 1. paste up to the first newline (default)
// 2. replace newlines with spaces
// 3. strip newlines
// 4. replace with commas
// 5. strip newlines and surrounding whitespace
// So find out what we're expected to do:
enum {
ePasteIntact = 0, ePasteFirstLine = 1,
eReplaceWithSpaces = 2, eStripNewlines = 3,
eReplaceWithCommas = 4
};
PRInt32 singleLineNewlineBehavior = 1;
nsCOMPtr<nsIPrefBranch> prefBranch =
do_GetService(NS_PREFSERVICE_CONTRACTID, &res);
if (NS_SUCCEEDED(res) && prefBranch)
res = prefBranch->GetIntPref("editor.singleLine.pasteNewlines",
&singleLineNewlineBehavior);
if (nsIPlaintextEditor::eEditorSingleLineMask & mFlags)
{
nsAutoString tString(*outString);
if (singleLineNewlineBehavior == eReplaceWithSpaces)
switch(mEditor->mNewlineHandling)
{
//nsAString destString;
//NormalizeCRLF(outString,destString);
case nsIPlaintextEditor::eNewlinesReplaceWithSpaces:
tString.ReplaceChar(CRLF, ' ');
}
else if (singleLineNewlineBehavior == eStripNewlines)
break;
case nsIPlaintextEditor::eNewlinesStrip:
tString.StripChars(CRLF);
else if (singleLineNewlineBehavior == ePasteFirstLine)
break;
case nsIPlaintextEditor::eNewlinesPasteToFirst:
default:
{
PRInt32 firstCRLF = tString.FindCharInSet(CRLF);
@ -603,13 +592,36 @@ nsTextEditRules::WillInsertText(PRInt32 aAction,
if (offset > 0)
tString.Cut(0, offset);
}
else if (singleLineNewlineBehavior == eReplaceWithCommas)
{
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);
}
else // even if we're pasting newlines, don't paste leading/trailing ones
}
break;
case nsIPlaintextEditor::eNewlinesPasteIntact:
// even if we're pasting newlines, don't paste leading/trailing ones
tString.Trim(CRLF, PR_TRUE, PR_TRUE);
break;
}
outString->Assign(tString);
}
@ -691,7 +703,7 @@ nsTextEditRules::WillInsertText(PRInt32 aAction,
{
if (nsIPlaintextEditor::eEditorSingleLineMask & mFlags)
{
NS_ASSERTION((singleLineNewlineBehavior == ePasteIntact),
NS_ASSERTION((mEditor->mNewlineHandling == nsIPlaintextEditor::eNewlinesPasteIntact),
"Newline improperly getting into single-line edit field!");
res = mEditor->InsertTextImpl(subStr, address_of(curNode), &curOffset, doc);
}

View File

@ -46,6 +46,7 @@
#include "mozISpellI18NManager.h"
#include "mozInlineSpellChecker.h"
#include "nsIDOMKeyEvent.h"
#include "nsIPlaintextEditor.h"
#include "nsIDOMDocument.h"
#include "nsIDOMDocumentRange.h"