Commit a91f1402 authored by Sandro Knauß's avatar Sandro Knauß

Fix: issue with multipart/alternative and no text/plain part.

multipart/alternative should have two subparts text/plain and text/html.
If text/plain is missing we should switch back to the text/html instead
of shoing a empty message
parent 9c3c8757
......@@ -289,4 +289,67 @@ void ObjectTreeParserTester::test_HTMLExternal()
QVERIFY(testWriter.html.contains(QStringLiteral("<b>SOME</b> HTML text.")));
QVERIFY(!testWriter.html.contains(QStringLiteral("This HTML message may contain external references to images etc. For security/privacy reasons external references are not loaded.")));
}
}
\ No newline at end of file
}
void ObjectTreeParserTester::test_Alternative()
{
KMime::Message::Ptr msg = Test::readAndParseMail(QStringLiteral("alternative.mbox"));
QCOMPARE(msg->contents().size(), 2);
{
Test::HtmlWriter testWriter;
Test::CSSHelper testCSSHelper;
Test::ObjectTreeSource emptySource(&testWriter, &testCSSHelper);
ObjectTreeParser otp(&emptySource);
emptySource.setHtmlMail(false);
otp.parseObjectTree(msg.data());
QVERIFY(otp.htmlContent().isEmpty());
QVERIFY(otp.plainTextContent().contains(QStringLiteral("If you can see this text it means that your email client couldn't display our newsletter properly.")));
QVERIFY(testWriter.html.contains(QStringLiteral("If you can see this text it means that your email client couldn't display our newsletter properly.")));
}
{
Test::HtmlWriter testWriter;
Test::CSSHelper testCSSHelper;
Test::ObjectTreeSource emptySource(&testWriter, &testCSSHelper);
ObjectTreeParser otp(&emptySource);
emptySource.setHtmlMail(true);
otp.parseObjectTree(msg.data());
QVERIFY(otp.plainTextContent().contains(QStringLiteral("If you can see this text it means that your email client couldn't display our newsletter properly.")));
QVERIFY(otp.htmlContent().contains(QStringLiteral("Some <span style=\" font-weight:600;\">HTML</span> text</p>")));
QVERIFY(testWriter.html.contains(QStringLiteral("Some <span style=\" font-weight:600;\">HTML</span> text</p>")));
}
msg = Test::readAndParseMail(QStringLiteral("alternative-notext.mbox"));
QCOMPARE(msg->contents().size(), 1);
{
Test::HtmlWriter testWriter;
Test::CSSHelper testCSSHelper;
Test::ObjectTreeSource emptySource(&testWriter, &testCSSHelper);
ObjectTreeParser otp(&emptySource);
emptySource.setHtmlMail(false);
otp.parseObjectTree(msg.data());
QVERIFY(otp.plainTextContent().isEmpty());
QVERIFY(otp.htmlContent().isEmpty());
QVERIFY(testWriter.html.contains(QStringLiteral("Some *HTML* text")));
}
{
Test::HtmlWriter testWriter;
Test::CSSHelper testCSSHelper;
Test::ObjectTreeSource emptySource(&testWriter, &testCSSHelper);
ObjectTreeParser otp(&emptySource);
emptySource.setHtmlMail(true);
otp.parseObjectTree(msg.data());
QVERIFY(otp.plainTextContent().isEmpty());
QVERIFY(otp.htmlContent().contains(QStringLiteral("Some <span style=\" font-weight:600;\">HTML</span> text</p>")));
QVERIFY(testWriter.html.contains(QStringLiteral("Some <span style=\" font-weight:600;\">HTML</span> text</p>")));
}
}
......@@ -39,6 +39,7 @@ private Q_SLOTS:
void test_HTMLOnly();
void test_HTMLOnlyText();
void test_HTMLExternal();
void test_Alternative();
};
#endif
......@@ -1233,11 +1233,14 @@ public:
aBlock = HTMLBlock::Ptr(new AttachmentMarkBlock(htmlWriter.data(), mp->attachmentNode()));
}
MimeMessagePart::Ptr part(mp->mTextPart);
if (mp->viewHtml() && mp->mHTMLPart) {
htmlWriter->queue(render(mp->mHTMLPart));
} else if (mp->mTextNode) {
htmlWriter->queue(render(mp->mTextPart));
part = mp->mHTMLPart;
} else if (mp->text().trimmed().isEmpty()) {
part = mp->mHTMLPart;
}
htmlWriter->queue(render(part));
}
return htmlWriter->html;
}
......
Return-Path: <konqi@example.org>
Date: Wed, 8 Jun 2016 20:34:44 -0700
From: Konqi <konqi@example.org>
To: konqi@kde.org
Subject: A random subject with a empty text alternative
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="----=_Part_12345678_12345678"
------=_Part_12345678_12345678
Content-Transfer-Encoding: 7Bit
Content-Type: text/html; charset="windows-1252"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">Some <span style=" font-weight:600;">HTML</span> text</p></body></html>
------=_Part_12345678_12345678--
<?xml version="1.0" encoding="UTF8"?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<div style="position: relative; word-wrap: break-word">
<a name="att"/>
<div id="attachmentDiv">
<a name="att1"/>
<div id="attachmentDiv1">
<div style="position: relative">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">Some <span style=" font-weight:600;">HTML</span> text</p>
</div>
</div>
</div>
</div>
</body>
</html>
* MimeTreeParser::MessagePartList
* MimeTreeParser::AlternativeMessagePart
Return-Path: <konqi@example.org>
Date: Wed, 8 Jun 2016 20:34:44 -0700
From: Konqi <konqi@example.org>
To: konqi@kde.org
Subject: A random subject with a empty text alternative
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="----=_Part_12345678_12345678"
------=_Part_12345678_12345678
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
If you can see this text it means that your email client couldn't display o=
ur newsletter properly.
Please visit this link to view the newsletter on our website: http://www.go=
g.com/newsletter/
=2D GOG.com Team
------=_Part_12345678_12345678
Content-Transfer-Encoding: 7Bit
Content-Type: text/html; charset="windows-1252"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">Some <span style=" font-weight:600;">HTML</span> text</p></body></html>
------=_Part_12345678_12345678--
<?xml version="1.0" encoding="UTF8"?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<div style="position: relative; word-wrap: break-word">
<a name="att"/>
<div id="attachmentDiv">
<a name="att2"/>
<div id="attachmentDiv2">
<div style="position: relative">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">Some <span style=" font-weight:600;">HTML</span> text</p>
</div>
</div>
</div>
</div>
</body>
</html>
* MimeTreeParser::MessagePartList
* MimeTreeParser::AlternativeMessagePart
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