Skip to content

Commit

Permalink
Merge pull request #1024 from ma8ma/update-misc-html_escape
Browse files Browse the repository at this point in the history
Update misc html escape
  • Loading branch information
ma8ma committed Aug 6, 2022
2 parents 442e8d5 + 8226174 commit 48f03ae
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 22 deletions.
27 changes: 10 additions & 17 deletions src/jdlib/miscutil.cpp
Expand Up @@ -837,12 +837,13 @@ std::string MISC::regex_unescape( const std::string& str )
}


//
// HTMLエスケープ
//
// include_url : URL中でもエスケープする( デフォルト = true )
//
std::string MISC::html_escape( const std::string& str, const bool include_url )
/** @brief HTMLで特別な意味を持つ記号(& " < >)を文字実体参照へエスケープする
*
* @param[in] str エスケープする入力
* @param[in] completely URL中でもエスケープする( デフォルト = true )
* @return エスケープした結果
*/
std::string MISC::html_escape( const std::string& str, const bool completely )
{
if( str.empty() ) return str;

Expand All @@ -856,7 +857,7 @@ std::string MISC::html_escape( const std::string& str, const bool include_url )
char tmpchar = str.c_str()[ pos ];

// URL中はエスケープしない場合
if( ! include_url )
if( ! completely )
{
// URLとして扱うかどうか
// エスケープには影響がないので loose_url としておく
Expand All @@ -875,17 +876,9 @@ std::string MISC::html_escape( const std::string& str, const bool include_url )
}
}

// include_url = false でURL中ならエスケープしない
// completely = false でURL中ならエスケープしない
if( is_url ) str_out += tmpchar;
else if( tmpchar == '&' )
{
const int bufsize = 64;
char out_char[ bufsize ];
int n_in, n_out;
const int type = DBTREE::decode_char( str.c_str() + pos, n_in, out_char, n_out, false );
if( type == DBTREE::NODE_NONE ) str_out += "&amp;";
else str_out += tmpchar;
}
else if( tmpchar == '&' ) str_out += "&amp;";
else if( tmpchar == '\"' ) str_out += "&quot;";
else if( tmpchar == '<' ) str_out += "&lt;";
else if( tmpchar == '>' ) str_out += "&gt;";
Expand Down
6 changes: 3 additions & 3 deletions src/jdlib/miscutil.h
Expand Up @@ -138,9 +138,9 @@ namespace MISC
// 正規表現のメタ文字をアンエスケープ
std::string regex_unescape( const std::string& str );

// HTMLエスケープ
// include_url : URL中でもエスケープする( デフォルト = true )
std::string html_escape( const std::string& str, const bool include_url = true );
// HTMLで特別な意味を持つ記号(& " < >)を文字実体参照へエスケープする
// completely : URL中でもエスケープする( デフォルト = true )
std::string html_escape( const std::string& str, const bool completely = true );

// HTMLアンエスケープ
std::string html_unescape( const std::string& str );
Expand Down
10 changes: 8 additions & 2 deletions src/message/messageviewbase.cpp
Expand Up @@ -880,8 +880,14 @@ void MessageViewBase::slot_switch_page( Gtk::Widget*, guint page )
// URLを除外してエスケープ
const bool include_url = false;
std::string msg;
if( m_text_message ) msg = MISC::html_escape( m_text_message->get_text(), include_url );
msg = MISC::replace_str( msg, "\n", " <br> " );
if( m_text_message ) {
msg = m_text_message->get_text();

constexpr bool completely = true;
msg = MISC::chref_decode( msg, completely );
msg = MISC::html_escape( msg, include_url );
msg = MISC::replace_str( msg, "\n", " <br> " );
}

std::stringstream ss;

Expand Down
47 changes: 47 additions & 0 deletions test/gtest_jdlib_miscutil.cpp
Expand Up @@ -329,6 +329,53 @@ TEST_F(ReplaceNewlinesToStrTest, replace_crlf)
}


class HtmlEscapeTest : public ::testing::Test {};

TEST_F(HtmlEscapeTest, empty_data)
{
EXPECT_EQ( "", MISC::html_escape( "", false ) );
EXPECT_EQ( "", MISC::html_escape( "", true ) );
}

TEST_F(HtmlEscapeTest, not_escape)
{
EXPECT_EQ( "hello world", MISC::html_escape( "hello world", false ) );
EXPECT_EQ( "hello world", MISC::html_escape( "hello world", true ) );
}

TEST_F(HtmlEscapeTest, escape_amp)
{
EXPECT_EQ( "hello&amp;world", MISC::html_escape( "hello&world", false ) );
EXPECT_EQ( "hello&amp;world", MISC::html_escape( "hello&world", true ) );
}

TEST_F(HtmlEscapeTest, escape_quot)
{
EXPECT_EQ( "hello&quot;world", MISC::html_escape( "hello\"world", false ) );
EXPECT_EQ( "hello&quot;world", MISC::html_escape( "hello\"world", true ) );
}

TEST_F(HtmlEscapeTest, escape_lt)
{
EXPECT_EQ( "hello&lt;world", MISC::html_escape( "hello<world", false ) );
EXPECT_EQ( "hello&lt;world", MISC::html_escape( "hello<world", true ) );
}

TEST_F(HtmlEscapeTest, escape_gt)
{
EXPECT_EQ( "hello&gt;world", MISC::html_escape( "hello>world", false ) );
EXPECT_EQ( "hello&gt;world", MISC::html_escape( "hello>world", true ) );
}

TEST_F(HtmlEscapeTest, completely)
{
// URLを含むテキスト
const std::string input = "& https://foobar.test/?a=b&c=d& &";
EXPECT_EQ( "&amp; https://foobar.test/?a=b&amp;c=d&amp; &amp;", MISC::html_escape( input, true ) );
EXPECT_EQ( "&amp; https://foobar.test/?a=b&c=d& &amp;", MISC::html_escape( input, false ) );
}


class IsUrlSchemeTest : public ::testing::Test {};

TEST_F(IsUrlSchemeTest, url_none)
Expand Down

0 comments on commit 48f03ae

Please sign in to comment.