Fix missing empty lines in Qt HTML when displayed in compliant browsers
authorEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>
Thu, 9 Jun 2011 08:06:00 +0000 (10:06 +0200)
committerQt Continuous Integration System <qt-info@nokia.com>
Fri, 10 Jun 2011 10:04:16 +0000 (12:04 +0200)
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 <p><br /></p> but
without altering the structure of the document.

Reviewed-by: Gunnar
(cherry picked from commit f541c78e1bc5b293466b40e6f10496199a4a5d73)

Change-Id: Ic0eae2c81609b8872eb2eb9344a3ec416cd09149
Reviewed-on: http://codereview.qt.nokia.com/445
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>
src/gui/text/qtextdocument.cpp
tests/auto/qtextdocument/tst_qtextdocument.cpp

index cfe4aee..41b6fe2 100644 (file)
@@ -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()),
index 1129219..68252ef 100644 (file)
@@ -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", "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"></p>\n");
+    expectedOutput.replace("EMPTYBLOCK", "<p style=\"-qt-paragraph-type:empty; height:1em; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"></p>\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"