21#include "stringfilters.h"
25#include <QtCore/QRegularExpression>
26#include <QtCore/QVariant>
27#include <QtCore/QJsonArray>
28#include <QtCore/QJsonObject>
29#include <QtCore/QJsonDocument>
33 bool autoescape)
const
43 .replace(
QLatin1Char(
'\''), QStringLiteral(
"\\\'"));
49 bool autoescape)
const
54 if (safeString.get().isEmpty())
57 return QVariant(safeString.get().at(0).toUpper()
59 safeString.get().right(safeString.get().size() - 1)));
62EscapeJsFilter::EscapeJsFilter() {}
68 QStringLiteral(
"\\u005C"))
70 QStringLiteral(
"\\u0027"))
72 QStringLiteral(
"\\u0022"))
74 QStringLiteral(
"\\u003E"))
76 QStringLiteral(
"\\u003C"))
78 QStringLiteral(
"\\u0026"))
80 QStringLiteral(
"\\u003D"))
82 QStringLiteral(
"\\u002D"))
84 QStringLiteral(
"\\u003B"))
85 << std::pair<QString, QString>(
QChar(0x2028), QStringLiteral(
"\\u2028"))
86 << std::pair<QString, QString>(
QChar(0x2029),
87 QStringLiteral(
"\\u2029"));
89 for (
auto i = 0; i < 32; ++i) {
90 jsEscapes << std::pair<QString, QString>(
92 QStringLiteral(
"\\u00")
93 + QStringLiteral(
"%1").arg(i, 2, 16,
QLatin1Char(
'0')).toUpper());
100 bool autoescape)
const
106 static const auto jsEscapes = getJsEscapes();
108 for (
auto &
escape : jsEscapes) {
116 bool autoescape)
const
123 QStringLiteral(
"&(?!(\\w+|#\\d+);)"));
125 safeString.get().replace(fixAmpersandsRegexp, QStringLiteral(
"&"));
131 bool autoescape)
const
137 auto inputSafe = retString.isSafe();
139 retString.get().remove(argString);
148 bool autoescape)
const
157 bool autoescape)
const
161 auto lines = safeString.get().split(
QLatin1Char(
'\n'));
164 const auto shouldEscape = (autoescape && !safeString.isSafe());
165 for (
auto i = 0; i < lines.size(); ++i) {
167 = QStringLiteral(
"%1. %2")
176 bool autoescape)
const
185 bool autoescape)
const
191 else if (input.
userType() == qMetaTypeId<QVariantList>()) {
192 a = toString(input.
value<QVariantList>());
200 bool autoescape)
const
207 auto it = str.
begin();
208 const auto end = str.
end();
211 for (; it != end; ++it) {
216 toUpper = it->isSpace();
224 bool autoescape)
const
230 auto numWords = s.get().toInt(&ok);
237#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
243 if (words.size() > numWords) {
244 words = words.mid(0, numWords);
245 if (!words.at(words.size() - 1).endsWith(QStringLiteral(
"..."))) {
246 words << QStringLiteral(
"...");
253 bool autoescape)
const
262 bool autoescape)
const
271 bool autoescape)
const
279 bool autoescape)
const
287 bool autoescape)
const
291 const auto valueWidth = value.
size();
293 const auto totalPadding = width - valueWidth;
294 const auto rightPadding = totalPadding >> 1;
300 bool autoescape)
const
309 bool autoescape)
const
318 bool autoescape)
const
325 QStringLiteral(
"<%1(/?>|(\\s+[^>]*>))").arg(tagRe));
329 const auto safeInput = value.isSafe();
330 value.get().remove(startTag);
331 value.get().remove(endTag);
339 bool autoescape)
const
353 bool autoescape)
const
357 auto width = argument.
value<
int>();
358#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
363 if (partList.isEmpty())
365 auto output = partList.takeFirst();
366 auto pos = output.size() - output.lastIndexOf(
QLatin1Char(
'\n')) - 1;
367 Q_FOREACH (
const QString &part, partList) {
374 pos += lines.
first().size() + 1;
377 pos += lines.
last().size();
380 if (lines.
size() > 1)
381 pos += lines.
last().size();
390 bool autoescape)
const
417 bool autoescape)
const
422 if (input.
userType() == qMetaTypeId<QVariantList>()) {
423 const auto inputList = input.
value<QVariantList>();
424 for (
const auto &item : inputList) {
433 bool autoescape)
const
440 Q_FOREACH (
const QString &bit, inputString.get().
split(re)) {
441 auto _bit =
SafeString(bit, inputString.isSafe());
444 _bit.get().replace(
QLatin1Char(
'\n'), QStringLiteral(
"<br />"));
445 output.
append(QStringLiteral(
"<p>%1</p>").arg(_bit));
452 bool autoescape)
const
460 inputString.get().replace(
QLatin1Char(
'\n'), QStringLiteral(
"<br />")));
470 static const QChar asciiEndPoint(128);
471 for (; it != end; ++it)
472 if (*it < asciiEndPoint)
480 bool autoescape)
const
486 inputString = nofailStringToAscii(inputString);
487 inputString = inputString.
trimmed()
496 bool autoescape)
const
502 bool numberConvert =
true;
506 size = input.
toReal(&numberConvert);
507 if (!numberConvert) {
508 qWarning(
"%s",
"Failed to convert input file size into floating point value.");
512 if (!numberConvert) {
513 qWarning(
"%s",
"Failed to convert input file size into floating point value.");
519 qreal multiplier = 1.0f;
521 if (!arg.get().isEmpty()) {
522#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
527 const auto numArgs = argList.size();
529 unitSystem = argList.at(0).toInt(&numberConvert);
530 if (!numberConvert) {
531 qWarning(
"%s",
"Failed to convert filse size format unit system into integer. Falling back to default 10.");
537 precision = argList.at(1).toInt(&numberConvert);
538 if (!numberConvert) {
539 qWarning(
"%s",
"Failed to convert file size format decimal precision into integer. Falling back to default 2.");
545 multiplier = argList.at(2).toDouble(&numberConvert);
546 if (!numberConvert) {
547 qWarning(
"%s",
"Failed to convert file size format multiplier into double value. Falling back to default 1.0");
550 if (multiplier == 0.0f) {
551 qWarning(
"%s",
"It makes no sense to multiply the file size by zero. Using default value 1.0.");
558 const double sizeMult = size * multiplier;
560 if (unitSystem == 10) {
561 if ((sizeMult > -1000) && (sizeMult < 1000)) {
564 }
else if (unitSystem == 2) {
565 if ((sizeMult > - 1024) && (sizeMult < 1024)) {
570 const std::pair<qreal,QString> sizePair =
calcFileSize(size, unitSystem, multiplier);
585 if(retString.
length() < count)
return retString;
587 retString.
append(QStringLiteral(
"..."));
594 const int len = input.
length();
595 esc.
reserve(
static_cast<int>(len * 1.2));
596 for (
int i = 0; i < len; ++i) {
599 esc += QStringLiteral(
"\\\\u003E");
601 esc += QStringLiteral(
"\\\\u003C");
603 esc += QStringLiteral(
"\\\\u0026");
622#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
623 if (input.
userType() == qMetaTypeId<QJsonDocument>()) {
625 }
else if (input.
userType() == qMetaTypeId<QJsonObject>()) {
627 }
else if (input.
userType() == qMetaTypeId<QJsonArray>()) {
629 }
else if (input.
userType() == qMetaTypeId<QVariantHash>()) {
631 }
else if (input.
userType() == qMetaTypeId<QVariantMap>()) {
633 }
else if (input.
userType() == qMetaTypeId<QVariantList>()) {
635 }
else if (input.
userType() == qMetaTypeId<QStringList>()) {
638 qWarning(
"%s",
"Can not convert input data into QJsonObject or QJSonArray.");
649 qWarning(
"%s",
"Can not convert input data into QJsonObject or QJSonArray.");
655 jsonString = escapeJson(jsonString);
657 const QString scriptString = u
"<script id=\"" + arg + u
"\" type=\"application/json\">" + jsonString + u
"</script>";
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
SafeString escape(const QString &input) const
SafeString conditionalEscape(const SafeString &input) const
A QString wrapper class for containing whether a string is safe or needs to be escaped.
const NestedString & get() const
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument=QVariant(), bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument=QVariant(), bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
std::pair< qreal, QString > calcFileSize(qreal size, int unitSystem=10, qreal multiplier=1.0)
bool isSafeString(const QVariant &input)
Cutelee::SafeString getSafeString(const QVariant &input)
Cutelee::SafeString markSafe(const Cutelee::SafeString &input)
Cutelee::SafeString markForEscaping(const Cutelee::SafeString &input)
QJsonArray fromStringList(const QStringList &list)
QJsonArray fromVariantList(const QVariantList &list)
void setArray(const QJsonArray &array)
void setObject(const QJsonObject &object)
QByteArray toJson() const const
QJsonObject fromVariantHash(const QVariantHash &hash)
QJsonObject fromVariantMap(const QVariantMap &map)
void append(const T &value)
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString & append(QChar ch)
const QChar at(int position) const const
const_iterator constBegin() const const
const_iterator constEnd() const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QString fromUtf8(const char *str, int size)
QString leftJustified(int width, QChar fill, bool truncate) const const
QString normalized(NormalizationForm mode, QChar::UnicodeVersion version) const const
QString number(int n, int base)
QString & remove(int position, int n)
QString & replace(int position, int n, QChar after)
QString rightJustified(int width, QChar fill, bool truncate) const const
double toDouble(bool *ok) const const
int toInt(bool *ok, int base) const const
QString toLower() const const
QString toUpper() const const
QString trimmed() const const
void truncate(int position)
QString join(const QString &separator) const const
bool canConvert(int targetTypeId) const const
bool isNull() const const
bool isValid() const const
void setValue(const T &value)
double toDouble(bool *ok) const const
QHash< QString, QVariant > toHash() const const
QJsonArray toJsonArray() const const
QJsonDocument toJsonDocument() const const
QJsonObject toJsonObject() const const
QList< QVariant > toList() const const
QMap< QString, QVariant > toMap() const const
qreal toReal(bool *ok) const const
QString toString() const const
QStringList toStringList() const const
int userType() const const
Utility functions used throughout Cutelee.