diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 69d93ef0..471d75af 100644 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1075,6 +1075,30 @@ const XMLElement* XMLNode::PreviousSiblingElement( const char* name ) const } +// XMLDeclaration also represents processing instructions. Only named targets +// other than the reserved XML name may appear outside the prolog. +static bool IsProcessingInstruction( const char* value ) +{ + TIXMLASSERT( value ); + const unsigned char* p = reinterpret_cast( value ); + if ( !XMLUtil::IsNameStartChar(*p) ) { + return false; + } + const unsigned char* const start = p; + while ( XMLUtil::IsNameChar(*p) ) { + ++p; + } + if ( *p && *p != ' ' && *p != '\t' && *p != '\r' && *p != '\n' ) { + return false; + } + const bool reservedXML = p - start == 3 + && (start[0] == 'x' || start[0] == 'X') + && (start[1] == 'm' || start[1] == 'M') + && (start[2] == 'l' || start[2] == 'L'); + return !reservedXML; +} + + char* XMLNode::ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) { // This is a recursive method, but thinking about it "at the current level" @@ -1122,8 +1146,8 @@ char* XMLNode::ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) } const XMLDeclaration* const decl = node->ToDeclaration(); - if ( decl ) { - // Declarations are only allowed at document level + if ( decl && !IsProcessingInstruction(decl->Value()) ) { + // XML declarations are only allowed at document level // // Multiple declarations are allowed but all declarations // must occur before anything else. diff --git a/xmltest.cpp b/xmltest.cpp index db9df56c..5331485a 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -102,6 +102,54 @@ template< class T > bool XMLTest( const char* testString, T expected, T found, b } +class ProcessingInstructionVisitor : public XMLVisitor +{ +public: + ProcessingInstructionVisitor() : count( 0 ), value( 0 ) {} + + bool Visit( const XMLDeclaration& declaration ) override + { + ++count; + value = declaration.Value(); + return true; + } + + int count; + const char* value; +}; + + +static void TestProcessingInstruction( const char* value, const char* expectedValue, size_t locationCount ) +{ + const char* prefixes[] = { "", "?>", "?>", "?>" }; + const char* names[] = { "PI inside root", "PI after root", "PI after comment", "PI in prolog" }; + for ( size_t i = 0; i < locationCount; ++i ) { + char xml[512]; + TIXMLASSERT( strlen(prefixes[i]) + strlen(value) + strlen(suffixes[i]) < sizeof(xml) ); + strcpy( xml, prefixes[i] ); + strcat( xml, value ); + strcat( xml, suffixes[i] ); + XMLDocument doc; + if ( XMLTest( names[i], XML_SUCCESS, doc.Parse(xml) ) ) { + ProcessingInstructionVisitor visitor; + doc.Accept( &visitor ); + XMLTest( "PI keeps the XMLDeclaration visitor", 1, visitor.count ); + XMLTest( "PI target and data", expectedValue, visitor.value ); + XMLPrinter printer( 0, true ); + doc.Print( &printer ); + XMLDocument reparsed; + if ( XMLTest( "PI print and reparse", XML_SUCCESS, reparsed.Parse(printer.CStr()) ) ) { + ProcessingInstructionVisitor reparseVisitor; + reparsed.Accept( &reparseVisitor ); + XMLTest( "Reparsed PI keeps the XMLDeclaration visitor", 1, reparseVisitor.count ); + XMLTest( "Reparsed PI target and data", expectedValue, reparseVisitor.value ); + } + } + } +} + + void NullLineEndings( char* p ) { while( p && *p ) { @@ -2368,6 +2416,64 @@ int main( int argc, const char ** argv ) XMLTest("Test that declaration inside a child is not allowed", XML_ERROR_PARSING_DECLARATION, doc.ErrorID() ); } + { + // Processing instructions use XMLDeclaration nodes without its prolog-only restriction. + const char* values[] = { + "pi_target pi_content", + "pi_target", + "xml-stylesheet type=\"text/xsl\" href=\"style.xsl\"", + "xmlx data", + "xml:thing data", + "\xCF\x80 data", + "p_name-1.2:target data", + "pi data > more", + "pi\tdata", + "pi\ndata", + "pi\rdata" + }; + for ( size_t i = 0; i < sizeof(values) / sizeof(*values); ++i ) { + const char* expected = i == 10 ? "pi\ndata" : values[i]; + TestProcessingInstruction( values[i], expected, i == 0 ? 4 : 1 ); + } + } + + { + // Keep the original location checks for reserved and malformed targets. + const char* values[] = { + "xml", "xmL", "xMl", "xML", "Xml", "XmL", "XMl", "XML", + "", " pi data", "1target data", "pi=bad", "pi?bad", "pi\fdata", "pi\vdata" + }; + for ( size_t i = 0; i < sizeof(values) / sizeof(*values); ++i ) { + char xml[128]; + TIXMLASSERT( strlen(values[i]) + 24 < sizeof(xml) ); + strcpy( xml, "\n" ); + XMLDocument nested; + XMLTest( "Reserved or malformed PI target keeps location error", + XML_ERROR_PARSING_DECLARATION, nested.Parse(xml) ); + XMLTest( "PI location error keeps line number", 2, nested.ErrorLineNum() ); + + // These opaque prolog values were accepted before; do not add validation. + strcpy( xml, "" ); + XMLDocument prolog; + XMLTest( "Opaque prolog declaration handling is unchanged", XML_SUCCESS, prolog.Parse(xml) ); + } + } + + { + XMLDocument doc; + XMLElement* root = doc.NewElement( "root" ); + doc.InsertEndChild( root ); + root->InsertEndChild( doc.NewDeclaration("pi_target data > more") ); + XMLPrinter printer( 0, true ); + doc.Print( &printer ); + XMLDocument reparsed; + XMLTest( "Programmatic PI print and reparse", XML_SUCCESS, reparsed.Parse(printer.CStr()) ); + } + { // No matter - before or after successfully parsing a text - // calling XMLDocument::Value() used to cause an assert in debug.