Bug 1247796. Use keyboardFocusIndicatorColor for ActiveBorder system color keyword...
[gecko.git] / editor / libeditor / nsInternetCiter.cpp
blobeb800a89dc00ef42aa76a4d3f3c11ce8c91f6b9a
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"
8 #include "nsAString.h"
9 #include "nsCOMPtr.h"
10 #include "nsCRT.h"
11 #include "nsDebug.h"
12 #include "nsDependentSubstring.h"
13 #include "nsError.h"
14 #include "nsILineBreaker.h"
15 #include "nsLWBrkCIID.h"
16 #include "nsServiceManagerUtils.h"
17 #include "nsString.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
28 nsresult
29 nsInternetCiter::GetCiteString(const nsAString& aInString, nsAString& aOutString)
31 aOutString.Truncate();
32 char16_t uch = nl;
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 &&
40 (*endIter == cr ||
41 *endIter == nl))
43 --endIter;
46 // Loop over the string:
47 while (beginIter != endIter)
49 if (uch == nl)
51 aOutString.Append(gt);
52 // No space between >: this is ">>> " style quoting, for
53 // compatibility with RFC 2646 and format=flowed.
54 if (*beginIter != gt)
55 aOutString.Append(space);
58 uch = *beginIter;
59 ++beginIter;
61 aOutString += uch;
64 if (uch != nl)
65 aOutString += nl;
67 return NS_OK;
70 nsresult
71 nsInternetCiter::StripCitesAndLinebreaks(const nsAString& aInString,
72 nsAString& aOutString,
73 bool aLinebreaksToo,
74 int32_t* aCiteLevel)
76 if (aCiteLevel)
77 *aCiteLevel = 0;
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;
90 ++beginIter;
93 // Now copy characters until line end:
94 while (beginIter != endIter && (*beginIter != '\r' && *beginIter != '\n'))
96 aOutString.Append(*beginIter);
97 ++beginIter;
99 if (aLinebreaksToo)
100 aOutString.Append(char16_t(' '));
101 else
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'))
105 ++beginIter;
107 // Done with this line -- update cite level
108 if (aCiteLevel && (thisLineCiteLevel > *aCiteLevel))
109 *aCiteLevel = thisLineCiteLevel;
111 return NS_OK;
114 nsresult
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);
124 if (citeLevel > 0)
125 aOutString.Append(space);
128 static inline void
129 BreakLine(nsAString& aOutString, uint32_t& outStringCol,
130 uint32_t citeLevel)
132 aOutString.Append(nl);
133 if (citeLevel > 0)
135 AddCite(aOutString, citeLevel);
136 outStringCol = citeLevel + 1;
138 else
139 outStringCol = 0;
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));
148 nsresult
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:
156 #ifdef DEBUG
157 int32_t cr = aInString.FindChar(char16_t('\r'));
158 NS_ASSERTION((cr < 0), "Rewrap: CR in string gotten from DOM!\n");
159 #endif /* DEBUG */
161 aOutString.Truncate();
163 nsresult rv;
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.
168 uint32_t length;
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)
180 ++newCiteLevel;
181 ++posInString;
182 while (posInString < length && tString[posInString] == space)
183 ++posInString;
185 if (posInString >= length)
186 break;
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);
197 ++posInString;
198 outStringCol = 0;
199 continue;
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,
204 // don't bother).
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
220 // previous text.
221 else if (outStringCol > citeLevel)
223 aOutString.Append(space);
224 ++outStringCol;
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.
238 if (citeLevel == 0)
240 aOutString.Append(Substring(tString, posInString,
241 nextNewline-posInString));
242 outStringCol += nextNewline - posInString;
243 if (nextNewline != (int32_t)length)
245 aOutString.Append(nl);
246 outStringCol = 0;
248 posInString = nextNewline+1;
249 continue;
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]))
259 ++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)
267 ++nextNewline;
269 // Trim trailing spaces:
270 int32_t lastRealChar = nextNewline;
271 while ((uint32_t)lastRealChar > posInString
272 && nsCRT::IsAsciiSpace(tString[lastRealChar-1]))
273 --lastRealChar;
275 aOutString += Substring(tString,
276 posInString, lastRealChar - posInString);
277 outStringCol += lastRealChar - posInString;
278 posInString = nextNewline + 1;
279 continue;
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
293 int32_t breakPt = 0;
294 rv = NS_ERROR_BASE;
295 if (lineBreaker)
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;
314 else rv = NS_OK;
316 else rv = NS_OK;
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.
321 if (NS_FAILED(rv))
323 breakPt = eol;
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.
333 const int SLOP = 6;
334 if (outStringCol + breakPt > aWrapCol + SLOP
335 && outStringCol > citeLevel+1)
337 BreakLine(aOutString, outStringCol, citeLevel);
338 continue;
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]))
345 --subend;
346 sub.Left(sub, subend);
347 aOutString += sub;
348 outStringCol += sub.Length();
349 // Advance past the whitespace which caused the wrap:
350 posInString += breakPt;
351 while (posInString < length && IsSpace(tString[posInString]))
352 ++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
361 return NS_OK;