Description: Fix first line indentation when compactMode = false
Forwarded: yes
Bug: https://github.com/leethomason/tinyxml2/pull/811

From 4a062f0121a3ee3fd5f03591da0d0c308bb02e1e Mon Sep 17 00:00:00 2001
From: Chow Loong Jin <hyperair@debian.org>
Date: Thu, 26 Mar 2020 16:30:37 +0800
Subject: [PATCH] XMLPrinter: Fix first line indentation

Fix indentation of first line when called with !compactMode.
---
 tinyxml2.cpp | 42 +++++++++++++++++++-----------------------
 tinyxml2.h   |  5 +++++
 2 files changed, 24 insertions(+), 23 deletions(-)

diff --git a/tinyxml2.cpp b/tinyxml2.cpp
index 9e7ff1a..3cf0a8b 100755
--- a/tinyxml2.cpp
+++ b/tinyxml2.cpp
@@ -2660,22 +2660,33 @@ void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
     }
 }
 
-
-void XMLPrinter::OpenElement( const char* name, bool compactMode )
+void XMLPrinter::PrepareForNewNode( bool compactMode )
 {
     SealElementIfJustOpened();
-    _stack.Push( name );
 
-    if ( _textDepth < 0 && !_firstElement && !compactMode ) {
+    if ( compactMode ) {
+        return;
+    }
+
+    if ( _firstElement ) {
+        PrintSpace ( _depth );
+    } else if ( _textDepth < 0 ) {
         Putc( '\n' );
         PrintSpace( _depth );
     }
 
+    _firstElement = false;
+}
+
+void XMLPrinter::OpenElement( const char* name, bool compactMode )
+{
+    PrepareForNewNode( compactMode );
+    _stack.Push( name );
+
     Write ( "<" );
     Write ( name );
 
     _elementJustOpened = true;
-    _firstElement = false;
     ++_depth;
 }
 
@@ -2851,12 +2862,7 @@ void XMLPrinter::PushText( double value )
 
 void XMLPrinter::PushComment( const char* comment )
 {
-    SealElementIfJustOpened();
-    if ( _textDepth < 0 && !_firstElement && !_compactMode) {
-        Putc( '\n' );
-        PrintSpace( _depth );
-    }
-    _firstElement = false;
+    PrepareForNewNode( _compactMode );
 
     Write( "<!--" );
     Write( comment );
@@ -2866,12 +2872,7 @@ void XMLPrinter::PushComment( const char* comment )
 
 void XMLPrinter::PushDeclaration( const char* value )
 {
-    SealElementIfJustOpened();
-    if ( _textDepth < 0 && !_firstElement && !_compactMode) {
-        Putc( '\n' );
-        PrintSpace( _depth );
-    }
-    _firstElement = false;
+    PrepareForNewNode( _compactMode );
 
     Write( "<?" );
     Write( value );
@@ -2881,12 +2882,7 @@ void XMLPrinter::PushDeclaration( const char* value )
 
 void XMLPrinter::PushUnknown( const char* value )
 {
-    SealElementIfJustOpened();
-    if ( _textDepth < 0 && !_firstElement && !_compactMode) {
-        Putc( '\n' );
-        PrintSpace( _depth );
-    }
-    _firstElement = false;
+    PrepareForNewNode( _compactMode );
 
     Write( "<!" );
     Write( value );
diff --git a/tinyxml2.h b/tinyxml2.h
index 1beadaa..43f6af5 100755
--- a/tinyxml2.h
+++ b/tinyxml2.h
@@ -2332,6 +2332,11 @@ protected:
     DynArray< const char*, 10 > _stack;
 
 private:
+    /**
+       Prepares to write a new node. This includes sealing an element that was
+       just opened, and writing any whitespace necessary if not in compact mode.
+     */
+    void PrepareForNewNode( bool compactMode );
     void PrintString( const char*, bool restrictedEntitySet );	// prints out, after detecting entities.
 
     bool _firstElement;
-- 
2.20.1

