diff --git a/tinyxml2.cpp b/tinyxml2.cpp index cb26e00..91f4011 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -580,7 +580,8 @@ XMLNode::XMLNode( XMLDocument* doc ) : _parent( 0 ), _firstChild( 0 ), _lastChild( 0 ), _prev( 0 ), _next( 0 ), - _memPool( 0 ) + _memPool( 0 ), + _forceCompactMode( false ) { } @@ -1945,17 +1946,17 @@ void XMLPrinter::PushHeader( bool writeBOM, bool writeDec ) } -void XMLPrinter::OpenElement( const char* name ) +void XMLPrinter::OpenElement( const char* name, bool compactMode ) { if ( _elementJustOpened ) { SealElement(); } _stack.Push( name ); - if ( _textDepth < 0 && !_firstElement && !_compactMode ) { + if ( _textDepth < 0 && !_firstElement && !compactMode ) { Print( "\n" ); } - if ( !_compactMode ) { + if ( !compactMode ) { PrintSpace( _depth ); } @@ -2151,7 +2152,7 @@ bool XMLPrinter::VisitEnter( const XMLDocument& doc ) bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute ) { - OpenElement( element.Name() ); + OpenElement( element.Name(), _compactMode ? true : element.Parent()->GetForceCompactMode() ); while ( attribute ) { PushAttribute( attribute->Name(), attribute->Value() ); attribute = attribute->Next(); diff --git a/tinyxml2.h b/tinyxml2.h index 41f1c70..09f6dd2 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -821,6 +821,9 @@ public: // internal virtual char* ParseDeep( char*, StrPair* ); + + bool GetForceCompactMode() const { if( _forceCompactMode || !Parent() ) return _forceCompactMode; return Parent()->GetForceCompactMode(); }; + void SetForceCompactMode( bool b ) { _forceCompactMode = b; }; protected: XMLNode( XMLDocument* ); @@ -837,6 +840,8 @@ protected: XMLNode* _prev; XMLNode* _next; + + bool _forceCompactMode; private: MemPool* _memPool; @@ -1463,7 +1468,7 @@ public: char* ParseDeep( char* p, StrPair* endTag ); virtual XMLNode* ShallowClone( XMLDocument* document ) const; virtual bool ShallowEqual( const XMLNode* compare ) const; - + private: XMLElement( XMLDocument* doc ); virtual ~XMLElement(); @@ -1961,7 +1966,7 @@ public: /** If streaming, start writing an element. The element must be closed with CloseElement() */ - void OpenElement( const char* name ); + void OpenElement( const char* name, bool compactMode ); /// If streaming, add an attribute to an open element. void PushAttribute( const char* name, const char* value ); void PushAttribute( const char* name, int value ); diff --git a/xmltest.cpp b/xmltest.cpp index 6fdc162..fa9e1a4 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -1012,6 +1012,27 @@ int main( int argc, const char ** argv ) ele->DeleteAttribute( "attrib3" ); XMLTest( "Attribute order (empty)", false, ele->FirstAttribute() ? true : false ); } + + { + XMLDocument doc0; + XMLElement* root = doc0.NewElement("root"); + doc0.InsertEndChild(root); + XMLElement* text = doc0.NewElement("text"); + text->SetForceCompactMode(true); + root->InsertEndChild(text); + XMLText* befText = doc0.NewText("Before "); + text->InsertEndChild(befText); + XMLElement* tag = doc0.NewElement("tag"); + text->InsertEndChild(tag); + XMLText* tagText = doc0.NewText("Tag"); + tag->InsertEndChild(tagText); + XMLText* aftText = doc0.NewText(" After"); + text->InsertEndChild(aftText); + + XMLPrinter printer; + doc0.Print( &printer ); + XMLTest( "Selective text wrapping", "\n Before Tag After\n\n", printer.CStr() ); + } { // Make sure an attribute with a space in it succeeds.