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