1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /*
8  * nsIContentSerializer implementation that can be used with an
9  * nsIDocumentEncoder to convert an XML DOM to an XML string that
10  * could be parsed into more or less the original DOM.
11  */
12 
13 #include "nsXMLContentSerializer.h"
14 
15 #include "nsGkAtoms.h"
16 #include "nsIDOMProcessingInstruction.h"
17 #include "nsIDOMComment.h"
18 #include "nsIDOMDocumentType.h"
19 #include "nsIContent.h"
20 #include "nsIContentInlines.h"
21 #include "nsIDocument.h"
22 #include "nsIDocumentEncoder.h"
23 #include "nsElementTable.h"
24 #include "nsNameSpaceManager.h"
25 #include "nsTextFragment.h"
26 #include "nsString.h"
27 #include "mozilla/Sprintf.h"
28 #include "nsUnicharUtils.h"
29 #include "nsCRT.h"
30 #include "nsContentUtils.h"
31 #include "nsAttrName.h"
32 #include "mozilla/dom/Element.h"
33 #include "mozilla/intl/LineBreaker.h"
34 #include "nsParserConstants.h"
35 #include "mozilla/Encoding.h"
36 
37 using namespace mozilla;
38 using namespace mozilla::dom;
39 
40 #define kXMLNS "xmlns"
41 
42 // to be readable, we assume that an indented line contains
43 // at least this number of characters (arbitrary value here).
44 // This is a limit for the indentation.
45 #define MIN_INDENTED_LINE_LENGTH 15
46 
47 // the string used to indent.
48 #define INDENT_STRING "  "
49 #define INDENT_STRING_LENGTH 2
50 
NS_NewXMLContentSerializer(nsIContentSerializer ** aSerializer)51 nsresult NS_NewXMLContentSerializer(nsIContentSerializer** aSerializer) {
52   RefPtr<nsXMLContentSerializer> it = new nsXMLContentSerializer();
53   it.forget(aSerializer);
54   return NS_OK;
55 }
56 
nsXMLContentSerializer()57 nsXMLContentSerializer::nsXMLContentSerializer()
58     : mPrefixIndex(0),
59       mColPos(0),
60       mIndentOverflow(0),
61       mIsIndentationAddedOnCurrentLine(false),
62       mInAttribute(false),
63       mAddNewlineForRootNode(false),
64       mAddSpace(false),
65       mMayIgnoreLineBreakSequence(false),
66       mBodyOnly(false),
67       mInBody(0) {}
68 
~nsXMLContentSerializer()69 nsXMLContentSerializer::~nsXMLContentSerializer() {}
70 
NS_IMPL_ISUPPORTS(nsXMLContentSerializer,nsIContentSerializer)71 NS_IMPL_ISUPPORTS(nsXMLContentSerializer, nsIContentSerializer)
72 
73 NS_IMETHODIMP
74 nsXMLContentSerializer::Init(uint32_t aFlags, uint32_t aWrapColumn,
75                              const Encoding* aEncoding, bool aIsCopying,
76                              bool aRewriteEncodingDeclaration,
77                              bool* aNeedsPreformatScanning) {
78   *aNeedsPreformatScanning = false;
79   mPrefixIndex = 0;
80   mColPos = 0;
81   mIndentOverflow = 0;
82   mIsIndentationAddedOnCurrentLine = false;
83   mInAttribute = false;
84   mAddNewlineForRootNode = false;
85   mAddSpace = false;
86   mMayIgnoreLineBreakSequence = false;
87   mBodyOnly = false;
88   mInBody = 0;
89 
90   if (aEncoding) {
91     aEncoding->Name(mCharset);
92   }
93   mFlags = aFlags;
94 
95   // Set the line break character:
96   if ((mFlags & nsIDocumentEncoder::OutputCRLineBreak) &&
97       (mFlags & nsIDocumentEncoder::OutputLFLineBreak)) {  // Windows
98     mLineBreak.AssignLiteral("\r\n");
99   } else if (mFlags & nsIDocumentEncoder::OutputCRLineBreak) {  // Mac
100     mLineBreak.Assign('\r');
101   } else if (mFlags & nsIDocumentEncoder::OutputLFLineBreak) {  // Unix/DOM
102     mLineBreak.Assign('\n');
103   } else {
104     mLineBreak.AssignLiteral(NS_LINEBREAK);  // Platform/default
105   }
106 
107   mDoRaw = !!(mFlags & nsIDocumentEncoder::OutputRaw);
108 
109   mDoFormat = (mFlags & nsIDocumentEncoder::OutputFormatted && !mDoRaw);
110 
111   mDoWrap = (mFlags & nsIDocumentEncoder::OutputWrap && !mDoRaw);
112 
113   mAllowLineBreaking =
114       !(mFlags & nsIDocumentEncoder::OutputDisallowLineBreaking);
115 
116   if (!aWrapColumn) {
117     mMaxColumn = 72;
118   } else {
119     mMaxColumn = aWrapColumn;
120   }
121 
122   mPreLevel = 0;
123   mIsIndentationAddedOnCurrentLine = false;
124   return NS_OK;
125 }
126 
AppendTextData(nsIContent * aNode,int32_t aStartOffset,int32_t aEndOffset,nsAString & aStr,bool aTranslateEntities)127 nsresult nsXMLContentSerializer::AppendTextData(nsIContent* aNode,
128                                                 int32_t aStartOffset,
129                                                 int32_t aEndOffset,
130                                                 nsAString& aStr,
131                                                 bool aTranslateEntities) {
132   nsIContent* content = aNode;
133   const nsTextFragment* frag;
134   if (!content || !(frag = content->GetText())) {
135     return NS_ERROR_FAILURE;
136   }
137 
138   int32_t fragLength = frag->GetLength();
139   int32_t endoffset =
140       (aEndOffset == -1) ? fragLength : std::min(aEndOffset, fragLength);
141   int32_t length = endoffset - aStartOffset;
142 
143   NS_ASSERTION(aStartOffset >= 0, "Negative start offset for text fragment!");
144   NS_ASSERTION(aStartOffset <= endoffset,
145                "A start offset is beyond the end of the text fragment!");
146 
147   if (length <= 0) {
148     // XXX Zero is a legal value, maybe non-zero values should be an
149     // error.
150     return NS_OK;
151   }
152 
153   if (frag->Is2b()) {
154     const char16_t* strStart = frag->Get2b() + aStartOffset;
155     if (aTranslateEntities) {
156       NS_ENSURE_TRUE(AppendAndTranslateEntities(
157                          Substring(strStart, strStart + length), aStr),
158                      NS_ERROR_OUT_OF_MEMORY);
159     } else {
160       NS_ENSURE_TRUE(aStr.Append(Substring(strStart, strStart + length),
161                                  mozilla::fallible),
162                      NS_ERROR_OUT_OF_MEMORY);
163     }
164   } else {
165     if (aTranslateEntities) {
166       NS_ENSURE_TRUE(
167           AppendAndTranslateEntities(
168               NS_ConvertASCIItoUTF16(frag->Get1b() + aStartOffset, length),
169               aStr),
170           NS_ERROR_OUT_OF_MEMORY);
171     } else {
172       NS_ENSURE_TRUE(aStr.Append(NS_ConvertASCIItoUTF16(
173                                      frag->Get1b() + aStartOffset, length),
174                                  mozilla::fallible),
175                      NS_ERROR_OUT_OF_MEMORY);
176     }
177   }
178 
179   return NS_OK;
180 }
181 
182 NS_IMETHODIMP
AppendText(nsIContent * aText,int32_t aStartOffset,int32_t aEndOffset,nsAString & aStr)183 nsXMLContentSerializer::AppendText(nsIContent* aText, int32_t aStartOffset,
184                                    int32_t aEndOffset, nsAString& aStr) {
185   NS_ENSURE_ARG(aText);
186 
187   nsAutoString data;
188   nsresult rv;
189 
190   rv = AppendTextData(aText, aStartOffset, aEndOffset, data, true);
191   if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
192 
193   if (mDoRaw || PreLevel() > 0) {
194     NS_ENSURE_TRUE(AppendToStringConvertLF(data, aStr), NS_ERROR_OUT_OF_MEMORY);
195   } else if (mDoFormat) {
196     NS_ENSURE_TRUE(AppendToStringFormatedWrapped(data, aStr),
197                    NS_ERROR_OUT_OF_MEMORY);
198   } else if (mDoWrap) {
199     NS_ENSURE_TRUE(AppendToStringWrapped(data, aStr), NS_ERROR_OUT_OF_MEMORY);
200   } else {
201     NS_ENSURE_TRUE(AppendToStringConvertLF(data, aStr), NS_ERROR_OUT_OF_MEMORY);
202   }
203 
204   return NS_OK;
205 }
206 
207 NS_IMETHODIMP
AppendCDATASection(nsIContent * aCDATASection,int32_t aStartOffset,int32_t aEndOffset,nsAString & aStr)208 nsXMLContentSerializer::AppendCDATASection(nsIContent* aCDATASection,
209                                            int32_t aStartOffset,
210                                            int32_t aEndOffset,
211                                            nsAString& aStr) {
212   NS_ENSURE_ARG(aCDATASection);
213   nsresult rv;
214 
215   NS_NAMED_LITERAL_STRING(cdata, "<![CDATA[");
216 
217   if (mDoRaw || PreLevel() > 0) {
218     NS_ENSURE_TRUE(AppendToString(cdata, aStr), NS_ERROR_OUT_OF_MEMORY);
219   } else if (mDoFormat) {
220     NS_ENSURE_TRUE(AppendToStringFormatedWrapped(cdata, aStr),
221                    NS_ERROR_OUT_OF_MEMORY);
222   } else if (mDoWrap) {
223     NS_ENSURE_TRUE(AppendToStringWrapped(cdata, aStr), NS_ERROR_OUT_OF_MEMORY);
224   } else {
225     NS_ENSURE_TRUE(AppendToString(cdata, aStr), NS_ERROR_OUT_OF_MEMORY);
226   }
227 
228   nsAutoString data;
229   rv = AppendTextData(aCDATASection, aStartOffset, aEndOffset, data, false);
230   if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
231 
232   NS_ENSURE_TRUE(AppendToStringConvertLF(data, aStr), NS_ERROR_OUT_OF_MEMORY);
233 
234   NS_ENSURE_TRUE(AppendToString(NS_LITERAL_STRING("]]>"), aStr),
235                  NS_ERROR_OUT_OF_MEMORY);
236 
237   return NS_OK;
238 }
239 
240 NS_IMETHODIMP
AppendProcessingInstruction(nsIContent * aPI,int32_t aStartOffset,int32_t aEndOffset,nsAString & aStr)241 nsXMLContentSerializer::AppendProcessingInstruction(nsIContent* aPI,
242                                                     int32_t aStartOffset,
243                                                     int32_t aEndOffset,
244                                                     nsAString& aStr) {
245   nsCOMPtr<nsIDOMProcessingInstruction> pi = do_QueryInterface(aPI);
246   NS_ENSURE_ARG(pi);
247   nsresult rv;
248   nsAutoString target, data, start;
249 
250   NS_ENSURE_TRUE(MaybeAddNewlineForRootNode(aStr), NS_ERROR_OUT_OF_MEMORY);
251 
252   rv = pi->GetTarget(target);
253   if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
254 
255   rv = pi->GetData(data);
256   if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
257 
258   NS_ENSURE_TRUE(start.AppendLiteral("<?", mozilla::fallible),
259                  NS_ERROR_OUT_OF_MEMORY);
260   NS_ENSURE_TRUE(start.Append(target, mozilla::fallible),
261                  NS_ERROR_OUT_OF_MEMORY);
262 
263   if (mDoRaw || PreLevel() > 0) {
264     NS_ENSURE_TRUE(AppendToString(start, aStr), NS_ERROR_OUT_OF_MEMORY);
265   } else if (mDoFormat) {
266     if (mAddSpace) {
267       NS_ENSURE_TRUE(AppendNewLineToString(aStr), NS_ERROR_OUT_OF_MEMORY);
268     }
269     NS_ENSURE_TRUE(AppendToStringFormatedWrapped(start, aStr),
270                    NS_ERROR_OUT_OF_MEMORY);
271   } else if (mDoWrap) {
272     NS_ENSURE_TRUE(AppendToStringWrapped(start, aStr), NS_ERROR_OUT_OF_MEMORY);
273   } else {
274     NS_ENSURE_TRUE(AppendToString(start, aStr), NS_ERROR_OUT_OF_MEMORY);
275   }
276 
277   if (!data.IsEmpty()) {
278     NS_ENSURE_TRUE(AppendToString(char16_t(' '), aStr), NS_ERROR_OUT_OF_MEMORY);
279     NS_ENSURE_TRUE(AppendToStringConvertLF(data, aStr), NS_ERROR_OUT_OF_MEMORY);
280   }
281   NS_ENSURE_TRUE(AppendToString(NS_LITERAL_STRING("?>"), aStr),
282                  NS_ERROR_OUT_OF_MEMORY);
283 
284   MaybeFlagNewlineForRootNode(aPI);
285 
286   return NS_OK;
287 }
288 
289 NS_IMETHODIMP
AppendComment(nsIContent * aComment,int32_t aStartOffset,int32_t aEndOffset,nsAString & aStr)290 nsXMLContentSerializer::AppendComment(nsIContent* aComment,
291                                       int32_t aStartOffset, int32_t aEndOffset,
292                                       nsAString& aStr) {
293   nsCOMPtr<nsIDOMComment> comment = do_QueryInterface(aComment);
294   NS_ENSURE_ARG(comment);
295   nsresult rv;
296   nsAutoString data;
297 
298   rv = comment->GetData(data);
299   if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
300 
301   int32_t dataLength = data.Length();
302   if (aStartOffset || (aEndOffset != -1 && aEndOffset < dataLength)) {
303     int32_t length =
304         (aEndOffset == -1) ? dataLength : std::min(aEndOffset, dataLength);
305     length -= aStartOffset;
306 
307     nsAutoString frag;
308     if (length > 0) {
309       data.Mid(frag, aStartOffset, length);
310     }
311     data.Assign(frag);
312   }
313 
314   NS_ENSURE_TRUE(MaybeAddNewlineForRootNode(aStr), NS_ERROR_OUT_OF_MEMORY);
315 
316   NS_NAMED_LITERAL_STRING(startComment, "<!--");
317 
318   if (mDoRaw || PreLevel() > 0) {
319     NS_ENSURE_TRUE(AppendToString(startComment, aStr), NS_ERROR_OUT_OF_MEMORY);
320   } else if (mDoFormat) {
321     if (mAddSpace) {
322       NS_ENSURE_TRUE(AppendNewLineToString(aStr), NS_ERROR_OUT_OF_MEMORY);
323     }
324     NS_ENSURE_TRUE(AppendToStringFormatedWrapped(startComment, aStr),
325                    NS_ERROR_OUT_OF_MEMORY);
326   } else if (mDoWrap) {
327     NS_ENSURE_TRUE(AppendToStringWrapped(startComment, aStr),
328                    NS_ERROR_OUT_OF_MEMORY);
329   } else {
330     NS_ENSURE_TRUE(AppendToString(startComment, aStr), NS_ERROR_OUT_OF_MEMORY);
331   }
332 
333   // Even if mDoformat, we don't format the content because it
334   // could have been preformated by the author
335   NS_ENSURE_TRUE(AppendToStringConvertLF(data, aStr), NS_ERROR_OUT_OF_MEMORY);
336   NS_ENSURE_TRUE(AppendToString(NS_LITERAL_STRING("-->"), aStr),
337                  NS_ERROR_OUT_OF_MEMORY);
338 
339   MaybeFlagNewlineForRootNode(aComment);
340 
341   return NS_OK;
342 }
343 
344 NS_IMETHODIMP
AppendDoctype(nsIContent * aDocType,nsAString & aStr)345 nsXMLContentSerializer::AppendDoctype(nsIContent* aDocType, nsAString& aStr) {
346   nsCOMPtr<nsIDOMDocumentType> docType = do_QueryInterface(aDocType);
347   NS_ENSURE_ARG(docType);
348   nsresult rv;
349   nsAutoString name, publicId, systemId;
350 
351   rv = docType->GetName(name);
352   if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
353   rv = docType->GetPublicId(publicId);
354   if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
355   rv = docType->GetSystemId(systemId);
356   if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
357 
358   NS_ENSURE_TRUE(MaybeAddNewlineForRootNode(aStr), NS_ERROR_OUT_OF_MEMORY);
359 
360   NS_ENSURE_TRUE(AppendToString(NS_LITERAL_STRING("<!DOCTYPE "), aStr),
361                  NS_ERROR_OUT_OF_MEMORY);
362   NS_ENSURE_TRUE(AppendToString(name, aStr), NS_ERROR_OUT_OF_MEMORY);
363 
364   char16_t quote;
365   if (!publicId.IsEmpty()) {
366     NS_ENSURE_TRUE(AppendToString(NS_LITERAL_STRING(" PUBLIC "), aStr),
367                    NS_ERROR_OUT_OF_MEMORY);
368     if (publicId.FindChar(char16_t('"')) == -1) {
369       quote = char16_t('"');
370     } else {
371       quote = char16_t('\'');
372     }
373     NS_ENSURE_TRUE(AppendToString(quote, aStr), NS_ERROR_OUT_OF_MEMORY);
374     NS_ENSURE_TRUE(AppendToString(publicId, aStr), NS_ERROR_OUT_OF_MEMORY);
375     NS_ENSURE_TRUE(AppendToString(quote, aStr), NS_ERROR_OUT_OF_MEMORY);
376 
377     if (!systemId.IsEmpty()) {
378       NS_ENSURE_TRUE(AppendToString(char16_t(' '), aStr),
379                      NS_ERROR_OUT_OF_MEMORY);
380       if (systemId.FindChar(char16_t('"')) == -1) {
381         quote = char16_t('"');
382       } else {
383         quote = char16_t('\'');
384       }
385       NS_ENSURE_TRUE(AppendToString(quote, aStr), NS_ERROR_OUT_OF_MEMORY);
386       NS_ENSURE_TRUE(AppendToString(systemId, aStr), NS_ERROR_OUT_OF_MEMORY);
387       NS_ENSURE_TRUE(AppendToString(quote, aStr), NS_ERROR_OUT_OF_MEMORY);
388     }
389   } else if (!systemId.IsEmpty()) {
390     if (systemId.FindChar(char16_t('"')) == -1) {
391       quote = char16_t('"');
392     } else {
393       quote = char16_t('\'');
394     }
395     NS_ENSURE_TRUE(AppendToString(NS_LITERAL_STRING(" SYSTEM "), aStr),
396                    NS_ERROR_OUT_OF_MEMORY);
397     NS_ENSURE_TRUE(AppendToString(quote, aStr), NS_ERROR_OUT_OF_MEMORY);
398     NS_ENSURE_TRUE(AppendToString(systemId, aStr), NS_ERROR_OUT_OF_MEMORY);
399     NS_ENSURE_TRUE(AppendToString(quote, aStr), NS_ERROR_OUT_OF_MEMORY);
400   }
401 
402   NS_ENSURE_TRUE(AppendToString(kGreaterThan, aStr), NS_ERROR_OUT_OF_MEMORY);
403   MaybeFlagNewlineForRootNode(aDocType);
404 
405   return NS_OK;
406 }
407 
PushNameSpaceDecl(const nsAString & aPrefix,const nsAString & aURI,nsIContent * aOwner)408 nsresult nsXMLContentSerializer::PushNameSpaceDecl(const nsAString& aPrefix,
409                                                    const nsAString& aURI,
410                                                    nsIContent* aOwner) {
411   NameSpaceDecl* decl = mNameSpaceStack.AppendElement();
412   if (!decl) return NS_ERROR_OUT_OF_MEMORY;
413 
414   decl->mPrefix.Assign(aPrefix);
415   decl->mURI.Assign(aURI);
416   // Don't addref - this weak reference will be removed when
417   // we pop the stack
418   decl->mOwner = aOwner;
419   return NS_OK;
420 }
421 
PopNameSpaceDeclsFor(nsIContent * aOwner)422 void nsXMLContentSerializer::PopNameSpaceDeclsFor(nsIContent* aOwner) {
423   int32_t index, count;
424 
425   count = mNameSpaceStack.Length();
426   for (index = count - 1; index >= 0; index--) {
427     if (mNameSpaceStack[index].mOwner != aOwner) {
428       break;
429     }
430     mNameSpaceStack.RemoveElementAt(index);
431   }
432 }
433 
ConfirmPrefix(nsAString & aPrefix,const nsAString & aURI,nsIContent * aElement,bool aIsAttribute)434 bool nsXMLContentSerializer::ConfirmPrefix(nsAString& aPrefix,
435                                            const nsAString& aURI,
436                                            nsIContent* aElement,
437                                            bool aIsAttribute) {
438   if (aPrefix.EqualsLiteral(kXMLNS)) {
439     return false;
440   }
441 
442   if (aURI.EqualsLiteral("http://www.w3.org/XML/1998/namespace")) {
443     // The prefix must be xml for this namespace. We don't need to declare it,
444     // so always just set the prefix to xml.
445     aPrefix.AssignLiteral("xml");
446 
447     return false;
448   }
449 
450   bool mustHavePrefix;
451   if (aIsAttribute) {
452     if (aURI.IsEmpty()) {
453       // Attribute in the null namespace.  This just shouldn't have a prefix.
454       // And there's no need to push any namespace decls
455       aPrefix.Truncate();
456       return false;
457     }
458 
459     // Attribute not in the null namespace -- must have a prefix
460     mustHavePrefix = true;
461   } else {
462     // Not an attribute, so doesn't _have_ to have a prefix
463     mustHavePrefix = false;
464   }
465 
466   // Keep track of the closest prefix that's bound to aURI and whether we've
467   // found such a thing.  closestURIMatch holds the prefix, and uriMatch
468   // indicates whether we actually have one.
469   nsAutoString closestURIMatch;
470   bool uriMatch = false;
471 
472   // Also keep track of whether we've seen aPrefix already.  If we have, that
473   // means that it's already bound to a URI different from aURI, so even if we
474   // later (so in a more outer scope) see it bound to aURI we can't reuse it.
475   bool haveSeenOurPrefix = false;
476 
477   int32_t count = mNameSpaceStack.Length();
478   int32_t index = count - 1;
479   while (index >= 0) {
480     NameSpaceDecl& decl = mNameSpaceStack.ElementAt(index);
481     // Check if we've found a prefix match
482     if (aPrefix.Equals(decl.mPrefix)) {
483       // If the URIs match and aPrefix is not bound to any other URI, we can
484       // use aPrefix
485       if (!haveSeenOurPrefix && aURI.Equals(decl.mURI)) {
486         // Just use our uriMatch stuff.  That will deal with an empty aPrefix
487         // the right way.  We can break out of the loop now, though.
488         uriMatch = true;
489         closestURIMatch = aPrefix;
490         break;
491       }
492 
493       haveSeenOurPrefix = true;
494 
495       // If they don't, and either:
496       // 1) We have a prefix (so we'd be redeclaring this prefix to point to a
497       //    different namespace) or
498       // 2) We're looking at an existing default namespace decl on aElement (so
499       //    we can't create a new default namespace decl for this URI)
500       // then generate a new prefix.  Note that we do NOT generate new prefixes
501       // if we happen to have aPrefix == decl->mPrefix == "" and mismatching
502       // URIs when |decl| doesn't have aElement as its owner.  In that case we
503       // can simply push the new namespace URI as the default namespace for
504       // aElement.
505       if (!aPrefix.IsEmpty() || decl.mOwner == aElement) {
506         NS_ASSERTION(!aURI.IsEmpty(),
507                      "Not allowed to add a xmlns attribute with an empty "
508                      "namespace name unless it declares the default "
509                      "namespace.");
510 
511         GenerateNewPrefix(aPrefix);
512         // Now we need to validate our new prefix/uri combination; check it
513         // against the full namespace stack again.  Note that just restarting
514         // the while loop is ok, since we haven't changed aURI, so the
515         // closestURIMatch and uriMatch state is not affected.
516         index = count - 1;
517         haveSeenOurPrefix = false;
518         continue;
519       }
520     }
521 
522     // If we've found a URI match, then record the first one
523     if (!uriMatch && aURI.Equals(decl.mURI)) {
524       // Need to check that decl->mPrefix is not declared anywhere closer to
525       // us.  If it is, we can't use it.
526       bool prefixOK = true;
527       int32_t index2;
528       for (index2 = count - 1; index2 > index && prefixOK; --index2) {
529         prefixOK = (mNameSpaceStack[index2].mPrefix != decl.mPrefix);
530       }
531 
532       if (prefixOK) {
533         uriMatch = true;
534         closestURIMatch.Assign(decl.mPrefix);
535       }
536     }
537 
538     --index;
539   }
540 
541   // At this point the following invariants hold:
542   // 1) The prefix in closestURIMatch is mapped to aURI in our scope if
543   //    uriMatch is set.
544   // 2) There is nothing on the namespace stack that has aPrefix as the prefix
545   //    and a _different_ URI, except for the case aPrefix.IsEmpty (and
546   //    possible default namespaces on ancestors)
547 
548   // So if uriMatch is set it's OK to use the closestURIMatch prefix.  The one
549   // exception is when closestURIMatch is actually empty (default namespace
550   // decl) and we must have a prefix.
551   if (uriMatch && (!mustHavePrefix || !closestURIMatch.IsEmpty())) {
552     aPrefix.Assign(closestURIMatch);
553     return false;
554   }
555 
556   if (aPrefix.IsEmpty()) {
557     // At this point, aPrefix is empty (which means we never had a prefix to
558     // start with).  If we must have a prefix, just generate a new prefix and
559     // then send it back through the namespace stack checks to make sure it's
560     // OK.
561     if (mustHavePrefix) {
562       GenerateNewPrefix(aPrefix);
563       return ConfirmPrefix(aPrefix, aURI, aElement, aIsAttribute);
564     }
565 
566     // One final special case.  If aPrefix is empty and we never saw an empty
567     // prefix (default namespace decl) on the namespace stack and we're in the
568     // null namespace there is no reason to output an |xmlns=""| here.  It just
569     // makes the output less readable.
570     if (!haveSeenOurPrefix && aURI.IsEmpty()) {
571       return false;
572     }
573   }
574 
575   // Now just set aURI as the new default namespace URI.  Indicate that we need
576   // to create a namespace decl for the final prefix
577   return true;
578 }
579 
GenerateNewPrefix(nsAString & aPrefix)580 void nsXMLContentSerializer::GenerateNewPrefix(nsAString& aPrefix) {
581   aPrefix.Assign('a');
582   char buf[128];
583   SprintfLiteral(buf, "%d", mPrefixIndex++);
584   AppendASCIItoUTF16(buf, aPrefix);
585 }
586 
SerializeAttr(const nsAString & aPrefix,const nsAString & aName,const nsAString & aValue,nsAString & aStr,bool aDoEscapeEntities)587 bool nsXMLContentSerializer::SerializeAttr(const nsAString& aPrefix,
588                                            const nsAString& aName,
589                                            const nsAString& aValue,
590                                            nsAString& aStr,
591                                            bool aDoEscapeEntities) {
592   nsAutoString attrString_;
593   // For innerHTML we can do faster appending without
594   // temporary strings.
595   bool rawAppend = mDoRaw && aDoEscapeEntities;
596   nsAString& attrString = (rawAppend) ? aStr : attrString_;
597 
598   NS_ENSURE_TRUE(attrString.Append(char16_t(' '), mozilla::fallible), false);
599   if (!aPrefix.IsEmpty()) {
600     NS_ENSURE_TRUE(attrString.Append(aPrefix, mozilla::fallible), false);
601     NS_ENSURE_TRUE(attrString.Append(char16_t(':'), mozilla::fallible), false);
602   }
603   NS_ENSURE_TRUE(attrString.Append(aName, mozilla::fallible), false);
604 
605   if (aDoEscapeEntities) {
606     // if problem characters are turned into character entity references
607     // then there will be no problem with the value delimiter characters
608     NS_ENSURE_TRUE(attrString.AppendLiteral("=\"", mozilla::fallible), false);
609 
610     mInAttribute = true;
611     bool result = AppendAndTranslateEntities(aValue, attrString);
612     mInAttribute = false;
613     NS_ENSURE_TRUE(result, false);
614 
615     NS_ENSURE_TRUE(attrString.Append(char16_t('"'), mozilla::fallible), false);
616     if (rawAppend) {
617       return true;
618     }
619   } else {
620     // Depending on whether the attribute value contains quotes or apostrophes
621     // we need to select the delimiter character and escape characters using
622     // character entity references, ignoring the value of aDoEscapeEntities.
623     // See http://www.w3.org/TR/REC-html40/appendix/notes.html#h-B.3.2.2 for
624     // the standard on character entity references in values.  We also have to
625     // make sure to escape any '&' characters.
626 
627     bool bIncludesSingle = false;
628     bool bIncludesDouble = false;
629     nsAString::const_iterator iCurr, iEnd;
630     aValue.BeginReading(iCurr);
631     aValue.EndReading(iEnd);
632     for (; iCurr != iEnd; ++iCurr) {
633       if (*iCurr == char16_t('\'')) {
634         bIncludesSingle = true;
635         if (bIncludesDouble) {
636           break;
637         }
638       } else if (*iCurr == char16_t('"')) {
639         bIncludesDouble = true;
640         if (bIncludesSingle) {
641           break;
642         }
643       }
644     }
645 
646     // Delimiter and escaping is according to the following table
647     //    bIncludesDouble   bIncludesSingle   Delimiter    Escape Double Quote
648     //    FALSE             FALSE             "            FALSE
649     //    FALSE             TRUE              "            FALSE
650     //    TRUE              FALSE             '            FALSE
651     //    TRUE              TRUE              "            TRUE
652     char16_t cDelimiter =
653         (bIncludesDouble && !bIncludesSingle) ? char16_t('\'') : char16_t('"');
654     NS_ENSURE_TRUE(attrString.Append(char16_t('='), mozilla::fallible), false);
655     NS_ENSURE_TRUE(attrString.Append(cDelimiter, mozilla::fallible), false);
656     nsAutoString sValue(aValue);
657     NS_ENSURE_TRUE(
658         sValue.ReplaceSubstring(NS_LITERAL_STRING("&"),
659                                 NS_LITERAL_STRING("&amp;"), mozilla::fallible),
660         false);
661     if (bIncludesDouble && bIncludesSingle) {
662       NS_ENSURE_TRUE(sValue.ReplaceSubstring(NS_LITERAL_STRING("\""),
663                                              NS_LITERAL_STRING("&quot;"),
664                                              mozilla::fallible),
665                      false);
666     }
667     NS_ENSURE_TRUE(attrString.Append(sValue, mozilla::fallible), false);
668     NS_ENSURE_TRUE(attrString.Append(cDelimiter, mozilla::fallible), false);
669   }
670   if (mDoRaw || PreLevel() > 0) {
671     NS_ENSURE_TRUE(AppendToStringConvertLF(attrString, aStr), false);
672   } else if (mDoFormat) {
673     NS_ENSURE_TRUE(AppendToStringFormatedWrapped(attrString, aStr), false);
674   } else if (mDoWrap) {
675     NS_ENSURE_TRUE(AppendToStringWrapped(attrString, aStr), false);
676   } else {
677     NS_ENSURE_TRUE(AppendToStringConvertLF(attrString, aStr), false);
678   }
679 
680   return true;
681 }
682 
ScanNamespaceDeclarations(Element * aElement,Element * aOriginalElement,const nsAString & aTagNamespaceURI)683 uint32_t nsXMLContentSerializer::ScanNamespaceDeclarations(
684     Element* aElement, Element* aOriginalElement,
685     const nsAString& aTagNamespaceURI) {
686   uint32_t index, count;
687   nsAutoString uriStr, valueStr;
688 
689   count = aElement->GetAttrCount();
690 
691   // First scan for namespace declarations, pushing each on the stack
692   uint32_t skipAttr = count;
693   for (index = 0; index < count; index++) {
694     const BorrowedAttrInfo info = aElement->GetAttrInfoAt(index);
695     const nsAttrName* name = info.mName;
696 
697     int32_t namespaceID = name->NamespaceID();
698     nsAtom* attrName = name->LocalName();
699 
700     if (namespaceID == kNameSpaceID_XMLNS ||
701         // Also push on the stack attrs named "xmlns" in the null
702         // namespace... because once we serialize those out they'll look like
703         // namespace decls.  :(
704         // XXXbz what if we have both "xmlns" in the null namespace and "xmlns"
705         // in the xmlns namespace?
706         (namespaceID == kNameSpaceID_None && attrName == nsGkAtoms::xmlns)) {
707       info.mValue->ToString(uriStr);
708 
709       if (!name->GetPrefix()) {
710         if (aTagNamespaceURI.IsEmpty() && !uriStr.IsEmpty()) {
711           // If the element is in no namespace we need to add a xmlns
712           // attribute to declare that. That xmlns attribute must not have a
713           // prefix (see http://www.w3.org/TR/REC-xml-names/#dt-prefix), ie it
714           // must declare the default namespace. We just found an xmlns
715           // attribute that declares the default namespace to something
716           // non-empty. We're going to ignore this attribute, for children we
717           // will detect that we need to add it again and attributes aren't
718           // affected by the default namespace.
719           skipAttr = index;
720         } else {
721           // Default NS attribute does not have prefix (and the name is "xmlns")
722           PushNameSpaceDecl(EmptyString(), uriStr, aOriginalElement);
723         }
724       } else {
725         PushNameSpaceDecl(nsDependentAtomString(attrName), uriStr,
726                           aOriginalElement);
727       }
728     }
729   }
730   return skipAttr;
731 }
732 
IsJavaScript(nsIContent * aContent,nsAtom * aAttrNameAtom,int32_t aAttrNamespaceID,const nsAString & aValueString)733 bool nsXMLContentSerializer::IsJavaScript(nsIContent* aContent,
734                                           nsAtom* aAttrNameAtom,
735                                           int32_t aAttrNamespaceID,
736                                           const nsAString& aValueString) {
737   bool isHtml = aContent->IsHTMLElement();
738   bool isXul = aContent->IsXULElement();
739   bool isSvg = aContent->IsSVGElement();
740 
741   if (aAttrNamespaceID == kNameSpaceID_None && (isHtml || isXul || isSvg) &&
742       (aAttrNameAtom == nsGkAtoms::href || aAttrNameAtom == nsGkAtoms::src)) {
743     static const char kJavaScript[] = "javascript";
744     int32_t pos = aValueString.FindChar(':');
745     if (pos < (int32_t)(sizeof kJavaScript - 1)) return false;
746     nsAutoString scheme(Substring(aValueString, 0, pos));
747     scheme.StripWhitespace();
748     if ((scheme.Length() == (sizeof kJavaScript - 1)) &&
749         scheme.EqualsIgnoreCase(kJavaScript))
750       return true;
751     else
752       return false;
753   }
754 
755   return aContent->IsEventAttributeName(aAttrNameAtom);
756 }
757 
SerializeAttributes(Element * aElement,Element * aOriginalElement,nsAString & aTagPrefix,const nsAString & aTagNamespaceURI,nsAtom * aTagName,nsAString & aStr,uint32_t aSkipAttr,bool aAddNSAttr)758 bool nsXMLContentSerializer::SerializeAttributes(
759     Element* aElement, Element* aOriginalElement, nsAString& aTagPrefix,
760     const nsAString& aTagNamespaceURI, nsAtom* aTagName, nsAString& aStr,
761     uint32_t aSkipAttr, bool aAddNSAttr) {
762   nsAutoString prefixStr, uriStr, valueStr;
763   nsAutoString xmlnsStr;
764   xmlnsStr.AssignLiteral(kXMLNS);
765   uint32_t index, count;
766 
767   // If we had to add a new namespace declaration, serialize
768   // and push it on the namespace stack
769   if (aAddNSAttr) {
770     if (aTagPrefix.IsEmpty()) {
771       // Serialize default namespace decl
772       NS_ENSURE_TRUE(
773           SerializeAttr(EmptyString(), xmlnsStr, aTagNamespaceURI, aStr, true),
774           false);
775     } else {
776       // Serialize namespace decl
777       NS_ENSURE_TRUE(
778           SerializeAttr(xmlnsStr, aTagPrefix, aTagNamespaceURI, aStr, true),
779           false);
780     }
781     PushNameSpaceDecl(aTagPrefix, aTagNamespaceURI, aOriginalElement);
782   }
783 
784   count = aElement->GetAttrCount();
785 
786   // Now serialize each of the attributes
787   // XXX Unfortunately we need a namespace manager to get
788   // attribute URIs.
789   for (index = 0; index < count; index++) {
790     if (aSkipAttr == index) {
791       continue;
792     }
793 
794     const nsAttrName* name = aElement->GetAttrNameAt(index);
795     int32_t namespaceID = name->NamespaceID();
796     nsAtom* attrName = name->LocalName();
797     nsAtom* attrPrefix = name->GetPrefix();
798 
799     // Filter out any attribute starting with [-|_]moz
800     nsDependentAtomString attrNameStr(attrName);
801     if (StringBeginsWith(attrNameStr, NS_LITERAL_STRING("_moz")) ||
802         StringBeginsWith(attrNameStr, NS_LITERAL_STRING("-moz"))) {
803       continue;
804     }
805 
806     if (attrPrefix) {
807       attrPrefix->ToString(prefixStr);
808     } else {
809       prefixStr.Truncate();
810     }
811 
812     bool addNSAttr = false;
813     if (kNameSpaceID_XMLNS != namespaceID) {
814       nsContentUtils::NameSpaceManager()->GetNameSpaceURI(namespaceID, uriStr);
815       addNSAttr = ConfirmPrefix(prefixStr, uriStr, aOriginalElement, true);
816     }
817 
818     aElement->GetAttr(namespaceID, attrName, valueStr);
819 
820     nsDependentAtomString nameStr(attrName);
821     bool isJS = IsJavaScript(aElement, attrName, namespaceID, valueStr);
822 
823     NS_ENSURE_TRUE(SerializeAttr(prefixStr, nameStr, valueStr, aStr, !isJS),
824                    false);
825 
826     if (addNSAttr) {
827       NS_ASSERTION(!prefixStr.IsEmpty(),
828                    "Namespaced attributes must have a prefix");
829       NS_ENSURE_TRUE(SerializeAttr(xmlnsStr, prefixStr, uriStr, aStr, true),
830                      false);
831       PushNameSpaceDecl(prefixStr, uriStr, aOriginalElement);
832     }
833   }
834 
835   return true;
836 }
837 
838 NS_IMETHODIMP
AppendElementStart(Element * aElement,Element * aOriginalElement,nsAString & aStr)839 nsXMLContentSerializer::AppendElementStart(Element* aElement,
840                                            Element* aOriginalElement,
841                                            nsAString& aStr) {
842   NS_ENSURE_ARG(aElement);
843 
844   bool forceFormat = false;
845   nsresult rv = NS_OK;
846   if (!CheckElementStart(aElement, forceFormat, aStr, rv)) {
847     // When we go to AppendElementEnd for this element, we're going to
848     // MaybeLeaveFromPreContent().  So make sure to MaybeEnterInPreContent()
849     // now, so our PreLevel() doesn't get confused.
850     MaybeEnterInPreContent(aElement);
851     return rv;
852   }
853 
854   NS_ENSURE_SUCCESS(rv, rv);
855 
856   nsAutoString tagPrefix, tagLocalName, tagNamespaceURI;
857   aElement->NodeInfo()->GetPrefix(tagPrefix);
858   aElement->NodeInfo()->GetName(tagLocalName);
859   aElement->NodeInfo()->GetNamespaceURI(tagNamespaceURI);
860 
861   uint32_t skipAttr =
862       ScanNamespaceDeclarations(aElement, aOriginalElement, tagNamespaceURI);
863 
864   nsAtom* name = aElement->NodeInfo()->NameAtom();
865   bool lineBreakBeforeOpen =
866       LineBreakBeforeOpen(aElement->GetNameSpaceID(), name);
867 
868   if ((mDoFormat || forceFormat) && !mDoRaw && !PreLevel()) {
869     if (mColPos && lineBreakBeforeOpen) {
870       NS_ENSURE_TRUE(AppendNewLineToString(aStr), NS_ERROR_OUT_OF_MEMORY);
871     } else {
872       NS_ENSURE_TRUE(MaybeAddNewlineForRootNode(aStr), NS_ERROR_OUT_OF_MEMORY);
873     }
874     if (!mColPos) {
875       NS_ENSURE_TRUE(AppendIndentation(aStr), NS_ERROR_OUT_OF_MEMORY);
876     } else if (mAddSpace) {
877       NS_ENSURE_TRUE(AppendToString(char16_t(' '), aStr),
878                      NS_ERROR_OUT_OF_MEMORY);
879       mAddSpace = false;
880     }
881   } else if (mAddSpace) {
882     NS_ENSURE_TRUE(AppendToString(char16_t(' '), aStr), NS_ERROR_OUT_OF_MEMORY);
883     mAddSpace = false;
884   } else {
885     NS_ENSURE_TRUE(MaybeAddNewlineForRootNode(aStr), NS_ERROR_OUT_OF_MEMORY);
886   }
887 
888   // Always reset to avoid false newlines in case MaybeAddNewlineForRootNode
889   // wasn't called
890   mAddNewlineForRootNode = false;
891 
892   bool addNSAttr;
893   addNSAttr =
894       ConfirmPrefix(tagPrefix, tagNamespaceURI, aOriginalElement, false);
895 
896   // Serialize the qualified name of the element
897   NS_ENSURE_TRUE(AppendToString(kLessThan, aStr), NS_ERROR_OUT_OF_MEMORY);
898   if (!tagPrefix.IsEmpty()) {
899     NS_ENSURE_TRUE(AppendToString(tagPrefix, aStr), NS_ERROR_OUT_OF_MEMORY);
900     NS_ENSURE_TRUE(AppendToString(NS_LITERAL_STRING(":"), aStr),
901                    NS_ERROR_OUT_OF_MEMORY);
902   }
903   NS_ENSURE_TRUE(AppendToString(tagLocalName, aStr), NS_ERROR_OUT_OF_MEMORY);
904 
905   MaybeEnterInPreContent(aElement);
906 
907   if ((mDoFormat || forceFormat) && !mDoRaw && !PreLevel()) {
908     NS_ENSURE_TRUE(IncrIndentation(name), NS_ERROR_OUT_OF_MEMORY);
909   }
910 
911   NS_ENSURE_TRUE(
912       SerializeAttributes(aElement, aOriginalElement, tagPrefix,
913                           tagNamespaceURI, name, aStr, skipAttr, addNSAttr),
914       NS_ERROR_OUT_OF_MEMORY);
915 
916   NS_ENSURE_TRUE(AppendEndOfElementStart(aElement, aOriginalElement, aStr),
917                  NS_ERROR_OUT_OF_MEMORY);
918 
919   if ((mDoFormat || forceFormat) && !mDoRaw && !PreLevel() &&
920       LineBreakAfterOpen(aElement->GetNameSpaceID(), name)) {
921     NS_ENSURE_TRUE(AppendNewLineToString(aStr), NS_ERROR_OUT_OF_MEMORY);
922   }
923 
924   NS_ENSURE_TRUE(AfterElementStart(aElement, aOriginalElement, aStr),
925                  NS_ERROR_OUT_OF_MEMORY);
926 
927   return NS_OK;
928 }
929 
930 // aElement is the actual element we're outputting.  aOriginalElement is the one
931 // in the original DOM, which is the one we have to test for kids.
ElementNeedsSeparateEndTag(Element * aElement,Element * aOriginalElement)932 static bool ElementNeedsSeparateEndTag(Element* aElement,
933                                        Element* aOriginalElement) {
934   if (aOriginalElement->GetChildCount()) {
935     // We have kids, so we need a separate end tag.  This needs to be checked on
936     // aOriginalElement because that's the one that's actually in the DOM and
937     // might have kids.
938     return true;
939   }
940 
941   if (!aElement->IsHTMLElement()) {
942     // Empty non-HTML elements can just skip a separate end tag.
943     return false;
944   }
945 
946   // HTML container tags should have a separate end tag even if empty, per spec.
947   // See
948   // https://w3c.github.io/DOM-Parsing/#dfn-concept-xml-serialization-algorithm
949   nsAtom* localName = aElement->NodeInfo()->NameAtom();
950   bool isHTMLContainer = nsHTMLElement::IsContainer(
951       nsHTMLTags::CaseSensitiveAtomTagToId(localName));
952   return isHTMLContainer;
953 }
954 
AppendEndOfElementStart(Element * aElement,Element * aOriginalElement,nsAString & aStr)955 bool nsXMLContentSerializer::AppendEndOfElementStart(Element* aElement,
956                                                      Element* aOriginalElement,
957                                                      nsAString& aStr) {
958   if (ElementNeedsSeparateEndTag(aElement, aOriginalElement)) {
959     return AppendToString(kGreaterThan, aStr);
960   }
961 
962   // We don't need a separate end tag.  For HTML elements (which at this point
963   // must be non-containers), append a space before the '/', per spec.  See
964   // https://w3c.github.io/DOM-Parsing/#dfn-concept-xml-serialization-algorithm
965   if (aOriginalElement->IsHTMLElement()) {
966     if (!AppendToString(kSpace, aStr)) {
967       return false;
968     }
969   }
970 
971   return AppendToString(NS_LITERAL_STRING("/>"), aStr);
972 }
973 
974 NS_IMETHODIMP
AppendElementEnd(Element * aElement,nsAString & aStr)975 nsXMLContentSerializer::AppendElementEnd(Element* aElement, nsAString& aStr) {
976   NS_ENSURE_ARG(aElement);
977 
978   nsIContent* content = aElement;
979 
980   bool forceFormat = false, outputElementEnd;
981   outputElementEnd = CheckElementEnd(aElement, forceFormat, aStr);
982 
983   nsAtom* name = content->NodeInfo()->NameAtom();
984 
985   if ((mDoFormat || forceFormat) && !mDoRaw && !PreLevel()) {
986     DecrIndentation(name);
987   }
988 
989   if (!outputElementEnd) {
990     // Keep this in sync with the cleanup at the end of this method.
991     PopNameSpaceDeclsFor(aElement);
992     MaybeLeaveFromPreContent(content);
993     MaybeFlagNewlineForRootNode(aElement);
994     AfterElementEnd(content, aStr);
995     return NS_OK;
996   }
997 
998   nsAutoString tagPrefix, tagLocalName, tagNamespaceURI;
999 
1000   aElement->NodeInfo()->GetPrefix(tagPrefix);
1001   aElement->NodeInfo()->GetName(tagLocalName);
1002   aElement->NodeInfo()->GetNamespaceURI(tagNamespaceURI);
1003 
1004 #ifdef DEBUG
1005   bool debugNeedToPushNamespace =
1006 #endif
1007       ConfirmPrefix(tagPrefix, tagNamespaceURI, aElement, false);
1008   NS_ASSERTION(!debugNeedToPushNamespace,
1009                "Can't push namespaces in closing tag!");
1010 
1011   if ((mDoFormat || forceFormat) && !mDoRaw && !PreLevel()) {
1012     bool lineBreakBeforeClose =
1013         LineBreakBeforeClose(content->GetNameSpaceID(), name);
1014 
1015     if (mColPos && lineBreakBeforeClose) {
1016       NS_ENSURE_TRUE(AppendNewLineToString(aStr), NS_ERROR_OUT_OF_MEMORY);
1017     }
1018     if (!mColPos) {
1019       NS_ENSURE_TRUE(AppendIndentation(aStr), NS_ERROR_OUT_OF_MEMORY);
1020     } else if (mAddSpace) {
1021       NS_ENSURE_TRUE(AppendToString(char16_t(' '), aStr),
1022                      NS_ERROR_OUT_OF_MEMORY);
1023       mAddSpace = false;
1024     }
1025   } else if (mAddSpace) {
1026     NS_ENSURE_TRUE(AppendToString(char16_t(' '), aStr), NS_ERROR_OUT_OF_MEMORY);
1027     mAddSpace = false;
1028   }
1029 
1030   NS_ENSURE_TRUE(AppendToString(kEndTag, aStr), NS_ERROR_OUT_OF_MEMORY);
1031   if (!tagPrefix.IsEmpty()) {
1032     NS_ENSURE_TRUE(AppendToString(tagPrefix, aStr), NS_ERROR_OUT_OF_MEMORY);
1033     NS_ENSURE_TRUE(AppendToString(NS_LITERAL_STRING(":"), aStr),
1034                    NS_ERROR_OUT_OF_MEMORY);
1035   }
1036   NS_ENSURE_TRUE(AppendToString(tagLocalName, aStr), NS_ERROR_OUT_OF_MEMORY);
1037   NS_ENSURE_TRUE(AppendToString(kGreaterThan, aStr), NS_ERROR_OUT_OF_MEMORY);
1038 
1039   // Keep what follows in sync with the cleanup in the !outputElementEnd case.
1040   PopNameSpaceDeclsFor(aElement);
1041 
1042   MaybeLeaveFromPreContent(content);
1043 
1044   if ((mDoFormat || forceFormat) && !mDoRaw && !PreLevel() &&
1045       LineBreakAfterClose(content->GetNameSpaceID(), name)) {
1046     NS_ENSURE_TRUE(AppendNewLineToString(aStr), NS_ERROR_OUT_OF_MEMORY);
1047   } else {
1048     MaybeFlagNewlineForRootNode(aElement);
1049   }
1050 
1051   AfterElementEnd(content, aStr);
1052 
1053   return NS_OK;
1054 }
1055 
1056 NS_IMETHODIMP
AppendDocumentStart(nsIDocument * aDocument,nsAString & aStr)1057 nsXMLContentSerializer::AppendDocumentStart(nsIDocument* aDocument,
1058                                             nsAString& aStr) {
1059   NS_ENSURE_ARG_POINTER(aDocument);
1060 
1061   nsAutoString version, encoding, standalone;
1062   aDocument->GetXMLDeclaration(version, encoding, standalone);
1063 
1064   if (version.IsEmpty())
1065     return NS_OK;  // A declaration must have version, or there is no decl
1066 
1067   NS_NAMED_LITERAL_STRING(endQuote, "\"");
1068 
1069   aStr += NS_LITERAL_STRING("<?xml version=\"") + version + endQuote;
1070 
1071   if (!mCharset.IsEmpty()) {
1072     aStr += NS_LITERAL_STRING(" encoding=\"") +
1073             NS_ConvertASCIItoUTF16(mCharset) + endQuote;
1074   }
1075     // Otherwise just don't output an encoding attr.  Not that we expect
1076     // mCharset to ever be empty.
1077 #ifdef DEBUG
1078   else {
1079     NS_WARNING("Empty mCharset?  How come?");
1080   }
1081 #endif
1082 
1083   if (!standalone.IsEmpty()) {
1084     aStr += NS_LITERAL_STRING(" standalone=\"") + standalone + endQuote;
1085   }
1086 
1087   NS_ENSURE_TRUE(aStr.AppendLiteral("?>", mozilla::fallible),
1088                  NS_ERROR_OUT_OF_MEMORY);
1089   mAddNewlineForRootNode = true;
1090 
1091   return NS_OK;
1092 }
1093 
CheckElementStart(Element *,bool & aForceFormat,nsAString & aStr,nsresult & aResult)1094 bool nsXMLContentSerializer::CheckElementStart(Element*, bool& aForceFormat,
1095                                                nsAString& aStr,
1096                                                nsresult& aResult) {
1097   aResult = NS_OK;
1098   aForceFormat = false;
1099   return true;
1100 }
1101 
CheckElementEnd(Element * aElement,bool & aForceFormat,nsAString & aStr)1102 bool nsXMLContentSerializer::CheckElementEnd(Element* aElement,
1103                                              bool& aForceFormat,
1104                                              nsAString& aStr) {
1105   // We don't output a separate end tag for empty element
1106   aForceFormat = false;
1107 
1108   // XXXbz this is a bit messed up, but by now we don't have our fixed-up
1109   // version of aElement anymore.  Let's hope fixup never changes the localName
1110   // or namespace...
1111   return ElementNeedsSeparateEndTag(aElement, aElement);
1112 }
1113 
AppendToString(const char16_t aChar,nsAString & aOutputStr)1114 bool nsXMLContentSerializer::AppendToString(const char16_t aChar,
1115                                             nsAString& aOutputStr) {
1116   if (mBodyOnly && !mInBody) {
1117     return true;
1118   }
1119   mColPos += 1;
1120   return aOutputStr.Append(aChar, mozilla::fallible);
1121 }
1122 
AppendToString(const nsAString & aStr,nsAString & aOutputStr)1123 bool nsXMLContentSerializer::AppendToString(const nsAString& aStr,
1124                                             nsAString& aOutputStr) {
1125   if (mBodyOnly && !mInBody) {
1126     return true;
1127   }
1128   mColPos += aStr.Length();
1129   return aOutputStr.Append(aStr, mozilla::fallible);
1130 }
1131 
1132 static const uint16_t kGTVal = 62;
1133 
1134 #define _ 0
1135 
1136 // This table indexes into kEntityStrings[].
1137 static const uint8_t kEntities[] = {
1138     // clang-format off
1139   _, _, _, _, _, _, _, _, _, _,
1140   _, _, _, _, _, _, _, _, _, _,
1141   _, _, _, _, _, _, _, _, _, _,
1142   _, _, _, _, _, _, _, _, 2, _,
1143   _, _, _, _, _, _, _, _, _, _,
1144   _, _, _, _, _, _, _, _, _, _,
1145   3, _, 4
1146     // clang-format on
1147 };
1148 
1149 // This table indexes into kEntityStrings[].
1150 static const uint8_t kAttrEntities[] = {
1151     // clang-format off
1152   _, _, _, _, _, _, _, _, _, 5,
1153   6, _, _, 7, _, _, _, _, _, _,
1154   _, _, _, _, _, _, _, _, _, _,
1155   _, _, _, _, 1, _, _, _, 2, _,
1156   _, _, _, _, _, _, _, _, _, _,
1157   _, _, _, _, _, _, _, _, _, _,
1158   3, _, 4
1159     // clang-format on
1160 };
1161 
1162 #undef _
1163 
1164 static const char* const kEntityStrings[] = {
1165     /* 0 */ nullptr,
1166     /* 1 */ "&quot;",
1167     /* 2 */ "&amp;",
1168     /* 3 */ "&lt;",
1169     /* 4 */ "&gt;",
1170     /* 5 */ "&#9;",
1171     /* 6 */ "&#xA;",
1172     /* 7 */ "&#xD;",
1173 };
1174 
AppendAndTranslateEntities(const nsAString & aStr,nsAString & aOutputStr)1175 bool nsXMLContentSerializer::AppendAndTranslateEntities(const nsAString& aStr,
1176                                                         nsAString& aOutputStr) {
1177   nsReadingIterator<char16_t> done_reading;
1178   aStr.EndReading(done_reading);
1179 
1180   // for each chunk of |aString|...
1181   uint32_t advanceLength = 0;
1182   nsReadingIterator<char16_t> iter;
1183 
1184   const uint8_t* entityTable = mInAttribute ? kAttrEntities : kEntities;
1185 
1186   for (aStr.BeginReading(iter); iter != done_reading;
1187        iter.advance(int32_t(advanceLength))) {
1188     uint32_t fragmentLength = done_reading - iter;
1189     const char16_t* c = iter.get();
1190     const char16_t* fragmentStart = c;
1191     const char16_t* fragmentEnd = c + fragmentLength;
1192     const char* entityText = nullptr;
1193 
1194     advanceLength = 0;
1195     // for each character in this chunk, check if it
1196     // needs to be replaced
1197     for (; c < fragmentEnd; c++, advanceLength++) {
1198       char16_t val = *c;
1199       if ((val <= kGTVal) && entityTable[val]) {
1200         entityText = kEntityStrings[entityTable[val]];
1201         break;
1202       }
1203     }
1204 
1205     NS_ENSURE_TRUE(
1206         aOutputStr.Append(fragmentStart, advanceLength, mozilla::fallible),
1207         false);
1208     if (entityText) {
1209       NS_ENSURE_TRUE(
1210           AppendASCIItoUTF16(entityText, aOutputStr, mozilla::fallible), false);
1211       advanceLength++;
1212     }
1213   }
1214 
1215   return true;
1216 }
1217 
MaybeAddNewlineForRootNode(nsAString & aStr)1218 bool nsXMLContentSerializer::MaybeAddNewlineForRootNode(nsAString& aStr) {
1219   if (mAddNewlineForRootNode) {
1220     return AppendNewLineToString(aStr);
1221   }
1222 
1223   return true;
1224 }
1225 
MaybeFlagNewlineForRootNode(nsINode * aNode)1226 void nsXMLContentSerializer::MaybeFlagNewlineForRootNode(nsINode* aNode) {
1227   nsINode* parent = aNode->GetParentNode();
1228   if (parent) {
1229     mAddNewlineForRootNode = parent->IsNodeOfType(nsINode::eDOCUMENT);
1230   }
1231 }
1232 
MaybeEnterInPreContent(nsIContent * aNode)1233 void nsXMLContentSerializer::MaybeEnterInPreContent(nsIContent* aNode) {
1234   // support of the xml:space attribute
1235   nsAutoString space;
1236   if (ShouldMaintainPreLevel() && aNode->IsElement() &&
1237       aNode->AsElement()->GetAttr(kNameSpaceID_XML, nsGkAtoms::space, space) &&
1238       space.EqualsLiteral("preserve")) {
1239     ++PreLevel();
1240   }
1241 }
1242 
MaybeLeaveFromPreContent(nsIContent * aNode)1243 void nsXMLContentSerializer::MaybeLeaveFromPreContent(nsIContent* aNode) {
1244   // support of the xml:space attribute
1245   nsAutoString space;
1246   if (ShouldMaintainPreLevel() && aNode->IsElement() &&
1247       aNode->AsElement()->GetAttr(kNameSpaceID_XML, nsGkAtoms::space, space) &&
1248       space.EqualsLiteral("preserve")) {
1249     --PreLevel();
1250   }
1251 }
1252 
AppendNewLineToString(nsAString & aStr)1253 bool nsXMLContentSerializer::AppendNewLineToString(nsAString& aStr) {
1254   bool result = AppendToString(mLineBreak, aStr);
1255   mMayIgnoreLineBreakSequence = true;
1256   mColPos = 0;
1257   mAddSpace = false;
1258   mIsIndentationAddedOnCurrentLine = false;
1259   return result;
1260 }
1261 
AppendIndentation(nsAString & aStr)1262 bool nsXMLContentSerializer::AppendIndentation(nsAString& aStr) {
1263   mIsIndentationAddedOnCurrentLine = true;
1264   bool result = AppendToString(mIndent, aStr);
1265   mAddSpace = false;
1266   mMayIgnoreLineBreakSequence = false;
1267   return result;
1268 }
1269 
IncrIndentation(nsAtom * aName)1270 bool nsXMLContentSerializer::IncrIndentation(nsAtom* aName) {
1271   // we want to keep the source readable
1272   if (mDoWrap &&
1273       mIndent.Length() >= uint32_t(mMaxColumn) - MIN_INDENTED_LINE_LENGTH) {
1274     ++mIndentOverflow;
1275   } else {
1276     return mIndent.AppendLiteral(INDENT_STRING, mozilla::fallible);
1277   }
1278 
1279   return true;
1280 }
1281 
DecrIndentation(nsAtom * aName)1282 void nsXMLContentSerializer::DecrIndentation(nsAtom* aName) {
1283   if (mIndentOverflow)
1284     --mIndentOverflow;
1285   else
1286     mIndent.Cut(0, INDENT_STRING_LENGTH);
1287 }
1288 
LineBreakBeforeOpen(int32_t aNamespaceID,nsAtom * aName)1289 bool nsXMLContentSerializer::LineBreakBeforeOpen(int32_t aNamespaceID,
1290                                                  nsAtom* aName) {
1291   return mAddSpace;
1292 }
1293 
LineBreakAfterOpen(int32_t aNamespaceID,nsAtom * aName)1294 bool nsXMLContentSerializer::LineBreakAfterOpen(int32_t aNamespaceID,
1295                                                 nsAtom* aName) {
1296   return false;
1297 }
1298 
LineBreakBeforeClose(int32_t aNamespaceID,nsAtom * aName)1299 bool nsXMLContentSerializer::LineBreakBeforeClose(int32_t aNamespaceID,
1300                                                   nsAtom* aName) {
1301   return mAddSpace;
1302 }
1303 
LineBreakAfterClose(int32_t aNamespaceID,nsAtom * aName)1304 bool nsXMLContentSerializer::LineBreakAfterClose(int32_t aNamespaceID,
1305                                                  nsAtom* aName) {
1306   return false;
1307 }
1308 
AppendToStringConvertLF(const nsAString & aStr,nsAString & aOutputStr)1309 bool nsXMLContentSerializer::AppendToStringConvertLF(const nsAString& aStr,
1310                                                      nsAString& aOutputStr) {
1311   if (mBodyOnly && !mInBody) {
1312     return true;
1313   }
1314 
1315   if (mDoRaw) {
1316     NS_ENSURE_TRUE(AppendToString(aStr, aOutputStr), false);
1317   } else {
1318     // Convert line-endings to mLineBreak
1319     uint32_t start = 0;
1320     uint32_t theLen = aStr.Length();
1321     while (start < theLen) {
1322       int32_t eol = aStr.FindChar('\n', start);
1323       if (eol == kNotFound) {
1324         nsDependentSubstring dataSubstring(aStr, start, theLen - start);
1325         NS_ENSURE_TRUE(AppendToString(dataSubstring, aOutputStr), false);
1326         start = theLen;
1327         // if there was a line break before this substring
1328         // AppendNewLineToString was called, so we should reverse
1329         // this flag
1330         mMayIgnoreLineBreakSequence = false;
1331       } else {
1332         nsDependentSubstring dataSubstring(aStr, start, eol - start);
1333         NS_ENSURE_TRUE(AppendToString(dataSubstring, aOutputStr), false);
1334         NS_ENSURE_TRUE(AppendNewLineToString(aOutputStr), false);
1335         start = eol + 1;
1336       }
1337     }
1338   }
1339 
1340   return true;
1341 }
1342 
AppendFormatedWrapped_WhitespaceSequence(nsAString::const_char_iterator & aPos,const nsAString::const_char_iterator aEnd,const nsAString::const_char_iterator aSequenceStart,bool & aMayIgnoreStartOfLineWhitespaceSequence,nsAString & aOutputStr)1343 bool nsXMLContentSerializer::AppendFormatedWrapped_WhitespaceSequence(
1344     nsAString::const_char_iterator& aPos,
1345     const nsAString::const_char_iterator aEnd,
1346     const nsAString::const_char_iterator aSequenceStart,
1347     bool& aMayIgnoreStartOfLineWhitespaceSequence, nsAString& aOutputStr) {
1348   // Handle the complete sequence of whitespace.
1349   // Continue to iterate until we find the first non-whitespace char.
1350   // Updates "aPos" to point to the first unhandled char.
1351   // Also updates the aMayIgnoreStartOfLineWhitespaceSequence flag,
1352   // as well as the other "global" state flags.
1353 
1354   bool sawBlankOrTab = false;
1355   bool leaveLoop = false;
1356 
1357   do {
1358     switch (*aPos) {
1359       case ' ':
1360       case '\t':
1361         sawBlankOrTab = true;
1362         MOZ_FALLTHROUGH;
1363       case '\n':
1364         ++aPos;
1365         // do not increase mColPos,
1366         // because we will reduce the whitespace to a single char
1367         break;
1368       default:
1369         leaveLoop = true;
1370         break;
1371     }
1372   } while (!leaveLoop && aPos < aEnd);
1373 
1374   if (mAddSpace) {
1375     // if we had previously been asked to add space,
1376     // our situation has not changed
1377   } else if (!sawBlankOrTab && mMayIgnoreLineBreakSequence) {
1378     // nothing to do in the case where line breaks have already been added
1379     // before the call of AppendToStringWrapped
1380     // and only if we found line break in the sequence
1381     mMayIgnoreLineBreakSequence = false;
1382   } else if (aMayIgnoreStartOfLineWhitespaceSequence) {
1383     // nothing to do
1384     aMayIgnoreStartOfLineWhitespaceSequence = false;
1385   } else {
1386     if (sawBlankOrTab) {
1387       if (mDoWrap && mColPos + 1 >= mMaxColumn) {
1388         // no much sense in delaying, we only have one slot left,
1389         // let's write a break now
1390         bool result = aOutputStr.Append(mLineBreak, mozilla::fallible);
1391         mColPos = 0;
1392         mIsIndentationAddedOnCurrentLine = false;
1393         mMayIgnoreLineBreakSequence = true;
1394         NS_ENSURE_TRUE(result, false);
1395       } else {
1396         // do not write out yet, we may write out either a space or a linebreak
1397         // let's delay writing it out until we know more
1398         mAddSpace = true;
1399         ++mColPos;  // eat a slot of available space
1400       }
1401     } else {
1402       // Asian text usually does not contain spaces, therefore we should not
1403       // transform a linebreak into a space.
1404       // Since we only saw linebreaks, but no spaces or tabs,
1405       // let's write a linebreak now.
1406       NS_ENSURE_TRUE(AppendNewLineToString(aOutputStr), false);
1407     }
1408   }
1409 
1410   return true;
1411 }
1412 
AppendWrapped_NonWhitespaceSequence(nsAString::const_char_iterator & aPos,const nsAString::const_char_iterator aEnd,const nsAString::const_char_iterator aSequenceStart,bool & aMayIgnoreStartOfLineWhitespaceSequence,bool & aSequenceStartAfterAWhiteSpace,nsAString & aOutputStr)1413 bool nsXMLContentSerializer::AppendWrapped_NonWhitespaceSequence(
1414     nsAString::const_char_iterator& aPos,
1415     const nsAString::const_char_iterator aEnd,
1416     const nsAString::const_char_iterator aSequenceStart,
1417     bool& aMayIgnoreStartOfLineWhitespaceSequence,
1418     bool& aSequenceStartAfterAWhiteSpace, nsAString& aOutputStr) {
1419   mMayIgnoreLineBreakSequence = false;
1420   aMayIgnoreStartOfLineWhitespaceSequence = false;
1421 
1422   // Handle the complete sequence of non-whitespace in this block
1423   // Iterate until we find the first whitespace char or an aEnd condition
1424   // Updates "aPos" to point to the first unhandled char.
1425   // Also updates the aMayIgnoreStartOfLineWhitespaceSequence flag,
1426   // as well as the other "global" state flags.
1427 
1428   bool thisSequenceStartsAtBeginningOfLine = !mColPos;
1429   bool onceAgainBecauseWeAddedBreakInFront = false;
1430   bool foundWhitespaceInLoop;
1431   uint32_t length, colPos;
1432 
1433   do {
1434     if (mColPos) {
1435       colPos = mColPos;
1436     } else {
1437       if (mDoFormat && !mDoRaw && !PreLevel() &&
1438           !onceAgainBecauseWeAddedBreakInFront) {
1439         colPos = mIndent.Length();
1440       } else
1441         colPos = 0;
1442     }
1443     foundWhitespaceInLoop = false;
1444     length = 0;
1445     // we iterate until the next whitespace character
1446     // or until we reach the maximum of character per line
1447     // or until the end of the string to add.
1448     do {
1449       if (*aPos == ' ' || *aPos == '\t' || *aPos == '\n') {
1450         foundWhitespaceInLoop = true;
1451         break;
1452       }
1453 
1454       ++aPos;
1455       ++length;
1456     } while ((!mDoWrap || colPos + length < mMaxColumn) && aPos < aEnd);
1457 
1458     // in the case we don't reached the end of the string, but we reached the
1459     // maxcolumn, we see if there is a whitespace after the maxcolumn if yes,
1460     // then we can append directly the string instead of appending a new line
1461     // etc.
1462     if (*aPos == ' ' || *aPos == '\t' || *aPos == '\n') {
1463       foundWhitespaceInLoop = true;
1464     }
1465 
1466     if (aPos == aEnd || foundWhitespaceInLoop) {
1467       // there is enough room for the complete block we found
1468       if (mDoFormat && !mColPos) {
1469         NS_ENSURE_TRUE(AppendIndentation(aOutputStr), false);
1470       } else if (mAddSpace) {
1471         bool result = aOutputStr.Append(char16_t(' '), mozilla::fallible);
1472         mAddSpace = false;
1473         NS_ENSURE_TRUE(result, false);
1474       }
1475 
1476       mColPos += length;
1477       NS_ENSURE_TRUE(aOutputStr.Append(aSequenceStart, aPos - aSequenceStart,
1478                                        mozilla::fallible),
1479                      false);
1480 
1481       // We have not yet reached the max column, we will continue to
1482       // fill the current line in the next outer loop iteration
1483       // (this one in AppendToStringWrapped)
1484       // make sure we return in this outer loop
1485       onceAgainBecauseWeAddedBreakInFront = false;
1486     } else {  // we reach the max column
1487       if (!thisSequenceStartsAtBeginningOfLine &&
1488           (mAddSpace || (!mDoFormat && aSequenceStartAfterAWhiteSpace))) {
1489         // when !mDoFormat, mAddSpace is not used, mAddSpace is always false
1490         // so, in the case where mDoWrap && !mDoFormat, if we want to enter in
1491         // this condition...
1492 
1493         // We can avoid to wrap. We try to add the whole block
1494         // in an empty new line
1495 
1496         NS_ENSURE_TRUE(AppendNewLineToString(aOutputStr), false);
1497         aPos = aSequenceStart;
1498         thisSequenceStartsAtBeginningOfLine = true;
1499         onceAgainBecauseWeAddedBreakInFront = true;
1500       } else {
1501         // we must wrap
1502         onceAgainBecauseWeAddedBreakInFront = false;
1503         bool foundWrapPosition = false;
1504         int32_t wrapPosition = 0;
1505 
1506         if (mAllowLineBreaking) {
1507           mozilla::intl::LineBreaker* lineBreaker =
1508               nsContentUtils::LineBreaker();
1509 
1510           wrapPosition =
1511               lineBreaker->Prev(aSequenceStart, (aEnd - aSequenceStart),
1512                                 (aPos - aSequenceStart) + 1);
1513           if (wrapPosition != NS_LINEBREAKER_NEED_MORE_TEXT) {
1514             foundWrapPosition = true;
1515           } else {
1516             wrapPosition =
1517                 lineBreaker->Next(aSequenceStart, (aEnd - aSequenceStart),
1518                                   (aPos - aSequenceStart));
1519             if (wrapPosition != NS_LINEBREAKER_NEED_MORE_TEXT) {
1520               foundWrapPosition = true;
1521             }
1522           }
1523         }
1524 
1525         if (foundWrapPosition) {
1526           if (!mColPos && mDoFormat) {
1527             NS_ENSURE_TRUE(AppendIndentation(aOutputStr), false);
1528           } else if (mAddSpace) {
1529             bool result = aOutputStr.Append(char16_t(' '), mozilla::fallible);
1530             mAddSpace = false;
1531             NS_ENSURE_TRUE(result, false);
1532           }
1533           NS_ENSURE_TRUE(aOutputStr.Append(aSequenceStart, wrapPosition,
1534                                            mozilla::fallible),
1535                          false);
1536 
1537           NS_ENSURE_TRUE(AppendNewLineToString(aOutputStr), false);
1538           aPos = aSequenceStart + wrapPosition;
1539           aMayIgnoreStartOfLineWhitespaceSequence = true;
1540         } else {
1541           // try some simple fallback logic
1542           // go forward up to the next whitespace position,
1543           // in the worst case this will be all the rest of the data
1544 
1545           // we update the mColPos variable with the length of
1546           // the part already parsed.
1547           mColPos += length;
1548 
1549           // now try to find the next whitespace
1550           do {
1551             if (*aPos == ' ' || *aPos == '\t' || *aPos == '\n') {
1552               break;
1553             }
1554 
1555             ++aPos;
1556             ++mColPos;
1557           } while (aPos < aEnd);
1558 
1559           if (mAddSpace) {
1560             bool result = aOutputStr.Append(char16_t(' '), mozilla::fallible);
1561             mAddSpace = false;
1562             NS_ENSURE_TRUE(result, false);
1563           }
1564           NS_ENSURE_TRUE(
1565               aOutputStr.Append(aSequenceStart, aPos - aSequenceStart,
1566                                 mozilla::fallible),
1567               false);
1568         }
1569       }
1570       aSequenceStartAfterAWhiteSpace = false;
1571     }
1572   } while (onceAgainBecauseWeAddedBreakInFront);
1573 
1574   return true;
1575 }
1576 
AppendToStringFormatedWrapped(const nsAString & aStr,nsAString & aOutputStr)1577 bool nsXMLContentSerializer::AppendToStringFormatedWrapped(
1578     const nsAString& aStr, nsAString& aOutputStr) {
1579   if (mBodyOnly && !mInBody) {
1580     return true;
1581   }
1582 
1583   nsAString::const_char_iterator pos, end, sequenceStart;
1584 
1585   aStr.BeginReading(pos);
1586   aStr.EndReading(end);
1587 
1588   bool sequenceStartAfterAWhitespace = false;
1589   if (pos < end) {
1590     nsAString::const_char_iterator end2;
1591     aOutputStr.EndReading(end2);
1592     --end2;
1593     if (*end2 == ' ' || *end2 == '\n' || *end2 == '\t') {
1594       sequenceStartAfterAWhitespace = true;
1595     }
1596   }
1597 
1598   // if the current line already has text on it, such as a tag,
1599   // leading whitespace is significant
1600   bool mayIgnoreStartOfLineWhitespaceSequence =
1601       (!mColPos ||
1602        (mIsIndentationAddedOnCurrentLine && sequenceStartAfterAWhitespace &&
1603         uint32_t(mColPos) == mIndent.Length()));
1604 
1605   while (pos < end) {
1606     sequenceStart = pos;
1607 
1608     // if beginning of a whitespace sequence
1609     if (*pos == ' ' || *pos == '\n' || *pos == '\t') {
1610       NS_ENSURE_TRUE(AppendFormatedWrapped_WhitespaceSequence(
1611                          pos, end, sequenceStart,
1612                          mayIgnoreStartOfLineWhitespaceSequence, aOutputStr),
1613                      false);
1614     } else {  // any other non-whitespace char
1615       NS_ENSURE_TRUE(
1616           AppendWrapped_NonWhitespaceSequence(
1617               pos, end, sequenceStart, mayIgnoreStartOfLineWhitespaceSequence,
1618               sequenceStartAfterAWhitespace, aOutputStr),
1619           false);
1620     }
1621   }
1622 
1623   return true;
1624 }
1625 
AppendWrapped_WhitespaceSequence(nsAString::const_char_iterator & aPos,const nsAString::const_char_iterator aEnd,const nsAString::const_char_iterator aSequenceStart,nsAString & aOutputStr)1626 bool nsXMLContentSerializer::AppendWrapped_WhitespaceSequence(
1627     nsAString::const_char_iterator& aPos,
1628     const nsAString::const_char_iterator aEnd,
1629     const nsAString::const_char_iterator aSequenceStart,
1630     nsAString& aOutputStr) {
1631   // Handle the complete sequence of whitespace.
1632   // Continue to iterate until we find the first non-whitespace char.
1633   // Updates "aPos" to point to the first unhandled char.
1634   mAddSpace = false;
1635   mIsIndentationAddedOnCurrentLine = false;
1636 
1637   bool leaveLoop = false;
1638   nsAString::const_char_iterator lastPos = aPos;
1639 
1640   do {
1641     switch (*aPos) {
1642       case ' ':
1643       case '\t':
1644         // if there are too many spaces on a line, we wrap
1645         if (mColPos >= mMaxColumn) {
1646           if (lastPos != aPos) {
1647             NS_ENSURE_TRUE(
1648                 aOutputStr.Append(lastPos, aPos - lastPos, mozilla::fallible),
1649                 false);
1650           }
1651           NS_ENSURE_TRUE(AppendToString(mLineBreak, aOutputStr), false);
1652           mColPos = 0;
1653           lastPos = aPos;
1654         }
1655 
1656         ++mColPos;
1657         ++aPos;
1658         break;
1659       case '\n':
1660         if (lastPos != aPos) {
1661           NS_ENSURE_TRUE(
1662               aOutputStr.Append(lastPos, aPos - lastPos, mozilla::fallible),
1663               false);
1664         }
1665         NS_ENSURE_TRUE(AppendToString(mLineBreak, aOutputStr), false);
1666         mColPos = 0;
1667         ++aPos;
1668         lastPos = aPos;
1669         break;
1670       default:
1671         leaveLoop = true;
1672         break;
1673     }
1674   } while (!leaveLoop && aPos < aEnd);
1675 
1676   if (lastPos != aPos) {
1677     NS_ENSURE_TRUE(
1678         aOutputStr.Append(lastPos, aPos - lastPos, mozilla::fallible), false);
1679   }
1680 
1681   return true;
1682 }
1683 
AppendToStringWrapped(const nsAString & aStr,nsAString & aOutputStr)1684 bool nsXMLContentSerializer::AppendToStringWrapped(const nsAString& aStr,
1685                                                    nsAString& aOutputStr) {
1686   if (mBodyOnly && !mInBody) {
1687     return true;
1688   }
1689 
1690   nsAString::const_char_iterator pos, end, sequenceStart;
1691 
1692   aStr.BeginReading(pos);
1693   aStr.EndReading(end);
1694 
1695   // not used in this case, but needed by AppendWrapped_NonWhitespaceSequence
1696   bool mayIgnoreStartOfLineWhitespaceSequence = false;
1697   mMayIgnoreLineBreakSequence = false;
1698 
1699   bool sequenceStartAfterAWhitespace = false;
1700   if (pos < end && !aOutputStr.IsEmpty()) {
1701     nsAString::const_char_iterator end2;
1702     aOutputStr.EndReading(end2);
1703     --end2;
1704     if (*end2 == ' ' || *end2 == '\n' || *end2 == '\t') {
1705       sequenceStartAfterAWhitespace = true;
1706     }
1707   }
1708 
1709   while (pos < end) {
1710     sequenceStart = pos;
1711 
1712     // if beginning of a whitespace sequence
1713     if (*pos == ' ' || *pos == '\n' || *pos == '\t') {
1714       sequenceStartAfterAWhitespace = true;
1715       NS_ENSURE_TRUE(
1716           AppendWrapped_WhitespaceSequence(pos, end, sequenceStart, aOutputStr),
1717           false);
1718     } else {  // any other non-whitespace char
1719       NS_ENSURE_TRUE(
1720           AppendWrapped_NonWhitespaceSequence(
1721               pos, end, sequenceStart, mayIgnoreStartOfLineWhitespaceSequence,
1722               sequenceStartAfterAWhitespace, aOutputStr),
1723           false);
1724     }
1725   }
1726 
1727   return true;
1728 }
1729 
ShouldMaintainPreLevel() const1730 bool nsXMLContentSerializer::ShouldMaintainPreLevel() const {
1731   // Only attempt to maintain the pre level for consumers who care about it.
1732   return !mDoRaw || (mFlags & nsIDocumentEncoder::OutputNoFormattingInPre);
1733 }
1734