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 + } +}