1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
11 #include "nsDependentSubstring.h"
13 #include "nsILineBreaker.h"
14 #include "nsInternetCiter.h"
15 #include "nsLWBrkCIID.h"
16 #include "nsServiceManagerUtils.h"
18 #include "nsStringIterator.h"
20 const PRUnichar
gt ('>');
21 const PRUnichar
space (' ');
22 const PRUnichar
nbsp (0xa0);
23 const PRUnichar
nl ('\n');
24 const PRUnichar
cr('\r');
26 /** Mail citations using the Internet style: > This is a citation
30 nsInternetCiter::GetCiteString(const nsAString
& aInString
, nsAString
& aOutString
)
32 aOutString
.Truncate();
35 // Strip trailing new lines which will otherwise turn up
36 // as ugly quoted empty lines.
37 nsReadingIterator
<PRUnichar
> beginIter
,endIter
;
38 aInString
.BeginReading(beginIter
);
39 aInString
.EndReading(endIter
);
40 while(beginIter
!= endIter
&&
47 // Loop over the string:
48 while (beginIter
!= endIter
)
52 aOutString
.Append(gt
);
53 // No space between >: this is ">>> " style quoting, for
54 // compatibility with RFC 2646 and format=flowed.
56 aOutString
.Append(space
);
72 nsInternetCiter::StripCitesAndLinebreaks(const nsAString
& aInString
,
73 nsAString
& aOutString
,
80 aOutString
.Truncate();
81 nsReadingIterator
<PRUnichar
> beginIter
,endIter
;
82 aInString
.BeginReading(beginIter
);
83 aInString
.EndReading(endIter
);
84 while (beginIter
!= endIter
) // loop over lines
86 // Clear out cites first, at the beginning of the line:
87 int32_t thisLineCiteLevel
= 0;
88 while (beginIter
!= endIter
&& (*beginIter
== gt
|| nsCRT::IsAsciiSpace(*beginIter
)))
90 if (*beginIter
== gt
) ++thisLineCiteLevel
;
94 // Now copy characters until line end:
95 while (beginIter
!= endIter
&& (*beginIter
!= '\r' && *beginIter
!= '\n'))
97 aOutString
.Append(*beginIter
);
101 aOutString
.Append(PRUnichar(' '));
103 aOutString
.Append(PRUnichar('\n')); // DOM linebreaks, not NS_LINEBREAK
104 // Skip over any more consecutive linebreak-like characters:
105 while (beginIter
!= endIter
&& (*beginIter
== '\r' || *beginIter
== '\n'))
108 // Done with this line -- update cite level
109 if (aCiteLevel
&& (thisLineCiteLevel
> *aCiteLevel
))
110 *aCiteLevel
= thisLineCiteLevel
;
116 nsInternetCiter::StripCites(const nsAString
& aInString
, nsAString
& aOutString
)
118 return StripCitesAndLinebreaks(aInString
, aOutString
, false, 0);
121 static void AddCite(nsAString
& aOutString
, int32_t citeLevel
)
123 for (int32_t i
= 0; i
< citeLevel
; ++i
)
124 aOutString
.Append(gt
);
126 aOutString
.Append(space
);
130 BreakLine(nsAString
& aOutString
, uint32_t& outStringCol
,
133 aOutString
.Append(nl
);
136 AddCite(aOutString
, citeLevel
);
137 outStringCol
= citeLevel
+ 1;
143 static inline bool IsSpace(PRUnichar c
)
145 return (nsCRT::IsAsciiSpace(c
) || (c
== nl
) || (c
== cr
) || (c
== nbsp
));
149 nsInternetCiter::Rewrap(const nsAString
& aInString
,
150 uint32_t aWrapCol
, uint32_t aFirstLineOffset
,
151 bool aRespectNewlines
,
152 nsAString
& aOutString
)
154 // There shouldn't be returns in this string, only dom newlines.
155 // Check to make sure:
157 int32_t cr
= aInString
.FindChar(PRUnichar('\r'));
158 NS_ASSERTION((cr
< 0), "Rewrap: CR in string gotten from DOM!\n");
161 aOutString
.Truncate();
164 nsCOMPtr
<nsILineBreaker
> lineBreaker
= do_GetService(NS_LBRK_CONTRACTID
, &rv
);
165 NS_ENSURE_SUCCESS(rv
, rv
);
167 // Loop over lines in the input string, rewrapping each one.
169 uint32_t posInString
= 0;
170 uint32_t outStringCol
= 0;
171 uint32_t citeLevel
= 0;
172 const nsPromiseFlatString
&tString
= PromiseFlatString(aInString
);
173 length
= tString
.Length();
174 #ifdef DEBUG_wrapping
177 while (posInString
< length
)
179 #ifdef DEBUG_wrapping
180 printf("Outer loop: '%s'\n",
181 NS_LossyConvertUTF16toASCII(Substring(tString
, posInString
,
182 length
-posInString
)).get());
183 printf("out string is now: '%s'\n",
184 NS_LossyConvertUTF16toASCII(aOutString
).get());
188 // Get the new cite level here since we're at the beginning of a line
189 uint32_t newCiteLevel
= 0;
190 while (posInString
< length
&& tString
[posInString
] == gt
)
194 while (posInString
< length
&& tString
[posInString
] == space
)
197 if (posInString
>= length
)
200 // Special case: if this is a blank line, maintain a blank line
201 // (retain the original paragraph breaks)
202 if (tString
[posInString
] == nl
&& !aOutString
.IsEmpty())
204 if (aOutString
.Last() != nl
)
205 aOutString
.Append(nl
);
206 AddCite(aOutString
, newCiteLevel
);
207 aOutString
.Append(nl
);
214 // If the cite level has changed, then start a new line with the
215 // new cite level (but if we're at the beginning of the string,
217 if (newCiteLevel
!= citeLevel
&& posInString
> newCiteLevel
+1
218 && outStringCol
!= 0)
220 BreakLine(aOutString
, outStringCol
, 0);
222 citeLevel
= newCiteLevel
;
224 // Prepend the quote level to the out string if appropriate
225 if (outStringCol
== 0)
227 AddCite(aOutString
, citeLevel
);
228 outStringCol
= citeLevel
+ (citeLevel
? 1 : 0);
230 // If it's not a cite, and we're not at the beginning of a line in
231 // the output string, add a space to separate new text from the
233 else if (outStringCol
> citeLevel
)
235 aOutString
.Append(space
);
239 // find the next newline -- don't want to go farther than that
240 int32_t nextNewline
= tString
.FindChar(nl
, posInString
);
241 if (nextNewline
< 0) nextNewline
= length
;
243 // For now, don't wrap unquoted lines at all.
244 // This is because the plaintext edit window has already wrapped them
245 // by the time we get them for rewrap, yet when we call the line
246 // breaker, it will refuse to break backwards, and we'll end up
247 // with a line that's too long and gets displayed as a lone word
248 // on a line by itself. Need special logic to detect this case
249 // and break it ourselves without resorting to the line breaker.
252 aOutString
.Append(Substring(tString
, posInString
,
253 nextNewline
-posInString
));
254 outStringCol
+= nextNewline
- posInString
;
255 if (nextNewline
!= (int32_t)length
)
257 aOutString
.Append(nl
);
260 posInString
= nextNewline
+1;
264 // Otherwise we have to use the line breaker and loop
265 // over this line of the input string to get all of it:
266 while ((int32_t)posInString
< nextNewline
)
268 #ifdef DEBUG_wrapping
269 if (++loopcount
> 1000)
270 NS_ASSERTION(false, "possible infinite loop in nsInternetCiter\n");
272 printf("Inner loop: '%s'\n",
273 NS_LossyConvertUTF16toASCII(Substring(tString
, posInString
,
274 nextNewline
-posInString
)).get());
277 // Skip over initial spaces:
278 while ((int32_t)posInString
< nextNewline
279 && nsCRT::IsAsciiSpace(tString
[posInString
]))
282 // If this is a short line, just append it and continue:
283 if (outStringCol
+ nextNewline
- posInString
<= aWrapCol
-citeLevel
-1)
285 // If this short line is the final one in the in string,
286 // then we need to include the final newline, if any:
287 if (nextNewline
+1 == (int32_t)length
&& tString
[nextNewline
-1] == nl
)
290 // Trim trailing spaces:
291 int32_t lastRealChar
= nextNewline
;
292 while ((uint32_t)lastRealChar
> posInString
293 && nsCRT::IsAsciiSpace(tString
[lastRealChar
-1]))
296 aOutString
+= Substring(tString
,
297 posInString
, lastRealChar
- posInString
);
298 outStringCol
+= lastRealChar
- posInString
;
299 posInString
= nextNewline
+ 1;
303 int32_t eol
= posInString
+ aWrapCol
- citeLevel
- outStringCol
;
304 // eol is the prospective end of line.
305 // We'll first look backwards from there for a place to break.
306 // If it's already less than our current position,
307 // then our line is already too long, so break now.
308 if (eol
<= (int32_t)posInString
)
310 BreakLine(aOutString
, outStringCol
, citeLevel
);
311 continue; // continue inner loop, with outStringCol now at bol
318 breakPt
= lineBreaker
->Prev(tString
.get() + posInString
,
319 length
- posInString
, eol
+ 1 - posInString
);
320 if (breakPt
== NS_LINEBREAKER_NEED_MORE_TEXT
)
322 // if we couldn't find a breakpoint looking backwards,
323 // and we're not starting a new line, then end this line
324 // and loop around again:
325 if (outStringCol
> citeLevel
+ 1)
327 BreakLine(aOutString
, outStringCol
, citeLevel
);
328 continue; // continue inner loop, with outStringCol now at bol
331 // Else try looking forwards:
332 breakPt
= lineBreaker
->Next(tString
.get() + posInString
,
333 length
- posInString
, eol
- posInString
);
334 if (breakPt
== NS_LINEBREAKER_NEED_MORE_TEXT
) rv
= NS_ERROR_BASE
;
339 // If rv is okay, then breakPt is the place to break.
340 // If we get out here and rv is set, something went wrong with line
341 // breaker. Just break the line, hard.
345 printf("nsInternetCiter: LineBreaker not working -- breaking hard\n");
350 // Special case: maybe we should have wrapped last time.
351 // If the first breakpoint here makes the current line too long,
352 // then if we already have text on the current line,
353 // break and loop around again.
354 // If we're at the beginning of the current line, though,
355 // don't force a break since the long word might be a url
356 // and breaking it would make it unclickable on the other end.
358 if (outStringCol
+ breakPt
> aWrapCol
+ SLOP
359 && outStringCol
> citeLevel
+1)
361 BreakLine(aOutString
, outStringCol
, citeLevel
);
365 nsAutoString
sub (Substring(tString
, posInString
, breakPt
));
366 // skip newlines or whitespace at the end of the string
367 int32_t subend
= sub
.Length();
368 while (subend
> 0 && IsSpace(sub
[subend
-1]))
370 sub
.Left(sub
, subend
);
372 outStringCol
+= sub
.Length();
373 // Advance past the whitespace which caused the wrap:
374 posInString
+= breakPt
;
375 while (posInString
< length
&& IsSpace(tString
[posInString
]))
378 // Add a newline and the quote level to the out string
379 if (posInString
< length
) // not for the last line, though
380 BreakLine(aOutString
, outStringCol
, citeLevel
);
382 } // end inner loop within one line of aInString
383 #ifdef DEBUG_wrapping
384 printf("---------\nEnd inner loop: out string is now '%s'\n-----------\n",
385 NS_LossyConvertUTF16toASCII(aOutString
).get());
387 } // end outer loop over lines of aInString
389 #ifdef DEBUG_wrapping
390 printf("Final out string is now: '%s'\n",
391 NS_LossyConvertUTF16toASCII(aOutString
).get());