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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
// Copyright 2017-2021 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 <QBuffer>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QtCore>
#include <QtTest>
#include "XMLTools.h"
class TestXMLTools : public QObject {
Q_OBJECT
public:
TestXMLTools(QObject *parent = 0);
private slots:
void testParseExtractBody();
void testParseConvertStyleBold();
void testParseConvertStyleUnderline();
void testParseConvertStyleItalic();
void testParseKeepTags();
void testParseDropEmptyFirstParagraphs();
void testUndupNoopB();
void testUndupNoopU();
void testUndupNoopI();
void testUndupNoopA();
void testUndupSimpleB();
void testUndupSimpleU();
void testUndupSimpleI();
void testUndupSimpleA();
void testUndupMixedBU();
void testUndupMixedBUIA();
void testUndupWithSpace();
void testUndupAnchor();
void testUndupLinkSameHref();
void testUndupLinkDifferingHref();
void testUndupNoRootUndupTag();
void testUndupUndupTagsRemoval();
void testUndupStandard();
// void testUndupInvalidXml1();
private:
QString doParse(QString input);
QString doUnduplicate(QString input);
};
TestXMLTools::TestXMLTools(QObject *parent) : QObject(parent) {
}
QString TestXMLTools::doParse(QString input) {
QXmlStreamReader reader(input);
QBuffer outBuf;
QXmlStreamWriter writerOut(&outBuf);
outBuf.open(QIODevice::WriteOnly);
int paragraphs = 0;
QMap< QString, QString > pstyle;
XMLTools::recurseParse(reader, writerOut, paragraphs, pstyle);
outBuf.close();
return QString(outBuf.data());
}
void TestXMLTools::testParseExtractBody() {
QCOMPARE(doParse(QLatin1String("<html><head></head><body>one</body></html>")), QLatin1String("one"));
QCOMPARE(doParse(QLatin1String("<!DOCTYPE invalid><html><head></head><body>one</body></html>")),
QLatin1String("one"));
QCOMPARE(doParse(QLatin1String("<html><head><title>ttt</title></head><body>one</body></html>")),
QLatin1String("one"));
QCOMPARE(doParse(QLatin1String("<html><head></head><body><b>one</b></body></html>")), QLatin1String("<b>one</b>"));
}
void TestXMLTools::testParseConvertStyleBold() {
// The numeric values for font-weight provide a more granular option to specify boldness.
// On fonts with two boldness levels (normal and bold) this is fine (100-500, 600-900).
// For fonts wiht more boldness levels, the conversion we do here from a weight value to a <b> tag is problematic.
// http://doc.qt.io/qt-4.8/stylesheet-reference.html#list-of-property-types
// https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight
QCOMPARE(
doParse(QLatin1String("<html><head></head><body><span style=\"font-weight:100\">one</span></body></html>")),
QLatin1String("<span style=\"font-weight:100\">one</span>"));
QCOMPARE(
doParse(QLatin1String("<html><head></head><body><span style=\"font-weight:100;\">one</span></body></html>")),
QLatin1String("<span style=\"font-weight:100\">one</span>"));
QCOMPARE(
doParse(QLatin1String("<html><head></head><body><span style=\"font-weight:200;\">one</span></body></html>")),
QLatin1String("<span style=\"font-weight:200\">one</span>"));
QCOMPARE(
doParse(QLatin1String("<html><head></head><body><span style=\"font-weight:300;\">one</span></body></html>")),
QLatin1String("<span style=\"font-weight:300\">one</span>"));
QCOMPARE(
doParse(QLatin1String("<html><head></head><body><span style=\"font-weight:400;\">one</span></body></html>")),
QLatin1String("<span style=\"font-weight:400\">one</span>"));
QCOMPARE(
doParse(QLatin1String("<html><head></head><body><span style=\"font-weight:500;\">one</span></body></html>")),
QLatin1String("<span style=\"font-weight:500\">one</span>"));
QCOMPARE(
doParse(QLatin1String("<html><head></head><body><span style=\"font-weight:600\">one</span></body></html>")),
QLatin1String("<b>one</b>"));
QCOMPARE(
doParse(QLatin1String("<html><head></head><body><span style=\"font-weight:600;\">one</span></body></html>")),
QLatin1String("<b>one</b>"));
QCOMPARE(
doParse(QLatin1String("<html><head></head><body><span style=\"font-weight: 600;\">one</span></body></html>")),
QLatin1String("<b>one</b>"));
QCOMPARE(
doParse(QLatin1String("<html><head></head><body><span style=\"font-weight: 600\">one</span></body></html>")),
QLatin1String("<b>one</b>"));
// These currently fail
// QCOMPARE(doParse(QLatin1String("<html><head></head><body><span
//style=\"font-weight:700;\">one</span></body></html>")), QLatin1String("<b>one</b>"));
// QCOMPARE(doParse(QLatin1String("<html><head></head><body><span
//style=\"font-weight:800;\">one</span></body></html>")), QLatin1String("<b>one</b>"));
// QCOMPARE(doParse(QLatin1String("<html><head></head><body><span
//style=\"font-weight:900;\">one</span></body></html>")), QLatin1String("<b>one</b>"));
// QCOMPARE(doParse(QLatin1String("<html><head></head><body><span
//style=\"font-weight:bold;\">one</span></body></html>")), QLatin1String("<b>one</b>"));
}
void TestXMLTools::testParseConvertStyleUnderline() {
QCOMPARE(doParse(QLatin1String(
"<html><head></head><body><span style=\"text-decoration:underline\">one</span></body></html>")),
QLatin1String("<u>one</u>"));
// We only convert spans, so this will not create a u tag
QCOMPARE(
doParse(QLatin1String("<html><head></head><body><b style=\"text-decoration:underline\">one</b></body></html>")),
QLatin1String("<b style=\"text-decoration:underline\">one</b>"));
}
void TestXMLTools::testParseConvertStyleItalic() {
QCOMPARE(
doParse(QLatin1String("<html><head></head><body><span style=\"font-style:italic\">one</span></body></html>")),
QLatin1String("<i>one</i>"));
}
void TestXMLTools::testParseKeepTags() {
QCOMPARE(doParse(QLatin1String("<html><head></head><body><b>one</b><i>two</i><u>three</u></body></html>")),
QLatin1String("<b>one</b><i>two</i><u>three</u>"));
QCOMPARE(doParse(QLatin1String("<html><head></head><body><i><b><u>one</u></b></i></body></html>")),
QLatin1String("<i><b><u>one</u></b></i>"));
}
void TestXMLTools::testParseDropEmptyFirstParagraphs() {
QCOMPARE(doParse(QLatin1String("<html><head></head><body><p></p><p>one</p></body></html>")),
QLatin1String("<p>one</p>"));
QCOMPARE(doParse(QLatin1String("<html><head></head><body><p></p><p>one</p><p></p></body></html>")),
QLatin1String("<p>one</p><p/>"));
QCOMPARE(doParse(QLatin1String("<html><head></head><body><p></p><p></p><p>one</p><p></p></body></html>")),
QLatin1String("<p/><p>one</p><p/>"));
QCOMPARE(doParse(QLatin1String("<html><head></head><body><p></p><div>one</div></body></html>")),
QLatin1String("<div>one</div>"));
// <br> would be invalid XML. It is not dropped "correctly".
// QCOMPARE(doParse(QLatin1String("<html><head></head><body><p><br></p><p>one</p></body></html>")),
//QLatin1String("<p>one</p>"));
// This is different from what is expected from a comment in the method being tested. It says
// contents of empty paragraph would be dropped as well, but the br/space persists.
QCOMPARE(doParse(QLatin1String("<html><head></head><body><p><br/></p><p>one</p></body></html>")),
QLatin1String("<br/><p>one</p>"));
QCOMPARE(doParse(QLatin1String("<html><head></head><body><p><br /></p><p>one</p></body></html>")),
QLatin1String("<br/><p>one</p>"));
QCOMPARE(doParse(QLatin1String("<html><head></head><body><p> </p><p>one</p></body></html>")),
QLatin1String(" <p>one</p>"));
}
QString TestXMLTools::doUnduplicate(QString input) {
QXmlStreamReader reader(input);
QBuffer outBuf;
QXmlStreamWriter writerOut(&outBuf);
outBuf.open(QIODevice::WriteOnly);
XMLTools::unduplicateTags(reader, writerOut);
outBuf.close();
return QString(outBuf.data());
}
void TestXMLTools::testUndupNoopB() {
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><b>test</b></unduplicate>")), QLatin1String("<b>test</b>"));
}
void TestXMLTools::testUndupNoopU() {
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><u>test</u></unduplicate>")), QLatin1String("<u>test</u>"));
}
void TestXMLTools::testUndupNoopI() {
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><i>test</i></unduplicate>")), QLatin1String("<i>test</i>"));
}
void TestXMLTools::testUndupNoopA() {
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><a>test</a></unduplicate>")), QLatin1String("<a>test</a>"));
}
void TestXMLTools::testUndupSimpleB() {
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><b>one</b><b>two</b></unduplicate>")),
QLatin1String("<b>onetwo</b>"));
}
void TestXMLTools::testUndupSimpleU() {
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><u>one</u><u>two</u></unduplicate>")),
QLatin1String("<u>onetwo</u>"));
}
void TestXMLTools::testUndupSimpleI() {
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><i>one</i><i>two</i></unduplicate>")),
QLatin1String("<i>onetwo</i>"));
}
void TestXMLTools::testUndupSimpleA() {
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><a>one</a><a>two</a></unduplicate>")),
QLatin1String("<a>onetwo</a>"));
}
void TestXMLTools::testUndupWithSpace() {
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><b>one</b><b> two</b></unduplicate>")),
QLatin1String("<b>one two</b>"));
}
void TestXMLTools::testUndupMixedBU() {
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><b>one</b><u>two</u></unduplicate>")),
QLatin1String("<b>one</b><u>two</u>"));
}
void TestXMLTools::testUndupMixedBUIA() {
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><a><i><b>one</b><u>two</u></i></a></unduplicate>")),
QLatin1String("<a><i><b>one</b><u>two</u></i></a>"));
}
void TestXMLTools::testUndupAnchor() {
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><a>one</a><a>two</a></unduplicate>")),
QLatin1String("<a>onetwo</a>"));
}
void TestXMLTools::testUndupLinkSameHref() {
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><a href=\"#\">one</a><a href=\"#\">two</a></unduplicate>")),
QLatin1String("<a href=\"#\">onetwo</a>"));
QCOMPARE(
doUnduplicate(QLatin1String(
"<unduplicate><a href=\"http://example.org\">one</a><a href=\"http://example.org\">two</a></unduplicate>")),
QLatin1String("<a href=\"http://example.org\">onetwo</a>"));
}
void TestXMLTools::testUndupLinkDifferingHref() {
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><a href=\"#\">one</a><a>two</a></unduplicate>")),
QLatin1String("<a href=\"#\">one</a><a>two</a>"));
QCOMPARE(
doUnduplicate(QLatin1String("<unduplicate><a href=\"http://example.org\">one</a><a>two</a></unduplicate>")),
QLatin1String("<a href=\"http://example.org\">one</a><a>two</a>"));
QCOMPARE(
doUnduplicate(QLatin1String("<unduplicate><a>one</a><a href=\"http://example.org\">two</a></unduplicate>")),
QLatin1String("<a>one</a><a href=\"http://example.org\">two</a>"));
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><a href=\"http://example.org/path1\">one</a><a "
"href=\"http://example.org/path2\">two</a></unduplicate>")),
QLatin1String("<a href=\"http://example.org/path1\">one</a><a href=\"http://example.org/path2\">two</a>"));
}
void TestXMLTools::testUndupNoRootUndupTag() {
QCOMPARE(doUnduplicate(QLatin1String("<u><b>one</b><b>two</b></u>")), QLatin1String("<u><b>onetwo</b></u>"));
QCOMPARE(doUnduplicate(QLatin1String("<i><b>one</b><b>two</b></i>")), QLatin1String("<i><b>onetwo</b></i>"));
}
void TestXMLTools::testUndupUndupTagsRemoval() {
QCOMPARE(
doUnduplicate(QLatin1String(
"<unduplicate><b>one</b><unduplicate><unduplicate></unduplicate><b>two</b></unduplicate></unduplicate>")),
QLatin1String("<b>onetwo</b>"));
}
void TestXMLTools::testUndupStandard() {
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><b>bold with </b><b><i>italic</i></b></unduplicate>")),
QLatin1String("<b>bold with <i>italic</i></b>"));
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><b>bold with </b><b><i></i><i>italic</i></b></unduplicate>")),
QLatin1String("<b>bold with <i>italic</i></b>"));
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><b>test</b></unduplicate>")), QLatin1String("<b>test</b>"));
QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><b>bold with </b><b><i></i><i>italic</i></b></unduplicate>")),
QLatin1String("<b>bold with <i>italic</i></b>"));
}
// Will warn and fail.
// void TestXMLTools::testUndupInvalidXml1() {
// QCOMPARE(doUnduplicate(QLatin1String("<unduplicate><b>bold with <i></b>italic</i></unduplicate>")),
//QLatin1String("<b>bold with <i>italic</i></b>"));
//}
QTEST_MAIN(TestXMLTools)
#include "TestXMLTools.moc"
|