Commit 44192c56 authored by Laurent Montel's avatar Laurent Montel 😁

improve converthtmltoplaintext

parent b69a9796
......@@ -49,8 +49,34 @@ void ConvertHtmlToPlainTextTest::shouldReturnEmptyStringIfInputTextIsEmpty()
void ConvertHtmlToPlainTextTest::shouldReturnNotEmptyStringIfInputTextIsNotEmpty()
{
MessageViewer::ConvertHtmlToPlainText convert;
convert.setHtmlString(QLatin1String("foo bla"));
QVERIFY(!convert.generatePlainText().isEmpty());
const QString str = QLatin1String("foo bla");
convert.setHtmlString(str);
const QString result = convert.generatePlainText();
QVERIFY(!result.isEmpty());
QCOMPARE(result, QString(str + QLatin1String("\n")));
}
void ConvertHtmlToPlainTextTest::shouldConvertToPlainText_data()
{
QTest::addColumn<QString>( "inputText" );
QTest::addColumn<QString>( "convertedText" );
QTest::newRow( "plainText" ) << "foo" << "foo\n";
QTest::newRow( "htmlText" ) << "<html><body>Hi! This is a KDE test</body></html>" << "Hi! This is a KDE test\n";
QTest::newRow( "htmlTextWithBold" ) << "<html><body><b>Hi!</b> This is a KDE test</body></html>" << "*Hi!* This is a KDE test\n";
QTest::newRow( "htmlTextWithH1" ) << "<html><body><h1>Hi!</h1> This is a KDE test</body></html>" << "*Hi!*\nThis is a KDE test\n";
QTest::newRow( "htmlTextWithUnderLine" ) << "<html><body><u>Hi!</u> This is a KDE test</body></html>" << "_Hi!_ This is a KDE test\n";
}
void ConvertHtmlToPlainTextTest::shouldConvertToPlainText()
{
QFETCH( QString, inputText );
QFETCH( QString, convertedText );
MessageViewer::ConvertHtmlToPlainText convert;
convert.setHtmlString(inputText);
const QString result = convert.generatePlainText();
QVERIFY(!result.isEmpty());
QCOMPARE(result, convertedText);
}
QTEST_KDEMAIN(ConvertHtmlToPlainTextTest, NoGUI)
......@@ -34,6 +34,8 @@ private Q_SLOTS:
void shouldHaveDefaultValue();
void shouldReturnEmptyStringIfInputTextIsEmpty();
void shouldReturnNotEmptyStringIfInputTextIsNotEmpty();
void shouldConvertToPlainText_data();
void shouldConvertToPlainText();
};
#endif // CONVERTHTMLTOPLAINTEXTTEST_H
......@@ -22,6 +22,7 @@
#include <grantlee/plaintextmarkupbuilder.h>
#include <QTextDocument>
#include <QDebug>
using namespace MessageViewer;
ConvertHtmlToPlainText::ConvertHtmlToPlainText()
{
......@@ -45,7 +46,8 @@ QString ConvertHtmlToPlainText::generatePlainText()
Grantlee::PlainTextMarkupBuilder *pb = new Grantlee::PlainTextMarkupBuilder();
Grantlee::MarkupDirector *pmd = new Grantlee::MarkupDirector( pb );
QTextDocument *doc = new QTextDocument(mHtmlString);
QTextDocument *doc = new QTextDocument;
doc->setHtml(mHtmlString);
pmd->processDocument( doc );
QString plainText = pb->getResult();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment