File indexing completed on 2024-04-28 11:35:09
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 0149 for (const QString &schema : std::as_const(schemas)) { 0150 for (QString url : std::as_const(urls)) { 0151 // by definition: if the URL is enclosed in brackets, the URL itself is not allowed 0152 // to contain the closing bracket, as this would be detected as the end of the URL 0153 if ((left.length() == 1) && (url.contains(right[0]))) { 0154 continue; 0155 } 0156 0157 // if the url contains a whitespace, it must be enclosed with brackets 0158 if ((url.contains(QLatin1Char('\n')) || url.contains(QLatin1Char('\t')) || url.contains(QLatin1Char(' '))) && left.isEmpty()) { 0159 continue; 0160 } 0161 0162 QString test(left + schema + url + right); 0163 KTextToHTMLHelper ll(test, left.length()); 0164 QString gotUrl = ll.getUrl(); 0165 0166 // we want to have the url without whitespace 0167 url.remove(QLatin1Char(' ')); 0168 url.remove(QLatin1Char('\n')); 0169 url.remove(QLatin1Char('\t')); 0170 0171 bool ok = (gotUrl == (schema + url)); 0172 if (!ok) { 0173 qCDebug(KCOREADDONS_DEBUG) << "got:" << gotUrl; 0174 } 0175 QVERIFY2(ok, qPrintable(test)); 0176 } 0177 } 0178 0179 QStringList urlsWithoutSchema; 0180 urlsWithoutSchema << QStringLiteral(".kde.org"); 0181 urlsWithoutSchema << QStringLiteral(".kde.org:1234/sub/path"); 0182 urlsWithoutSchema << QStringLiteral(".kde.org:1234/sub/path?a=1"); 0183 urlsWithoutSchema << QStringLiteral(".kde.org:1234/sub/path?a=1#anchor"); 0184 urlsWithoutSchema << QStringLiteral(".kde.org:1234/sub/path/special(123)?a=1#anchor"); 0185 urlsWithoutSchema << QStringLiteral(".kde.org:1234/sub/path:with:colon/special(123)?a=1#anchor"); 0186 urlsWithoutSchema << QStringLiteral(".kde.org:1234/sub/path:with:colon/special(123)?a=1#anchor[bla"); 0187 urlsWithoutSchema << QStringLiteral(".kde.org:1234/sub/path:with:colon/special(123)?a=1#anchor[bla]"); 0188 urlsWithoutSchema << QStringLiteral(".kde.org:1234/\nsub/path:with:colon/\nspecial(123)?\na=1#anchor[bla]"); 0189 urlsWithoutSchema << QStringLiteral(".kde.org:1234/ \n sub/path:with:colon/ \n\t \t special(123)?") + QStringLiteral("\n\t \n\t a=1#anchor[bla]"); 0190 0191 QStringList starts; 0192 starts << QStringLiteral("www") << QStringLiteral("ftp") << QStringLiteral("news:www"); 0193 0194 for (const QString &start : std::as_const(starts)) { 0195 for (QString url : std::as_const(urlsWithoutSchema)) { 0196 // by definition: if the URL is enclosed in brackets, the URL itself is not allowed 0197 // to contain the closing bracket, as this would be detected as the end of the URL 0198 if ((left.length() == 1) && (url.contains(right[0]))) { 0199 continue; 0200 } 0201 0202 // if the url contains a whitespace, it must be enclosed with brackets 0203 if ((url.contains(QLatin1Char('\n')) || url.contains(QLatin1Char('\t')) || url.contains(QLatin1Char(' '))) && left.isEmpty()) { 0204 continue; 0205 } 0206 0207 QString test(left + start + url + right); 0208 KTextToHTMLHelper ll(test, left.length()); 0209 QString gotUrl = ll.getUrl(); 0210 0211 // we want to have the url without whitespace 0212 url.remove(QLatin1Char(' ')); 0213 url.remove(QLatin1Char('\n')); 0214 url.remove(QLatin1Char('\t')); 0215 0216 bool ok = (gotUrl == (start + url)); 0217 if (!ok) { 0218 qCDebug(KCOREADDONS_DEBUG) << "got:" << gotUrl; 0219 } 0220 QVERIFY2(ok, qPrintable(gotUrl)); 0221 } 0222 } 0223 0224 // test max url length 0225 QString url = QStringLiteral("https://www.kde.org/this/is/a_very_loooooong_url/test/test/test"); 0226 { 0227 KTextToHTMLHelper ll(url, 0, 10); 0228 QVERIFY(ll.getUrl().isEmpty()); // url too long 0229 } 0230 { 0231 KTextToHTMLHelper ll(url, 0, url.length() - 1); 0232 QVERIFY(ll.getUrl().isEmpty()); // url too long 0233 } 0234 { 0235 KTextToHTMLHelper ll(url, 0, url.length()); 0236 QCOMPARE(ll.getUrl(), url); 0237 } 0238 { 0239 KTextToHTMLHelper ll(url, 0, url.length() + 1); 0240 QCOMPARE(ll.getUrl(), url); 0241 } 0242 0243 // mailto 0244 { 0245 QString addr = QStringLiteral("mailto:test@kde.org"); 0246 QString test(left + addr + right); 0247 KTextToHTMLHelper ll(test, left.length()); 0248 0249 QString gotUrl = ll.getUrl(); 0250 0251 bool ok = (gotUrl == addr); 0252 if (!ok) { 0253 qCDebug(KCOREADDONS_DEBUG) << "got:" << gotUrl; 0254 } 0255 QVERIFY2(ok, qPrintable(gotUrl)); 0256 } 0257 } 0258 0259 void KTextToHTMLTest::testHtmlConvert_data() 0260 { 0261 QTest::addColumn<QString>("plainText"); 0262 QTest::addColumn<KTextToHTML::Options>("flags"); 0263 QTest::addColumn<QString>("htmlText"); 0264 0265 // Linker error when using PreserveSpaces, therefore the hardcoded 0x01 or 0x09 0266 0267 // Test preserving whitespace correctly 0268 QTest::newRow("") << " foo" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << " foo"; 0269 QTest::newRow("") << " foo" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << " foo"; 0270 QTest::newRow("") << " foo " << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << " foo "; 0271 QTest::newRow("") << " foo " << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << " foo "; 0272 QTest::newRow("") << "bla bla bla bla bla" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "bla bla bla bla bla"; 0273 QTest::newRow("") << "bla bla bla \n bla bla bla " << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0274 << "bla bla bla <br />\n bla bla bla "; 0275 QTest::newRow("") << "bla bla bla" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "bla bla bla"; 0276 QTest::newRow("") << " bla bla \n bla bla a\n bla bla " << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0277 << " bla bla <br />\n bla bla a<br />\n" 0278 " bla bla "; 0279 0280 // Test highlighting with *, / and _ 0281 QTest::newRow("") << "Ce paragraphe _contient_ des mots ou des _groupes de mots_ à mettre en" 0282 " forme…" 0283 << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText) 0284 << "Ce paragraphe <u>_contient_</u> des mots ou des" 0285 " <u>_groupes de mots_</u> à mettre en forme…"; 0286 QTest::newRow("punctation-bug") << "Ce texte *a l'air* de _fonctionner_, à condition" 0287 " d’utiliser le guillemet ASCII." 0288 << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText) 0289 << "Ce texte <b>*a l'air*</b> de <u>_fonctionner_</u>, à" 0290 " condition d’utiliser le guillemet ASCII."; 0291 QTest::newRow("punctation-bug") << "Un répertoire /est/ un *dossier* où on peut mettre des" 0292 " *fichiers*." 0293 << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText) 0294 << "Un répertoire <i>/est/</i> un" 0295 " <b>*dossier*</b> où on peut mettre des <b>*fichiers*</b>."; 0296 QTest::newRow("punctation-bug") << "*BLA BLA BLA BLA*." << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText) 0297 << "<b>BLA BLA BLA BLA</b>."; 0298 QTest::newRow("") << "Je vais tenter de repérer des faux positif*" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText) 0299 << "Je vais tenter de repérer des faux positif*"; 0300 QTest::newRow("") << "*Ouais !* *Yes!*" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText) 0301 << "<b>*Ouais !*</b> <b>*Yes!*</b>"; 0302 0303 QTest::newRow("multispace") << "*Ouais foo*" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText) 0304 << "<b>*Ouais foo*</b>"; 0305 0306 QTest::newRow("multispace3") << "*Ouais: foo*" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText) 0307 << "<b>*Ouais: foo*</b>"; 0308 0309 QTest::newRow("multi-") << "** Ouais: foo **" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText) 0310 << "** Ouais: foo **"; 0311 0312 QTest::newRow("multi-") << "*** Ouais: foo ***" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText) 0313 << "*** Ouais: foo ***"; 0314 0315 QTest::newRow("nohtmlversion") << "* Ouais: foo *" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText) 0316 << "* Ouais: foo *"; 0317 0318 QTest::newRow("nohtmlversion2") << "*Ouais: foo *" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText) 0319 << "*Ouais: foo *"; 0320 0321 QTest::newRow("nohtmlversion3") << "* Ouais: foo*" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText) 0322 << "* Ouais: foo*"; 0323 0324 QTest::newRow("nohtmlversion3") << "* Ouais: *ff sfsdf* foo *" << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText) 0325 << "* Ouais: <b>*ff sfsdf*</b> foo *"; 0326 0327 QTest::newRow("") << "the /etc/{rsyslog.d,syslog-ng.d}/package.rpmnew file" 0328 << KTextToHTML::Options(KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText) 0329 << "the /etc/{rsyslog.d,syslog-ng.d}/package.rpmnew file"; 0330 0331 // This test has problems with the encoding, apparently. 0332 // QTest::newRow( "" ) << "*Ça fait plaisir de pouvoir utiliser des lettres accentuées dans du" 0333 // " texte mis en forme*." << 0x09 << "<b>Ça fait plaisir de pouvoir" 0334 // " utiliser des lettres accentuées dans du texte mis en forme</b>."; 0335 0336 // Bug reported by dfaure, the <hostname> would get lost 0337 QTest::newRow("") << "QUrl url(\"http://strange<hostname>/\");" << KTextToHTML::Options(KTextToHTML::ReplaceSmileys | KTextToHTML::HighlightText) 0338 << "QUrl url("<a href=\"http://strange<hostname>/\">" 0339 "http://strange<hostname>/</a>");"; 0340 0341 // Bug: 211128 - plain text emails should not replace ampersand & with & 0342 QTest::newRow("bug211128") << "https://green-site/?Ticket=85&Page=next" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0343 << "<a href=\"https://green-site/?Ticket=85&Page=next\">" 0344 "https://green-site/?Ticket=85&Page=next</a>"; 0345 0346 QTest::newRow("dotBeforeEnd") << "Look at this file: www.example.com/example.h" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0347 << "Look at this file: <a href=\"http://www.example.com/example.h\">" 0348 "www.example.com/example.h</a>"; 0349 QTest::newRow("dotInMiddle") << "Look at this file: www.example.com/.bashrc" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0350 << "Look at this file: <a href=\"http://www.example.com/.bashrc\">" 0351 "www.example.com/.bashrc</a>"; 0352 0353 // A dot at the end of an URL is explicitly ignored 0354 QTest::newRow("dotAtEnd") << "Look at this file: www.example.com/test.cpp." << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0355 << "Look at this file: <a href=\"http://www.example.com/test.cpp\">" 0356 "www.example.com/test.cpp</a>."; 0357 0358 // Bug 313719 - URL in parenthesis 0359 QTest::newRow("url-in-parenthesis-1") << "KDE (website https://www.kde.org)" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0360 << "KDE (website <a href=\"https://www.kde.org\">https://www.kde.org</a>)"; 0361 QTest::newRow("url-in-parenthesis-2") << "KDE website (https://www.kde.org)" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0362 << "KDE website (<a href=\"https://www.kde.org\">https://www.kde.org</a>)"; 0363 QTest::newRow("url-in-parenthesis-3") << "bla (https://www.kde.org - section 5.2)" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0364 << "bla (<a href=\"https://www.kde.org\">https://www.kde.org</a> - section 5.2)"; 0365 0366 // Fix url as foo <<url> <url>> when we concatened them. 0367 QTest::newRow("url-with-url") 0368 << "foo <https://www.kde.org/ <https://www.kde.org/>>" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0369 << "foo <<a href=\"https://www.kde.org/ \">https://www.kde.org/ </a><<a href=\"https://www.kde.org/\">https://www.kde.org/</a>>>"; 0370 0371 // Fix url exploit 0372 QTest::newRow("url-exec-html") << "https://\"><!--" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "https://"><!--"; 0373 0374 QTest::newRow("url-exec-html-2") << "https://192.168.1.1:\"><!--" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0375 << "https://192.168.1.1:"><!--"; 0376 0377 QTest::newRow("url-exec-html-3") << "https://<IP>:\"><!--" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "https://<IP>:"><!--"; 0378 0379 QTest::newRow("url-exec-html-4") << "https://<IP>:/\"><!--" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "https://<IP>:/"><!--"; 0380 0381 QTest::newRow("url-exec-html-5") << "https://<IP>:/\"><script>alert(1);</script><!--" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0382 << "https://<IP>:/"><script>alert(1);</script><!--"; 0383 0384 QTest::newRow("url-exec-html-6") << "https://<IP>:/\"><script>alert(1);</script><!--\nTest2" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0385 << "https://<IP>:/"><script>alert(1);</script><!--\nTest2"; 0386 0387 QTest::newRow("url-with-ref-in-[") << "https://www.kde.org[1]" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0388 << "<a href=\"https://www.kde.org\">https://www.kde.org</a>[1]"; 0389 0390 QTest::newRow("url-with-ref-in-[2") << "[http://www.example.org/][whatever]" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0391 << "[<a href=\"http://www.example.org/\">http://www.example.org/</a>][whatever]"; 0392 // Bug 346132 0393 QTest::newRow("url-with-ref-in-<") << "http://www.foo.bar<http://foo.bar/>" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0394 << "<a href=\"http://www.foo.bar\">http://www.foo.bar</a><<a href=\"http://foo.bar/\">http://foo.bar/</a>>"; 0395 0396 QTest::newRow("url-with-ref-in-]") << "[Please visit our booth 24-25 http://example.com/]" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0397 << "[Please visit our booth 24-25 <a href=\"http://example.com/\">http://example.com/</a>]"; 0398 0399 QTest::newRow("two url with space") << "http://www.kde.org/standards/kcfg/1.0 http://www.kde.org/" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0400 << "<a href=\"http://www.kde.org/standards/kcfg/1.0\">http://www.kde.org/standards/kcfg/1.0</a> <a " 0401 "href=\"http://www.kde.org/\">http://www.kde.org/</a>"; 0402 0403 // Bug kmail 0404 QTest::newRow("two url with space-2") 0405 << "@@ -55,6 +55,10 @@ xsi:schemaLocation=\"http://www.kde.org/standards/kcfg/1.0 http://www.kde.org/" 0406 << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0407 << "@@ -55,6 +55,10 @@ xsi:schemaLocation="<a href=\"http://www.kde.org/standards/kcfg/1.0\">http://www.kde.org/standards/kcfg/1.0</a> <a " 0408 "href=\"http://www.kde.org/\">http://www.kde.org/</a>"; 0409 0410 const auto opt = KTextToHTML::PreserveSpaces | KTextToHTML::ConvertPhoneNumbers; 0411 // tel: urls 0412 QTest::newRow("tel url compact") << "bla bla <tel:+491234567890> bla bla" << opt 0413 << "bla bla <<a href=\"tel:+491234567890\">tel:+491234567890</a>> bla bla"; 0414 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"; 0415 0416 // negative tel: url tests 0417 QTest::newRow("empty tel url") << "bla tel: blub" << opt << "bla tel: blub"; 0418 0419 // phone numbers 0420 QTest::newRow("tel compact international") << "call +49123456789, then hang up" << opt 0421 << "call <a href=\"tel:+49123456789\">+49123456789</a>, then hang up"; 0422 QTest::newRow("tel parenthesis/spaces international") 0423 << "phone:+33 (01) 12 34 56 78 blub" << opt << "phone:<a href=\"tel:+330112345678\">+33 (01) 12 34 56 78</a> blub"; 0424 QTest::newRow("tel dashes international") << "bla +44-321-1-234-567" << opt << "bla <a href=\"tel:+443211234567\">+44-321-1-234-567</a>"; 0425 QTest::newRow("tel dashes/spaces international") << "+1 123-456-7000 blub" << opt << "<a href=\"tel:+11234567000\">+1 123-456-7000</a> blub"; 0426 QTest::newRow("tel spaces international") << "bla +32 1 234 5678 blub" << opt << "bla <a href=\"tel:+3212345678\">+32 1 234 5678</a> blub"; 0427 QTest::newRow("tel slash domestic") << "bla 030/12345678 blub" << opt << "bla <a href=\"tel:03012345678\">030/12345678</a> blub"; 0428 QTest::newRow("tel slash/space domestic") << "Tel.: 089 / 12 34 56 78" << opt << "Tel.: <a href=\"tel:08912345678\">089 / 12 34 56 78</a>"; 0429 QTest::newRow("tel follow by parenthesis") << "Telefon: 0 18 05 / 12 23 46 (14 Cent/Min.*)" << opt 0430 << "Telefon: <a href=\"tel:01805122346\">0 18 05 / 12 23 46</a> (14 Cent/Min.*)"; 0431 QTest::newRow("tel space single digit at end") << "0123/123 456 7" << opt << "<a href=\"tel:01231234567\">0123/123 456 7</a>"; 0432 QTest::newRow("tel space around dash") << "bla +49 (0) 12 23 - 45 6000 blub" << opt 0433 << "bla <a href=\"tel:+4901223456000\">+49 (0) 12 23 - 45 6000</a> blub"; 0434 QTest::newRow("tel two numbers speparated by dash") 0435 << "bla +49 (0) 12 23 46 78 - +49 0123/123 456 78 blub" << opt 0436 << "bla <a href=\"tel:+49012234678\">+49 (0) 12 23 46 78</a> - <a href=\"tel:+49012312345678\">+49 0123/123 456 78</a> blub"; 0437 0438 // negative tests for phone numbers 0439 QTest::newRow("non-tel number") << "please send 1200 cakes" << opt << "please send 1200 cakes"; 0440 QTest::newRow("non-tel alpha-numeric") << "bla 1-123-456-ABCD blub" << opt << "bla 1-123-456-ABCD blub"; 0441 QTest::newRow("non-tel alpha prefix") << "ABCD0123-456-789" << opt << "ABCD0123-456-789"; 0442 QTest::newRow("non-tel date") << "bla 02/03/2019 blub" << opt << "bla 02/03/2019 blub"; 0443 QTest::newRow("non-tel too long") << "bla +012-4567890123456 blub" << opt << "bla +012-4567890123456 blub"; 0444 QTest::newRow("non-tel unbalanced") << "bla +012-456789(01 blub" << opt << "bla +012-456789(01 blub"; 0445 QTest::newRow("non-tel nested") << "bla +012-4(56(78)90)1 blub" << opt << "bla +012-4(56(78)90)1 blub"; 0446 QTest::newRow("tel extraction disabled") << "call +49123456789 now" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "call +49123456789 now"; 0447 0448 QTest::newRow("bug-414360") 0449 << "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" 0450 << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0451 << "<a " 0452 "href=\"https://www.openstreetmap.org/directions?engine=graphhopper_foot&route=44.85765%2C-0.55931%3B44.85713%2C-0.56117#map=18/44.85756/" 0453 "-0.56094\">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</a>"; 0455 0456 // xmpp bug 422291 0457 QTest::newRow("xmpp1") << "xmpp:username@server.tld" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0458 << "<a href=\"xmpp:username@server.tld\">xmpp:username@server.tld</a>"; 0459 QTest::newRow("xmpp2") << "xmpp:conversations@conference.siacs.eu" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0460 << "<a href=\"xmpp:conversations@conference.siacs.eu\">xmpp:conversations@conference.siacs.eu</a>"; 0461 QTest::newRow("xmpp3") << "xmpp:conversations@conference.siacs.eu?join" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) 0462 << "<a href=\"xmpp:conversations@conference.siacs.eu?join\">xmpp:conversations@conference.siacs.eu?join</a>"; 0463 0464 // Test news: only 0465 QTest::newRow("news") << "news: " << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "news: "; 0466 0467 QTest::newRow("ftp") << "ftp: " << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "ftp: "; 0468 QTest::newRow("mailto") << "mailto: " << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << "mailto: "; 0469 QTest::newRow("empty") << "" << KTextToHTML::Options(KTextToHTML::PreserveSpaces) << ""; 0470 } 0471 0472 void KTextToHTMLTest::testHtmlConvert() 0473 { 0474 QFETCH(QString, plainText); 0475 QFETCH(KTextToHTML::Options, flags); 0476 QFETCH(QString, htmlText); 0477 0478 QEXPECT_FAIL("punctation-bug", "Linklocator does not properly detect punctation as boundaries", Continue); 0479 0480 const QString actualHtml = KTextToHTML::convertToHtml(plainText, flags); 0481 QCOMPARE(actualHtml, htmlText); 0482 } 0483 0484 #include "moc_ktexttohtmltest.cpp"