Ver Fonte

Merge branch 'pernodecompactwhitespace'

* pernodecompactwhitespace:
  SetForceCompactMode() now also handles case of a single tag inside another correctly.
  Give tests a few more chances to fail, e.g. on tag sequences or attributes.
  Added SetForceCompactMode() for overriding the compact setting on a per-node level. All sub-nodes will be printed compact as well.

Conflicts:
	xmltest.cpp
Uli Kusterer há 12 anos atrás
pai
commit
b85b4e0a60
3 ficheiros alterados com 27 adições e 7 exclusões
  1. 5 5
      tinyxml2.cpp
  2. 1 1
      tinyxml2.h
  3. 21 1
      xmltest.cpp

+ 5 - 5
tinyxml2.cpp

@@ -2044,7 +2044,7 @@ void XMLPrinter::PushAttribute( const char* name, double v )
 }
 
 
-void XMLPrinter::CloseElement()
+void XMLPrinter::CloseElement( bool compactMode )
 {
     --_depth;
     const char* name = _stack.Pop();
@@ -2053,7 +2053,7 @@ void XMLPrinter::CloseElement()
         Print( "/>" );
     }
     else {
-        if ( _textDepth < 0 && !_compactMode) {
+        if ( _textDepth < 0 && !compactMode) {
             Print( "\n" );
             PrintSpace( _depth );
         }
@@ -2063,7 +2063,7 @@ void XMLPrinter::CloseElement()
     if ( _textDepth == _depth ) {
         _textDepth = -1;
     }
-    if ( _depth == 0 && !_compactMode) {
+    if ( _depth == 0 && !compactMode) {
         Print( "\n" );
     }
     _elementJustOpened = false;
@@ -2197,9 +2197,9 @@ bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attr
 }
 
 
-bool XMLPrinter::VisitExit( const XMLElement& )
+bool XMLPrinter::VisitExit( const XMLElement& element )
 {
-    CloseElement();
+    CloseElement( _compactMode ? true : element.GetForceCompactMode() );
     return true;
 }
 

+ 1 - 1
tinyxml2.h

@@ -2020,7 +2020,7 @@ public:
     void PushAttribute( const char* name, bool value );
     void PushAttribute( const char* name, double value );
     /// If streaming, close the Element.
-    virtual void CloseElement();
+    virtual void CloseElement( bool compactMode );
 
     /// Add a text node.
     void PushText( const char* text, bool cdata=false );

+ 21 - 1
xmltest.cpp

@@ -1026,12 +1026,32 @@ int main( int argc, const char ** argv )
 		text->InsertEndChild(tag);
 		XMLText*	tagText = doc0.NewText("Tag");
 		tag->InsertEndChild(tagText);
+		tag = doc0.NewElement("tagtwo");
+		tag->SetAttribute("two", "2");
+		text->InsertEndChild(tag);
+		tagText = doc0.NewText("TagTwo");
+		tag->InsertEndChild(tagText);
 		XMLText*	aftText = doc0.NewText(" After");
 		text->InsertEndChild(aftText);
 		
 		XMLPrinter printer;
     	doc0.Print( &printer );
-		XMLTest( "Selective text wrapping", "<root>\n    <text>Before <tag>Tag</tag> After</text>\n</root>\n", printer.CStr() );
+		XMLTest( "Selective text wrapping", "<root>\n    <text>Before <tag>Tag</tag><tagtwo two=\"2\">TagTwo</tagtwo> After</text>\n</root>\n", printer.CStr() );
+	}
+
+	{
+		XMLDocument	doc0;
+		XMLElement*	root = doc0.NewElement("root");
+		doc0.InsertEndChild(root);
+		XMLElement*	cool = doc0.NewElement("cool");
+		cool->SetForceCompactMode(true);
+		root->InsertEndChild(cool);
+		XMLElement*	tag = doc0.NewElement("true");
+		cool->InsertEndChild(tag);
+		
+		XMLPrinter printer;
+    	doc0.Print( &printer );
+		XMLTest( "Selective text around single tag", "<root>\n    <cool><true/></cool>\n</root>\n", printer.CStr() );
 	}
 
 	{