File indexing completed on 2024-04-14 03:51:32

0001 /*
0002     SPDX-FileCopyrightText: 2005 Ingo Kloecker <kloecker@kde.org>
0003     SPDX-FileCopyrightText: 2007 Allen Winter <winter@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "ktexttohtmltest.h"
0009 #include "kcoreaddons_debug.h"
0010 
0011 #include "../src/lib/text/ktexttohtml.h"
0012 #include "../src/lib/text/ktexttohtml_p.h"
0013 
0014 #include <QDebug>
0015 #include <QTest>
0016 #include <QUrl>
0017 
0018 QTEST_MAIN(KTextToHTMLTest)
0019 
0020 Q_DECLARE_METATYPE(KTextToHTML::Options)
0021 
0022 #ifndef Q_OS_WIN
0023 void initLocale()
0024 {
0025     setenv("LC_ALL", "en_US.utf-8", 1);
0026 }
0027 Q_CONSTRUCTOR_FUNCTION(initLocale)
0028 #endif
0029 
0030 void KTextToHTMLTest::testGetEmailAddress()
0031 {
0032     // empty input
0033     const QString emptyQString;
0034     KTextToHTMLHelper ll1(emptyQString, 0);
0035     QVERIFY(ll1.getEmailAddress().isEmpty());
0036 
0037     // no '@' at scan position
0038     KTextToHTMLHelper ll2(QStringLiteral("foo@bar.baz"), 0);
0039     QVERIFY(ll2.getEmailAddress().isEmpty());
0040 
0041     // '@' in local part
0042     KTextToHTMLHelper ll3(QStringLiteral("foo@bar@bar.baz"), 7);
0043     QVERIFY(ll3.getEmailAddress().isEmpty());
0044 
0045     // empty local part
0046     KTextToHTMLHelper ll4(QStringLiteral("@bar.baz"), 0);
0047     QVERIFY(ll4.getEmailAddress().isEmpty());
0048     KTextToHTMLHelper ll5(QStringLiteral(".@bar.baz"), 1);
0049     QVERIFY(ll5.getEmailAddress().isEmpty());
0050     KTextToHTMLHelper ll6(QStringLiteral(" @bar.baz"), 1);
0051     QVERIFY(ll6.getEmailAddress().isEmpty());
0052     KTextToHTMLHelper ll7(QStringLiteral(".!#$%&'*+-/=?^_`{|}~@bar.baz"), qstrlen(".!#$%&'*+-/=?^_`{|}~"));
0053     QVERIFY(ll7.getEmailAddress().isEmpty());
0054 
0055     // allowed special chars in local part of address
0056     KTextToHTMLHelper ll8(QStringLiteral("a.!#$%&'*+-/=?^_`{|}~@bar.baz"), qstrlen("a.!#$%&'*+-/=?^_`{|}~"));
0057     QCOMPARE(ll8.getEmailAddress(), QStringLiteral("a.!#$%&'*+-/=?^_`{|}~@bar.baz"));
0058 
0059     // '@' in domain part
0060     KTextToHTMLHelper ll9(QStringLiteral("foo@bar@bar.baz"), 3);
0061     QVERIFY(ll9.getEmailAddress().isEmpty());
0062 
0063     // domain part without dot
0064     KTextToHTMLHelper lla(QStringLiteral("foo@bar"), 3);
0065     QVERIFY(lla.getEmailAddress().isEmpty());
0066     KTextToHTMLHelper llb(QStringLiteral("foo@bar."), 3);
0067     QVERIFY(llb.getEmailAddress().isEmpty());
0068     KTextToHTMLHelper llc(QStringLiteral(".foo@bar"), 4);
0069     QVERIFY(llc.getEmailAddress().isEmpty());
0070     KTextToHTMLHelper lld(QStringLiteral("foo@bar "), 3);
0071     QVERIFY(lld.getEmailAddress().isEmpty());
0072     KTextToHTMLHelper lle(QStringLiteral(" foo@bar"), 4);
0073     QVERIFY(lle.getEmailAddress().isEmpty());
0074     KTextToHTMLHelper llf(QStringLiteral("foo@bar-bar"), 3);
0075     QVERIFY(llf.getEmailAddress().isEmpty());
0076 
0077     // empty domain part
0078     KTextToHTMLHelper llg(QStringLiteral("foo@"), 3);
0079     QVERIFY(llg.getEmailAddress().isEmpty());
0080     KTextToHTMLHelper llh(QStringLiteral("foo@."), 3);
0081     QVERIFY(llh.getEmailAddress().isEmpty());
0082     KTextToHTMLHelper lli(QStringLiteral("foo@-"), 3);
0083     QVERIFY(lli.getEmailAddress().isEmpty());
0084 
0085     // simple address
0086     KTextToHTMLHelper llj(QStringLiteral("foo@bar.baz"), 3);
0087     QCOMPARE(llj.getEmailAddress(), QStringLiteral("foo@bar.baz"));
0088     KTextToHTMLHelper llk(QStringLiteral("foo@bar.baz."), 3);
0089     QCOMPARE(llk.getEmailAddress(), QStringLiteral("foo@bar.baz"));
0090     KTextToHTMLHelper lll(QStringLiteral(".foo@bar.baz"), 4);
0091     QCOMPARE(lll.getEmailAddress(), QStringLiteral("foo@bar.baz"));
0092     KTextToHTMLHelper llm(QStringLiteral("foo@bar.baz-"), 3);
0093     QCOMPARE(llm.getEmailAddress(), QStringLiteral("foo@bar.baz"));
0094     KTextToHTMLHelper lln(QStringLiteral("-foo@bar.baz"), 4);
0095     QCOMPARE(lln.getEmailAddress(), QStringLiteral("foo@bar.baz"));
0096     KTextToHTMLHelper llo(QStringLiteral("foo@bar.baz "), 3);
0097     QCOMPARE(llo.getEmailAddress(), QStringLiteral("foo@bar.baz"));
0098     KTextToHTMLHelper llp(QStringLiteral(" foo@bar.baz"), 4);
0099     QCOMPARE(llp.getEmailAddress(), QStringLiteral("foo@bar.baz"));
0100     KTextToHTMLHelper llq(QStringLiteral("foo@bar-bar.baz"), 3);
0101     QCOMPARE(llq.getEmailAddress(), QStringLiteral("foo@bar-bar.baz"));
0102 }
0103 
0104 void KTextToHTMLTest::testGetUrl()
0105 {
0106     QStringList brackets;
0107     brackets << QString() << QString(); // no brackets
0108     brackets << QStringLiteral("<") << QStringLiteral(">");
0109     brackets << QStringLiteral("[") << QStringLiteral("]");
0110     brackets << QStringLiteral("\"") << QStringLiteral("\"");
0111     brackets << QStringLiteral("<link>") << QStringLiteral("</link>");
0112 
0113     for (int i = 0; i < brackets.count(); i += 2) {
0114         testGetUrl2(brackets[i], brackets[i + 1]);
0115     }
0116 }
0117 
0118 void KTextToHTMLTest::testGetUrl2(const QString &left, const QString &right)
0119 {
0120     QStringList schemas;
0121     schemas << QStringLiteral("http://");
0122     schemas << QStringLiteral("https://");
0123     schemas << QStringLiteral("vnc://");
0124     schemas << QStringLiteral("fish://");
0125     schemas << QStringLiteral("ftp://");
0126     schemas << QStringLiteral("ftps://");
0127     schemas << QStringLiteral("sftp://");
0128     schemas << QStringLiteral("smb://");
0129     schemas << QStringLiteral("file://");
0130     schemas << QStringLiteral("irc://");
0131     schemas << QStringLiteral("ircs://");
0132 
0133     QStringList urls;
0134     urls << QStringLiteral("www.kde.org");
0135     urls << QStringLiteral("user@www.kde.org");
0136     urls << QStringLiteral("user:pass@www.kde.org");
0137     urls << QStringLiteral("user:pass@www.kde.org:1234");
0138     urls << QStringLiteral("user:pass@www.kde.org:1234/sub/path");
0139     urls << QStringLiteral("user:pass@www.kde.org:1234/sub/path?a=1");
0140     urls << QStringLiteral("user:pass@www.kde.org:1234/sub/path?a=1#anchor");
0141     urls << QStringLiteral("user:pass@www.kde.org:1234/sub/\npath  \n /long/  path \t  ?a=1#anchor");
0142     urls << QStringLiteral("user:pass@www.kde.org:1234/sub/path/special(123)?a=1#anchor");
0143     urls << QStringLiteral("user:pass@www.kde.org:1234/sub/path:with:colon/special(123)?a=1#anchor");
0144     urls << QStringLiteral("user:pass@www.kde.org:1234/sub/path:with:colon/special(123)?a=1#anchor[bla");
0145     urls << QStringLiteral("user:pass@www.kde.org:1234/sub/path:with:colon/special(123)?a=1#anchor[bla]");
0146     urls << QStringLiteral("user:pass@www.kde.org:1234/\nsub/path:with:colon/\nspecial(123)?\na=1#anchor[bla]");
0147     urls << QStringLiteral("user:pass@www.kde.org:1234/  \n  sub/path:with:colon/  \n\t   \t   special(123)?") + QStringLiteral("\n\t  \n\t   a=1#anchor[bla]");
0148     urls << QStringLiteral("en.wikipedia.org/wiki/%C3%98_(disambiguation)");
0149 
0150     for (const QString &schema : std::as_const(schemas)) {
0151         for (QString url : std::as_const(urls)) {
0152             // by definition: if the URL is enclosed in brackets, the URL itself is not allowed
0153             // to contain the closing bracket, as this would be detected as the end of the URL
0154             if ((left.length() == 1) && (url.contains(right[0]))) {
0155                 continue;
0156             }
0157 
0158             // if the url contains a whitespace, it must be enclosed with brackets
0159             if ((url.contains(QLatin1Char('\n')) || url.contains(QLatin1Char('\t')) || url.contains(QLatin1Char(' '))) && left.isEmpty()) {
0160                 continue;
0161             }
0162 
0163             QString test(left + schema + url + right);
0164             KTextToHTMLHelper ll(test, left.length());
0165             QString gotUrl = ll.getUrl();
0166 
0167             // we want to have the url without whitespace
0168             url.remove(QLatin1Char(' '));
0169             url.remove(QLatin1Char('\n'));
0170             url.remove(QLatin1Char('\t'));
0171 
0172             bool ok = (gotUrl == (schema + url));
0173             if (!ok) {
0174                 qCDebug(KCOREADDONS_DEBUG) << "got:" << gotUrl;
0175             }
0176             QVERIFY2(ok, qPrintable(test));
0177         }
0178     }
0179 
0180     QStringList urlsWithoutSchema;
0181     urlsWithoutSchema << QStringLiteral(".kde.org");
0182     urlsWithoutSchema << QStringLiteral(".kde.org:1234/sub/path");
0183     urlsWithoutSchema << QStringLiteral(".kde.org:1234/sub/path?a=1");
0184     urlsWithoutSchema << QStringLiteral(".kde.org:1234/sub/path?a=1#anchor");
0185     urlsWithoutSchema << QStringLiteral(".kde.org:1234/sub/path/special(123)?a=1#anchor");
0186     urlsWithoutSchema << QStringLiteral(".kde.org:1234/sub/path:with:colon/special(123)?a=1#anchor");
0187     urlsWithoutSchema << QStringLiteral(".kde.org:1234/sub/path:with:colon/special(123)?a=1#anchor[bla");
0188     urlsWithoutSchema << QStringLiteral(".kde.org:1234/sub/path:with:colon/special(123)?a=1#anchor[bla]");
0189     urlsWithoutSchema << QStringLiteral(".kde.org:1234/\nsub/path:with:colon/\nspecial(123)?\na=1#anchor[bla]");
0190     urlsWithoutSchema << QStringLiteral(".kde.org:1234/  \n  sub/path:with:colon/  \n\t   \t   special(123)?") + QStringLiteral("\n\t  \n\t   a=1#anchor[bla]");
0191 
0192     QStringList starts;
0193     starts << QStringLiteral("www") << QStringLiteral("ftp") << QStringLiteral("news:www");
0194 
0195     for (const QString &start : std::as_const(starts)) {
0196         for (QString url : std::as_const(urlsWithoutSchema)) {
0197             // by definition: if the URL is enclosed in brackets, the URL itself is not allowed
0198             // to contain the closing bracket, as this would be detected as the end of the URL
0199             if ((left.length() == 1) && (url.contains(right[0]))) {
0200                 continue;
0201             }
0202 
0203             // if the url contains a whitespace, it must be enclosed with brackets
0204             if ((url.contains(QLatin1Char('\n')) || url.contains(QLatin1Char('\t')) || url.contains(QLatin1Char(' '))) && left.isEmpty()) {
0205                 continue;
0206             }
0207 
0208             QString test(left + start + url + right);
0209             KTextToHTMLHelper ll(test, left.length());
0210             QString gotUrl = ll.getUrl();
0211 
0212             // we want to have the url without whitespace
0213             url.remove(QLatin1Char(' '));
0214             url.remove(QLatin1Char('\n'));
0215             url.remove(QLatin1Char('\t'));
0216 
0217             bool ok = (gotUrl == (start + url));
0218             if (!ok) {
0219                 qCDebug(KCOREADDONS_DEBUG) << "got:" << gotUrl;
0220             }
0221             QVERIFY2(ok, qPrintable(gotUrl));
0222         }
0223     }
0224 
0225     // test max url length
0226     QString url = QStringLiteral("https://www.kde.org/this/is/a_very_loooooong_url/test/test/test");
0227     {
0228         KTextToHTMLHelper ll(url, 0, 10);
0229         QVERIFY(ll.getUrl().isEmpty()); // url too long
0230     }
0231     {
0232         KTextToHTMLHelper ll(url, 0, url.length() - 1);
0233         QVERIFY(ll.getUrl().isEmpty()); // url too long
0234     }
0235     {
0236         KTextToHTMLHelper ll(url, 0, url.length());
0237         QCOMPARE(ll.getUrl(), url);
0238     }
0239     {
0240         KTextToHTMLHelper ll(url, 0, url.length() + 1);
0241         QCOMPARE(ll.getUrl(), url);
0242     }
0243 
0244     // mailto
0245     {
0246         QString addr = QStringLiteral("mailto:test@kde.org");
0247         QString test(left + addr + right);
0248         KTextToHTMLHelper ll(test, left.length());
0249 
0250         QString gotUrl = ll.getUrl();
0251 
0252         bool ok = (gotUrl == addr);
0253         if (!ok) {
0254             qCDebug(KCOREADDONS_DEBUG) << "got:" << gotUrl;
0255         }
0256         QVERIFY2(ok, qPrintable(gotUrl));
0257     }
0258 }
0259 
0260 void KTextToHTMLTest::testHtmlConvert_data()
0261 {
0262     QTest::addColumn<QString>("plainText");
0263     QTest::addColumn<KTextToHTML::Options>("flags");
0264     QTest::addColumn<QString>("htmlText");
0265 
0266     // Linker error when using PreserveSpaces, therefore the hardcoded 0x01 or 0x09
0267 
0268     // Test preserving whitespace correctly
0269     QTest::newRow("") << " foo" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "&nbsp;foo";
0270     QTest::newRow("") << "  foo" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "&nbsp;&nbsp;foo";
0271     QTest::newRow("") << "  foo  " << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "&nbsp;&nbsp;foo&nbsp;&nbsp;";
0272     QTest::newRow("") << "  foo " << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "&nbsp;&nbsp;foo&nbsp;";
0273     QTest::newRow("") << "bla bla bla bla bla" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "bla bla bla bla bla";
0274     QTest::newRow("") << "bla bla bla \n  bla bla bla " << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0275                       << "bla bla bla&nbsp;<br />\n&nbsp;&nbsp;bla bla bla&nbsp;";
0276     QTest::newRow("") << "bla bla  bla" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "bla bla&nbsp;&nbsp;bla";
0277     QTest::newRow("") << " bla bla \n bla bla a\n  bla bla " << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0278                       << "&nbsp;bla bla&nbsp;<br />\n&nbsp;bla bla a<br />\n"
0279                          "&nbsp;&nbsp;bla bla&nbsp;";
0280 
0281     // Test highlighting with *, / and _
0282     QTest::newRow("") << "Ce paragraphe _contient_ des mots ou des _groupes de mots_ à mettre en"
0283                          " forme…"
0284                       << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText)
0285                       << "Ce paragraphe <u>_contient_</u> des mots ou des"
0286                          " <u>_groupes de mots_</u> à mettre en forme…";
0287     QTest::newRow("punctation-bug") << "Ce texte *a l'air* de _fonctionner_, à condition"
0288                                        " d’utiliser le guillemet ASCII."
0289                                     << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText)
0290                                     << "Ce texte <b>*a l'air*</b> de <u>_fonctionner_</u>, à"
0291                                        " condition d’utiliser le guillemet ASCII.";
0292     QTest::newRow("punctation-bug") << "Un répertoire /est/ un *dossier* où on peut mettre des"
0293                                        " *fichiers*."
0294                                     << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText)
0295                                     << "Un répertoire <i>/est/</i> un"
0296                                        " <b>*dossier*</b> où on peut mettre des <b>*fichiers*</b>.";
0297     QTest::newRow("punctation-bug") << "*BLA BLA BLA BLA*." << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText)
0298                                     << "<b>BLA BLA BLA BLA</b>.";
0299     QTest::newRow("") << "Je vais tenter de repérer des faux positif*" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText)
0300                       << "Je vais tenter de repérer des faux positif*";
0301     QTest::newRow("") << "*Ouais !* *Yes!*" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText)
0302                       << "<b>*Ouais !*</b> <b>*Yes!*</b>";
0303 
0304     QTest::newRow("multispace") << "*Ouais     foo*" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText)
0305                                 << "<b>*Ouais     foo*</b>";
0306 
0307     QTest::newRow("multispace3") << "*Ouais:     foo*" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText)
0308                                  << "<b>*Ouais:     foo*</b>";
0309 
0310     QTest::newRow("multi-") << "** Ouais:  foo **" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText)
0311                             << "** Ouais:&nbsp;&nbsp;foo **";
0312 
0313     QTest::newRow("multi-") << "*** Ouais:  foo ***" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText)
0314                             << "*** Ouais:&nbsp;&nbsp;foo ***";
0315 
0316     QTest::newRow("nohtmlversion") << "* Ouais:     foo *" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText)
0317                                    << "* Ouais:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo *";
0318 
0319     QTest::newRow("nohtmlversion2") << "*Ouais:     foo *" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText)
0320                                     << "*Ouais:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo *";
0321 
0322     QTest::newRow("nohtmlversion3") << "* Ouais:     foo*" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText)
0323                                     << "* Ouais:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo*";
0324 
0325     QTest::newRow("nohtmlversion3") << "* Ouais: *ff sfsdf* foo *" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText)
0326                                     << "* Ouais: <b>*ff sfsdf*</b> foo *";
0327 
0328     QTest::newRow("") << "the /etc/{rsyslog.d,syslog-ng.d}/package.rpmnew file"
0329                       << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText)
0330                       << "the /etc/{rsyslog.d,syslog-ng.d}/package.rpmnew file";
0331 
0332     // This test has problems with the encoding, apparently.
0333     // QTest::newRow( "" ) << "*Ça fait plaisir de pouvoir utiliser des lettres accentuées dans du"
0334     //                       " texte mis en forme*." << 0x09 << "<b>Ça fait plaisir de pouvoir"
0335     //                       " utiliser des lettres accentuées dans du texte mis en forme</b>.";
0336 
0337     // Bug reported by dfaure, the <hostname> would get lost
0338     QTest::newRow("") << "QUrl url(\"http://strange<hostname>/\");" << KTextToHTML::Options(KTextToHTML::ReplaceSmileys | KTextToHTML::HighlightText)
0339                       << "QUrl url(&quot;<a href=\"http://strange<hostname>/\">"
0340                          "http://strange&lt;hostname&gt;/</a>&quot;);";
0341 
0342     // Bug: 211128 - plain text emails should not replace ampersand & with &amp;
0343     QTest::newRow("bug211128") << "https://green-site/?Ticket=85&Page=next" << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0344                                << "<a href=\"https://green-site/?Ticket=85&Page=next\">"
0345                                   "https://green-site/?Ticket=85&amp;Page=next</a>";
0346 
0347     QTest::newRow("dotBeforeEnd") << "Look at this file: www.example.com/example.h" << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0348                                   << "Look at this file: <a href=\"http://www.example.com/example.h\">"
0349                                      "www.example.com/example.h</a>";
0350     QTest::newRow("dotInMiddle") << "Look at this file: www.example.com/.bashrc" << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0351                                  << "Look at this file: <a href=\"http://www.example.com/.bashrc\">"
0352                                     "www.example.com/.bashrc</a>";
0353 
0354     // A dot at the end of an URL is explicitly ignored
0355     QTest::newRow("dotAtEnd") << "Look at this file: www.example.com/test.cpp." << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0356                               << "Look at this file: <a href=\"http://www.example.com/test.cpp\">"
0357                                  "www.example.com/test.cpp</a>.";
0358 
0359     // Bug 313719 - URL in parenthesis
0360     QTest::newRow("url-in-parenthesis-1") << "KDE (website https://www.kde.org)" << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0361                                           << "KDE (website <a href=\"https://www.kde.org\">https://www.kde.org</a>)";
0362     QTest::newRow("url-in-parenthesis-2") << "KDE website (https://www.kde.org)" << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0363                                           << "KDE website (<a href=\"https://www.kde.org\">https://www.kde.org</a>)";
0364     QTest::newRow("url-in-parenthesis-3") << "bla (https://www.kde.org - section 5.2)" << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0365                                           << "bla (<a href=\"https://www.kde.org\">https://www.kde.org</a> - section 5.2)";
0366 
0367     // Fix url as foo <<url> <url>> when we concatened them.
0368     QTest::newRow("url-with-url")
0369         << "foo <https://www.kde.org/ <https://www.kde.org/>>" << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0370         << "foo &lt;<a href=\"https://www.kde.org/ \">https://www.kde.org/ </a>&lt;<a href=\"https://www.kde.org/\">https://www.kde.org/</a>&gt;&gt;";
0371 
0372     // Fix url exploit
0373     QTest::newRow("url-exec-html") << "https://\"><!--" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "https://&quot;&gt;&lt;!--";
0374 
0375     QTest::newRow("url-exec-html-2") << "https://192.168.1.1:\"><!--" << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0376                                      << "https://192.168.1.1:&quot;&gt;&lt;!--";
0377 
0378     QTest::newRow("url-exec-html-3") << "https://<IP>:\"><!--" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "https://&lt;IP&gt;:&quot;&gt;&lt;!--";
0379 
0380     QTest::newRow("url-exec-html-4") << "https://<IP>:/\"><!--" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "https://&lt;IP&gt;:/&quot;&gt;&lt;!--";
0381 
0382     QTest::newRow("url-exec-html-5") << "https://<IP>:/\"><script>alert(1);</script><!--" << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0383                                      << "https://&lt;IP&gt;:/&quot;&gt;&lt;script&gt;alert(1);&lt;/script&gt;&lt;!--";
0384 
0385     QTest::newRow("url-exec-html-6") << "https://<IP>:/\"><script>alert(1);</script><!--\nTest2" << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0386                                      << "https://&lt;IP&gt;:/&quot;&gt;&lt;script&gt;alert(1);&lt;/script&gt;&lt;!--\nTest2";
0387 
0388     QTest::newRow("url-with-ref-in-[") << "https://www.kde.org[1]" << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0389                                        << "<a href=\"https://www.kde.org\">https://www.kde.org</a>[1]";
0390 
0391     QTest::newRow("url-with-ref-in-[2") << "[http://www.example.org/][whatever]" << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0392                                         << "[<a href=\"http://www.example.org/\">http://www.example.org/</a>][whatever]";
0393     // Bug 346132
0394     QTest::newRow("url-with-ref-in-<") << "http://www.foo.bar<http://foo.bar/>" << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0395                                        << "<a href=\"http://www.foo.bar\">http://www.foo.bar</a>&lt;<a href=\"http://foo.bar/\">http://foo.bar/</a>&gt;";
0396 
0397     QTest::newRow("url-with-ref-in-]") << "[Please visit our booth 24-25 http://example.com/]" << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0398                                        << "[Please visit our booth 24-25 <a href=\"http://example.com/\">http://example.com/</a>]";
0399 
0400     QTest::newRow("two url with space") << "http://www.kde.org/standards/kcfg/1.0 http://www.kde.org/" << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0401                                         << "<a href=\"http://www.kde.org/standards/kcfg/1.0\">http://www.kde.org/standards/kcfg/1.0</a> <a "
0402                                            "href=\"http://www.kde.org/\">http://www.kde.org/</a>";
0403 
0404     // Bug kmail
0405     QTest::newRow("two url with space-2")
0406         << "@@ -55,6 +55,10 @@ xsi:schemaLocation=\"http://www.kde.org/standards/kcfg/1.0 http://www.kde.org/"
0407         << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0408         << "@@ -55,6 +55,10 @@ xsi:schemaLocation=&quot;<a href=\"http://www.kde.org/standards/kcfg/1.0\">http://www.kde.org/standards/kcfg/1.0</a> <a "
0409            "href=\"http://www.kde.org/\">http://www.kde.org/</a>";
0410 
0411     const auto opt = KTextToHTML::PreserveSpaces | KTextToHTML::ConvertPhoneNumbers;
0412     // tel: urls
0413     QTest::newRow("tel url compact") << "bla bla <tel:+491234567890> bla bla" << opt
0414                                      << "bla bla &lt;<a href=\"tel:+491234567890\">tel:+491234567890</a>&gt; bla bla";
0415     QTest::newRow("tel url fancy") << "bla bla tel:+49-321-123456 bla bla" << opt << "bla bla <a href=\"tel:+49-321-123456\">tel:+49-321-123456</a> bla bla";
0416 
0417     // negative tel: url tests
0418     QTest::newRow("empty tel url") << "bla tel: blub" << opt << "bla tel: blub";
0419 
0420     // phone numbers
0421     QTest::newRow("tel compact international") << "call +49123456789, then hang up" << opt
0422                                                << "call <a href=\"tel:+49123456789\">+49123456789</a>, then hang up";
0423     QTest::newRow("tel parenthesis/spaces international")
0424         << "phone:+33 (01) 12 34 56 78 blub" << opt << "phone:<a href=\"tel:+330112345678\">+33 (01) 12 34 56 78</a> blub";
0425     QTest::newRow("tel dashes international") << "bla +44-321-1-234-567" << opt << "bla <a href=\"tel:+443211234567\">+44-321-1-234-567</a>";
0426     QTest::newRow("tel dashes/spaces international") << "+1 123-456-7000 blub" << opt << "<a href=\"tel:+11234567000\">+1 123-456-7000</a> blub";
0427     QTest::newRow("tel spaces international") << "bla +32 1 234 5678 blub" << opt << "bla <a href=\"tel:+3212345678\">+32 1 234 5678</a> blub";
0428     QTest::newRow("tel slash domestic") << "bla 030/12345678 blub" << opt << "bla <a href=\"tel:03012345678\">030/12345678</a> blub";
0429     QTest::newRow("tel slash/space domestic") << "Tel.: 089 / 12 34 56 78" << opt << "Tel.: <a href=\"tel:08912345678\">089 / 12 34 56 78</a>";
0430     QTest::newRow("tel follow by parenthesis") << "Telefon: 0 18 05 / 12 23 46 (14 Cent/Min.*)" << opt
0431                                                << "Telefon: <a href=\"tel:01805122346\">0 18 05 / 12 23 46</a> (14 Cent/Min.*)";
0432     QTest::newRow("tel space single digit at end") << "0123/123 456 7" << opt << "<a href=\"tel:01231234567\">0123/123 456 7</a>";
0433     QTest::newRow("tel space around dash") << "bla +49 (0) 12 23 - 45 6000 blub" << opt
0434                                            << "bla <a href=\"tel:+4901223456000\">+49 (0) 12 23 - 45 6000</a> blub";
0435     QTest::newRow("tel two numbers speparated by dash")
0436         << "bla +49 (0) 12 23 46 78 - +49 0123/123 456 78 blub" << opt
0437         << "bla <a href=\"tel:+49012234678\">+49 (0) 12 23 46 78</a> - <a href=\"tel:+49012312345678\">+49 0123/123 456 78</a> blub";
0438 
0439     // negative tests for phone numbers
0440     QTest::newRow("non-tel number") << "please send 1200 cakes" << opt << "please send 1200 cakes";
0441     QTest::newRow("non-tel alpha-numeric") << "bla 1-123-456-ABCD blub" << opt << "bla 1-123-456-ABCD blub";
0442     QTest::newRow("non-tel alpha prefix") << "ABCD0123-456-789" << opt << "ABCD0123-456-789";
0443     QTest::newRow("non-tel date") << "bla 02/03/2019 blub" << opt << "bla 02/03/2019 blub";
0444     QTest::newRow("non-tel too long") << "bla +012-4567890123456 blub" << opt << "bla +012-4567890123456 blub";
0445     QTest::newRow("non-tel unbalanced") << "bla +012-456789(01 blub" << opt << "bla +012-456789(01 blub";
0446     QTest::newRow("non-tel nested") << "bla +012-4(56(78)90)1 blub" << opt << "bla +012-4(56(78)90)1 blub";
0447     QTest::newRow("tel extraction disabled") << "call +49123456789 now" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "call +49123456789 now";
0448 
0449     QTest::newRow("bug-414360")
0450         << "https://www.openstreetmap.org/directions?engine=graphhopper_foot&route=44.85765%2C-0.55931%3B44.85713%2C-0.56117#map=18/44.85756/-0.56094"
0451         << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0452         << "<a "
0453            "href=\"https://www.openstreetmap.org/directions?engine=graphhopper_foot&route=44.85765%2C-0.55931%3B44.85713%2C-0.56117#map=18/44.85756/"
0454            "-0.56094\">https://www.openstreetmap.org/directions?engine=graphhopper_foot&amp;route=44.85765%2C-0.55931%3B44.85713%2C-0.56117#map=18/44.85756/"
0455            "-0.56094</a>";
0456 
0457     // xmpp bug 422291
0458     QTest::newRow("xmpp1") << "xmpp:username@server.tld" << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0459                            << "<a href=\"xmpp:username@server.tld\">xmpp:username@server.tld</a>";
0460     QTest::newRow("xmpp2") << "xmpp:conversations@conference.siacs.eu" << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0461                            << "<a href=\"xmpp:conversations@conference.siacs.eu\">xmpp:conversations@conference.siacs.eu</a>";
0462     QTest::newRow("xmpp3") << "xmpp:conversations@conference.siacs.eu?join" << KTextToHTML::Options(KTextToHTML::PreserveSpaces)
0463                            << "<a href=\"xmpp:conversations@conference.siacs.eu?join\">xmpp:conversations@conference.siacs.eu?join</a>";
0464 
0465     // Test news: only
0466     QTest::newRow("news") << "news: " << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "news:&nbsp;";
0467 
0468     QTest::newRow("ftp") << "ftp: " << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "ftp:&nbsp;";
0469     QTest::newRow("mailto") << "mailto: " << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "mailto:&nbsp;";
0470     QTest::newRow("empty") << "" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "";
0471 }
0472 
0473 void KTextToHTMLTest::testHtmlConvert()
0474 {
0475     QFETCH(QString, plainText);
0476     QFETCH(KTextToHTML::Options, flags);
0477     QFETCH(QString, htmlText);
0478 
0479     QEXPECT_FAIL("punctation-bug", "Linklocator does not properly detect punctation as boundaries", Continue);
0480 
0481     const QString actualHtml = KTextToHTML::convertToHtml(plainText, flags);
0482     QCOMPARE(actualHtml, htmlText);
0483 }
0484 
0485 #define s(x) QStringLiteral(x)
0486 
0487 void KTextToHTMLTest::testEmoticons_data()
0488 {
0489     QTest::addColumn<QString>("input");
0490     QTest::addColumn<QString>("output");
0491     QTest::newRow("empty") << QString() << QString();
0492     QTest::newRow("trailing") << s("Hello :-)") << s("Hello 🙂");
0493     QTest::newRow("embedded") << s("Hello :-) How are you?") << s("Hello 🙂 How are you?");
0494     QTest::newRow("leading") << s(":-( Bye") << s("🙁 Bye");
0495     QTest::newRow("embedded-html") << s("<b>:(</b>") << s("&lt;b&gt;:(&lt;/b&gt;");
0496     QTest::newRow("html-attribute") << s("<img src=\"...\" title=\":-)\" />") << s("&lt;img src=&quot;...&quot; title=&quot;:-)&quot; /&gt;");
0497     QTest::newRow("broken-1") << s(":))") << s("😆");
0498     QTest::newRow("broken-4") << s(":D and :-D are not the same as :d and :-d") << s("😀 and 😀 are not the same as :d and :-d");
0499     QTest::newRow("broken-5") << s("4d:D>:)F:/&gt;:-(:Pu:d9") << s("4d:D&gt;:)F:/&amp;gt;:-(:Pu:d9");
0500     QTest::newRow("broken-6") << s("&lt;::pvar:: test=1&gt;") << s("&amp;lt;::pvar:: test=1&amp;gt;");
0501     QTest::newRow("working-5") << s("(&amp;)") << s("(&amp;amp;)");
0502     QTest::newRow("working-6") << s("Bla (&nbsp;)") << s("Bla (&amp;nbsp;)");
0503     QTest::newRow("working-7") << s("a non-breaking space (&nbsp;) character") << s("a non-breaking space (&amp;nbsp;) character");
0504 
0505     QTest::newRow("angle-bracket-1") << s(">:)") << s("😈");
0506     QTest::newRow("angle-bracket-2") << s("<b>:)") << s("&lt;b&gt;:)");
0507 }
0508 
0509 void KTextToHTMLTest::testEmoticons()
0510 {
0511     QFETCH(QString, input);
0512     QFETCH(QString, output);
0513     QCOMPARE(KTextToHTML::convertToHtml(input, KTextToHTML::ReplaceSmileys | KTextToHTML::IgnoreUrls), output);
0514 }
0515 
0516 void KTextToHTMLTest::testEmoticonsNoReplace_data()
0517 {
0518     QTest::addColumn<QString>("input");
0519     QTest::newRow("empty") << QString();
0520     QTest::newRow("no-space-spearator") << s("Very happy! :-):-)");
0521     QTest::newRow("broken-2") << s("In a sentence:practical example");
0522     QTest::newRow("broken-8") << s("-+-[-:-(-:-)-:-]-+-");
0523     QTest::newRow("broken-9") << s("::shrugs::");
0524     QTest::newRow("broken-10") << s(":Ptesting:P");
0525     QTest::newRow("working-1") << s(":):)");
0526     QTest::newRow("working-4") << s("http://www.kde.org");
0527     QTest::newRow("working-3") << s("End of sentence:p");
0528     QTest::newRow("xmpp-1") << s("an xmpp emoticon (%)");
0529 }
0530 
0531 void KTextToHTMLTest::testEmoticonsNoReplace()
0532 {
0533     QFETCH(QString, input);
0534     QCOMPARE(KTextToHTML::convertToHtml(input, KTextToHTML::ReplaceSmileys | KTextToHTML::IgnoreUrls), input);
0535 }
0536 
0537 #include "moc_ktexttohtmltest.cpp"