From f541c78e1bc5b293466b40e6f10496199a4a5d73 Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Thu, 9 Jun 2011 10:06:00 +0200 Subject: Fix missing empty lines in Qt HTML when displayed in compliant browsers When QTextDocument exports HTML, it makes an effort to be compatible with its own importer, hence it has to be compatible with the dialect of HTML which Qt has developed over the years. One incorrect interpretation in Qt is that an empty paragraph is interpreted as an empty line. So if you use a QTextDocument to produce HTML for text where an empty line has been added, this empty line will not be visible when the document is viewed in a compliant browser. The fix is to set the height of the empty paragraph to 1em, so that it will match the current pixel size of the font, thus look the same as a


but without altering the structure of the document. Reviewed-by: Gunnar --- src/gui/text/qtextdocument.cpp | 2 +- tests/auto/qtextdocument/tst_qtextdocument.cpp | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp index fe3c993022..74b1c69243 100644 --- a/src/gui/text/qtextdocument.cpp +++ b/src/gui/text/qtextdocument.cpp @@ -2553,7 +2553,7 @@ void QTextHtmlExporter::emitBlockAttributes(const QTextBlock &block) const bool emptyBlock = block.begin().atEnd(); if (emptyBlock) { - html += QLatin1String("-qt-paragraph-type:empty;"); + html += QLatin1String("-qt-paragraph-type:empty; height:1em;"); } emitMargins(QString::number(format.topMargin()), diff --git a/tests/auto/qtextdocument/tst_qtextdocument.cpp b/tests/auto/qtextdocument/tst_qtextdocument.cpp index 1129219b1f..68252ef6ae 100644 --- a/tests/auto/qtextdocument/tst_qtextdocument.cpp +++ b/tests/auto/qtextdocument/tst_qtextdocument.cpp @@ -182,6 +182,8 @@ private slots: void copiedFontSize(); + void htmlExportImportBlockCount(); + private: void backgroundImage_checkExpectedHtml(const QTextDocument &doc); @@ -1582,7 +1584,7 @@ void tst_QTextDocument::toHtml() expectedOutput.replace("OPENDEFAULTBLOCKSTYLE", "style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"); expectedOutput.replace("DEFAULTBLOCKSTYLE", "style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\""); - expectedOutput.replace("EMPTYBLOCK", "

\n"); + expectedOutput.replace("EMPTYBLOCK", "

\n"); if (expectedOutput.endsWith(QLatin1Char('\n'))) expectedOutput.chop(1); expectedOutput.append(htmlTail); @@ -2760,5 +2762,27 @@ void tst_QTextDocument::copiedFontSize() QCOMPARE(cursorOutput.charFormat().font().pixelSize(), 24); } +void tst_QTextDocument::htmlExportImportBlockCount() +{ + QTextDocument document; + { + QTextCursor cursor(&document); + cursor.insertText("Foo"); + cursor.insertBlock(); + cursor.insertBlock(); + cursor.insertBlock(); + cursor.insertBlock(); + cursor.insertText("Bar"); + } + + QCOMPARE(document.blockCount(), 5); + QString html = document.toHtml(); + + document.clear(); + document.setHtml(html); + + QCOMPARE(document.blockCount(), 5); +} + QTEST_MAIN(tst_QTextDocument) #include "tst_qtextdocument.moc" -- cgit v1.2.3