2001-09-26 02:53:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
1999-05-27 03:14:05 +04:00
|
|
|
*
|
2001-09-26 02:53:13 +04:00
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
* compliance with the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/NPL/
|
1999-05-27 03:14:05 +04:00
|
|
|
*
|
2001-09-26 02:53:13 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-05-27 03:14:05 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2001-09-26 02:53:13 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-09-26 02:53:13 +04:00
|
|
|
* Contributor(s):
|
2000-01-11 23:49:15 +03:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
2001-09-26 02:53:13 +04:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-05-27 03:14:05 +04:00
|
|
|
|
|
|
|
|
|
|
|
#include "nsString.h"
|
2001-09-29 12:28:41 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2001-04-07 04:45:26 +04:00
|
|
|
#include "nsInternetCiter.h"
|
2002-05-15 22:55:21 +04:00
|
|
|
#include "nsCRT.h"
|
1999-05-27 03:14:05 +04:00
|
|
|
|
2000-04-21 02:45:54 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
|
|
|
// Line breaker stuff
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsILineBreakerFactory.h"
|
|
|
|
#include "nsLWBrkCIID.h"
|
|
|
|
|
2000-09-14 23:55:40 +04:00
|
|
|
static PRUnichar gt ('>');
|
|
|
|
static PRUnichar space (' ');
|
2002-03-27 01:09:47 +03:00
|
|
|
static PRUnichar nbsp (0xa0);
|
2000-09-14 23:55:40 +04:00
|
|
|
static PRUnichar nl ('\n');
|
|
|
|
static PRUnichar cr('\r');
|
|
|
|
|
2000-08-22 03:54:59 +04:00
|
|
|
/** Mail citations using the Internet style: > This is a citation
|
1999-05-27 03:14:05 +04:00
|
|
|
*/
|
|
|
|
|
2000-04-21 02:45:54 +04:00
|
|
|
static NS_DEFINE_CID(kLWBrkCID, NS_LWBRK_CID);
|
|
|
|
|
1999-05-27 03:14:05 +04:00
|
|
|
nsInternetCiter::nsInternetCiter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsInternetCiter::~nsInternetCiter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-06-27 18:19:40 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(nsInternetCiter, nsICiter)
|
1999-05-27 03:14:05 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-24 01:08:20 +03:00
|
|
|
nsInternetCiter::GetCiteString(const nsAString& aInString, nsAString& aOutString)
|
1999-05-27 03:14:05 +04:00
|
|
|
{
|
2000-04-18 11:52:02 +04:00
|
|
|
aOutString.SetLength(0);
|
2000-09-14 23:55:40 +04:00
|
|
|
PRUnichar uch = nl;
|
1999-05-27 03:14:05 +04:00
|
|
|
|
2000-08-07 10:05:27 +04:00
|
|
|
// Strip trailing new lines which will otherwise turn up
|
|
|
|
// as ugly quoted empty lines.
|
2001-04-18 10:26:19 +04:00
|
|
|
nsReadingIterator <PRUnichar> beginIter,endIter;
|
|
|
|
aInString.BeginReading(beginIter);
|
|
|
|
aInString.EndReading(endIter);
|
|
|
|
while(beginIter!= endIter &&
|
|
|
|
(*endIter == cr ||
|
|
|
|
*endIter == nl))
|
2000-08-07 10:05:27 +04:00
|
|
|
{
|
2001-04-18 10:26:19 +04:00
|
|
|
--endIter;
|
2000-08-07 10:05:27 +04:00
|
|
|
}
|
|
|
|
|
1999-05-27 03:14:05 +04:00
|
|
|
// Loop over the string:
|
2001-04-18 10:26:19 +04:00
|
|
|
while (beginIter != endIter)
|
1999-05-27 03:14:05 +04:00
|
|
|
{
|
2000-09-14 23:55:40 +04:00
|
|
|
if (uch == nl)
|
|
|
|
{
|
|
|
|
aOutString.Append(gt);
|
|
|
|
// No space between >: this is ">>> " style quoting, for
|
|
|
|
// compatability with RFC 2646 and format=flowed.
|
2001-04-18 10:26:19 +04:00
|
|
|
if (*beginIter != gt)
|
2000-09-14 23:55:40 +04:00
|
|
|
aOutString.Append(space);
|
|
|
|
}
|
1999-05-27 03:14:05 +04:00
|
|
|
|
2001-04-18 10:26:19 +04:00
|
|
|
uch = *beginIter;
|
|
|
|
++beginIter;
|
2000-09-14 23:55:40 +04:00
|
|
|
|
1999-05-27 03:14:05 +04:00
|
|
|
aOutString += uch;
|
|
|
|
}
|
|
|
|
|
2000-09-14 23:55:40 +04:00
|
|
|
if (uch != nl)
|
|
|
|
aOutString += nl;
|
1999-05-27 03:14:05 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-04-21 02:45:54 +04:00
|
|
|
nsresult
|
2002-03-24 01:08:20 +03:00
|
|
|
nsInternetCiter::StripCitesAndLinebreaks(const nsAString& aInString,
|
|
|
|
nsAString& aOutString,
|
2000-04-21 02:45:54 +04:00
|
|
|
PRBool aLinebreaksToo,
|
|
|
|
PRInt32* aCiteLevel)
|
2000-04-19 02:39:10 +04:00
|
|
|
{
|
2000-04-21 02:45:54 +04:00
|
|
|
if (aCiteLevel)
|
|
|
|
*aCiteLevel = 0;
|
|
|
|
|
2000-04-19 02:39:10 +04:00
|
|
|
aOutString.SetLength(0);
|
2001-04-18 10:17:05 +04:00
|
|
|
nsReadingIterator <PRUnichar> beginIter,endIter;
|
|
|
|
aInString.BeginReading(beginIter);
|
|
|
|
aInString.EndReading(endIter);
|
|
|
|
while (beginIter!= endIter) // loop over lines
|
2000-04-19 02:39:10 +04:00
|
|
|
{
|
2000-04-21 02:45:54 +04:00
|
|
|
// Clear out cites first, at the beginning of the line:
|
|
|
|
PRInt32 thisLineCiteLevel = 0;
|
2001-04-18 10:17:05 +04:00
|
|
|
while (beginIter!= endIter && (*beginIter == gt || nsCRT::IsAsciiSpace(*beginIter)))
|
2000-04-21 02:45:54 +04:00
|
|
|
{
|
2001-04-18 10:17:05 +04:00
|
|
|
if (*beginIter == gt) ++thisLineCiteLevel;
|
|
|
|
++beginIter;
|
2000-04-21 02:45:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now copy characters until line end:
|
2001-04-18 10:17:05 +04:00
|
|
|
while (beginIter != endIter && (*beginIter != '\r' && *beginIter != '\n'))
|
2000-04-21 02:45:54 +04:00
|
|
|
{
|
2001-04-18 10:17:05 +04:00
|
|
|
aOutString.Append(*beginIter);
|
|
|
|
++beginIter;
|
2000-04-21 02:45:54 +04:00
|
|
|
}
|
2001-04-18 10:17:05 +04:00
|
|
|
if (aLinebreaksToo)
|
|
|
|
aOutString.Append(PRUnichar(' '));
|
|
|
|
else
|
|
|
|
aOutString.Append(PRUnichar('\n')); // DOM linebreaks, not NS_LINEBREAK
|
|
|
|
// Skip over any more consecutive linebreak-like characters:
|
|
|
|
while (beginIter != endIter && (*beginIter == '\r' || *beginIter == '\n'))
|
|
|
|
++beginIter;
|
2000-04-21 02:45:54 +04:00
|
|
|
|
|
|
|
// Done with this line -- update cite level
|
|
|
|
if (aCiteLevel && (thisLineCiteLevel > *aCiteLevel))
|
|
|
|
*aCiteLevel = thisLineCiteLevel;
|
2000-04-19 02:39:10 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-05-27 03:14:05 +04:00
|
|
|
|
2000-04-21 02:45:54 +04:00
|
|
|
NS_IMETHODIMP
|
2002-03-24 01:08:20 +03:00
|
|
|
nsInternetCiter::StripCites(const nsAString& aInString, nsAString& aOutString)
|
2000-04-21 02:45:54 +04:00
|
|
|
{
|
|
|
|
return StripCitesAndLinebreaks(aInString, aOutString, PR_FALSE, 0);
|
|
|
|
}
|
|
|
|
|
2002-03-24 01:08:20 +03:00
|
|
|
static void AddCite(nsAString& aOutString, PRInt32 citeLevel)
|
2000-09-14 23:55:40 +04:00
|
|
|
{
|
|
|
|
for (PRInt32 i = 0; i < citeLevel; ++i)
|
2001-04-07 04:45:26 +04:00
|
|
|
aOutString.Append(PRUnichar(gt));
|
2000-09-14 23:55:40 +04:00
|
|
|
if (citeLevel > 0)
|
2001-04-07 04:45:26 +04:00
|
|
|
aOutString.Append(PRUnichar(space));
|
2000-09-14 23:55:40 +04:00
|
|
|
}
|
|
|
|
|
2002-02-02 00:29:16 +03:00
|
|
|
static inline void
|
2002-03-24 01:08:20 +03:00
|
|
|
BreakLine(nsAString& aOutString, PRUint32& outStringCol,
|
2002-02-02 00:29:16 +03:00
|
|
|
PRUint32 citeLevel)
|
|
|
|
{
|
|
|
|
aOutString.Append(nl);
|
2002-03-27 01:09:47 +03:00
|
|
|
if (citeLevel > 0)
|
|
|
|
{
|
|
|
|
AddCite(aOutString, citeLevel);
|
|
|
|
outStringCol = citeLevel + 1;
|
|
|
|
}
|
|
|
|
else outStringCol = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline PRBool IsSpace(PRUnichar c)
|
|
|
|
{
|
|
|
|
return (nsCRT::IsAsciiSpace(c) || (c == nl) || (c == cr) || (c == nbsp));
|
2002-02-02 00:29:16 +03:00
|
|
|
}
|
|
|
|
|
2000-04-19 02:39:10 +04:00
|
|
|
NS_IMETHODIMP
|
2002-03-24 01:08:20 +03:00
|
|
|
nsInternetCiter::Rewrap(const nsAString& aInString,
|
2000-04-19 02:39:10 +04:00
|
|
|
PRUint32 aWrapCol, PRUint32 aFirstLineOffset,
|
2000-04-21 02:45:54 +04:00
|
|
|
PRBool aRespectNewlines,
|
2002-03-24 01:08:20 +03:00
|
|
|
nsAString& aOutString)
|
2000-04-19 02:39:10 +04:00
|
|
|
{
|
2001-05-09 17:01:48 +04:00
|
|
|
// There shouldn't be returns in this string, only dom newlines.
|
|
|
|
// Check to make sure:
|
|
|
|
#ifdef DEBUG
|
|
|
|
PRInt32 cr = aInString.FindChar(PRUnichar('\r'));
|
|
|
|
NS_ASSERTION((cr < 0), "Rewrap: CR in string gotten from DOM!\n");
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2000-09-14 23:55:40 +04:00
|
|
|
nsCOMPtr<nsILineBreaker> lineBreaker;
|
|
|
|
nsILineBreakerFactory *lf;
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
rv = nsServiceManager::GetService(kLWBrkCID,
|
|
|
|
NS_GET_IID(nsILineBreakerFactory),
|
|
|
|
(nsISupports **)&lf);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
nsAutoString lbarg;
|
|
|
|
rv = lf->GetBreaker(lbarg, getter_AddRefs(lineBreaker));
|
|
|
|
nsServiceManager::ReleaseService(kLWBrkCID, lf);
|
|
|
|
}
|
|
|
|
|
|
|
|
aOutString.SetLength(0);
|
|
|
|
|
|
|
|
// Loop over lines in the input string, rewrapping each one.
|
|
|
|
PRUint32 length = aInString.Length();
|
|
|
|
PRUint32 posInString = 0;
|
|
|
|
PRUint32 outStringCol = 0;
|
|
|
|
PRUint32 citeLevel = 0;
|
2001-04-07 04:45:26 +04:00
|
|
|
const nsPromiseFlatString &tString = PromiseFlatString(aInString);//MJUDGE SCC NEED HELP
|
2002-01-16 01:43:42 +03:00
|
|
|
#ifdef DEBUG_wrapping
|
|
|
|
int loopcount = 0;
|
|
|
|
#endif
|
2000-09-14 23:55:40 +04:00
|
|
|
while (posInString < length)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_wrapping
|
2001-11-15 03:04:26 +03:00
|
|
|
printf("Outer loop: '%s'\n",
|
|
|
|
NS_LossyConvertUCS2toASCII(Substring(tString, posInString,
|
|
|
|
length-posInString)).get());
|
2002-01-16 01:43:42 +03:00
|
|
|
printf("out string is now: '%s'\n",
|
|
|
|
NS_LossyConvertUCS2toASCII(aOutString).get());
|
|
|
|
|
2000-09-14 23:55:40 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Get the new cite level here since we're at the beginning of a line
|
|
|
|
PRUint32 newCiteLevel = 0;
|
2001-04-07 04:45:26 +04:00
|
|
|
while (posInString < length && tString[posInString] == gt)
|
2000-09-14 23:55:40 +04:00
|
|
|
{
|
|
|
|
++newCiteLevel;
|
|
|
|
++posInString;
|
2001-04-07 04:45:26 +04:00
|
|
|
while (posInString < length && tString[posInString] == space)
|
2000-09-14 23:55:40 +04:00
|
|
|
++posInString;
|
|
|
|
}
|
|
|
|
if (posInString >= length)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Special case: if this is a blank line, maintain a blank line
|
|
|
|
// (retain the original paragraph breaks)
|
2003-05-24 01:34:47 +04:00
|
|
|
if (tString[posInString] == nl && !aOutString.IsEmpty())
|
2000-09-14 23:55:40 +04:00
|
|
|
{
|
2002-01-16 01:43:42 +03:00
|
|
|
if (aOutString.Last() != nl)
|
2000-09-14 23:55:40 +04:00
|
|
|
aOutString.Append(nl);
|
|
|
|
AddCite(aOutString, newCiteLevel);
|
|
|
|
aOutString.Append(nl);
|
2000-04-21 02:45:54 +04:00
|
|
|
|
2000-09-14 23:55:40 +04:00
|
|
|
++posInString;
|
|
|
|
outStringCol = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the cite level has changed, then start a new line with the
|
|
|
|
// new cite level (but if we're at the beginning of the string,
|
|
|
|
// don't bother).
|
|
|
|
if (newCiteLevel != citeLevel && posInString > newCiteLevel+1
|
|
|
|
&& outStringCol != 0)
|
|
|
|
{
|
2002-02-02 00:29:16 +03:00
|
|
|
BreakLine(aOutString, outStringCol, 0);
|
2000-09-14 23:55:40 +04:00
|
|
|
}
|
|
|
|
citeLevel = newCiteLevel;
|
|
|
|
|
|
|
|
// Prepend the quote level to the out string if appropriate
|
|
|
|
if (outStringCol == 0)
|
|
|
|
{
|
|
|
|
AddCite(aOutString, citeLevel);
|
2002-02-02 00:29:16 +03:00
|
|
|
outStringCol = citeLevel + (citeLevel ? 1 : 0);
|
2000-09-14 23:55:40 +04:00
|
|
|
}
|
|
|
|
// If it's not a cite, and we're not at the beginning of a line in
|
|
|
|
// the output string, add a space to separate new text from the
|
|
|
|
// previous text.
|
2002-03-27 01:09:47 +03:00
|
|
|
else if (outStringCol > citeLevel)
|
2000-09-14 23:55:40 +04:00
|
|
|
{
|
2002-03-27 01:09:47 +03:00
|
|
|
aOutString.Append(space);
|
2000-09-14 23:55:40 +04:00
|
|
|
++outStringCol;
|
|
|
|
}
|
2000-04-21 02:45:54 +04:00
|
|
|
|
2000-09-14 23:55:40 +04:00
|
|
|
// find the next newline -- don't want to go farther than that
|
2001-04-07 04:45:26 +04:00
|
|
|
PRInt32 nextNewline = tString.FindChar(nl, posInString);
|
2000-09-14 23:55:40 +04:00
|
|
|
if (nextNewline < 0) nextNewline = length;
|
2000-04-21 02:45:54 +04:00
|
|
|
|
2000-09-14 23:55:40 +04:00
|
|
|
// For now, don't wrap unquoted lines at all.
|
|
|
|
// This is because the plaintext edit window has already wrapped them
|
|
|
|
// by the time we get them for rewrap, yet when we call the line
|
|
|
|
// breaker, it will refuse to break backwards, and we'll end up
|
|
|
|
// with a line that's too long and gets displayed as a lone word
|
|
|
|
// on a line by itself. Need special logic to detect this case
|
|
|
|
// and break it ourselves without resorting to the line breaker.
|
|
|
|
if (citeLevel == 0)
|
|
|
|
{
|
2001-05-19 15:27:30 +04:00
|
|
|
aOutString.Append(Substring(tString, posInString,
|
2000-09-14 23:55:40 +04:00
|
|
|
nextNewline-posInString));
|
|
|
|
outStringCol += nextNewline - posInString;
|
2001-02-01 01:55:15 +03:00
|
|
|
if (nextNewline != (PRInt32)length)
|
2000-09-14 23:55:40 +04:00
|
|
|
{
|
|
|
|
aOutString.Append(nl);
|
|
|
|
outStringCol = 0;
|
|
|
|
}
|
|
|
|
posInString = nextNewline+1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we have to use the line breaker and loop
|
|
|
|
// over this line of the input string to get all of it:
|
2001-02-01 01:55:15 +03:00
|
|
|
while ((PRInt32)posInString < nextNewline)
|
2000-09-14 23:55:40 +04:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_wrapping
|
2002-01-16 01:43:42 +03:00
|
|
|
if (++loopcount > 1000)
|
|
|
|
NS_ASSERTION(PR_FALSE, "possible infinite loop in nsInternetCiter\n");
|
|
|
|
|
2001-11-15 03:04:26 +03:00
|
|
|
printf("Inner loop: '%s'\n",
|
|
|
|
NS_LossyConvertUCS2toASCII(Substring(tString, posInString,
|
|
|
|
nextNewline-posInString)).get());
|
2000-09-14 23:55:40 +04:00
|
|
|
#endif
|
2002-02-02 00:29:16 +03:00
|
|
|
|
2001-11-15 03:04:26 +03:00
|
|
|
// Skip over initial spaces:
|
2001-12-07 01:04:40 +03:00
|
|
|
while ((PRInt32)posInString < nextNewline
|
2001-11-15 03:04:26 +03:00
|
|
|
&& nsCRT::IsAsciiSpace(tString[posInString]))
|
|
|
|
++posInString;
|
2000-09-14 23:55:40 +04:00
|
|
|
|
|
|
|
// If this is a short line, just append it and continue:
|
|
|
|
if (outStringCol + nextNewline - posInString <= aWrapCol-citeLevel-1)
|
|
|
|
{
|
|
|
|
// If this short line is the final one in the in string,
|
|
|
|
// then we need to include the final newline, if any:
|
2001-04-07 04:45:26 +04:00
|
|
|
if (nextNewline+1 == (PRInt32)length && tString[nextNewline-1] == nl)
|
2000-09-14 23:55:40 +04:00
|
|
|
++nextNewline;
|
2001-11-15 03:04:26 +03:00
|
|
|
|
|
|
|
// Trim trailing spaces:
|
|
|
|
PRInt32 lastRealChar = nextNewline;
|
2001-12-07 01:04:40 +03:00
|
|
|
while ((PRUint32)lastRealChar > posInString
|
2001-11-15 03:04:26 +03:00
|
|
|
&& nsCRT::IsAsciiSpace(tString[lastRealChar-1]))
|
|
|
|
--lastRealChar;
|
|
|
|
|
2001-05-19 15:27:30 +04:00
|
|
|
aOutString += Substring(tString,
|
2001-11-15 03:04:26 +03:00
|
|
|
posInString, lastRealChar - posInString);
|
|
|
|
outStringCol += lastRealChar - posInString;
|
2000-09-14 23:55:40 +04:00
|
|
|
posInString = nextNewline + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2002-02-02 00:29:16 +03:00
|
|
|
PRInt32 eol = posInString + aWrapCol - citeLevel - outStringCol;
|
2002-03-27 01:09:47 +03:00
|
|
|
// eol is the prospective end of line.
|
2002-02-02 00:29:16 +03:00
|
|
|
// We'll first look backwards from there for a place to break.
|
|
|
|
// If it's already less than our current position,
|
|
|
|
// then our line is already too long, so break now.
|
|
|
|
if (eol <= (PRInt32)posInString)
|
|
|
|
{
|
|
|
|
BreakLine(aOutString, outStringCol, citeLevel);
|
|
|
|
continue; // continue inner loop, with outStringCol now at bol
|
|
|
|
}
|
|
|
|
|
2000-09-14 23:55:40 +04:00
|
|
|
PRUint32 breakPt;
|
|
|
|
PRBool needMore;
|
|
|
|
rv = NS_ERROR_BASE;
|
|
|
|
if (lineBreaker)
|
|
|
|
{
|
2002-02-02 00:29:16 +03:00
|
|
|
rv = lineBreaker->Prev(tString.get() + posInString,
|
|
|
|
length - posInString,
|
2002-03-27 01:09:47 +03:00
|
|
|
eol + 1 - posInString, &breakPt, &needMore);
|
2000-09-14 23:55:40 +04:00
|
|
|
if (NS_FAILED(rv) || needMore)
|
|
|
|
{
|
|
|
|
// if we couldn't find a breakpoint looking backwards,
|
2002-03-27 01:09:47 +03:00
|
|
|
// and we're not starting a new line, then end this line
|
|
|
|
// and loop around again:
|
|
|
|
if (outStringCol > citeLevel + 1)
|
|
|
|
{
|
|
|
|
BreakLine(aOutString, outStringCol, citeLevel);
|
|
|
|
continue; // continue inner loop, with outStringCol now at bol
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else try looking forwards:
|
2001-04-07 04:45:26 +04:00
|
|
|
rv = lineBreaker->Next(tString.get() + posInString,
|
2000-09-14 23:55:40 +04:00
|
|
|
length - posInString,
|
|
|
|
eol - posInString, &breakPt, &needMore);
|
|
|
|
if (needMore) rv = NS_ERROR_BASE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If rv is okay, then breakPt is the place to break.
|
|
|
|
// If we get out here and rv is set, something went wrong with line
|
|
|
|
// breaker. Just break the line, hard.
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_akkana
|
2000-10-29 02:17:53 +04:00
|
|
|
printf("nsInternetCiter: LineBreaker not working -- breaking hard\n");
|
2000-09-14 23:55:40 +04:00
|
|
|
#endif
|
|
|
|
breakPt = eol;
|
|
|
|
}
|
2001-11-15 03:04:26 +03:00
|
|
|
|
|
|
|
// Special case: maybe we should have wrapped last time.
|
|
|
|
// If the first breakpoint here makes the current line too long,
|
2002-01-16 01:43:42 +03:00
|
|
|
// then if we already have text on the current line,
|
2002-08-07 03:54:19 +04:00
|
|
|
// break and loop around again.
|
|
|
|
// If we're at the beginning of the current line, though,
|
|
|
|
// don't force a break since the long word might be a url
|
|
|
|
// and breaking it would make it unclickable on the other end.
|
2002-01-16 01:43:42 +03:00
|
|
|
const int SLOP = 6;
|
2002-08-07 03:54:19 +04:00
|
|
|
if (outStringCol + breakPt > aWrapCol + SLOP
|
|
|
|
&& outStringCol > citeLevel+1)
|
2001-11-15 03:04:26 +03:00
|
|
|
{
|
2002-02-02 00:29:16 +03:00
|
|
|
BreakLine(aOutString, outStringCol, citeLevel);
|
2001-11-15 03:04:26 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2002-03-27 01:09:47 +03:00
|
|
|
nsAutoString sub (Substring(tString, posInString, breakPt));
|
|
|
|
// skip newlines or whitespace at the end of the string
|
|
|
|
PRInt32 subend = sub.Length();
|
|
|
|
while (subend > 0 && IsSpace(sub[subend-1]))
|
|
|
|
--subend;
|
|
|
|
sub.Left(sub, subend);
|
|
|
|
aOutString += sub;
|
|
|
|
outStringCol += sub.Length();
|
|
|
|
// Advance past the whitespace which caused the wrap:
|
|
|
|
posInString += breakPt;
|
|
|
|
while (posInString < length && IsSpace(tString[posInString]))
|
|
|
|
++posInString;
|
2000-09-14 23:55:40 +04:00
|
|
|
|
|
|
|
// Add a newline and the quote level to the out string
|
|
|
|
if (posInString < length) // not for the last line, though
|
2002-02-02 00:29:16 +03:00
|
|
|
BreakLine(aOutString, outStringCol, citeLevel);
|
|
|
|
|
2000-09-14 23:55:40 +04:00
|
|
|
} // end inner loop within one line of aInString
|
|
|
|
#ifdef DEBUG_wrapping
|
2000-10-29 02:17:53 +04:00
|
|
|
printf("---------\nEnd inner loop: out string is now '%s'\n-----------\n",
|
2001-09-29 12:28:41 +04:00
|
|
|
NS_LossyConvertUCS2toASCII(aOutString).get());
|
2000-09-14 23:55:40 +04:00
|
|
|
#endif
|
|
|
|
} // end outer loop over lines of aInString
|
|
|
|
|
2002-10-18 23:25:32 +04:00
|
|
|
#ifdef DEBUG_wrapping
|
|
|
|
printf("Final out string is now: '%s'\n",
|
|
|
|
NS_LossyConvertUCS2toASCII(aOutString).get());
|
|
|
|
|
|
|
|
#endif
|
2000-09-14 23:55:40 +04:00
|
|
|
return NS_OK;
|
2000-04-19 02:39:10 +04:00
|
|
|
}
|
1999-05-27 03:14:05 +04:00
|
|
|
|