Skip to content

Commit

Permalink
Use a more efficient escaping method
Browse files Browse the repository at this point in the history
  • Loading branch information
dantti authored and steveire committed Oct 23, 2020
1 parent aff2b0c commit b27333e
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions templates/lib/outputstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,28 @@ OutputStream::~OutputStream() {}

QString OutputStream::escape(const QString &input) const
{
auto temp = input;
temp.replace(QLatin1Char('&'), QStringLiteral("&"));
temp.replace(QLatin1Char('<'), QStringLiteral("&lt;"));
temp.replace(QLatin1Char('>'), QStringLiteral("&gt;"));
temp.replace(QLatin1Char('\''), QStringLiteral("&#39;"));
temp.replace(QLatin1Char('"'), QStringLiteral("&quot;"));
return temp;
// This could be replaced by QString::toHtmlEscaped()
// but atm it does not escape single quotes
QString rich;
const int len = input.length();
rich.reserve(int(len * 1.1));
for (int i = 0; i < len; ++i) {
const QChar ch = input.at(i);
if (ch == QLatin1Char('<'))
rich += QLatin1String("&lt;");
else if (ch == QLatin1Char('>'))
rich += QLatin1String("&gt;");
else if (ch == QLatin1Char('&'))
rich += QLatin1String("&amp;");
else if (ch == QLatin1Char('"'))
rich += QLatin1String("&quot;");
else if (ch == QLatin1Char('\''))
rich += QLatin1String("&#39;");
else
rich += ch;
}
rich.squeeze();
return rich;
}

QString OutputStream::escape(const Grantlee::SafeString &input) const
Expand Down

0 comments on commit b27333e

Please sign in to comment.