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/. */
6 #include "nsInternetCiter.h"
12 #include "nsDependentSubstring.h"
14 #include "nsILineBreaker.h"
15 #include "nsLWBrkCIID.h"
16 #include "nsServiceManagerUtils.h"
18 #include "nsStringIterator.h"
20 const char16_t
gt ('>');
21 const char16_t
space (' ');
22 const char16_t
nl ('\n');
23 const char16_t
cr('\r');
25 /** Mail citations using the Internet style: > This is a citation
29 nsInternetCiter::GetCiteString(const nsAString
& aInString
, nsAString
& aOutString
)
31 aOutString
.Truncate();
34 // Strip trailing new lines which will otherwise turn up
35 // as ugly quoted empty lines.
36 nsReadingIterator
<char16_t
> beginIter
,endIter
;
37 aInString
.BeginReading(beginIter
);
38 aInString
.EndReading(endIter
);
39 while(beginIter
!= endIter
&&
46 // Loop over the string:
47 while (beginIter
!= endIter
)
51 aOutString
.Append(gt
);
52 // No space between >: this is ">>> " style quoting, for
53 // compatibility with RFC 2646 and format=flowed.
55 aOutString
.Append(space
);
71 nsInternetCiter::StripCitesAndLinebreaks(const nsAString
& aInString
,
72 nsAString
& aOutString
,
79 aOutString
.Truncate();
80 nsReadingIterator
<char16_t
> beginIter
,endIter
;
81 aInString
.BeginReading(beginIter
);
82 aInString
.EndReading(endIter
);
83 while (beginIter
!= endIter
) // loop over lines
85 // Clear out cites first, at the beginning of the line:
86 int32_t thisLineCiteLevel
= 0;
87 while (beginIter
!= endIter
&& (*beginIter
== gt
|| nsCRT::IsAsciiSpace(*beginIter
)))
89 if (*beginIter
== gt
) ++thisLineCiteLevel
;
93 // Now copy characters until line end:
94 while (beginIter
!= endIter
&& (*beginIter
!= '\r' && *beginIter
!= '\n'))
96 aOutString
.Append(*beginIter
);
100 aOutString
.Append(char16_t(' '));
102 aOutString
.Append(char16_t('\n')); // DOM linebreaks, not NS_LINEBREAK
103 // Skip over any more consecutive linebreak-like characters:
104 while (beginIter
!= endIter
&& (*beginIter
== '\r' || *beginIter
== '\n'))
107 // Done with this line -- update cite level
108 if (aCiteLevel
&& (thisLineCiteLevel
> *aCiteLevel
))
109 *aCiteLevel
= thisLineCiteLevel
;
115 nsInternetCiter::StripCites(const nsAString
& aInString
, nsAString
& aOutString
)
117 return StripCitesAndLinebreaks(aInString
, aOutString
, false, 0);
120 static void AddCite(nsAString
& aOutString
, int32_t citeLevel
)
122 for (int32_t i
= 0; i
< citeLevel
; ++i
)
123 aOutString
.Append(gt
);
125 aOutString
.Append(space
);
129 BreakLine(nsAString
& aOutString
, uint32_t& outStringCol
,
132 aOutString
.Append(nl
);
135 AddCite(aOutString
, citeLevel
);
136 outStringCol
= citeLevel
+ 1;
142 static inline bool IsSpace(char16_t c
)
144 const char16_t
nbsp (0xa0);
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(char16_t('\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 while (posInString
< length
)
176 // Get the new cite level here since we're at the beginning of a line
177 uint32_t newCiteLevel
= 0;
178 while (posInString
< length
&& tString
[posInString
] == gt
)
182 while (posInString
< length
&& tString
[posInString
] == space
)
185 if (posInString
>= length
)
188 // Special case: if this is a blank line, maintain a blank line
189 // (retain the original paragraph breaks)
190 if (tString
[posInString
] == nl
&& !aOutString
.IsEmpty())
192 if (aOutString
.Last() != nl
)
193 aOutString
.Append(nl
);
194 AddCite(aOutString
, newCiteLevel
);
195 aOutString
.Append(nl
);
202 // If the cite level has changed, then start a new line with the
203 // new cite level (but if we're at the beginning of the string,
205 if (newCiteLevel
!= citeLevel
&& posInString
> newCiteLevel
+1
206 && outStringCol
!= 0)
208 BreakLine(aOutString
, outStringCol
, 0);
210 citeLevel
= newCiteLevel
;
212 // Prepend the quote level to the out string if appropriate
213 if (outStringCol
== 0)
215 AddCite(aOutString
, citeLevel
);
216 outStringCol
= citeLevel
+ (citeLevel
? 1 : 0);
218 // If it's not a cite, and we're not at the beginning of a line in
219 // the output string, add a space to separate new text from the
221 else if (outStringCol
> citeLevel
)
223 aOutString
.Append(space
);
227 // find the next newline -- don't want to go farther than that
228 int32_t nextNewline
= tString
.FindChar(nl
, posInString
);
229 if (nextNewline
< 0) nextNewline
= length
;
231 // For now, don't wrap unquoted lines at all.
232 // This is because the plaintext edit window has already wrapped them
233 // by the time we get them for rewrap, yet when we call the line
234 // breaker, it will refuse to break backwards, and we'll end up
235 // with a line that's too long and gets displayed as a lone word
236 // on a line by itself. Need special logic to detect this case
237 // and break it ourselves without resorting to the line breaker.
240 aOutString
.Append(Substring(tString
, posInString
,
241 nextNewline
-posInString
));
242 outStringCol
+= nextNewline
- posInString
;
243 if (nextNewline
!= (int32_t)length
)
245 aOutString
.Append(nl
);
248 posInString
= nextNewline
+1;
252 // Otherwise we have to use the line breaker and loop
253 // over this line of the input string to get all of it:
254 while ((int32_t)posInString
< nextNewline
)
256 // Skip over initial spaces:
257 while ((int32_t)posInString
< nextNewline
258 && nsCRT::IsAsciiSpace(tString
[posInString
]))
261 // If this is a short line, just append it and continue:
262 if (outStringCol
+ nextNewline
- posInString
<= aWrapCol
-citeLevel
-1)
264 // If this short line is the final one in the in string,
265 // then we need to include the final newline, if any:
266 if (nextNewline
+1 == (int32_t)length
&& tString
[nextNewline
-1] == nl
)
269 // Trim trailing spaces:
270 int32_t lastRealChar
= nextNewline
;
271 while ((uint32_t)lastRealChar
> posInString
272 && nsCRT::IsAsciiSpace(tString
[lastRealChar
-1]))
275 aOutString
+= Substring(tString
,
276 posInString
, lastRealChar
- posInString
);
277 outStringCol
+= lastRealChar
- posInString
;
278 posInString
= nextNewline
+ 1;
282 int32_t eol
= posInString
+ aWrapCol
- citeLevel
- outStringCol
;
283 // eol is the prospective end of line.
284 // We'll first look backwards from there for a place to break.
285 // If it's already less than our current position,
286 // then our line is already too long, so break now.
287 if (eol
<= (int32_t)posInString
)
289 BreakLine(aOutString
, outStringCol
, citeLevel
);
290 continue; // continue inner loop, with outStringCol now at bol
297 breakPt
= lineBreaker
->Prev(tString
.get() + posInString
,
298 length
- posInString
, eol
+ 1 - posInString
);
299 if (breakPt
== NS_LINEBREAKER_NEED_MORE_TEXT
)
301 // if we couldn't find a breakpoint looking backwards,
302 // and we're not starting a new line, then end this line
303 // and loop around again:
304 if (outStringCol
> citeLevel
+ 1)
306 BreakLine(aOutString
, outStringCol
, citeLevel
);
307 continue; // continue inner loop, with outStringCol now at bol
310 // Else try looking forwards:
311 breakPt
= lineBreaker
->Next(tString
.get() + posInString
,
312 length
- posInString
, eol
- posInString
);
313 if (breakPt
== NS_LINEBREAKER_NEED_MORE_TEXT
) rv
= NS_ERROR_BASE
;
318 // If rv is okay, then breakPt is the place to break.
319 // If we get out here and rv is set, something went wrong with line
320 // breaker. Just break the line, hard.
326 // Special case: maybe we should have wrapped last time.
327 // If the first breakpoint here makes the current line too long,
328 // then if we already have text on the current line,
329 // break and loop around again.
330 // If we're at the beginning of the current line, though,
331 // don't force a break since the long word might be a url
332 // and breaking it would make it unclickable on the other end.
334 if (outStringCol
+ breakPt
> aWrapCol
+ SLOP
335 && outStringCol
> citeLevel
+1)
337 BreakLine(aOutString
, outStringCol
, citeLevel
);
341 nsAutoString
sub (Substring(tString
, posInString
, breakPt
));
342 // skip newlines or whitespace at the end of the string
343 int32_t subend
= sub
.Length();
344 while (subend
> 0 && IsSpace(sub
[subend
-1]))
346 sub
.Left(sub
, subend
);
348 outStringCol
+= sub
.Length();
349 // Advance past the whitespace which caused the wrap:
350 posInString
+= breakPt
;
351 while (posInString
< length
&& IsSpace(tString
[posInString
]))
354 // Add a newline and the quote level to the out string
355 if (posInString
< length
) // not for the last line, though
356 BreakLine(aOutString
, outStringCol
, citeLevel
);
358 } // end inner loop within one line of aInString
359 } // end outer loop over lines of aInString