File indexing completed on 2025-01-05 04:54:43

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "plaintextmarkupbuildertest.h"
0008 #include <KPIMTextEdit/MarkupDirector>
0009 #include <KPIMTextEdit/PlainTextMarkupBuilder>
0010 #include <QRegularExpression>
0011 #include <QTest>
0012 #include <QTextDocument>
0013 QTEST_MAIN(PlainTextMarkupBuilderTest)
0014 
0015 PlainTextMarkupBuilderTest::PlainTextMarkupBuilderTest(QObject *parent)
0016     : QObject(parent)
0017 {
0018 }
0019 
0020 void PlainTextMarkupBuilderTest::testPlainText_data()
0021 {
0022     QTest::addColumn<QString>("text");
0023     QTest::addColumn<QString>("regexpText");
0024     QTest::newRow("text with leading space") << QString() << QString();
0025     QTest::newRow("bold") << QStringLiteral("This <b>text</b> is bold.") << QStringLiteral("^This \\*text\\* is bold.\\n$");
0026     QTest::newRow("double format") << QStringLiteral("Some <b><i>formatted</i></b> text.") << QStringLiteral("^Some (\\*/|/\\*)formatted(\\*/|/\\*) text.\\n$");
0027     QTest::newRow("italic") << QStringLiteral("This <b>text is <i>italic</i> and</b> bold.") << QStringLiteral("^This \\*text is /italic/ and\\* bold.\\n$");
0028     QTest::newRow("double span") << QStringLiteral(
0029         "Some <span "
0030         "style=\"color:#ff0000;background-color:#00ff00;"
0031         "\">formatted</span> text.")
0032                                  << QStringLiteral("^Some formatted text.\\n$");
0033 
0034     // FIXME bug is about id="..."
0035     QTest::newRow("bug345360") << QStringLiteral(
0036         "<html><body><center><table><tr><td><div id=\"foo\"><span>    Dear User,       </span> <br> <br> <span>    We     have updated our Privacy Policy and "
0037         "Terms of Service. Please take a    few  minutes to read and understand them. To help you understand, we    have  summarized the changes and provided "
0038         "a comparison of the current    and the  previous versions.       </span> <br> <br> <span>    These     changes will take effect on April 19, "
0039         "2015.&nbsp; If you continue to use     Zoho after April 19, 2015, you will be governed by the new Privacy     Policy and Terms of Service.       "
0040         "</span> <br> <br> <span>  ddd &nbsp;&nbsp;&nbsp;&nbsp;       </span></td></tr></table></center></body></html>")
0041                                << QStringLiteral("foo");
0042 }
0043 
0044 void PlainTextMarkupBuilderTest::testPlainText()
0045 {
0046     QFETCH(QString, text);
0047     QFETCH(QString, regexpText);
0048     QEXPECT_FAIL("bug345360", "Currently it failed", Continue);
0049     auto doc = new QTextDocument();
0050     doc->setHtml(text);
0051 
0052     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0053     auto md = new KPIMTextEdit::MarkupDirector(hb);
0054     md->processDocument(doc);
0055     const auto result = hb->getResult();
0056 
0057     const QRegularExpression regex(regexpText);
0058 
0059     const bool regexpHasResult = regex.match(result).hasMatch();
0060     if (!regexpHasResult) {
0061         qDebug() << " result found " << result;
0062     }
0063     QVERIFY(regexpHasResult);
0064     delete md;
0065     delete hb;
0066     delete doc;
0067 }
0068 
0069 void PlainTextMarkupBuilderTest::testSingleFormat()
0070 {
0071     auto doc = new QTextDocument();
0072 
0073     // One format
0074     doc->setHtml(QStringLiteral("This <b>text</b> is bold."));
0075 
0076     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0077     auto md = new KPIMTextEdit::MarkupDirector(hb);
0078     md->processDocument(doc);
0079     auto result = hb->getResult();
0080     QRegularExpression regex(QStringLiteral("^This \\*text\\* is bold.\\n$"));
0081 
0082     QVERIFY(regex.match(result).hasMatch());
0083     delete md;
0084     delete hb;
0085     delete doc;
0086 }
0087 
0088 void PlainTextMarkupBuilderTest::testDoubleFormat()
0089 {
0090     auto doc = new QTextDocument();
0091 
0092     // One format
0093     doc->setHtml(QStringLiteral("Some <b><i>formatted</i></b> text."));
0094 
0095     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0096     auto md = new KPIMTextEdit::MarkupDirector(hb);
0097     md->processDocument(doc);
0098     auto result = hb->getResult();
0099     QRegularExpression regex(QStringLiteral("^Some (\\*/|/\\*)formatted(\\*/|/\\*) text.\\n$"));
0100 
0101     QVERIFY(regex.match(result).hasMatch());
0102     delete md;
0103     delete hb;
0104     delete doc;
0105 }
0106 
0107 void PlainTextMarkupBuilderTest::testAnchor()
0108 {
0109     auto doc = new QTextDocument();
0110     doc->setHtml(QStringLiteral("A <a href=\"http://www.kde.org\">link</a> to KDE."));
0111 
0112     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0113     auto md = new KPIMTextEdit::MarkupDirector(hb);
0114     md->processDocument(doc);
0115     auto result = hb->getResult();
0116 
0117     QRegularExpression regex(QStringLiteral("^A link\\[1\\] to KDE.\\n\\n--------\\n\\[1\\] http://www.kde.org\\n$"));
0118 
0119     regex.match(result).hasMatch();
0120 
0121     QVERIFY(regex.match(result).hasMatch());
0122     delete md;
0123     delete hb;
0124     delete doc;
0125 }
0126 
0127 void PlainTextMarkupBuilderTest::testAnchorWithFormattedContent()
0128 {
0129     auto doc = new QTextDocument();
0130     doc->setHtml(QStringLiteral("A <a href=\"http://www.kde.org\"><b>formatted</b> link</a> to KDE."));
0131 
0132     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0133     auto md = new KPIMTextEdit::MarkupDirector(hb);
0134     md->processDocument(doc);
0135     auto result = hb->getResult();
0136 
0137     QRegularExpression regex(
0138         QStringLiteral("^A \\*formatted\\* link\\[1\\] to "
0139                        "KDE.\\n\\n--------\\n\\[1\\] http://www.kde.org\\n$"));
0140 
0141     QVERIFY(regex.match(result).hasMatch());
0142     delete md;
0143     delete hb;
0144     delete doc;
0145 }
0146 
0147 void PlainTextMarkupBuilderTest::testAdjacentAnchors()
0148 {
0149     auto doc = new QTextDocument();
0150     doc->setHtml(
0151         QStringLiteral("Two <a href=\"http://www.kde.org\">links</a><a "
0152                        "href=\"http://www.google.com\">next</a> to each other."));
0153 
0154     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0155     auto md = new KPIMTextEdit::MarkupDirector(hb);
0156     md->processDocument(doc);
0157     auto result = hb->getResult();
0158 
0159     QRegularExpression regex(
0160         QStringLiteral("^Two links\\[1\\]next\\[2\\] to each other.\\n\\n--------\\n\\[1\\] "
0161                        "http://www.kde.org\\n\\[2\\] http://www.google.com\\n$"));
0162 
0163     QVERIFY(regex.match(result).hasMatch());
0164     delete md;
0165     delete hb;
0166     delete doc;
0167 }
0168 
0169 void PlainTextMarkupBuilderTest::testNestedFormatting()
0170 {
0171     auto doc = new QTextDocument();
0172     doc->setHtml(QStringLiteral("This <b>text is <i>italic</i> and</b> bold."));
0173 
0174     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0175     auto md = new KPIMTextEdit::MarkupDirector(hb);
0176     md->processDocument(doc);
0177     auto result = hb->getResult();
0178 
0179     QRegularExpression regex(QStringLiteral("^This \\*text is /italic/ and\\* bold.\\n$"));
0180 
0181     QVERIFY(regex.match(result).hasMatch());
0182     delete md;
0183     delete hb;
0184     delete doc;
0185 }
0186 
0187 void PlainTextMarkupBuilderTest::testSpan()
0188 {
0189     auto doc = new QTextDocument();
0190     doc->setHtml(QStringLiteral("Some <span style=\"color:#ff0000;\">formatted</span> text."));
0191 
0192     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0193     auto md = new KPIMTextEdit::MarkupDirector(hb);
0194     md->processDocument(doc);
0195     auto result = hb->getResult();
0196 
0197     auto regex = QRegularExpression(QStringLiteral("^Some formatted text.\\n$"));
0198 
0199     QVERIFY(regex.match(result).hasMatch());
0200     delete md;
0201     delete hb;
0202     delete doc;
0203 }
0204 
0205 void PlainTextMarkupBuilderTest::testDoubleSpan()
0206 {
0207     auto doc = new QTextDocument();
0208     doc->setHtml(
0209         QStringLiteral("Some <span "
0210                        "style=\"color:#ff0000;background-color:#00ff00;"
0211                        "\">formatted</span> text."));
0212 
0213     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0214     auto md = new KPIMTextEdit::MarkupDirector(hb);
0215     md->processDocument(doc);
0216     auto result = hb->getResult();
0217 
0218     auto regex = QRegularExpression(QStringLiteral("^Some formatted text.\\n$"));
0219 
0220     QVERIFY(regex.match(result).hasMatch());
0221     delete md;
0222     delete hb;
0223     delete doc;
0224 }
0225 
0226 void PlainTextMarkupBuilderTest::testSpanNesting()
0227 {
0228     auto doc = new QTextDocument();
0229     doc->setHtml(
0230         QStringLiteral("Paragraph <span style=\"background-color:#00ff00;\">with some <span "
0231                        "style=\"color:#ff0000;\">formatted</span> nested</span> text."));
0232 
0233     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0234     auto md = new KPIMTextEdit::MarkupDirector(hb);
0235     md->processDocument(doc);
0236     auto result = hb->getResult();
0237 
0238     auto regex = QRegularExpression(QStringLiteral("^Paragraph with some formatted nested text.\\n$"));
0239 
0240     QVERIFY(regex.match(result).hasMatch());
0241     delete md;
0242     delete hb;
0243     delete doc;
0244 }
0245 
0246 void PlainTextMarkupBuilderTest::testDoubleStartDifferentFinish()
0247 {
0248     auto doc = new QTextDocument();
0249     doc->setHtml(QStringLiteral("Paragraph <i><b>with</b> some formatted</i> text."));
0250 
0251     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0252     auto md = new KPIMTextEdit::MarkupDirector(hb);
0253     md->processDocument(doc);
0254     auto result = hb->getResult();
0255 
0256     auto regex = QRegularExpression(QStringLiteral("^Paragraph /\\*with\\* some formatted/ text.\\n$"));
0257 
0258     QVERIFY(regex.match(result).hasMatch());
0259     delete md;
0260     delete hb;
0261     delete doc;
0262 }
0263 
0264 void PlainTextMarkupBuilderTest::testDoubleStartDifferentFinishReverseOrder()
0265 {
0266     auto doc = new QTextDocument();
0267     doc->setHtml(QStringLiteral("Paragraph <b><i>with</i> some formatted</b> text."));
0268 
0269     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0270     auto md = new KPIMTextEdit::MarkupDirector(hb);
0271     md->processDocument(doc);
0272     auto result = hb->getResult();
0273 
0274     auto regex = QRegularExpression(QStringLiteral("^Paragraph \\*/with/ some formatted\\* text.\\n$"));
0275 
0276     QVERIFY(regex.match(result).hasMatch());
0277     delete md;
0278     delete hb;
0279     delete doc;
0280 }
0281 
0282 void PlainTextMarkupBuilderTest::testDifferentStartDoubleFinish()
0283 {
0284     auto doc = new QTextDocument();
0285     doc->setHtml(QStringLiteral("Paragraph <i>with some <b>formatted<b></i> text."));
0286 
0287     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0288     auto md = new KPIMTextEdit::MarkupDirector(hb);
0289     md->processDocument(doc);
0290     auto result = hb->getResult();
0291 
0292     auto regex = QRegularExpression(QStringLiteral("^Paragraph /with some \\*formatted\\*/ text.\\n$"));
0293 
0294     QVERIFY(regex.match(result).hasMatch());
0295     delete md;
0296     delete hb;
0297     delete doc;
0298 }
0299 
0300 void PlainTextMarkupBuilderTest::testDifferentStartDoubleFinishReverseOrder()
0301 {
0302     auto doc = new QTextDocument();
0303     doc->setHtml(QStringLiteral("Paragraph <b>with some <i>formatted</i></b> text."));
0304 
0305     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0306     auto md = new KPIMTextEdit::MarkupDirector(hb);
0307     md->processDocument(doc);
0308     auto result = hb->getResult();
0309 
0310     auto regex = QRegularExpression(QStringLiteral("^Paragraph \\*with some /formatted/\\* text.\\n$"));
0311 
0312     QVERIFY(regex.match(result).hasMatch());
0313     delete md;
0314     delete hb;
0315     delete doc;
0316 }
0317 
0318 void PlainTextMarkupBuilderTest::testOverlap()
0319 {
0320     auto doc = new QTextDocument();
0321     doc->setHtml(QStringLiteral("Paragraph <b>with <i>some</i></b><i> formatted</i> text."));
0322 
0323     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0324     auto md = new KPIMTextEdit::MarkupDirector(hb);
0325     md->processDocument(doc);
0326     auto result = hb->getResult();
0327 
0328     auto regex = QRegularExpression(QStringLiteral("^Paragraph \\*with /some/\\*/ formatted/ text.\\n$"));
0329 
0330     QVERIFY(regex.match(result).hasMatch());
0331     delete md;
0332     delete hb;
0333     delete doc;
0334 }
0335 
0336 void PlainTextMarkupBuilderTest::testEdgeCaseLeft()
0337 {
0338     auto doc = new QTextDocument();
0339     doc->setHtml(QStringLiteral("Paragraph <b>with some formatted text.</b>"));
0340 
0341     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0342     auto md = new KPIMTextEdit::MarkupDirector(hb);
0343     md->processDocument(doc);
0344     auto result = hb->getResult();
0345 
0346     auto regex = QRegularExpression(QStringLiteral("^Paragraph \\*with some formatted text.\\*\\n$"));
0347 
0348     QVERIFY(regex.match(result).hasMatch());
0349     delete md;
0350     delete hb;
0351     delete doc;
0352 }
0353 
0354 void PlainTextMarkupBuilderTest::testEdgeCaseRight()
0355 {
0356     auto doc = new QTextDocument();
0357     doc->setHtml(QStringLiteral("<b>Paragraph with some formatted</b> text."));
0358 
0359     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0360     auto md = new KPIMTextEdit::MarkupDirector(hb);
0361     md->processDocument(doc);
0362     auto result = hb->getResult();
0363 
0364     auto regex = QRegularExpression(QStringLiteral("^\\*Paragraph with some formatted\\* text.\\n$"));
0365 
0366     QVERIFY(regex.match(result).hasMatch());
0367     delete md;
0368     delete hb;
0369     delete doc;
0370 }
0371 
0372 void PlainTextMarkupBuilderTest::testImage()
0373 {
0374     auto doc = new QTextDocument();
0375     doc->setHtml(
0376         QStringLiteral("Paragraph with an inline <img "
0377                        "src=\"http://kde.org/img/kde41.png\" /> image."));
0378 
0379     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0380     auto md = new KPIMTextEdit::MarkupDirector(hb);
0381     md->processDocument(doc);
0382     auto result = hb->getResult();
0383 
0384     auto regex =
0385         QRegularExpression(QStringLiteral("^Paragraph with an inline \\[1\\] image.\\n\\n--------\\n\\[1\\] "
0386                                           "http://kde.org/img/kde41.png\\n$"));
0387 
0388     QVERIFY(regex.match(result).hasMatch());
0389     delete md;
0390     delete hb;
0391     delete doc;
0392 }
0393 
0394 void PlainTextMarkupBuilderTest::testImageResized()
0395 {
0396     QString result;
0397     QRegularExpression regex;
0398     KPIMTextEdit::PlainTextMarkupBuilder *hb;
0399     KPIMTextEdit::MarkupDirector *md;
0400     auto doc = new QTextDocument();
0401 
0402     // width
0403     doc->setHtml(
0404         QStringLiteral("Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
0405                        "width=\"10\" /> image."));
0406 
0407     hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0408     md = new KPIMTextEdit::MarkupDirector(hb);
0409     md->processDocument(doc);
0410     result = hb->getResult();
0411 
0412     regex =
0413         QRegularExpression(QStringLiteral("^Paragraph with an inline \\[1\\] image.\\n\\n--------\\n\\[1\\] "
0414                                           "http://kde.org/img/kde41.png\\n$"));
0415     QVERIFY(regex.match(result).hasMatch());
0416 
0417     // height
0418     doc->setHtml(
0419         QStringLiteral("Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
0420                        "height=\"10\" /> image."));
0421 
0422     delete md;
0423     delete hb;
0424 
0425     hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0426     md = new KPIMTextEdit::MarkupDirector(hb);
0427     md->processDocument(doc);
0428     result = hb->getResult();
0429 
0430     regex =
0431         QRegularExpression(QStringLiteral("^Paragraph with an inline \\[1\\] image.\\n\\n--------\\n\\[1\\] "
0432                                           "http://kde.org/img/kde41.png\\n$"));
0433     QVERIFY(regex.match(result).hasMatch());
0434 
0435     // height and width
0436     doc->setHtml(
0437         QStringLiteral("Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
0438                        "height=\"10\" width=\"10\" /> image."));
0439 
0440     delete md;
0441     delete hb;
0442 
0443     hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0444     md = new KPIMTextEdit::MarkupDirector(hb);
0445     md->processDocument(doc);
0446     result = hb->getResult();
0447 
0448     regex =
0449         QRegularExpression(QStringLiteral("^Paragraph with an inline \\[1\\] image.\\n\\n--------\\n\\[1\\] "
0450                                           "http://kde.org/img/kde41.png\\n$"));
0451 
0452     QVERIFY(regex.match(result).hasMatch());
0453     delete md;
0454     delete hb;
0455     delete doc;
0456 }
0457 
0458 void PlainTextMarkupBuilderTest::testEachFormatTagSingly()
0459 {
0460     QString result;
0461     QRegularExpression regex;
0462     KPIMTextEdit::PlainTextMarkupBuilder *hb;
0463     KPIMTextEdit::MarkupDirector *md;
0464     auto doc = new QTextDocument();
0465 
0466     // Test bold
0467     doc->setHtml(QStringLiteral("Some <b>formatted</b> text."));
0468     hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0469     md = new KPIMTextEdit::MarkupDirector(hb);
0470     md->processDocument(doc);
0471     result = hb->getResult();
0472 
0473     regex = QRegularExpression(QStringLiteral("^Some \\*formatted\\* text.\\n$"));
0474     QVERIFY(regex.match(result).hasMatch());
0475 
0476     // Test Italic
0477     doc->setHtml(QStringLiteral("Some <i>formatted</i> text."));
0478     delete md;
0479     delete hb;
0480 
0481     hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0482     md = new KPIMTextEdit::MarkupDirector(hb);
0483     md->processDocument(doc);
0484     result = hb->getResult();
0485 
0486     regex = QRegularExpression(QStringLiteral("^Some /formatted/ text.\\n$"));
0487     QVERIFY(regex.match(result).hasMatch());
0488 
0489     // Test Underline
0490     doc->setHtml(QStringLiteral("Some <u>formatted</u> text."));
0491     delete md;
0492     delete hb;
0493 
0494     hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0495     md = new KPIMTextEdit::MarkupDirector(hb);
0496     md->processDocument(doc);
0497     result = hb->getResult();
0498 
0499     regex = QRegularExpression(QStringLiteral("^Some _formatted_ text.\\n$"));
0500     QVERIFY(regex.match(result).hasMatch());
0501 
0502     // Test Strikeout
0503     doc->setHtml(QStringLiteral("Some <s>formatted</s> text."));
0504     delete md;
0505     delete hb;
0506 
0507     hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0508     md = new KPIMTextEdit::MarkupDirector(hb);
0509     md->processDocument(doc);
0510     result = hb->getResult();
0511 
0512     regex = QRegularExpression(QStringLiteral("^Some -formatted- text.\\n$"));
0513     QVERIFY(regex.match(result).hasMatch());
0514 
0515     // Test Superscript
0516     doc->setHtml(QStringLiteral("Some <sup>formatted</sup> text."));
0517     delete md;
0518     delete hb;
0519 
0520     hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0521     md = new KPIMTextEdit::MarkupDirector(hb);
0522     md->processDocument(doc);
0523     result = hb->getResult();
0524 
0525     regex = QRegularExpression(QStringLiteral("^Some \\^\\{formatted\\} text.\\n$"));
0526     QVERIFY(regex.match(result).hasMatch());
0527 
0528     // Test Subscript
0529     doc->setHtml(QStringLiteral("Some <sub>formatted</sub> text."));
0530     delete md;
0531     delete hb;
0532 
0533     hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0534     md = new KPIMTextEdit::MarkupDirector(hb);
0535     md->processDocument(doc);
0536     result = hb->getResult();
0537 
0538     regex = QRegularExpression(QStringLiteral("^Some _\\{formatted\\} text.\\n$"));
0539     QVERIFY(regex.match(result).hasMatch());
0540 
0541     // Test Foreground
0542     doc->setHtml(QStringLiteral("Some <span style=\"color:#ff0000;\">formatted</span> text."));
0543     delete md;
0544     delete hb;
0545 
0546     hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0547     md = new KPIMTextEdit::MarkupDirector(hb);
0548     md->processDocument(doc);
0549     result = hb->getResult();
0550 
0551     regex = QRegularExpression(QStringLiteral("^Some formatted text.\\n$"));
0552     QVERIFY(regex.match(result).hasMatch());
0553 
0554     // Test Background
0555     doc->setHtml(QStringLiteral("Some <span style=\"background-color:#ff0000;\">formatted</span> text."));
0556     delete md;
0557     delete hb;
0558 
0559     hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0560     md = new KPIMTextEdit::MarkupDirector(hb);
0561     md->processDocument(doc);
0562     result = hb->getResult();
0563 
0564     regex = QRegularExpression(QStringLiteral("^Some formatted text.\\n$"));
0565     QVERIFY(regex.match(result).hasMatch());
0566 
0567     // Test Font Family
0568     doc->setHtml(QStringLiteral("Some <span style=\"font-family:courier;\">formatted</span> text."));
0569     delete md;
0570     delete hb;
0571 
0572     hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0573     md = new KPIMTextEdit::MarkupDirector(hb);
0574     md->processDocument(doc);
0575     result = hb->getResult();
0576 
0577     regex = QRegularExpression(QStringLiteral("^Some formatted text.\\n$"));
0578     QVERIFY(regex.match(result).hasMatch());
0579 
0580     // Test Font Size
0581     doc->setHtml(QStringLiteral("Some <span style=\"font-size:20pt;\">formatted</span> text."));
0582     delete md;
0583     delete hb;
0584 
0585     hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0586     md = new KPIMTextEdit::MarkupDirector(hb);
0587     md->processDocument(doc);
0588     result = hb->getResult();
0589 
0590     regex = QRegularExpression(QStringLiteral("^Some formatted text.\\n$"));
0591     QVERIFY(regex.match(result).hasMatch());
0592     delete md;
0593     delete hb;
0594     delete doc;
0595 }
0596 
0597 void PlainTextMarkupBuilderTest::testHorizontalRule()
0598 {
0599     auto doc = new QTextDocument();
0600     doc->setHtml(
0601         QStringLiteral("<p style=\"margin-top:0;margin-bottom:0;\">Foo</p><hr "
0602                        "/><p style=\"margin-top:0;margin-bottom:0;\">Bar</p>"));
0603 
0604     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0605     auto md = new KPIMTextEdit::MarkupDirector(hb);
0606     md->processDocument(doc);
0607     auto result = hb->getResult();
0608 
0609     auto regex = QRegularExpression(QStringLiteral("^Foo\\n--------------------\\nBar\\n$"));
0610 
0611     QVERIFY(regex.match(result).hasMatch());
0612     delete md;
0613     delete hb;
0614     delete doc;
0615 }
0616 
0617 void PlainTextMarkupBuilderTest::testNewlines()
0618 {
0619     auto doc = new QTextDocument();
0620     doc->setHtml(QStringLiteral("<p>Foo</p>\n<br /><br />\n<p>Bar</p>"));
0621 
0622     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0623     auto md = new KPIMTextEdit::MarkupDirector(hb);
0624     md->processDocument(doc);
0625     auto result = hb->getResult();
0626 
0627     auto regex = QRegularExpression(QStringLiteral("^Foo\\n\\n\\nBar\\n$"));
0628 
0629     QVERIFY(regex.match(result).hasMatch());
0630     delete md;
0631     delete hb;
0632     delete doc;
0633 }
0634 
0635 void PlainTextMarkupBuilderTest::testEmptyParagraphs()
0636 {
0637     auto doc = new QTextDocument();
0638     doc->setHtml(QStringLiteral("<p>Foo</p>\n<br /><br />\n<p>Bar</p>"));
0639 
0640     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0641     auto md = new KPIMTextEdit::MarkupDirector(hb);
0642     md->processDocument(doc);
0643     auto result = hb->getResult();
0644 
0645     auto regex = QRegularExpression(QStringLiteral("^Foo\\n\\n\\nBar\\n$"));
0646 
0647     QVERIFY(regex.match(result).hasMatch());
0648     delete md;
0649     delete hb;
0650     delete doc;
0651 }
0652 
0653 void PlainTextMarkupBuilderTest::testNewlinesThroughQTextCursor()
0654 {
0655     auto doc = new QTextDocument(this);
0656     QTextCursor cursor(doc);
0657     cursor.movePosition(QTextCursor::Start);
0658     cursor.insertText(QStringLiteral("Foo"));
0659     cursor.insertText(QStringLiteral("\n"));
0660     cursor.insertText(QStringLiteral("\n"));
0661     cursor.insertText(QStringLiteral("\n"));
0662     cursor.insertText(QStringLiteral("Bar"));
0663 
0664     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0665     auto md = new KPIMTextEdit::MarkupDirector(hb);
0666     md->processDocument(doc);
0667     auto result = hb->getResult();
0668 
0669     auto regex = QRegularExpression(QStringLiteral("^Foo\\n\\n\\nBar\\n$"));
0670 
0671     QVERIFY(regex.match(result).hasMatch());
0672     delete md;
0673     delete hb;
0674     delete doc;
0675 }
0676 
0677 void PlainTextMarkupBuilderTest::testBrInsideParagraph()
0678 {
0679     auto doc = new QTextDocument();
0680     doc->setHtml(QStringLiteral("<p>Foo<br /><br /><br />Bar</p>"));
0681 
0682     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0683     auto md = new KPIMTextEdit::MarkupDirector(hb);
0684     md->processDocument(doc);
0685     auto result = hb->getResult();
0686 
0687     // Two paragraphs separated by two line breaks
0688 
0689     auto regex = QRegularExpression(QStringLiteral("^Foo\\n\\n\\nBar\\n$"));
0690 
0691     QVERIFY(regex.match(result).hasMatch());
0692     delete md;
0693     delete hb;
0694     delete doc;
0695 }
0696 
0697 void PlainTextMarkupBuilderTest::testLongDocument()
0698 {
0699     QTextDocument doc;
0700 
0701     QFile sourceHtml(QFINDTESTDATA("sourcehtml"));
0702     QVERIFY(sourceHtml.open(QIODevice::ReadOnly));
0703     doc.setHtml(QString::fromLatin1(sourceHtml.readAll().constData()));
0704 
0705     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0706     auto md = new KPIMTextEdit::MarkupDirector(hb);
0707     md->processDocument(&doc);
0708     auto result = hb->getResult();
0709     QVERIFY2(result.startsWith(QLatin1StringView("Hello,\nThis is some text\nIt shows how "
0710                                                  "grantlee is used from kmail\n")),
0711              qPrintable(result));
0712     QVERIFY2(result.endsWith(QLatin1StringView("This is the end of the signature\n")), qPrintable(result));
0713     // qDebug() << result;
0714     delete md;
0715     delete hb;
0716 }
0717 
0718 void PlainTextMarkupBuilderTest::testBrInsideAnchor()
0719 {
0720     auto doc = new QTextDocument();
0721     doc->setHtml(QStringLiteral(
0722         "<html><body><p >url: <a href=\"https://www.kde.org\"><span>foo</span></a><br />line1<br />line2</p><p>-- </p><p>bla</p><br /></p></body></html>"));
0723 
0724     auto hb = new KPIMTextEdit::PlainTextMarkupBuilder();
0725     auto md = new KPIMTextEdit::MarkupDirector(hb);
0726     md->processDocument(doc);
0727     auto result = hb->getResult();
0728 
0729     auto regex = QRegularExpression(QStringLiteral("^url: foo\\[1\\]\\nline1\\nline2\\n-- \\nbla\\n\n\\n\\n--------\\n\\[1\\] https://www.kde.org\\n$"));
0730     QVERIFY(regex.match(result).hasMatch());
0731     delete md;
0732     delete hb;
0733     delete doc;
0734 }
0735 
0736 #include "moc_plaintextmarkupbuildertest.cpp"