21#include <QtCore/QRegularExpression>
23#include <QtGui/QTextCursor>
24#include <QtGui/QTextDocument>
25#include <QtTest/QtTest>
26#include <QtTest/qtestevent.h>
28#include "coverageobject.h"
29#include "markupdirector.h"
30#include "texthtmlbuilder.h"
40 void testSingleFormat();
41 void testDoubleFormat();
42 void testDoubleStartDifferentFinish();
43 void testDoubleStartDifferentFinishReverseOrder();
44 void testDifferentStartDoubleFinish();
45 void testDifferentStartDoubleFinishReverseOrder();
48 void testAnchorWithFormattedContent();
49 void testAdjacentAnchors();
50 void testNestedFormatting();
52 void testDoubleSpan();
53 void testSpanNesting();
54 void testEdgeCaseLeft();
55 void testEdgeCaseRight();
57 void testImageResized();
58 void testEachFormatTagSingly();
59 void testHorizontalRule();
61 void testNewlinesThroughQTextCursor();
64void TestHtmlOutput::testSingleFormat()
69 doc->setHtml(QStringLiteral(
"This <b>text</b> is bold."));
73 md->processDocument(doc);
74 auto result = hb->getResult();
76 QStringLiteral(
"^<p>This <strong>text</strong> is bold.</p>\\n$"));
78 QVERIFY(regex.match(result).hasMatch());
81void TestHtmlOutput::testDoubleFormat()
83 auto doc =
new QTextDocument();
86 doc->setHtml(QStringLiteral(
"Some <b><i>formatted</i></b> text."));
88 auto hb =
new TextHTMLBuilder();
89 auto md =
new MarkupDirector(hb);
90 md->processDocument(doc);
91 auto result = hb->getResult();
92 QRegularExpression regex(
93 QStringLiteral(
"^<p>Some "
94 "(<strong><em>|<em><strong>)formatted(</em></strong>|</"
95 "strong></em>) text.</p>\\n$"));
97 QVERIFY(regex.match(result).hasMatch());
100void TestHtmlOutput::testAnchor()
102 auto doc =
new QTextDocument();
104 QStringLiteral(
"A <a href=\"http://www.kde.org\">link</a> to KDE."));
106 auto hb =
new TextHTMLBuilder();
107 auto md =
new MarkupDirector(hb);
108 md->processDocument(doc);
109 auto result = hb->getResult();
111 QRegularExpression regex(QStringLiteral(
112 "^<p>A <a href=\"http://www.kde.org\">link</a> to KDE.</p>\\n$"));
114 QVERIFY(regex.match(result).hasMatch());
117void TestHtmlOutput::testAnchorWithFormattedContent()
119 auto doc =
new QTextDocument();
120 doc->setHtml(QStringLiteral(
121 "A <a href=\"http://www.kde.org\"><b>formatted</b> link</a> to KDE."));
123 auto hb =
new TextHTMLBuilder();
124 auto md =
new MarkupDirector(hb);
125 md->processDocument(doc);
126 auto result = hb->getResult();
128 QRegularExpression regex(QStringLiteral(
129 "^<p>A <a href=\"http://www.kde.org\"><strong>formatted</strong> "
130 "link</a> to KDE.</p>\\n$"));
132 QVERIFY(regex.match(result).hasMatch());
135void TestHtmlOutput::testAdjacentAnchors()
137 auto doc =
new QTextDocument();
139 QStringLiteral(
"Two <a href=\"http://www.kde.org\">links</a><a "
140 "href=\"http://www.google.com\">next</a> to eachother."));
142 auto hb =
new TextHTMLBuilder();
143 auto md =
new MarkupDirector(hb);
144 md->processDocument(doc);
145 auto result = hb->getResult();
147 QRegularExpression regex(QStringLiteral(
148 "^<p>Two <a href=\"http://www.kde.org\">links</a><a "
149 "href=\"http://www.google.com\">next</a> to eachother.</p>\\n$"));
151 QVERIFY(regex.match(result).hasMatch());
154void TestHtmlOutput::testNestedFormatting()
156 auto doc =
new QTextDocument();
157 doc->setHtml(QStringLiteral(
"This <b>text is <i>italic</i> and</b> bold."));
159 auto hb =
new TextHTMLBuilder();
160 auto md =
new MarkupDirector(hb);
161 md->processDocument(doc);
162 auto result = hb->getResult();
164 QRegularExpression regex(QStringLiteral(
165 "^<p>This <strong>text is <em>italic</em> and</strong> bold.</p>\\n$"));
167 QVERIFY(regex.match(result).hasMatch());
170void TestHtmlOutput::testSpan()
172 auto doc =
new QTextDocument();
173 doc->setHtml(QStringLiteral(
174 "Some <span style=\"color:#ff0000;\">formatted</span> text."));
176 auto hb =
new TextHTMLBuilder();
177 auto md =
new MarkupDirector(hb);
178 md->processDocument(doc);
179 auto result = hb->getResult();
181 auto regex = QRegularExpression(
182 QStringLiteral(
"^<p>Some <span style=\"color:#ff0000;\">formatted</span> "
185 QVERIFY(regex.match(result).hasMatch());
188void TestHtmlOutput::testDoubleSpan()
190 auto doc =
new QTextDocument();
191 doc->setHtml(QStringLiteral(
"Some <span "
192 "style=\"color:#ff0000;background-color:#00ff00;"
193 "\">formatted</span> text."));
195 auto hb =
new TextHTMLBuilder();
196 auto md =
new MarkupDirector(hb);
197 md->processDocument(doc);
198 auto result = hb->getResult();
200 auto regex = QRegularExpression(QStringLiteral(
202 "style=\"(color:#ff0000|background-color:#00ff00);\"><span "
203 "style=\"(color:#ff0000|background-color:#00ff00);\">formatted</span></"
204 "span> text.</p>\\n$"));
206 QVERIFY(regex.match(result).hasMatch());
209void TestHtmlOutput::testSpanNesting()
211 auto doc =
new QTextDocument();
212 doc->setHtml(QStringLiteral(
213 "Paragraph <span style=\"background-color:#00ff00;\">with some <span "
214 "style=\"color:#ff0000;\">formatted</span> nested</span> text."));
216 auto hb =
new TextHTMLBuilder();
217 auto md =
new MarkupDirector(hb);
218 md->processDocument(doc);
219 auto result = hb->getResult();
221 auto regex = QRegularExpression(QStringLiteral(
222 "^<p>Paragraph <span style=\"background-color:#00ff00;\">with some <span "
223 "style=\"color:#ff0000;\">formatted</span> nested</span> text.</p>\\n$"));
225 QVERIFY(regex.match(result).hasMatch());
228void TestHtmlOutput::testDoubleStartDifferentFinish()
230 auto doc =
new QTextDocument();
232 QStringLiteral(
"Paragraph <i><b>with</b> some formatted</i> text."));
234 auto hb =
new TextHTMLBuilder();
235 auto md =
new MarkupDirector(hb);
236 md->processDocument(doc);
237 auto result = hb->getResult();
239 auto regex = QRegularExpression(
240 QStringLiteral(
"^<p>Paragraph <em><strong>with</strong> some "
241 "formatted</em> text.</p>\\n$"));
243 QVERIFY(regex.match(result).hasMatch());
246void TestHtmlOutput::testDoubleStartDifferentFinishReverseOrder()
248 auto doc =
new QTextDocument();
250 QStringLiteral(
"Paragraph <b><i>with</i> some formatted</b> text."));
252 auto hb =
new TextHTMLBuilder();
253 auto md =
new MarkupDirector(hb);
254 md->processDocument(doc);
255 auto result = hb->getResult();
257 auto regex = QRegularExpression(
258 QStringLiteral(
"^<p>Paragraph <strong><em>with</em> some "
259 "formatted</strong> text.</p>\\n$"));
261 QVERIFY(regex.match(result).hasMatch());
264void TestHtmlOutput::testDifferentStartDoubleFinish()
266 auto doc =
new QTextDocument();
268 QStringLiteral(
"Paragraph <i>with some <b>formatted<b></i> text."));
270 auto hb =
new TextHTMLBuilder();
271 auto md =
new MarkupDirector(hb);
272 md->processDocument(doc);
273 auto result = hb->getResult();
275 auto regex = QRegularExpression(
276 QStringLiteral(
"^<p>Paragraph <em>with some "
277 "<strong>formatted</strong></em> text.</p>\\n$"));
279 QVERIFY(regex.match(result).hasMatch());
282void TestHtmlOutput::testDifferentStartDoubleFinishReverseOrder()
284 auto doc =
new QTextDocument();
286 QStringLiteral(
"Paragraph <b>with some <i>formatted</i></b> text."));
288 auto hb =
new TextHTMLBuilder();
289 auto md =
new MarkupDirector(hb);
290 md->processDocument(doc);
291 auto result = hb->getResult();
293 auto regex = QRegularExpression(
294 QStringLiteral(
"^<p>Paragraph <strong>with some "
295 "<em>formatted</em></strong> text.</p>\\n$"));
297 QVERIFY(regex.match(result).hasMatch());
300void TestHtmlOutput::testOverlap()
302 auto doc =
new QTextDocument();
303 doc->setHtml(QStringLiteral(
304 "Paragraph <b>with <i>some</i></b><i> formatted</i> text."));
306 auto hb =
new TextHTMLBuilder();
307 auto md =
new MarkupDirector(hb);
308 md->processDocument(doc);
309 auto result = hb->getResult();
311 auto regex = QRegularExpression(
312 QStringLiteral(
"^<p>Paragraph <strong>with <em>some</em></strong><em> "
313 "formatted</em> text.</p>\\n$"));
315 QVERIFY(regex.match(result).hasMatch());
318void TestHtmlOutput::testEdgeCaseLeft()
320 auto doc =
new QTextDocument();
321 doc->setHtml(QStringLiteral(
"Paragraph <b>with some formatted text.</b>"));
323 auto hb =
new TextHTMLBuilder();
324 auto md =
new MarkupDirector(hb);
325 md->processDocument(doc);
326 auto result = hb->getResult();
328 auto regex = QRegularExpression(QStringLiteral(
329 "^<p>Paragraph <strong>with some formatted text.</strong></p>\\n$"));
331 QVERIFY(regex.match(result).hasMatch());
334void TestHtmlOutput::testEdgeCaseRight()
336 auto doc =
new QTextDocument();
337 doc->setHtml(QStringLiteral(
"<b>Paragraph with some formatted</b> text."));
339 auto hb =
new TextHTMLBuilder();
340 auto md =
new MarkupDirector(hb);
341 md->processDocument(doc);
342 auto result = hb->getResult();
344 auto regex = QRegularExpression(QStringLiteral(
345 "^<p><strong>Paragraph with some formatted</strong> text.</p>\\n$"));
347 QVERIFY(regex.match(result).hasMatch());
350void TestHtmlOutput::testImage()
352 auto doc =
new QTextDocument();
354 QStringLiteral(
"Paragraph with an inline <img "
355 "src=\"http://kde.org/img/kde41.png\" /> image."));
357 auto hb =
new TextHTMLBuilder();
358 auto md =
new MarkupDirector(hb);
359 md->processDocument(doc);
360 auto result = hb->getResult();
362 auto regex = QRegularExpression(
363 QStringLiteral(
"^<p>Paragraph with an inline <img "
364 "src=\"http://kde.org/img/kde41.png\" /> image.</p>\\n$"));
366 QVERIFY(regex.match(result).hasMatch());
369void TestHtmlOutput::testImageResized()
372 QRegularExpression regex;
375 auto doc =
new QTextDocument();
378 doc->setHtml(QStringLiteral(
379 "Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
380 "width=\"10\" /> image."));
382 hb =
new TextHTMLBuilder();
383 md =
new MarkupDirector(hb);
387 regex = QRegularExpression(QStringLiteral(
388 "^<p>Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
389 "width=\"10\" /> image.</p>\\n$"));
393 doc->setHtml(QStringLiteral(
394 "Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
395 "height=\"10\" /> image."));
397 hb =
new TextHTMLBuilder();
398 md =
new MarkupDirector(hb);
402 regex = QRegularExpression(QStringLiteral(
403 "^<p>Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
404 "height=\"10\" /> image.</p>\\n$"));
408 doc->setHtml(QStringLiteral(
409 "Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
410 "height=\"10\" width=\"10\" /> image."));
412 hb =
new TextHTMLBuilder();
413 md =
new MarkupDirector(hb);
417 regex = QRegularExpression(QStringLiteral(
418 "^<p>Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
419 "width=\"10\" height=\"10\" /> image.</p>\\n$"));
424void TestHtmlOutput::testEachFormatTagSingly()
427 QRegularExpression regex;
430 auto doc =
new QTextDocument();
433 doc->setHtml(QStringLiteral(
"Some <b>formatted</b> text."));
434 hb =
new TextHTMLBuilder();
435 md =
new MarkupDirector(hb);
439 regex = QRegularExpression(
440 QStringLiteral(
"^<p>Some <strong>formatted</strong> text.</p>\\n$"));
444 doc->setHtml(QStringLiteral(
"Some <i>formatted</i> text."));
445 hb =
new TextHTMLBuilder();
446 md =
new MarkupDirector(hb);
450 regex = QRegularExpression(
451 QStringLiteral(
"^<p>Some <em>formatted</em> text.</p>\\n$"));
455 doc->setHtml(QStringLiteral(
"Some <u>formatted</u> text."));
456 hb =
new TextHTMLBuilder();
457 md =
new MarkupDirector(hb);
461 regex = QRegularExpression(
462 QStringLiteral(
"^<p>Some <u>formatted</u> text.</p>\\n$"));
466 doc->setHtml(QStringLiteral(
"Some <s>formatted</s> text."));
467 hb =
new TextHTMLBuilder();
468 md =
new MarkupDirector(hb);
472 regex = QRegularExpression(
473 QStringLiteral(
"^<p>Some <s>formatted</s> text.</p>\\n$"));
477 doc->setHtml(QStringLiteral(
"Some <sup>formatted</sup> text."));
478 hb =
new TextHTMLBuilder();
479 md =
new MarkupDirector(hb);
483 regex = QRegularExpression(
484 QStringLiteral(
"^<p>Some <sup>formatted</sup> text.</p>\\n$"));
488 doc->setHtml(QStringLiteral(
"Some <sub>formatted</sub> text."));
489 hb =
new TextHTMLBuilder();
490 md =
new MarkupDirector(hb);
494 regex = QRegularExpression(
495 QStringLiteral(
"^<p>Some <sub>formatted</sub> text.</p>\\n$"));
499 doc->setHtml(QStringLiteral(
500 "Some <span style=\"color:#ff0000;\">formatted</span> text."));
501 hb =
new TextHTMLBuilder();
502 md =
new MarkupDirector(hb);
506 regex = QRegularExpression(
507 QStringLiteral(
"^<p>Some <span style=\"color:#ff0000;\">formatted</span> "
512 doc->setHtml(QStringLiteral(
513 "Some <span style=\"background-color:#ff0000;\">formatted</span> text."));
514 hb =
new TextHTMLBuilder();
515 md =
new MarkupDirector(hb);
519 regex = QRegularExpression(QStringLiteral(
520 "^<p>Some <span style=\"background-color:#ff0000;\">formatted</span> "
525 doc->setHtml(QStringLiteral(
526 "Some <span style=\"font-family:courier;\">formatted</span> text."));
527 hb =
new TextHTMLBuilder();
528 md =
new MarkupDirector(hb);
532 regex = QRegularExpression(QStringLiteral(
533 "^<p>Some <span style=\"font-family:courier;\">formatted</span> "
538 doc->setHtml(QStringLiteral(
539 "Some <span style=\"font-size:20pt;\">formatted</span> text."));
540 hb =
new TextHTMLBuilder();
541 md =
new MarkupDirector(hb);
545 regex = QRegularExpression(QStringLiteral(
546 "^<p>Some <span style=\"font-size:20pt;\">formatted</span> "
551void TestHtmlOutput::testHorizontalRule()
553 auto doc =
new QTextDocument();
555 QStringLiteral(
"<p style=\"margin-top:0;margin-bottom:0;\">Foo</p><hr "
556 "/><p style=\"margin-top:0;margin-bottom:0;\">Bar</p>"));
558 auto hb =
new TextHTMLBuilder();
559 auto md =
new MarkupDirector(hb);
563 auto regex = QRegularExpression(
564 QStringLiteral(
"^<p>Foo</p>\\n<hr />\\n<p>Bar</p>\\n$"));
569void TestHtmlOutput::testNewlines()
571 auto doc =
new QTextDocument();
572 doc->setHtml(QStringLiteral(
"<p>Foo<br /><br />\n<p>Bar</p>"));
574 auto hb =
new TextHTMLBuilder();
575 auto md =
new MarkupDirector(hb);
579 auto regex = QRegularExpression(
580 QStringLiteral(
"^<p>Foo</p>\\n<p> <p> </p>\\n<p>Bar</p>\\n$"));
585void TestHtmlOutput::testNewlinesThroughQTextCursor()
587 auto doc =
new QTextDocument(
this);
588 QTextCursor cursor(doc);
590 cursor.insertText(QStringLiteral(
"Foo"));
591 cursor.insertText(QStringLiteral(
"\n"));
592 cursor.insertText(QStringLiteral(
"\n"));
593 cursor.insertText(QStringLiteral(
"\n"));
594 cursor.insertText(QStringLiteral(
"Bar"));
596 auto hb =
new TextHTMLBuilder();
597 auto md =
new MarkupDirector(hb);
601 auto regex = QRegularExpression(
602 QStringLiteral(
"^<p>Foo</p>\\n<p> <p> <p>Bar</p>\\n$"));
608#include "htmlbuildertest.moc"
Instructs a builder object to create markup output.
virtual void processDocument(QTextDocument *doc)
The TextHTMLBuilder creates a clean html markup output.
QString getResult() override
The Cutelee namespace holds all public Cutelee API.
QRegularExpressionMatch match(const QString &subject, int offset, MatchType matchType, MatchOptions matchOptions) const const
bool hasMatch() const const