diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp index bf8c10f0d9..2c0c3582ed 100644 --- a/src/gui/text/qtextdocument.cpp +++ b/src/gui/text/qtextdocument.cpp @@ -3078,10 +3078,12 @@ void QTextHtmlExporter::emitBlock(const QTextBlock &block) if (fragmentMarkers && block.position() + block.length() == QTextDocumentPrivate::get(doc)->length()) html += QLatin1String(""); + QString closeTags; + if (pre) html += QLatin1String(""); else if (list) - html += QLatin1String(""); + closeTags += QLatin1String(""); else { int headingLevel = blockFormat.headingLevel(); if (headingLevel > 0 && headingLevel <= 6) @@ -3093,9 +3095,30 @@ void QTextHtmlExporter::emitBlock(const QTextBlock &block) if (list) { if (list->itemNumber(block) == list->count() - 1) { // last item? close list if (isOrderedList(list->format().style())) - html += QLatin1String(""); + closeTags += QLatin1String(""); else - html += QLatin1String(""); + closeTags += QLatin1String(""); + } + const QTextBlock nextBlock = block.next(); + // If the next block is the beginning of a new deeper nested list, then we don't + // want to close the current list item just yet. This should be closed when this + // item is fully finished + if (nextBlock.isValid() && nextBlock.textList() && + nextBlock.textList()->itemNumber(nextBlock) == 0 && + nextBlock.textList()->format().indent() > list->format().indent()) { + QString lastTag; + if (!closingTags.isEmpty() && list->itemNumber(block) == list->count() - 1) + lastTag = closingTags.takeLast(); + lastTag.prepend(closeTags); + closingTags << lastTag; + } else if (list->itemNumber(block) == list->count() - 1) { + // If we are at the end of the list now then we can add in the closing tags for that + // current block + html += closeTags; + if (!closingTags.isEmpty()) + html += closingTags.takeLast(); + } else { + html += closeTags; } } diff --git a/src/gui/text/qtextdocument_p.h b/src/gui/text/qtextdocument_p.h index adebd0251f..b26d7657ec 100644 --- a/src/gui/text/qtextdocument_p.h +++ b/src/gui/text/qtextdocument_p.h @@ -438,6 +438,7 @@ private: QTextCharFormat defaultCharFormat; const QTextDocument *doc; bool fragmentMarkers; + QStringList closingTags; }; QT_END_NAMESPACE diff --git a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp index 43278d4d26..281d7e7d5c 100644 --- a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp +++ b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp @@ -1717,6 +1717,59 @@ void tst_QTextDocument::toHtml_data() << QString("EMPTYBLOCK") + QString("