Move XML escaping to a separate function

This commit is contained in:
Robbert van der Helm
2021-06-28 12:41:53 +02:00
parent fed7fb84c0
commit 388d9739a9
3 changed files with 40 additions and 27 deletions
+4 -27
View File
@@ -407,37 +407,14 @@ bool send_notification(const std::string& title, const std::string body) {
// I think there's a zero chance that we're going to call this function with
// anything that even somewhat resembles HTML, but we should still do a
// basic XML escape anyways.
// Implementation idea stolen from https://stackoverflow.com/a/5665377
std::string escaped_body{};
escaped_body.reserve(
static_cast<size_t>(static_cast<double>(body.size()) * 1.1));
for (const char& character : body) {
switch (character) {
case '&':
escaped_body.append("&amp;");
break;
case '\"':
escaped_body.append("&quot;");
break;
case '\'':
escaped_body.append("&apos;");
break;
case '<':
escaped_body.append("&lt;");
break;
case '>':
escaped_body.append("&gt;");
break;
default:
escaped_body.push_back(character);
break;
}
}
std::ostringstream formatted_body;
formatted_body << xml_escape(body);
try {
return bp::system(notify_send_path, "--urgency=normal",
"--expire-time=30000", "--app-name=yabridge", title,
escaped_body, bp::posix::use_vfork) == EXIT_SUCCESS;
formatted_body.str(),
bp::posix::use_vfork) == EXIT_SUCCESS;
} catch (const boost::process::process_error&) {
// We will have printed the message to the terminal anyways, so if the
// user doesn't have libnotify installed we'll just fail silently