Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Fixed GitHub Issue #70

Interleave XML entities are now encoded correctly
Commit
0b09d1c08979acad95356e7471257c2f02320aec
Author
Phil Nash ·
Parents
a162e22fa308

Files · Download .tar.gz

Changes

 .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, "<", "&lt;" ) &&
-               !findReplaceableString( text, "&", "&amp;" ) &&
-               !findReplaceableString( text, "\"", "&quot;" ) )
+            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() << "&lt;";
+                        break;
+                    case '&':
+                        stream() << "&amp;";
+                        break;
+                    case '\"':
+                        stream() << "&quot;";
+                        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", "<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
+    }
+}