1 /*
2 SPDX-FileCopyrightText: 2020-2021 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "texthtmlbuildertest.h"
8 #include "grantleebuilder/markupdirector.h"
9 #include "grantleebuilder/texthtmlbuilder.h"
10 #include <QRegularExpression>
11 #include <QTest>
12 #include <QTextDocument>
QTEST_MAIN(TextHTMLBuilderTest)13 QTEST_MAIN(TextHTMLBuilderTest)
14 TextHTMLBuilderTest::TextHTMLBuilderTest(QObject *parent)
15 : QObject(parent)
16 {
17 }
18
testHtmlWithTab()19 void TextHTMLBuilderTest::testHtmlWithTab()
20 {
21 auto doc = new QTextDocument(this);
22 QTextCursor cursor(doc);
23 cursor.movePosition(QTextCursor::Start);
24 cursor.insertText(QStringLiteral("\n"));
25 cursor.insertText(QStringLiteral("\t"));
26 cursor.insertText(QStringLiteral("foo"));
27
28 auto hb = new KPIMTextEdit::TextHTMLBuilder();
29 auto md = new KPIMTextEdit::MarkupDirector(hb);
30 md->processDocument(doc);
31 auto result = hb->getResult();
32
33 auto regex =
34 QRegularExpression(QStringLiteral("^<br /><p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\"> foo</p>\\n$"));
35
36 const bool regexpHasResult = regex.match(result).hasMatch();
37 if (!regexpHasResult) {
38 qDebug() << " result found " << result;
39 }
40 QVERIFY(regexpHasResult);
41 delete md;
42 delete hb;
43 delete doc;
44 }
45
testHtmlText_data()46 void TextHTMLBuilderTest::testHtmlText_data()
47 {
48 QTest::addColumn<QString>("text");
49 QTest::addColumn<QString>("regexpText");
50 QTest::addColumn<bool>("htmlFormat");
51 QTest::newRow("link")
52 << QStringLiteral("A <a href=\"http://www.kde.org\">link</a> to KDE.")
53 << QStringLiteral(
54 "^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">A <a href=\"http://www.kde.org\">link</a> to KDE.</p>\\n$")
55 << true;
56 QTest::newRow("text with space")
57 << QStringLiteral(" foo")
58 << QStringLiteral(
59 "^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\"> foo</p>\\n$")
60 << false;
61 QTest::newRow("text with leading space") << QStringLiteral(" foo")
62 << QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\"> foo</p>\\n$")
63 << false;
64 }
65
testHtmlText()66 void TextHTMLBuilderTest::testHtmlText()
67 {
68 QFETCH(QString, text);
69 QFETCH(QString, regexpText);
70 QFETCH(bool, htmlFormat);
71
72 auto doc = new QTextDocument();
73 if (htmlFormat) {
74 doc->setHtml(text);
75 } else {
76 doc->setPlainText(text);
77 }
78
79 auto hb = new KPIMTextEdit::TextHTMLBuilder();
80 auto md = new KPIMTextEdit::MarkupDirector(hb);
81 md->processDocument(doc);
82 const auto result = hb->getResult();
83
84 const QRegularExpression regex(regexpText);
85
86 const bool regexpHasResult = regex.match(result).hasMatch();
87 if (!regexpHasResult) {
88 qDebug() << " result found " << result;
89 }
90 QVERIFY(regexpHasResult);
91 delete md;
92 delete hb;
93 delete doc;
94 }
95
testSingleFormat()96 void TextHTMLBuilderTest::testSingleFormat()
97 {
98 auto doc = new QTextDocument();
99
100 // One format
101 doc->setHtml(QStringLiteral("This <b>text</b> is bold."));
102
103 auto hb = new KPIMTextEdit::TextHTMLBuilder();
104 auto md = new KPIMTextEdit::MarkupDirector(hb);
105 md->processDocument(doc);
106 auto result = hb->getResult();
107 QRegularExpression regex(
108 QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">This <strong>text</strong> is bold.</p>\\n$"));
109
110 QVERIFY(regex.match(result).hasMatch());
111 delete md;
112 delete hb;
113 delete doc;
114 }
115
testDoubleFormat()116 void TextHTMLBuilderTest::testDoubleFormat()
117 {
118 auto doc = new QTextDocument();
119
120 // One format
121 doc->setHtml(QStringLiteral("Some <b><i>formatted</i></b> text."));
122
123 auto hb = new KPIMTextEdit::TextHTMLBuilder();
124 auto md = new KPIMTextEdit::MarkupDirector(hb);
125 md->processDocument(doc);
126 auto result = hb->getResult();
127 QRegularExpression regex(
128 QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Some "
129 "(<strong><em>|<em><strong>)formatted(</em></strong>|</"
130 "strong></em>) text.</p>\\n$"));
131
132 QVERIFY(regex.match(result).hasMatch());
133 delete md;
134 delete hb;
135 delete doc;
136 }
137
testAnchor()138 void TextHTMLBuilderTest::testAnchor()
139 {
140 auto doc = new QTextDocument();
141 doc->setHtml(QStringLiteral("A <a href=\"http://www.kde.org\">link</a> to KDE."));
142
143 auto hb = new KPIMTextEdit::TextHTMLBuilder();
144 auto md = new KPIMTextEdit::MarkupDirector(hb);
145 md->processDocument(doc);
146 auto result = hb->getResult();
147
148 QRegularExpression regex(QStringLiteral(
149 "^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">A <a href=\"http://www.kde.org\">link</a> to KDE.</p>\\n$"));
150
151 QVERIFY(regex.match(result).hasMatch());
152 delete md;
153 delete hb;
154 delete doc;
155 }
156
testAnchorWithFormattedContent()157 void TextHTMLBuilderTest::testAnchorWithFormattedContent()
158 {
159 auto doc = new QTextDocument();
160 doc->setHtml(QStringLiteral("A <a href=\"http://www.kde.org\"><b>formatted</b> link</a> to KDE."));
161
162 auto hb = new KPIMTextEdit::TextHTMLBuilder();
163 auto md = new KPIMTextEdit::MarkupDirector(hb);
164 md->processDocument(doc);
165 auto result = hb->getResult();
166
167 QRegularExpression regex(QStringLiteral(
168 "^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">A <a href=\"http://www.kde.org\"><strong>formatted</strong> "
169 "link</a> to KDE.</p>\\n$"));
170
171 QVERIFY(regex.match(result).hasMatch());
172 delete md;
173 delete hb;
174 delete doc;
175 }
176
testAdjacentAnchors()177 void TextHTMLBuilderTest::testAdjacentAnchors()
178 {
179 auto doc = new QTextDocument();
180 doc->setHtml(
181 QStringLiteral("Two <a href=\"http://www.kde.org\">links</a><a "
182 "href=\"http://www.google.com\">next</a> to each other."));
183
184 auto hb = new KPIMTextEdit::TextHTMLBuilder();
185 auto md = new KPIMTextEdit::MarkupDirector(hb);
186 md->processDocument(doc);
187 auto result = hb->getResult();
188
189 QRegularExpression regex(
190 QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Two <a href=\"http://www.kde.org\">links</a><a "
191 "href=\"http://www.google.com\">next</a> to each other.</p>\\n$"));
192
193 QVERIFY(regex.match(result).hasMatch());
194 delete md;
195 delete hb;
196 delete doc;
197 }
198
testNestedFormatting()199 void TextHTMLBuilderTest::testNestedFormatting()
200 {
201 auto doc = new QTextDocument();
202 doc->setHtml(QStringLiteral("This <b>text is <i>italic</i> and</b> bold."));
203
204 auto hb = new KPIMTextEdit::TextHTMLBuilder();
205 auto md = new KPIMTextEdit::MarkupDirector(hb);
206 md->processDocument(doc);
207 auto result = hb->getResult();
208
209 QRegularExpression regex(QStringLiteral(
210 "^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">This <strong>text is <em>italic</em> and</strong> bold.</p>\\n$"));
211
212 QVERIFY(regex.match(result).hasMatch());
213 delete md;
214 delete hb;
215 delete doc;
216 }
217
testSpan()218 void TextHTMLBuilderTest::testSpan()
219 {
220 auto doc = new QTextDocument();
221 doc->setHtml(QStringLiteral("Some <span style=\"color:#ff0000;\">formatted</span> text."));
222
223 auto hb = new KPIMTextEdit::TextHTMLBuilder();
224 auto md = new KPIMTextEdit::MarkupDirector(hb);
225 md->processDocument(doc);
226 auto result = hb->getResult();
227
228 auto regex = QRegularExpression(
229 QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Some <span style=\"color:#ff0000;\">formatted</span> "
230 "text.</p>\\n$"));
231
232 QVERIFY(regex.match(result).hasMatch());
233 delete md;
234 delete hb;
235 delete doc;
236 }
237
testDoubleSpan()238 void TextHTMLBuilderTest::testDoubleSpan()
239 {
240 auto doc = new QTextDocument();
241 doc->setHtml(
242 QStringLiteral("Some <span "
243 "style=\"color:#ff0000;background-color:#00ff00;"
244 "\">formatted</span> text."));
245
246 auto hb = new KPIMTextEdit::TextHTMLBuilder();
247 auto md = new KPIMTextEdit::MarkupDirector(hb);
248 md->processDocument(doc);
249 auto result = hb->getResult();
250
251 auto regex =
252 QRegularExpression(QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Some <span "
253 "style=\"(color:#ff0000|background-color:#00ff00);\"><span "
254 "style=\"(color:#ff0000|background-color:#00ff00);\">formatted</span></"
255 "span> text.</p>\\n$"));
256
257 QVERIFY(regex.match(result).hasMatch());
258 delete md;
259 delete hb;
260 delete doc;
261 }
262
testSpanNesting()263 void TextHTMLBuilderTest::testSpanNesting()
264 {
265 auto doc = new QTextDocument();
266 doc->setHtml(
267 QStringLiteral("Paragraph <span style=\"background-color:#00ff00;\">with some <span "
268 "style=\"color:#ff0000;\">formatted</span> nested</span> text."));
269
270 auto hb = new KPIMTextEdit::TextHTMLBuilder();
271 auto md = new KPIMTextEdit::MarkupDirector(hb);
272 md->processDocument(doc);
273 auto result = hb->getResult();
274
275 auto regex = QRegularExpression(QStringLiteral(
276 "^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Paragraph <span style=\"background-color:#00ff00;\">with some <span "
277 "style=\"color:#ff0000;\">formatted</span> nested</span> text.</p>\\n$"));
278 QVERIFY(regex.match(result).hasMatch());
279 delete md;
280 delete hb;
281 delete doc;
282 }
283
testDoubleStartDifferentFinish()284 void TextHTMLBuilderTest::testDoubleStartDifferentFinish()
285 {
286 auto doc = new QTextDocument();
287 doc->setHtml(QStringLiteral("Paragraph <i><b>with</b> some formatted</i> text."));
288
289 auto hb = new KPIMTextEdit::TextHTMLBuilder();
290 auto md = new KPIMTextEdit::MarkupDirector(hb);
291 md->processDocument(doc);
292 auto result = hb->getResult();
293
294 auto regex = QRegularExpression(
295 QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Paragraph <em><strong>with</strong> some "
296 "formatted</em> text.</p>\\n$"));
297 QVERIFY(regex.match(result).hasMatch());
298 delete md;
299 delete hb;
300 delete doc;
301 }
302
testDoubleStartDifferentFinishReverseOrder()303 void TextHTMLBuilderTest::testDoubleStartDifferentFinishReverseOrder()
304 {
305 auto doc = new QTextDocument();
306 doc->setHtml(QStringLiteral("Paragraph <b><i>with</i> some formatted</b> text."));
307
308 auto hb = new KPIMTextEdit::TextHTMLBuilder();
309 auto md = new KPIMTextEdit::MarkupDirector(hb);
310 md->processDocument(doc);
311 auto result = hb->getResult();
312
313 auto regex =
314 QRegularExpression(QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Paragraph <strong><em>with</em> some "
315 "formatted</strong> text.</p>\\n$"));
316
317 QVERIFY(regex.match(result).hasMatch());
318 delete md;
319 delete hb;
320 delete doc;
321 }
322
testDifferentStartDoubleFinish()323 void TextHTMLBuilderTest::testDifferentStartDoubleFinish()
324 {
325 auto doc = new QTextDocument();
326 doc->setHtml(QStringLiteral("Paragraph <i>with some <b>formatted<b></i> text."));
327
328 auto hb = new KPIMTextEdit::TextHTMLBuilder();
329 auto md = new KPIMTextEdit::MarkupDirector(hb);
330 md->processDocument(doc);
331 auto result = hb->getResult();
332
333 auto regex =
334 QRegularExpression(QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Paragraph <em>with some "
335 "<strong>formatted</strong></em> text.</p>\\n$"));
336
337 QVERIFY(regex.match(result).hasMatch());
338 delete md;
339 delete hb;
340 delete doc;
341 }
342
testDifferentStartDoubleFinishReverseOrder()343 void TextHTMLBuilderTest::testDifferentStartDoubleFinishReverseOrder()
344 {
345 auto doc = new QTextDocument();
346 doc->setHtml(QStringLiteral("Paragraph <b>with some <i>formatted</i></b> text."));
347
348 auto hb = new KPIMTextEdit::TextHTMLBuilder();
349 auto md = new KPIMTextEdit::MarkupDirector(hb);
350 md->processDocument(doc);
351 auto result = hb->getResult();
352
353 auto regex =
354 QRegularExpression(QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Paragraph <strong>with some "
355 "<em>formatted</em></strong> text.</p>\\n$"));
356
357 QVERIFY(regex.match(result).hasMatch());
358 delete md;
359 delete hb;
360 delete doc;
361 }
362
testOverlap()363 void TextHTMLBuilderTest::testOverlap()
364 {
365 auto doc = new QTextDocument();
366 doc->setHtml(QStringLiteral("Paragraph <b>with <i>some</i></b><i> formatted</i> text."));
367
368 auto hb = new KPIMTextEdit::TextHTMLBuilder();
369 auto md = new KPIMTextEdit::MarkupDirector(hb);
370 md->processDocument(doc);
371 auto result = hb->getResult();
372
373 auto regex = QRegularExpression(
374 QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Paragraph <strong>with <em>some</em></strong><em> "
375 "formatted</em> text.</p>\\n$"));
376
377 QVERIFY(regex.match(result).hasMatch());
378 delete md;
379 delete hb;
380 delete doc;
381 }
382
testEdgeCaseLeft()383 void TextHTMLBuilderTest::testEdgeCaseLeft()
384 {
385 auto doc = new QTextDocument();
386 doc->setHtml(QStringLiteral("Paragraph <b>with some formatted text.</b>"));
387
388 auto hb = new KPIMTextEdit::TextHTMLBuilder();
389 auto md = new KPIMTextEdit::MarkupDirector(hb);
390 md->processDocument(doc);
391 auto result = hb->getResult();
392
393 auto regex = QRegularExpression(QStringLiteral(
394 "^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Paragraph <strong>with some formatted text.</strong></p>\\n$"));
395
396 QVERIFY(regex.match(result).hasMatch());
397 delete md;
398 delete hb;
399 delete doc;
400 }
401
testEdgeCaseRight()402 void TextHTMLBuilderTest::testEdgeCaseRight()
403 {
404 auto doc = new QTextDocument();
405 doc->setHtml(QStringLiteral("<b>Paragraph with some formatted</b> text."));
406
407 auto hb = new KPIMTextEdit::TextHTMLBuilder();
408 auto md = new KPIMTextEdit::MarkupDirector(hb);
409 md->processDocument(doc);
410 auto result = hb->getResult();
411
412 auto regex = QRegularExpression(QStringLiteral(
413 "^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\"><strong>Paragraph with some formatted</strong> text.</p>\\n$"));
414
415 QVERIFY(regex.match(result).hasMatch());
416 delete md;
417 delete hb;
418 delete doc;
419 }
420
testImage()421 void TextHTMLBuilderTest::testImage()
422 {
423 auto doc = new QTextDocument();
424 doc->setHtml(
425 QStringLiteral("Paragraph with an inline <img "
426 "src=\"http://kde.org/img/kde41.png\" /> image."));
427
428 auto hb = new KPIMTextEdit::TextHTMLBuilder();
429 auto md = new KPIMTextEdit::MarkupDirector(hb);
430 md->processDocument(doc);
431 auto result = hb->getResult();
432
433 auto regex =
434 QRegularExpression(QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Paragraph with an inline <img "
435 "src=\"http://kde.org/img/kde41.png\" /> image.</p>\\n$"));
436
437 QVERIFY(regex.match(result).hasMatch());
438 delete md;
439 delete hb;
440 delete doc;
441 }
442
testImageResized()443 void TextHTMLBuilderTest::testImageResized()
444 {
445 QString result;
446 QRegularExpression regex;
447 auto doc = new QTextDocument();
448
449 // width
450 doc->setHtml(
451 QStringLiteral("Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
452 "width=\"10\" /> image."));
453
454 auto hb = new KPIMTextEdit::TextHTMLBuilder();
455 auto md = new KPIMTextEdit::MarkupDirector(hb);
456 md->processDocument(doc);
457 result = hb->getResult();
458
459 regex = QRegularExpression(QStringLiteral(
460 "^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
461 "width=\"10\" /> image.</p>\\n$"));
462 QVERIFY(regex.match(result).hasMatch());
463
464 // height
465 doc->setHtml(
466 QStringLiteral("Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
467 "height=\"10\" /> image."));
468
469 delete hb;
470 delete md;
471 hb = new KPIMTextEdit::TextHTMLBuilder();
472 md = new KPIMTextEdit::MarkupDirector(hb);
473 md->processDocument(doc);
474 result = hb->getResult();
475
476 regex = QRegularExpression(QStringLiteral(
477 "^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
478 "height=\"10\" /> image.</p>\\n$"));
479 QVERIFY(regex.match(result).hasMatch());
480
481 // height and width
482 doc->setHtml(
483 QStringLiteral("Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
484 "height=\"10\" width=\"10\" /> image."));
485
486 delete hb;
487 delete md;
488 hb = new KPIMTextEdit::TextHTMLBuilder();
489 md = new KPIMTextEdit::MarkupDirector(hb);
490 md->processDocument(doc);
491 result = hb->getResult();
492
493 regex = QRegularExpression(QStringLiteral(
494 "^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
495 "width=\"10\" height=\"10\" /> image.</p>\\n$"));
496
497 QVERIFY(regex.match(result).hasMatch());
498 delete md;
499 delete hb;
500 delete doc;
501 }
502
testEachFormatTagSingly()503 void TextHTMLBuilderTest::testEachFormatTagSingly()
504 {
505 QString result;
506 QRegularExpression regex;
507 auto doc = new QTextDocument();
508
509 // Test bold
510 doc->setHtml(QStringLiteral("Some <b>formatted</b> text."));
511 auto hb = new KPIMTextEdit::TextHTMLBuilder();
512 auto md = new KPIMTextEdit::MarkupDirector(hb);
513 md->processDocument(doc);
514 result = hb->getResult();
515
516 regex = QRegularExpression(
517 QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Some <strong>formatted</strong> text.</p>\\n$"));
518 QVERIFY(regex.match(result).hasMatch());
519
520 // Test Italic
521 doc->setHtml(QStringLiteral("Some <i>formatted</i> text."));
522 delete hb;
523 delete md;
524
525 hb = new KPIMTextEdit::TextHTMLBuilder();
526 md = new KPIMTextEdit::MarkupDirector(hb);
527 md->processDocument(doc);
528 result = hb->getResult();
529
530 regex = QRegularExpression(
531 QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Some <em>formatted</em> text.</p>\\n$"));
532 QVERIFY(regex.match(result).hasMatch());
533
534 // Test Underline
535 doc->setHtml(QStringLiteral("Some <u>formatted</u> text."));
536 delete hb;
537 delete md;
538
539 hb = new KPIMTextEdit::TextHTMLBuilder();
540 md = new KPIMTextEdit::MarkupDirector(hb);
541 md->processDocument(doc);
542 result = hb->getResult();
543
544 regex =
545 QRegularExpression(QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Some <u>formatted</u> text.</p>\\n$"));
546 QVERIFY(regex.match(result).hasMatch());
547
548 // Test Strikeout
549 doc->setHtml(QStringLiteral("Some <s>formatted</s> text."));
550 delete hb;
551 delete md;
552
553 hb = new KPIMTextEdit::TextHTMLBuilder();
554 md = new KPIMTextEdit::MarkupDirector(hb);
555 md->processDocument(doc);
556 result = hb->getResult();
557
558 regex =
559 QRegularExpression(QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Some <s>formatted</s> text.</p>\\n$"));
560 QVERIFY(regex.match(result).hasMatch());
561
562 // Test Superscript
563 doc->setHtml(QStringLiteral("Some <sup>formatted</sup> text."));
564 delete hb;
565 delete md;
566
567 hb = new KPIMTextEdit::TextHTMLBuilder();
568 md = new KPIMTextEdit::MarkupDirector(hb);
569 md->processDocument(doc);
570 result = hb->getResult();
571
572 regex = QRegularExpression(
573 QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Some <sup>formatted</sup> text.</p>\\n$"));
574 QVERIFY(regex.match(result).hasMatch());
575
576 // Test Subscript
577 doc->setHtml(QStringLiteral("Some <sub>formatted</sub> text."));
578 delete hb;
579 delete md;
580
581 hb = new KPIMTextEdit::TextHTMLBuilder();
582 md = new KPIMTextEdit::MarkupDirector(hb);
583 md->processDocument(doc);
584 result = hb->getResult();
585
586 regex = QRegularExpression(
587 QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Some <sub>formatted</sub> text.</p>\\n$"));
588 QVERIFY(regex.match(result).hasMatch());
589
590 // Test Foreground
591 doc->setHtml(QStringLiteral("Some <span style=\"color:#ff0000;\">formatted</span> text."));
592 delete hb;
593 delete md;
594
595 hb = new KPIMTextEdit::TextHTMLBuilder();
596 md = new KPIMTextEdit::MarkupDirector(hb);
597 md->processDocument(doc);
598 result = hb->getResult();
599
600 regex = QRegularExpression(
601 QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Some <span style=\"color:#ff0000;\">formatted</span> "
602 "text.</p>\\n$"));
603 QVERIFY(regex.match(result).hasMatch());
604
605 // Test Background
606 doc->setHtml(QStringLiteral("Some <span style=\"background-color:#ff0000;\">formatted</span> text."));
607 delete hb;
608 delete md;
609
610 hb = new KPIMTextEdit::TextHTMLBuilder();
611 md = new KPIMTextEdit::MarkupDirector(hb);
612 md->processDocument(doc);
613 result = hb->getResult();
614
615 regex = QRegularExpression(QStringLiteral(
616 "^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Some <span style=\"background-color:#ff0000;\">formatted</span> "
617 "text.</p>\\n$"));
618 QVERIFY(regex.match(result).hasMatch());
619
620 // Test Font Family
621 doc->setHtml(QStringLiteral("Some <span style=\"font-family:courier;\">formatted</span> text."));
622 delete hb;
623 delete md;
624
625 hb = new KPIMTextEdit::TextHTMLBuilder();
626 md = new KPIMTextEdit::MarkupDirector(hb);
627 md->processDocument(doc);
628 result = hb->getResult();
629
630 regex = QRegularExpression(QStringLiteral(
631 "^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Some <span style=\"font-family:courier;\">formatted</span> "
632 "text.</p>\\n$"));
633 QVERIFY(regex.match(result).hasMatch());
634
635 // Test Font Size
636 doc->setHtml(QStringLiteral("Some <span style=\"font-size:20pt;\">formatted</span> text."));
637 delete hb;
638 delete md;
639
640 hb = new KPIMTextEdit::TextHTMLBuilder();
641 md = new KPIMTextEdit::MarkupDirector(hb);
642 md->processDocument(doc);
643 result = hb->getResult();
644
645 regex = QRegularExpression(
646 QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Some <span style=\"font-size:20pt;\">formatted</span> "
647 "text.</p>\\n$"));
648 QVERIFY(regex.match(result).hasMatch());
649 delete md;
650 delete hb;
651 delete doc;
652 }
653
testHorizontalRule()654 void TextHTMLBuilderTest::testHorizontalRule()
655 {
656 auto doc = new QTextDocument();
657 doc->setHtml(
658 QStringLiteral("<p style=\"margin-top:0;margin-bottom:0;\">Foo</p><hr "
659 "/><p style=\"margin-top:0;margin-bottom:0;\">Bar</p>"));
660
661 auto hb = new KPIMTextEdit::TextHTMLBuilder();
662 auto md = new KPIMTextEdit::MarkupDirector(hb);
663 md->processDocument(doc);
664 auto result = hb->getResult();
665
666 auto regex =
667 QRegularExpression(QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Foo</p>\\n<hr />\\n<p "
668 "style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Bar</p>\\n$"));
669
670 QVERIFY(regex.match(result).hasMatch());
671 delete md;
672 delete hb;
673 delete doc;
674 }
675
testNewlines()676 void TextHTMLBuilderTest::testNewlines()
677 {
678 auto doc = new QTextDocument();
679 doc->setHtml(QStringLiteral("<p>Foo<br /><br />\n<p>Bar</p>"));
680
681 auto hb = new KPIMTextEdit::TextHTMLBuilder();
682 auto md = new KPIMTextEdit::MarkupDirector(hb);
683 md->processDocument(doc);
684 auto result = hb->getResult();
685
686 auto regex =
687 QRegularExpression(QStringLiteral("^<p style=\"margin-top:12;margin-bottom:12;margin-left:0;margin-right:0;\">Foo</p>\\n<p> <p> </p>\\n<p "
688 "style=\"margin-top:12;margin-bottom:12;margin-left:0;margin-right:0;\">Bar</p>\\n$"));
689 QVERIFY(regex.match(result).hasMatch());
690 delete md;
691 delete hb;
692 delete doc;
693 }
694
testNewlinesThroughQTextCursor()695 void TextHTMLBuilderTest::testNewlinesThroughQTextCursor()
696 {
697 auto doc = new QTextDocument(this);
698 QTextCursor cursor(doc);
699 cursor.movePosition(QTextCursor::Start);
700 cursor.insertText(QStringLiteral("Foo"));
701 cursor.insertText(QStringLiteral("\n"));
702 cursor.insertText(QStringLiteral("\n"));
703 cursor.insertText(QStringLiteral("\n"));
704 cursor.insertText(QStringLiteral("Bar"));
705
706 auto hb = new KPIMTextEdit::TextHTMLBuilder();
707 auto md = new KPIMTextEdit::MarkupDirector(hb);
708 md->processDocument(doc);
709 auto result = hb->getResult();
710
711 auto regex =
712 QRegularExpression(QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Foo</p>\\n<br /><br /><p "
713 "style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Bar</p>\\n$"));
714 // qDebug() << "result " << result;
715 QVERIFY(regex.match(result).hasMatch());
716 delete md;
717 delete hb;
718 delete doc;
719 }
720
testInsertImage()721 void TextHTMLBuilderTest::testInsertImage()
722 {
723 auto doc = new QTextDocument(this);
724 QTextCursor cursor(doc);
725 cursor.movePosition(QTextCursor::Start);
726 cursor.insertText(QStringLiteral("Foo"));
727 cursor.insertText(QStringLiteral("\n"));
728 cursor.insertText(QStringLiteral("\n"));
729 cursor.insertText(QStringLiteral("\n"));
730 cursor.insertText(QStringLiteral("Bar"));
731
732 const QString imagePath = QStringLiteral(GRANTLEEBUILDER_DIR "/object-fill.svg");
733 const QImage image(imagePath);
734 QString imageNameToAdd = QStringLiteral("imagename");
735 doc->addResource(QTextDocument::ImageResource, QUrl(imageNameToAdd), image);
736 cursor.insertImage(imageNameToAdd);
737
738 auto hb = new KPIMTextEdit::TextHTMLBuilder();
739 auto md = new KPIMTextEdit::MarkupDirector(hb);
740 md->processDocument(doc);
741 auto result = hb->getResult();
742
743 auto regex =
744 QRegularExpression(QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Foo</p>\\n<br /><br /><p "
745 "style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Bar<img src=\"imagename\" /></p>\\n$"));
746 QVERIFY(regex.match(result).hasMatch());
747 delete md;
748 delete hb;
749 delete doc;
750 }
751
testInsertImageWithSize()752 void TextHTMLBuilderTest::testInsertImageWithSize()
753 {
754 auto doc = new QTextDocument(this);
755 QTextCursor cursor(doc);
756 cursor.movePosition(QTextCursor::Start);
757 cursor.insertText(QStringLiteral("Foo"));
758 cursor.insertText(QStringLiteral("\n"));
759 cursor.insertText(QStringLiteral("\n"));
760 cursor.insertText(QStringLiteral("\n"));
761 cursor.insertText(QStringLiteral("Bar"));
762
763 const QString imagePath = QStringLiteral(GRANTLEEBUILDER_DIR "/object-fill.svg");
764 const QImage image(imagePath);
765 QString imageNameToAdd = QStringLiteral("imagename");
766 doc->addResource(QTextDocument::ImageResource, QUrl(imageNameToAdd), image);
767
768 QTextImageFormat format;
769 format.setName(imageNameToAdd);
770 format.setWidth(100);
771 format.setHeight(120);
772 cursor.insertImage(format);
773
774 auto hb = new KPIMTextEdit::TextHTMLBuilder();
775 auto md = new KPIMTextEdit::MarkupDirector(hb);
776 md->processDocument(doc);
777 auto result = hb->getResult();
778
779 auto regex = QRegularExpression(QStringLiteral(
780 "^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Foo</p>\\n<br /><br /><p "
781 "style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">Bar<img src=\"imagename\" width=\"100\" height=\"120\" /></p>\\n$"));
782 QVERIFY(regex.match(result).hasMatch());
783 delete md;
784 delete hb;
785 delete doc;
786 }
787
testTitle1()788 void TextHTMLBuilderTest::testTitle1()
789 {
790 const int boundedLevel = 1;
791 auto doc = new QTextDocument(this);
792 QTextCursor cursor(doc);
793 cursor.movePosition(QTextCursor::Start);
794 cursor.insertText(QStringLiteral("Foo"));
795
796 const int sizeAdjustment = boundedLevel > 0 ? 5 - boundedLevel : 0;
797 QTextBlockFormat blkfmt;
798 blkfmt.setHeadingLevel(boundedLevel);
799 cursor.mergeBlockFormat(blkfmt);
800
801 QTextCharFormat chrfmt;
802 chrfmt.setFontWeight(boundedLevel > 0 ? QFont::Bold : QFont::Normal);
803 chrfmt.setProperty(QTextFormat::FontSizeAdjustment, sizeAdjustment);
804 QTextCursor selectCursor = cursor;
805 QTextCursor top = selectCursor;
806 top.setPosition(qMin(top.anchor(), top.position()));
807 top.movePosition(QTextCursor::StartOfBlock);
808
809 QTextCursor bottom = selectCursor;
810 bottom.setPosition(qMax(bottom.anchor(), bottom.position()));
811 bottom.movePosition(QTextCursor::EndOfBlock);
812
813 selectCursor.setPosition(top.position(), QTextCursor::MoveAnchor);
814 selectCursor.setPosition(bottom.position(), QTextCursor::KeepAnchor);
815 selectCursor.mergeCharFormat(chrfmt);
816
817 cursor.mergeBlockCharFormat(chrfmt);
818
819 auto hb = new KPIMTextEdit::TextHTMLBuilder();
820 auto md = new KPIMTextEdit::MarkupDirector(hb);
821 md->processDocument(doc);
822 auto result = hb->getResult();
823
824 auto regex = QRegularExpression(QStringLiteral(
825 "^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\"><span style=\"font-size:29pt;\"><strong>Foo</strong></span></p>\n$"));
826 // qDebug() << " result " << result;
827 // TODO implement header support now.
828 delete md;
829 delete hb;
830 delete doc;
831 QEXPECT_FAIL("", "Problem with title", Continue);
832 QVERIFY(regex.match(result).hasMatch());
833 }
834
testBug421908()835 void TextHTMLBuilderTest::testBug421908()
836 {
837 auto doc = new QTextDocument();
838 doc->setHtml(QStringLiteral("<p><span style=\" color:#aaaaff;\">some colored text<br />some colored text</span></p>"));
839
840 auto hb = new KPIMTextEdit::TextHTMLBuilder();
841 auto md = new KPIMTextEdit::MarkupDirector(hb);
842 md->processDocument(doc);
843 auto result = hb->getResult();
844
845 auto regex =
846 QRegularExpression(QStringLiteral("^<p style=\"margin-top:12;margin-bottom:12;margin-left:0;margin-right:0;\"><span style=\"color:#aaaaff;\">some "
847 "colored text<br />some colored text</span></p>\n$"));
848 QVERIFY(regex.match(result).hasMatch());
849 delete md;
850 delete hb;
851 delete doc;
852 }
853
testBug421908_2()854 void TextHTMLBuilderTest::testBug421908_2()
855 {
856 auto doc = new QTextDocument();
857 doc->setHtml(
858 QStringLiteral("<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">phone: "
859 "+123456 7890<br />mail: some@mail.com</p>"
860 "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; "
861 "text-indent:0px;\"><br /></p>"
862 "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span "
863 "style=\" font-size:7pt;\">small text</span></p>"));
864
865 auto hb = new KPIMTextEdit::TextHTMLBuilder();
866 auto md = new KPIMTextEdit::MarkupDirector(hb);
867 md->processDocument(doc);
868 auto result = hb->getResult();
869
870 auto regex = QRegularExpression(
871 QStringLiteral("<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">phone: \\+123456 7890<br />mail: some@mail.com</p>\n<br /><p "
872 "style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\"><span style=\"font-size:7pt;\">small text</span></p>$"));
873 // qDebug() << " result "<< result;
874 QVERIFY(regex.match(result).hasMatch());
875 delete md;
876 delete hb;
877 delete doc;
878 }
879
testBug421908_full()880 void TextHTMLBuilderTest::testBug421908_full()
881 {
882 auto doc = new QTextDocument();
883 doc->setHtml(
884 QStringLiteral("<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; "
885 "text-indent:0px;\"><!--StartFragment-->phone: +123456 7890<br />mail: some@mail.com</p>"
886 "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; "
887 "text-indent:0px;\"><br /></p>"
888 "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span "
889 "style=\" font-size:7pt;\">small text</span></p>"
890 "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; "
891 "text-indent:0px;\"><br /></p>"
892 "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span "
893 "style=\" color:#aaaaff;\">some colored text<br />some colored text</span></p>"
894 "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; "
895 "text-indent:0px;\"><br /><!--EndFragment--></p>"));
896
897 auto hb = new KPIMTextEdit::TextHTMLBuilder();
898 auto md = new KPIMTextEdit::MarkupDirector(hb);
899 md->processDocument(doc);
900 auto result = hb->getResult();
901
902 auto regex = QRegularExpression(
903 QStringLiteral("<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\">phone: \\+123456 7890<br />mail: some@mail.com</p>\n<br /><p "
904 "style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\"><span style=\"font-size:7pt;\">small text</span></p>\n<br /><p "
905 "style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\"><span style=\"color:\\#aaaaff;\">some colored text<br />some "
906 "colored text</span></p>\n<br />$"));
907 // qDebug() << " result "<< result;
908 QVERIFY(regex.match(result).hasMatch());
909 delete md;
910 delete hb;
911 delete doc;
912 }
913
testBug436880()914 void TextHTMLBuilderTest::testBug436880()
915 {
916 auto doc = new QTextDocument();
917 doc->setHtml(QStringLiteral("<p dir='rtl'>test</ p>"));
918
919 auto hb = new KPIMTextEdit::TextHTMLBuilder();
920 auto md = new KPIMTextEdit::MarkupDirector(hb);
921 md->processDocument(doc);
922 auto result = hb->getResult();
923
924 // qDebug() << " result " << result;
925 auto regex = QRegularExpression(QStringLiteral("^<p style=\"margin-top:12;margin-bottom:12;margin-left:0;margin-right:0;\" dir='rtl'>test</p>\n"));
926
927 QVERIFY(regex.match(result).hasMatch());
928 delete md;
929 delete hb;
930 delete doc;
931 }
932
testBug442416()933 void TextHTMLBuilderTest::testBug442416()
934 {
935 auto doc = new QTextDocument();
936 doc->setHtml(QStringLiteral(
937 "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span "
938 "style=\" background-color:#ffff00;\">Sss</span></p>\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; "
939 "-qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" background-color:#ffff00;\">sss</span></p>"));
940
941 auto hb = new KPIMTextEdit::TextHTMLBuilder();
942 auto md = new KPIMTextEdit::MarkupDirector(hb);
943 md->processDocument(doc);
944 auto result = hb->getResult();
945
946 // qDebug() << " result " << result;
947 auto regex = QRegularExpression(
948 QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\"><span style=\"background-color:#ffff00;\">Sss</span></p>\n<p "
949 "style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\"><span style=\"background-color:#ffff00;\">sss</span></p>\n"));
950
951 QVERIFY(regex.match(result).hasMatch());
952 delete md;
953 delete hb;
954 delete doc;
955 }
956
testBug442416Bis()957 void TextHTMLBuilderTest::testBug442416Bis()
958 {
959 auto doc = new QTextDocument();
960 doc->setHtml(QStringLiteral(
961 "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span "
962 "style=\" background-color:#ffff00;\">Sss</span></p>\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; "
963 "-qt-block-indent:0; text-indent:0px; -qt-user-state:0;\"><span style=\" background-color:#ff0000;\">sss</span></p>"));
964
965 auto hb = new KPIMTextEdit::TextHTMLBuilder();
966 auto md = new KPIMTextEdit::MarkupDirector(hb);
967 md->processDocument(doc);
968 auto result = hb->getResult();
969
970 // qDebug() << " result " << result;
971 auto regex = QRegularExpression(
972 QStringLiteral("^<p style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\"><span style=\"background-color:#ffff00;\">Sss</span></p>\n<p "
973 "style=\"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;\"><span style=\"background-color:#ff0000;\">sss</span></p>\n"));
974 QVERIFY(regex.match(result).hasMatch());
975 delete md;
976 delete hb;
977 delete doc;
978 }
979
testBugTextColor()980 void TextHTMLBuilderTest::testBugTextColor()
981 {
982 auto doc = new QTextDocument();
983 doc->setHtml(QStringLiteral("<p><span style=\"color:#ffff00;\">BBBB</span></p><p><span style=\"color:#ffff00;\">AAA</p>"));
984
985 auto hb = new KPIMTextEdit::TextHTMLBuilder();
986 auto md = new KPIMTextEdit::MarkupDirector(hb);
987 md->processDocument(doc);
988 auto result = hb->getResult();
989
990 // qDebug() << " result " << result;
991 auto regex = QRegularExpression(
992 QStringLiteral("^<p style=\"margin-top:12;margin-bottom:12;margin-left:0;margin-right:0;\"><span style=\"color:#ffff00;\">BBBB</span></p>\n<p "
993 "style=\"margin-top:12;margin-bottom:12;margin-left:0;margin-right:0;\"><span style=\"color:#ffff00;\">AAA</span></p>\n"));
994 QVERIFY(regex.match(result).hasMatch());
995 delete md;
996 delete hb;
997 delete doc;
998 }
999
testBugIndent443534()1000 void TextHTMLBuilderTest::testBugIndent443534()
1001 {
1002 auto doc = new QTextDocument();
1003 doc->setHtml(QStringLiteral(
1004 "<ul style=\"margin-top: 0px; margin-bottom: 0px; margin-right: 0px; -qt-list-indent: 1;\"><li style=\" margin-top:0px; margin-bottom:0px; "
1005 "margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\">Test1</li><li style=\" margin-top:0px; "
1006 "margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\">Test2</li><ul style=\"margin-top: "
1007 "0px; margin-bottom: 0px; margin-right: 0px; -qt-list-indent: 2;\"><li style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; "
1008 "-qt-block-indent:0; text-indent:0px; -qt-user-state:0;\">Indent1</li></ul><li style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; "
1009 "margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;\">deindent1</li></ul>"));
1010
1011 auto hb = new KPIMTextEdit::TextHTMLBuilder();
1012 auto md = new KPIMTextEdit::MarkupDirector(hb);
1013 md->processDocument(doc);
1014 auto result = hb->getResult();
1015
1016 qDebug() << " result " << result;
1017 auto regex = QRegularExpression(
1018 QStringLiteral("^<p style=\"margin-top:12;margin-bottom:12;margin-left:0;margin-right:0;\"><span style=\"color:#ffff00;\">BBBB</span></p>\n<p "
1019 "style=\"margin-top:12;margin-bottom:12;margin-left:0;margin-right:0;\"><span style=\"color:#ffff00;\">AAA</span></p>\n"));
1020 QEXPECT_FAIL("", "Problem with list bug 443534", Continue);
1021 QVERIFY(regex.match(result).hasMatch());
1022 delete md;
1023 delete hb;
1024 delete doc;
1025 }
1026