Bug 207779

Summary: Kmail removes empty lines in HTML
Product: [Unmaintained] kmail Reporter: Tomas <nicco.ts>
Component: generalAssignee: kdepim bugs <kdepim-bugs>
Status: RESOLVED FIXED    
Severity: normal CC: bielen, business, masaj, mathias.v44, rado.kljucevsek
Priority: NOR    
Version: unspecified   
Target Milestone: ---   
Platform: openSUSE   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description Tomas 2009-09-18 13:11:08 UTC
Version:            (using KDE 4.3.1)
OS:                Linux
Installed from:    SuSE RPMs

This bug seems to be back again. I can't find it in backlog, but I remember it was one of the most annoying things in poor HTML Kmail support together with Replays destroying HTML formating of senders.

KMail Version 1.12.2
Version 4.3.1 (KDE 4.3.1) "release 163"

If I compose message in HTML mode, all empty lines are removed, when e-mail is sent out.
If I reply to some e-mail, then empty lines are removed in Sender's e-mail as well.

It has something to do with markup <BR> which is added by composer, but then is not accepted / is removed.

Thank you for fixing it.
Comment 1 Thomas McGuire 2009-09-18 13:28:30 UTC
This was a Qt bug we worked around in KDE.
With Qt 4.5.2, this seems to be fixed, and we removed the workaround, since it was causing other problems.

