From 0b09d1c08979acad95356e7471257c2f02320aec Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Fri, 10 Feb 2012 18:58:06 +0000 Subject: [PATCH] Fixed GitHub Issue #70 Interleave XML entities are now encoded correctly --- .gitignore | 3 +- include/internal/catch_xmlwriter.hpp | 46 +++++++++++++--------------- projects/SelfTest/MiscTests.cpp | 12 ++++++++ 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 9c61780b..835e80a5 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ Release *.user *.xcuserstate .DS_Store -xcuserdata \ No newline at end of file +xcuserdata +CatchSelfTest.xcscheme \ No newline at end of file diff --git a/include/internal/catch_xmlwriter.hpp b/include/internal/catch_xmlwriter.hpp index 577e1774..593c069b 100644 --- a/include/internal/catch_xmlwriter.hpp +++ b/include/internal/catch_xmlwriter.hpp @@ -294,32 +294,30 @@ namespace Catch const std::string& text ) { - // !TBD finish this - if( !findReplaceableString( text, "<", "<" ) && - !findReplaceableString( text, "&", "&" ) && - !findReplaceableString( text, "\"", """ ) ) + static const char* charsToEncode = "<&\""; + std::string mtext = text; + std::string::size_type pos = mtext.find_first_of( charsToEncode ); + while( pos != std::string::npos ) { - stream() << text; + stream() << mtext.substr( 0, pos ); + + switch( mtext[pos] ) + { + case '<': + stream() << "<"; + break; + case '&': + stream() << "&"; + break; + case '\"': + stream() << """; + break; + } + mtext = mtext.substr( pos+1 ); + pos = mtext.find_first_of( charsToEncode ); } - } - - /////////////////////////////////////////////////////////////////////// - bool findReplaceableString - ( - const std::string& text, - const std::string& replaceWhat, - const std::string& replaceWith - ) - { - std::string::size_type pos = text.find_first_of( replaceWhat ); - if( pos != std::string::npos ) - { - stream() << text.substr( 0, pos ) << replaceWith; - writeEncodedText( text.substr( pos+1 ) ); - return true; - } - return false; - } + stream() << mtext; + } bool m_tagIsOpen; bool m_needsNewline; diff --git a/projects/SelfTest/MiscTests.cpp b/projects/SelfTest/MiscTests.cpp index 60a57350..e509b4a1 100644 --- a/projects/SelfTest/MiscTests.cpp +++ b/projects/SelfTest/MiscTests.cpp @@ -157,3 +157,15 @@ TEST_CASE( "./failing/checkedelse", "" ) { REQUIRE( testCheckedElse( false ) ); } + +TEST_CASE( "./misc/xmlentitycheck", "" ) +{ + SECTION( "embedded xml", "it should be possible to embed xml characters, such as <, \" or &, or even whole documents within an attribute" ) + { + // No test + } + SECTION( "encoded chars", "these should all be encoded: &&&\"\"\"<<<&\"<<&\"" ) + { + // No test + } +}