Просмотр исходного кода

Merge branch 'patch-1' of https://github.com/SirR4T/tinyxml2 into SirR4T-patch-1

Lee Thomason 10 лет назад
Родитель
Сommit
2ecc203835
2 измененных файлов с 30 добавлено и 0 удалено
  1. 11 0
      tinyxml2.cpp
  2. 19 0
      xmltest.cpp

+ 11 - 0
tinyxml2.cpp

@@ -887,6 +887,17 @@ char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
             break;
         }
 
+        XMLDeclaration* decl = node->ToDeclaration();
+        if ( decl ) {
+                // A declaration can only be the first child of a document.
+                // Set error, if document already has children.
+                if ( !_document->NoChildren() ) {
+                        _document->SetError( XML_ERROR_PARSING_DECLARATION, decl->Value(), 0);
+                        DeleteNode( decl );
+                        break;
+                }
+        }
+
         XMLElement* ele = node->ToElement();
         if ( ele ) {
             // We read the end tag. Return it to the parent.

+ 19 - 0
xmltest.cpp

@@ -1460,6 +1460,25 @@ int main( int argc, const char ** argv )
 		XMLTest( "Error should be cleared", false, doc.Error() );
 	}
 
+	{
+		// Check that declarations are parsed only as the FirstChild
+                const char* xml0 = "<?xml version=\"1.0\" ?>"
+                                   "   <!-- xml version=\"1.1\" -->"
+                                   "<first />";
+                const char* xml1 = "<?xml version=\"1.0\" ?>"
+                                   "   <?xml version=\"1.1\" ?>"
+                                   "<first />";
+                const char* xml2 = "<first />"
+                                   "<?xml version=\"1.0\" ?>";
+                XMLDocument doc;
+                doc.Parse(xml0);
+                XMLTest("Test that the code changes do not affect normal parsing", doc.Error(), false);
+                doc.Parse(xml1);
+                XMLTest("Test that the second declaration throws an error", doc.Error(), true);
+                doc.Parse(xml2);
+                XMLTest("Test that declaration after a child throws an error", doc.Error(), true);
+	}
+
 	// ----------- Performance tracking --------------
 	{
 #if defined( _MSC_VER )