Bug 417384, r+sr=peterv, a=mtschrep
authorOlli.Pettay@helsinki.fi
Tue, 19 Feb 2008 05:17:29 -0800
changeset 11862 310559e938356a468afee5707e4b122d4247a1a9
parent 11861 01eb3f6401d517426983511931077fb17e662f3f
child 11863 057effe236369269d6abb05820236c6dc5955157
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersmtschrep
bugs417384
milestone1.9b4pre
Bug 417384, r+sr=peterv, a=mtschrep
content/base/src/nsPlainTextSerializer.cpp
content/base/src/nsPlainTextSerializer.h
content/base/test/Makefile.in
content/base/test/test_bug417384.html
--- a/content/base/src/nsPlainTextSerializer.cpp
+++ b/content/base/src/nsPlainTextSerializer.cpp
@@ -95,17 +95,17 @@ nsresult NS_NewPlainTextSerializer(nsICo
   return CallQueryInterface(it, aSerializer);
 }
 
 nsPlainTextSerializer::nsPlainTextSerializer()
   : kSpace(NS_LITERAL_STRING(" ")) // Init of "constant"
 {
 
   mOutputString = nsnull;
-  mInHead = PR_FALSE;
+  mHeadLevel = 0;
   mAtFirstColumn = PR_TRUE;
   mIndent = 0;
   mCiteQuoteLevel = 0;
   mStructs = PR_TRUE;       // will be read from prefs later
   mHeaderStrategy = 1 /*indent increasingly*/;   // ditto
   mQuotesPreformatted = PR_FALSE;                // ditto
   mDontWrapAnyQuotes = PR_FALSE;                 // ditto
   mHasWrittenCiteBlockquote = PR_FALSE;
@@ -401,18 +401,19 @@ nsPlainTextSerializer::AppendElementStar
   }
   else {
     rv = DoAddLeaf(nsnull, id, EmptyString());
   }
 
   mContent = 0;
   mOutputString = nsnull;
 