At least here it works, we even have a unit test for that now, in kdelibs.
Comment 2 Tomas 2009-09-18 14:39:10 UTC
(In reply to comment #1)
> This was a Qt bug we worked around in KDE.
> With Qt 4.5.2, this seems to be fixed, and we removed the workaround, since it
> was causing other problems.
> 
> At least here it works, we even have a unit test for that now, in kdelibs.
Hi Thomas,
I have libqt4-452 since 2nd July:
libqt4-4.5.2-51.1                             Thu 02 Jul 2009 10:08:18 PM CEST

but I noticed this issue after last upgrade:
kontact-4.3.1-160.1                           Wed 16 Sep 2009 02:56:16 PM CEST
kmail-4.3.1-160.1                             Wed 16 Sep 2009 02:53:56 PM CEST

For me it seems to be not working without the workaround you had mentioned.
Comment 3 Rado 2009-09-20 23:07:05 UTC
Version:           (using KDE 4.3.1)
OS:                Linux
Installed from:    Fedora 10
kdepim:            4.3.1-1.fc10
qt:                4.5.2-3.fc10
Architecture:      x86_64

I experienced the same problem with the latest upgrade to 4.3.1. This bug finally disappeared with 4.3.0. for me but it is here again. It completely ruins the view of a e-mail after it has been sent out. This can be seen afterward in the preview window. Strange enough if you try to resend such an e-mail the composer shows you the originally formatted text.

For me it is a nightmare. I will have to resend some messages and apologize to people because of the mess they received, all my new messages will have to be without HTML formatting, I have to change my signature, ...

Please fix it with high priority or revert back. Thank you.
Comment 4 Tomas 2009-09-21 09:43:45 UTC
(In reply to comment #3)
> Version:           (using KDE 4.3.1)
> OS:                Linux
> Installed from:    Fedora 10
> kdepim:            4.3.1-1.fc10
> qt:                4.5.2-3.fc10
> Architecture:      x86_64
> 
> I experienced the same problem with the latest upgrade to 4.3.1. This bug
> finally disappeared with 4.3.0. for me but it is here again. It completely
> ruins the view of a e-mail after it has been sent out. This can be seen
> afterward in the preview window. Strange enough if you try to resend such an
> e-mail the composer shows you the originally formatted text.
> 
> For me it is a nightmare. I will have to resend some messages and apologize to
> people because of the mess they received, all my new messages will have to be
> without HTML formatting, I have to change my signature, ...
> 
> Please fix it with high priority or revert back. Thank you.

I'm pleased I'm not alone in this problem. Nightmare called HTML support in KMail continue. Problem is, Kmail is the best e-mail client for KDE, apart HTML support, otherwise I would use Zimbra (It has great HTML support, same as Evolution or Thunderbird, but LDAP search in exchange contacts is missing).
Comment 5 Jaime Torres 2009-09-30 17:42:14 UTC
*** Bug 194378 has been marked as a duplicate of this bug. ***
Comment 6 Mathias VĂ©drines 2009-10-03 15:43:45 UTC
I can experience exactly the same problem running KMail 1.12.1 under KDE 4.3.1, on Fedora 11. 

kdepim-4.3.1-5.fc11.x86_64
qt-4.5.2-3.fc11.x86_64

I confirm this is a very annoying bug since the presentation of an email becomes awful as soon as we send it. Please fix it.
Comment 7 masaj 2009-10-07 08:46:09 UTC
Guys don't worry.

kdepim-kmail 4.3.2-1
qt 4.5.3-2

Still not repaired.
I'm very dissapointed, it's nice that Kontact contains a lot of modules and pretty things, but basic HTML support is still not supported, for that it becomes kind of "useful" in all ways..
Comment 8 Jaime Torres 2009-10-08 10:27:50 UTC
@masaj.
Let's do it the OSS way. Instead of just complain (we're sorry, but fixing > 1000 bugs in the spare time is not a speedy task), help KDE sorting out which bugs are still valid, just try to reproduce them. Join the bugsquad in that task. http://techbase.kde.org/Contribute/Bugsquad
Comment 9 Rado 2010-02-02 01:22:18 UTC
More information why this is happening is here:

http://osdir.com/ml/kdepim-bugs/2009-11/msg00075.html

The settings margin-top:0px; margin-bottom:0px; make the problem. This shouldn't be too hard to fix as the cause is known?

At least this bug can be finally confirmed ...
Comment 10 Tomas 2010-03-05 12:50:06 UTC
Now it is OK in Kmail, but other e-mail client see it without empty lines:
Bug 229527
Comment 11 Tomas 2010-05-06 15:18:53 UTC
Bug 229527 still exists in kmail-4.4.3-210.5
Comment 12 Salvatore 2010-05-09 19:07:52 UTC
*** This bug has been confirmed by popular vote. ***
Comment 13 Torgny Nyblom 2011-01-01 21:21:18 UTC
commit 366e26a75817bcf246bfe04453c5f69e198eee93
branch master
Author: Torgny Nyblom <kde@nyblom.org>
Date:   Sat Jan 1 21:17:50 2011 +0100

    Do not remove empty lines in HTML for Outlook
    
    Thanks to Frank Vanderham for the patch.
    
    BUG: 207779

diff --git a/messagecomposer/CMakeLists.txt b/messagecomposer/CMakeLists.txt
index 477bc29..53933fa 100644
--- a/messagecomposer/CMakeLists.txt
+++ b/messagecomposer/CMakeLists.txt
@@ -84,7 +84,7 @@ kde4_add_kcfg_files(messagecomposer_src
 
 kde4_add_library( messagecomposer ${LIBRARY_TYPE} ${messagecomposer_src} )
 target_link_libraries( messagecomposer ${KDE4_KIO_LIBS} ${KDEPIMLIBS_KMIME_LIBS} ${KDEPIMLIBS_AKONADI_KMIME_LIBS} ${KDEPIMLIBS_AKONADI_LIBS} ${KDEPIMLIBS_MAILTRANSPORT_LIBS} ${KDEPIMLIBS_KPIMTEXTEDIT_LIBS} ${KDEPIMLIBS_KPIMIDENTITIES_LIBS}
- messagecore messageviewer templateparser kleo kdepim ${QGPGME_LIBRARIES} )
+ messagecore messageviewer templateparser kleo kdepim ${QGPGME_LIBRARIES} ${QT_QTWEBKIT_LIBRARY} )
 set_target_properties( messagecomposer PROPERTIES VERSION ${GENERIC_LIB_VERSION} SOVERSION ${GENERIC_LIB_SOVERSION} )
 
 install( TARGETS messagecomposer ${INSTALL_TARGETS_DEFAULT_ARGS} )
diff --git a/messagecomposer/kmeditor.cpp b/messagecomposer/kmeditor.cpp
index 7ab1288..30e9b4c 100644
--- a/messagecomposer/kmeditor.cpp
+++ b/messagecomposer/kmeditor.cpp
@@ -39,6 +39,10 @@
 #include <QShortcut>
 #include <QTextLayout>
 #include <QTimer>
+#include <QWebElement>
+#include <QWebElementCollection>
+#include <QWebFrame>
+#include <QWebPage>
 
 using namespace KPIMTextEdit;
 
@@ -686,4 +690,39 @@ void KMeditor::fillComposerTextPart ( TextPart* textPart ) const
   }
 }
 
