blob: cb7724a74756ae20a5bf68889624f2b85bcdeffe (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
// Copyright 2005-2020 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.
#include "HTMLFilter.h"
#include <QtCore/QXmlStreamReader>
QString HTMLFilter::escapeTags(const QString &in) {
QString out;
for (int i = 0; i < in.size(); i++) {
if (in.at(i) == QLatin1Char('<')) {
out += QLatin1String("<");
} else if (in.at(i) == QLatin1Char('>')) {
out += QLatin1String(">");
} else {
out += in.at(i);
}
}
return out;
}
bool HTMLFilter::filter(const QString &in, QString &out) {
if (!in.contains(QLatin1Char('<'))) {
out = in.simplified();
} else {
QXmlStreamReader qxsr(QString::fromLatin1("<document>%1</document>").arg(in));
QString qs;
while (!qxsr.atEnd()) {
switch (qxsr.readNext()) {
case QXmlStreamReader::Invalid:
return false;
case QXmlStreamReader::Characters:
qs += qxsr.text();
break;
case QXmlStreamReader::EndElement:
if ((qxsr.name() == QLatin1String("br")) || (qxsr.name() == QLatin1String("p")))
qs += QLatin1Char('\n');
break;
default:
break;
}
}
out = escapeTags(qs.simplified());
}
return true;
}
|