Fixed GitHub Issue #70

Interleave XML entities are now encoded correctly
This commit is contained in:
Phil Nash 2012-02-10 18:58:06 +00:00
parent a162e22fa3
commit 0b09d1c089
3 changed files with 36 additions and 25 deletions

3
.gitignore vendored
View File

@ -8,4 +8,5 @@ Release
*.user *.user
*.xcuserstate *.xcuserstate
.DS_Store .DS_Store
xcuserdata xcuserdata
CatchSelfTest.xcscheme

View File

@ -294,32 +294,30 @@ namespace Catch
const std::string& text const std::string& text
) )
{ {
// !TBD finish this static const char* charsToEncode = "<&\"";
if( !findReplaceableString( text, "<", "&lt;" ) && std::string mtext = text;
!findReplaceableString( text, "&", "&amp;" ) && std::string::size_type pos = mtext.find_first_of( charsToEncode );
!findReplaceableString( text, "\"", "&quot;" ) ) while( pos != std::string::npos )
{ {
stream() << text; stream() << mtext.substr( 0, pos );
switch( mtext[pos] )
{
case '<':
stream() << "&lt;";
break;
case '&':
stream() << "&amp;";
break;
case '\"':
stream() << "&quot;";
break;
}
mtext = mtext.substr( pos+1 );
pos = mtext.find_first_of( charsToEncode );
} }
} stream() << mtext;
}
///////////////////////////////////////////////////////////////////////
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;
}
bool m_tagIsOpen; bool m_tagIsOpen;
bool m_needsNewline; bool m_needsNewline;

View File

@ -157,3 +157,15 @@ TEST_CASE( "./failing/checkedelse", "" )
{ {
REQUIRE( testCheckedElse( false ) ); REQUIRE( testCheckedElse( false ) );
} }
TEST_CASE( "./misc/xmlentitycheck", "" )
{
SECTION( "embedded xml", "<test>it should be possible to embed xml characters, such as <, \" or &, or even whole <xml>documents</xml> within an attribute</test>" )
{
// No test
}
SECTION( "encoded chars", "these should all be encoded: &&&\"\"\"<<<&\"<<&\"" )
{
// No test
}
}