Skip to content

Commit bffe99d

Browse files
feat(json): add escapeForHtml parameter to jsonEscape function
1 parent 791dc3f commit bffe99d

3 files changed

Lines changed: 20 additions & 6 deletions

File tree

include/nfx/detail/string/Utils.inl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,7 @@ namespace nfx::string
15091509
}
15101510
} // namespace detail
15111511

1512-
inline std::string jsonEscape( std::string_view str, bool escapeNonAscii )
1512+
inline std::string jsonEscape( std::string_view str, bool escapeNonAscii, bool escapeForHtml )
15131513
{
15141514
std::string result;
15151515
result.reserve( str.size() * 2 ); // Reserve for common case
@@ -1544,7 +1544,14 @@ namespace nfx::string
15441544
result += "\\\\";
15451545
break;
15461546
case '/':
1547-
result += "\\/";
1547+
if ( escapeForHtml )
1548+
{
1549+
result += "\\/";
1550+
}
1551+
else
1552+
{
1553+
result += '/';
1554+
}
15481555
break;
15491556
case '\b':
15501557
result += "\\b";

include/nfx/string/Utils.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,14 +1072,16 @@ namespace nfx::string
10721072
* @brief Escape string for use in JSON (RFC 8259)
10731073
* @param str String to escape
10741074
* @param escapeNonAscii If true, encode non-ASCII UTF-8 sequences as \\uXXXX escape sequences (default: false)
1075+
* @param escapeForHtml If true, escape forward slashes to prevent script/style tag issues in HTML (default: false)
10751076
* @return JSON-escaped string with special characters properly escaped
1076-
* @details Escapes: quote, backslash, slash, backspace, form-feed, newline, carriage-return, tab
1077+
* @details Escapes: quote, backslash, backspace, form-feed, newline, carriage-return, tab
10771078
* and control characters (U+0000 to U+001F) as \\uXXXX Unicode escape sequences.
10781079
* When escapeNonAscii is true, also converts UTF-8 encoded characters to \\uXXXX format.
1080+
* When escapeForHtml is true, also escapes forward slashes (/) as \\/ for safe HTML embedding.
10791081
* This function allocates a new std::string.
10801082
* @note This function is marked [[nodiscard]] - the return value should not be ignored
10811083
*/
1082-
[[nodiscard]] inline std::string jsonEscape( std::string_view str, bool escapeNonAscii = false );
1084+
[[nodiscard]] inline std::string jsonEscape( std::string_view str, bool escapeNonAscii = false, bool escapeForHtml = false );
10831085

10841086
/**
10851087
* @brief Unescape JSON string literal (RFC 8259)

test/TESTS_EscapeUnescape.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ namespace nfx::string::test
5252
EXPECT_EQ( "\\\\", jsonEscape( "\\" ) );
5353
EXPECT_EQ( "path\\\\to\\\\file", jsonEscape( "path\\to\\file" ) );
5454

55-
// Forward slash escaping
56-
EXPECT_EQ( "path\\/to\\/file", jsonEscape( "path/to/file" ) );
55+
// Forward slash - NOT escaped by default (RFC 8259: optional)
56+
EXPECT_EQ( "path/to/file", jsonEscape( "path/to/file" ) );
57+
58+
// Forward slash - escaped when escapeForHtml is true (for HTML safety)
59+
EXPECT_EQ( "path\\/to\\/file", jsonEscape( "path/to/file", false, true ) );
60+
EXPECT_EQ( "<\\/script>", jsonEscape( "</script>", false, true ) );
61+
EXPECT_EQ( "<\\/style>", jsonEscape( "</style>", false, true ) );
5762
}
5863

5964
TEST( EscapeUnescape, JSON_ControlCharacters )

0 commit comments

Comments
 (0)