bugfix #3231 - when XmlWriterSettings specifies Indent, some WriteWhitespace() is...
authorAtsushi Eno <atsushi@ximian.com>
Thu, 8 Mar 2012 13:42:10 +0000 (22:42 +0900)
committerAtsushi Eno <atsushi@ximian.com>
Thu, 8 Mar 2012 13:42:10 +0000 (22:42 +0900)
mcs/class/System.XML/System.Xml/XmlTextWriter2.cs
mcs/class/System.XML/Test/System.Xml/XmlWriterSettingsTests.cs

index a47d0edc80b5fb13c25696cb5891d83240d5f2d2..008a22366d6a0977d125cc285795a8db2b275fcf 100644 (file)
@@ -226,6 +226,7 @@ namespace Mono.Xml
                XmlNodeType node_state = XmlNodeType.None;
                XmlNamespaceManager nsmanager;
                int open_count;
+               bool top_level_space_ignored;
                XmlNodeInfo [] elements = new XmlNodeInfo [10];
                Stack new_local_namespaces = new Stack ();
                ArrayList explicit_nsdecls = new ArrayList ();
@@ -1082,9 +1083,11 @@ namespace Mono.Xml
                            XmlChar.IndexOfNonWhitespace (text) >= 0)
                                throw ArgumentError ("WriteWhitespace method accepts only whitespaces.");
 
+                       bool pastTopLevelWSIgnored = top_level_space_ignored;
                        ShiftStateTopLevel ("Whitespace", true, false, true);
-
-                       writer.Write (text);
+                       if (!indent || WriteState != WriteState.Prolog || pastTopLevelWSIgnored)
+                               writer.Write (text);
+                       top_level_space_ignored = true;
                }
 
                public override void WriteCData (string text)
@@ -1330,7 +1333,7 @@ namespace Mono.Xml
                                        CheckMixedContentState ();
                                break;
                        }
-
+                       top_level_space_ignored = false;
                }
 
                void CheckMixedContentState ()
index b5bd9b2c070e84362ce27f2a5eda14cb1137502a..1c2a24ab259c98cf59bd0e7760340bbe37f894da 100644 (file)
@@ -284,6 +284,25 @@ namespace MonoTests.System.Xml
                        // no heading newline.
                        Assert.AreEqual ("<root />", sw.ToString ());
                }
+               
+               [Test] // surprisingly niche behavior yet caused bug #3231.
+               public void IndentAndTopLevelWhitespaces ()
+               {
+                       var sw = new StringWriter ();
+                       var xw = XmlWriter.Create (sw, new XmlWriterSettings () { Indent = true });
+                       xw.WriteProcessingInstruction ("xml", "version='1.0'");
+                       xw.WriteWhitespace ("\n");
+                       xw.WriteComment ("AAA");
+                       xw.WriteWhitespace ("\n");
+                       xw.WriteWhitespace ("\n");
+                       xw.WriteStartElement ("root");
+                       xw.Close ();
+                       string xml = @"<?xml version='1.0'?>
+<!--AAA-->
+
+<root />";
+                       Assert.AreEqual (xml, sw.ToString ().Replace ("\r\n", "\n"), "#1");
+               }
        }
 }
 #endif