1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
41 #include "nsReadableUtils.h"
42 #include "nsInternetCiter.h"
48 #include "nsIServiceManager.h"
49 #include "nsLWBrkCIID.h"
50 #include "nsILineBreaker.h"
52 const PRUnichar
gt ('>');
53 const PRUnichar
space (' ');
54 const PRUnichar
nbsp (0xa0);
55 const PRUnichar
nl ('\n');
56 const PRUnichar
cr('\r');
58 /** Mail citations using the Internet style: > This is a citation
62 nsInternetCiter::nsInternetCiter()
66 nsInternetCiter::~nsInternetCiter()
70 NS_IMPL_ISUPPORTS1(nsInternetCiter
, nsICiter
)
73 nsInternetCiter::GetCiteString(const nsAString
& aInString
, nsAString
& aOutString
)
75 aOutString
.Truncate();
78 // Strip trailing new lines which will otherwise turn up
79 // as ugly quoted empty lines.
80 nsReadingIterator
<PRUnichar
> beginIter
,endIter
;
81 aInString
.BeginReading(beginIter
);
82 aInString
.EndReading(endIter
);
83 while(beginIter
!= endIter
&&
90 // Loop over the string:
91 while (beginIter
!= endIter
)
95 aOutString
.Append(gt
);
96 // No space between >: this is ">>> " style quoting, for
97 // compatability with RFC 2646 and format=flowed.
99 aOutString
.Append(space
);
115 nsInternetCiter::StripCitesAndLinebreaks(const nsAString
& aInString
,
116 nsAString
& aOutString
,
117 PRBool aLinebreaksToo
,
123 aOutString
.Truncate();
124 nsReadingIterator
<PRUnichar
> beginIter
,endIter
;
125 aInString
.BeginReading(beginIter
);
126 aInString
.EndReading(endIter
);
127 while (beginIter
!= endIter
) // loop over lines
129 // Clear out cites first, at the beginning of the line:
130 PRInt32 thisLineCiteLevel
= 0;
131 while (beginIter
!= endIter
&& (*beginIter
== gt
|| nsCRT::IsAsciiSpace(*beginIter
)))
133 if (*beginIter
== gt
) ++thisLineCiteLevel
;
137 // Now copy characters until line end:
138 while (beginIter
!= endIter
&& (*beginIter
!= '\r' && *beginIter
!= '\n'))
140 aOutString
.Append(*beginIter
);
144 aOutString
.Append(PRUnichar(' '));
146 aOutString
.Append(PRUnichar('\n')); // DOM linebreaks, not NS_LINEBREAK
147 // Skip over any more consecutive linebreak-like characters:
148 while (beginIter
!= endIter
&& (*beginIter
== '\r' || *beginIter
== '\n'))
151 // Done with this line -- update cite level
152 if (aCiteLevel
&& (thisLineCiteLevel
> *aCiteLevel
))
153 *aCiteLevel
= thisLineCiteLevel
;
159 nsInternetCiter::StripCites(const nsAString
& aInString
, nsAString
& aOutString
)
161 return StripCitesAndLinebreaks(aInString
, aOutString
, PR_FALSE
, 0);
164 static void AddCite(nsAString
& aOutString
, PRInt32 citeLevel
)
166 for (PRInt32 i
= 0; i
< citeLevel
; ++i
)
167 aOutString
.Append(gt
);
169 aOutString
.Append(space
);
173 BreakLine(nsAString
& aOutString
, PRUint32
& outStringCol
,
176 aOutString
.Append(nl
);
179 AddCite(aOutString
, citeLevel
);
180 outStringCol
= citeLevel
+ 1;
186 static inline PRBool
IsSpace(PRUnichar c
)
188 return (nsCRT::IsAsciiSpace(c
) || (c
== nl
) || (c
== cr
) || (c
== nbsp
));
192 nsInternetCiter::Rewrap(const nsAString
& aInString
,
193 PRUint32 aWrapCol
, PRUint32 aFirstLineOffset
,
194 PRBool aRespectNewlines
,
195 nsAString
& aOutString
)
197 // There shouldn't be returns in this string, only dom newlines.
198 // Check to make sure:
200 PRInt32 cr
= aInString
.FindChar(PRUnichar('\r'));
201 NS_ASSERTION((cr
< 0), "Rewrap: CR in string gotten from DOM!\n");
204 aOutString
.Truncate();
207 nsCOMPtr
<nsILineBreaker
> lineBreaker
= do_GetService(NS_LBRK_CONTRACTID
, &rv
);
208 NS_ENSURE_SUCCESS(rv
, rv
);
210 // Loop over lines in the input string, rewrapping each one.
212 PRUint32 posInString
= 0;
213 PRUint32 outStringCol
= 0;
214 PRUint32 citeLevel
= 0;
215 const nsPromiseFlatString
&tString
= PromiseFlatString(aInString
);
216 length
= tString
.Length();
217 #ifdef DEBUG_wrapping
220 while (posInString
< length
)
222 #ifdef DEBUG_wrapping
223 printf("Outer loop: '%s'\n",
224 NS_LossyConvertUTF16toASCII(Substring(tString
, posInString
,
225 length
-posInString
)).get());
226 printf("out string is now: '%s'\n",
227 NS_LossyConvertUTF16toASCII(aOutString
).get());
231 // Get the new cite level here since we're at the beginning of a line
232 PRUint32 newCiteLevel
= 0;
233 while (posInString
< length
&& tString
[posInString
] == gt
)
237 while (posInString
< length
&& tString
[posInString
] == space
)
240 if (posInString
>= length
)
243 // Special case: if this is a blank line, maintain a blank line
244 // (retain the original paragraph breaks)
245 if (tString
[posInString
] == nl
&& !aOutString
.IsEmpty())
247 if (aOutString
.Last() != nl
)
248 aOutString
.Append(nl
);
249 AddCite(aOutString
, newCiteLevel
);
250 aOutString
.Append(nl
);
257 // If the cite level has changed, then start a new line with the
258 // new cite level (but if we're at the beginning of the string,
260 if (newCiteLevel
!= citeLevel
&& posInString
> newCiteLevel
+1
261 && outStringCol
!= 0)
263 BreakLine(aOutString
, outStringCol
, 0);
265 citeLevel
= newCiteLevel
;
267 // Prepend the quote level to the out string if appropriate
268 if (outStringCol
== 0)
270 AddCite(aOutString
, citeLevel
);
271 outStringCol
= citeLevel
+ (citeLevel
? 1 : 0);
273 // If it's not a cite, and we're not at the beginning of a line in
274 // the output string, add a space to separate new text from the
276 else if (outStringCol
> citeLevel
)
278 aOutString
.Append(space
);
282 // find the next newline -- don't want to go farther than that
283 PRInt32 nextNewline
= tString
.FindChar(nl
, posInString
);
284 if (nextNewline
< 0) nextNewline
= length
;
286 // For now, don't wrap unquoted lines at all.
287 // This is because the plaintext edit window has already wrapped them
288 // by the time we get them for rewrap, yet when we call the line
289 // breaker, it will refuse to break backwards, and we'll end up
290 // with a line that's too long and gets displayed as a lone word
291 // on a line by itself. Need special logic to detect this case
292 // and break it ourselves without resorting to the line breaker.
295 aOutString
.Append(Substring(tString
, posInString
,
296 nextNewline
-posInString
));
297 outStringCol
+= nextNewline
- posInString
;
298 if (nextNewline
!= (PRInt32
)length
)
300 aOutString
.Append(nl
);
303 posInString
= nextNewline
+1;
307 // Otherwise we have to use the line breaker and loop
308 // over this line of the input string to get all of it:
309 while ((PRInt32
)posInString
< nextNewline
)
311 #ifdef DEBUG_wrapping
312 if (++loopcount
> 1000)
313 NS_ASSERTION(PR_FALSE
, "possible infinite loop in nsInternetCiter\n");
315 printf("Inner loop: '%s'\n",
316 NS_LossyConvertUTF16toASCII(Substring(tString
, posInString
,
317 nextNewline
-posInString
)).get());
320 // Skip over initial spaces:
321 while ((PRInt32
)posInString
< nextNewline
322 && nsCRT::IsAsciiSpace(tString
[posInString
]))
325 // If this is a short line, just append it and continue:
326 if (outStringCol
+ nextNewline
- posInString
<= aWrapCol
-citeLevel
-1)
328 // If this short line is the final one in the in string,
329 // then we need to include the final newline, if any:
330 if (nextNewline
+1 == (PRInt32
)length
&& tString
[nextNewline
-1] == nl
)
333 // Trim trailing spaces:
334 PRInt32 lastRealChar
= nextNewline
;
335 while ((PRUint32
)lastRealChar
> posInString
336 && nsCRT::IsAsciiSpace(tString
[lastRealChar
-1]))
339 aOutString
+= Substring(tString
,
340 posInString
, lastRealChar
- posInString
);
341 outStringCol
+= lastRealChar
- posInString
;
342 posInString
= nextNewline
+ 1;
346 PRInt32 eol
= posInString
+ aWrapCol
- citeLevel
- outStringCol
;
347 // eol is the prospective end of line.
348 // We'll first look backwards from there for a place to break.
349 // If it's already less than our current position,
350 // then our line is already too long, so break now.
351 if (eol
<= (PRInt32
)posInString
)
353 BreakLine(aOutString
, outStringCol
, citeLevel
);
354 continue; // continue inner loop, with outStringCol now at bol
361 breakPt
= lineBreaker
->Prev(tString
.get() + posInString
,
362 length
- posInString
, eol
+ 1 - posInString
);
363 if (breakPt
== NS_LINEBREAKER_NEED_MORE_TEXT
)
365 // if we couldn't find a breakpoint looking backwards,
366 // and we're not starting a new line, then end this line
367 // and loop around again:
368 if (outStringCol
> citeLevel
+ 1)
370 BreakLine(aOutString
, outStringCol
, citeLevel
);
371 continue; // continue inner loop, with outStringCol now at bol
374 // Else try looking forwards:
375 breakPt
= lineBreaker
->Next(tString
.get() + posInString
,
376 length
- posInString
, eol
- posInString
);
377 if (breakPt
== NS_LINEBREAKER_NEED_MORE_TEXT
) rv
= NS_ERROR_BASE
;
382 // If rv is okay, then breakPt is the place to break.
383 // If we get out here and rv is set, something went wrong with line
384 // breaker. Just break the line, hard.
388 printf("nsInternetCiter: LineBreaker not working -- breaking hard\n");
393 // Special case: maybe we should have wrapped last time.
394 // If the first breakpoint here makes the current line too long,
395 // then if we already have text on the current line,
396 // break and loop around again.
397 // If we're at the beginning of the current line, though,
398 // don't force a break since the long word might be a url
399 // and breaking it would make it unclickable on the other end.
401 if (outStringCol
+ breakPt
> aWrapCol
+ SLOP
402 && outStringCol
> citeLevel
+1)
404 BreakLine(aOutString
, outStringCol
, citeLevel
);
408 nsAutoString
sub (Substring(tString
, posInString
, breakPt
));
409 // skip newlines or whitespace at the end of the string
410 PRInt32 subend
= sub
.Length();
411 while (subend
> 0 && IsSpace(sub
[subend
-1]))
413 sub
.Left(sub
, subend
);
415 outStringCol
+= sub
.Length();
416 // Advance past the whitespace which caused the wrap:
417 posInString
+= breakPt
;
418 while (posInString
< length
&& IsSpace(tString
[posInString
]))
421 // Add a newline and the quote level to the out string
422 if (posInString
< length
) // not for the last line, though
423 BreakLine(aOutString
, outStringCol
, citeLevel
);
425 } // end inner loop within one line of aInString
426 #ifdef DEBUG_wrapping
427 printf("---------\nEnd inner loop: out string is now '%s'\n-----------\n",
428 NS_LossyConvertUTF16toASCII(aOutString
).get());
430 } // end outer loop over lines of aInString
432 #ifdef DEBUG_wrapping
433 printf("Final out string is now: '%s'\n",
434 NS_LossyConvertUTF16toASCII(aOutString
).get());