-  if (!mInHead && id == eHTMLTag_head)
-    mInHead = PR_TRUE;    
+  if (id == eHTMLTag_head) {
+    ++mHeadLevel;
+  }
 
   return rv;
 } 
  
 NS_IMETHODIMP 
 nsPlainTextSerializer::AppendElementEnd(nsIDOMElement *aElement,
                                         nsAString& aStr)
 {
@@ -431,18 +432,19 @@ nsPlainTextSerializer::AppendElementEnd(
   rv = NS_OK;
   if (isContainer) {
     rv = DoCloseContainer(id);
   }
 
   mContent = 0;
   mOutputString = nsnull;
 
-  if (mInHead && id == eHTMLTag_head)
-    mInHead = PR_FALSE;    
+  if (id == eHTMLTag_head) {
+    --mHeadLevel;
+  }
 
   return rv;
 }
 
 NS_IMETHODIMP 
 nsPlainTextSerializer::Flush(nsAString& aStr)
 {
   mOutputString = &aStr;
@@ -459,28 +461,28 @@ nsPlainTextSerializer::AppendDocumentSta
 }
 
 NS_IMETHODIMP
 nsPlainTextSerializer::OpenContainer(const nsIParserNode& aNode)
 {
   PRInt32 type = aNode.GetNodeType();
 
   if (type == eHTMLTag_head) {
-    mInHead = PR_TRUE;
+    ++mHeadLevel;
     return NS_OK;
   }
 
   return DoOpenContainer(&aNode, type);
 }
 
 NS_IMETHODIMP 
 nsPlainTextSerializer::CloseContainer(const nsHTMLTag aTag)
 {
   if (aTag == eHTMLTag_head) {
-    mInHead = PR_FALSE;
+    --mHeadLevel;
     return NS_OK;
   }
 
   return DoCloseContainer(aTag);
 }
 
 NS_IMETHODIMP 
 nsPlainTextSerializer::AddLeaf(const nsIParserNode& aNode)
@@ -507,17 +509,16 @@ nsPlainTextSerializer::AddLeaf(const nsI
   else {
     return DoAddLeaf(&aNode, type, text);
   }
 }
 
 NS_IMETHODIMP 
 nsPlainTextSerializer::OpenHead()
 {
-  mInHead = PR_TRUE;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsPlainTextSerializer::IsEnabled(PRInt32 aTag, PRBool* aReturn)
 {
   nsHTMLTag theHTMLTag = nsHTMLTag(aTag);
 
--- a/content/base/src/nsPlainTextSerializer.h
+++ b/content/base/src/nsPlainTextSerializer.h
@@ -155,29 +155,28 @@ protected:
   {
     return mWrapColumn &&
       ((mFlags & nsIDocumentEncoder::OutputFormatted) ||
        (mFlags & nsIDocumentEncoder::OutputWrap));
   }
 
   inline PRBool DoOutput()
   {
-    return !mInHead;
+    return mHeadLevel == 0;
   }
 
   // Stack handling functions
   PRBool GetLastBool(const nsVoidArray& aStack);
   void SetLastBool(nsVoidArray& aStack, PRBool aValue);
   void PushBool(nsVoidArray& aStack, PRBool aValue);
   PRBool PopBool(nsVoidArray& aStack);
   
 protected:
   nsString         mCurrentLine;
-
-  PRPackedBool     mInHead;
+  PRUint32         mHeadLevel;
   PRPackedBool     mAtFirstColumn;
 
   // Handling of quoted text (for mail):
   // Quotes need to be wrapped differently from non-quoted text,
   // because quoted text has a few extra characters (e.g. ">> ")
   // which makes the line length longer.
   // Mail can represent quotes in different ways: it can wrap
   // quotes in a <pre> (if editor.quotesPreformatted is set),
--- a/content/base/test/Makefile.in
+++ b/content/base/test/Makefile.in
@@ -138,12 +138,13 @@ include $(topsrcdir)/config/rules.mk
 		test_bug403868.xml \
 		test_bug405182.html \
 		test_bug403841.html \
 		test_bug410229.html \
 		test_bug415860.html \
 		test_bug414190.html \
 		test_bug414796.html \
 		test_bug416383.html \
+		test_bug417384.html \
 		$(NULL)
 
 libs:: $(_TEST_FILES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir)
new file mode 100644
--- /dev/null
+++ b/content/base/test/test_bug417384.html
@@ -0,0 +1,55 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=417384
+-->
+<head>
+  <title>Test for Bug 417384</title>
+  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=417384">Mozilla Bug 417384</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+  
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+/** Test for Bug 417384 **/
+
+var expectedSerialization = "about:blank document";
+function testSerializer() {
+  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+
+  var doc = document.getElementById('test_iframe').contentDocument;
+  doc.body.textContent = expectedSerialization;
+  var head1 = doc.createElement("head");
+  doc.body.appendChild(head1);
+  var span = doc.createElement("span");
+  head1.appendChild(span);
+  span.appendChild(doc.createTextNode("before inner head\n"));
+  span.appendChild(doc.createElement("head"));
+  span.appendChild(doc.createTextNode("\nafter inner head"));
+
+  var encoder =
+    Components.classes["@mozilla.org/layout/documentEncoder;1?type=text/html"]
+      .createInstance(Components.interfaces.nsIDocumentEncoder);
+  encoder.init(doc, "text/plain", 0);
+  encoder.setCharset("UTF-8");
+  var out = encoder.encodeToString();
+  ok(out == expectedSerialization, "Wrong serialization!");
+}
+
+SimpleTest.waitForExplicitFinish();
+addLoadEvent(testSerializer);
+addLoadEvent(SimpleTest.finish);
+
+</script>
+</pre>
+<iframe id="test_iframe" src="about:blank"></iframe>
+</body>
+</html>
+