+QString KMeditor::toCleanHtml() const
+{
+  const QString textEditHTML = toHtml();
+
+  // construct a non-visual QWebPage - that'll hook us into Qt's HTML parser from WebKit
+  QWebPage webpage( 0 );
+  QWebFrame *webframe = webpage.mainFrame();
+  webframe->setHtml( textEditHTML );
+  QWebElement docElement = webframe->documentElement();
+
+  // fix 1 - empty lines should show as empty lines - MS Outlook treats margin-top:0px; as
+  // a non-existing line.
+  // Although we can simply remove the margin-top style property, we still get unwanted results
+  // if you have three or more empty lines. It's best to replace empty <p> elements with <br>.
+  // As per http://www.w3.org/TR/xhtml1/dtds.html#a_dtd_XHTML-1.0-Strict, <br> elements are still proper
+  // HTML.
+  QWebElementCollection paragraphs = docElement.findAll( QString::fromAscii( "p" ) );
+  foreach (QWebElement paraElement, paragraphs) {
+    QString paraContent = paraElement.toPlainText();
+    // Only make a change when the paragraph content is empty
+    if ( paraContent.isEmpty() ) {
+      paraElement.replace( QString::fromAscii( "<br />" ) );
+    }
+  }
+  // fix 2 - ordered and unordered lists - MS Outlook treats margin-left:0px; as
+  // a non-existing number; e.g: "1. First item" turns into "First Item"
+  QWebElementCollection lists = docElement.findAll( QString::fromAscii( "ol,ul" ) );
+  foreach (QWebElement listElement, lists) {
+    //TODO in the future, we may want to explicitly set the margin-left=0 with the Composer window itself, which
+    //     would be overwritten here again. This will likely require a rewrite of the KMEditor altogether, though.
+    listElement.setStyleProperty( QString::fromAscii( "margin-left" ), QString( ) );
+  }
+  return webframe->toHtml();
+}
+
 #include "kmeditor.moc"
diff --git a/messagecomposer/kmeditor.h b/messagecomposer/kmeditor.h
index af9bf89..b24e3ba 100644
--- a/messagecomposer/kmeditor.h
+++ b/messagecomposer/kmeditor.h
@@ -176,6 +176,12 @@ class MESSAGECOMPOSER_EXPORT KMeditor : public KPIMTextEdit::TextEdit
      */
     void fillComposerTextPart( Message::TextPart* textPart ) const;
 
+    /**
+     * Overloaded function from KRichTextEdit to deal with problems viewing HTML formatted
+     * emails in MS Outlook.
+     */
+    QString toCleanHtml() const;
+
   public Q_SLOTS:
 
     void slotAddQuotes();
Comment 14 Thomas McGuire 2011-01-04 20:16:24 UTC
SVN commit 1211736 by tmcguire:

Fix the HTML output in cleanHTML() again.

This fixes MS Outlook not displaying mails from KMail
correctly.

Thanks to "Frank Vanderham" <twelve dot eighty at gmail dot com>
for the patch!

BUG: 207779


 M  +41 -3     krichtextedit.cpp  


WebSVN link: http://websvn.kde.org/?view=rev&revision=1211736
Comment 15 Albert Astals Cid 2011-01-04 20:59:20 UTC
SVN commit 1211745 by aacid:

Backport r1211736 | tmcguire | 2011-01-04 19:15:21 +0000 (dt, 04 gen 2011) | 10 lines

Fix the HTML output in cleanHTML() again.

This fixes MS Outlook not displaying mails from KMail
correctly.

Thanks to "Frank Vanderham" <twelve dot eighty at gmail dot com>
for the patch!

BUG: 207779


 M  +41 -3     krichtextedit.cpp  


WebSVN link: http://websvn.kde.org/?view=rev&revision=1211745
Comment 16 Albert Astals Cid 2011-01-04 20:59:58 UTC
SVN commit 1211746 by aacid:

Backport r1211736 | tmcguire | 2011-01-04 19:15:21 +0000 (dt, 04 gen 2011) | 10 lines

Fix the HTML output in cleanHTML() again.

This fixes MS Outlook not displaying mails from KMail
correctly.

Thanks to "Frank Vanderham" <twelve dot eighty at gmail dot com>
for the patch!

BUG: 207779


 M  +41 -3     krichtextedit.cpp  


WebSVN link: http://websvn.kde.org/?view=rev&revision=1211746