Rewrote escape_some_html() slightly and enhanced the test.
All checks were successful
the build was successful

This commit is contained in:
tastytea 2019-04-22 11:16:44 +02:00
parent 6ff05f11ff
commit 067fba7447
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 9 additions and 8 deletions

View File

@ -62,6 +62,6 @@ const string get_repo(const string &url);
const string get_project(const string &url);
//! Escape some characters to named HTML entities.
const string escape_some_html(const string &html);
const string escape_some_html(string html);
#endif // GITEA2RSS_HPP

View File

@ -42,19 +42,19 @@ const string get_project(const string &url)
return repo.substr(repo.find('/') + 1);
}
const string escape_some_html(const string &html)
const string escape_some_html(string html)
{
string output = html;
const std::map<char, string> names =
const std::map<const char, const string> names =
{
{ '<', "&lt;" },
{ '>', "&gt;" }
};
for (auto &pair : names)
{
const std::regex re(string(1, pair.first));
output = std::regex_replace(output, re, pair.second);
html = std::regex_replace(html, re, pair.second);
}
return output;
return html;
}

View File

@ -25,11 +25,12 @@ SCENARIO ("escape_some_html() works as expected", "[strings]")
WHEN ("String with escape-able characters")
{
const string escaped
= escape_some_html("<Anna & Arthur are escaping HTML.>");
= escape_some_html("<p><small>.</small></p>");
THEN ("The HTML is escaped")
{
REQUIRE(escaped == "&lt;Anna & Arthur are escaping HTML.&gt;");
REQUIRE(escaped ==
"&lt;p&gt;&lt;small&gt;.&lt;/small&gt;&lt;/p&gt;");
}
}
}