diff options
author | Andy Shaw <andy.shaw@qt.io> | 2020-11-27 14:39:46 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-05-06 11:12:15 +0000 |
commit | 4f5c8fecaca5d875b19bc62fd8d8264f16eaedcd (patch) | |
tree | 7081fc2deb4a593e90c88fd26ecb5f475bc9447b /src | |
parent | 7bba5a5fc5db6c339d3df4169aabe424f68fe092 (diff) |
Write out the HTML correctly for nested lists
When we are having nested lists then we need to ensure that the HTML is
outputted correctly so that the closing list and item tags are placed
in the right order.
[ChangeLog][QtGui][QTextDocument] The output of toHtml() now handles
nested lists correctly.
Fixes: QTBUG-88374
Change-Id: I88afba0f897aeef78d4835a3124097fe6fd4d55e
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
(cherry picked from commit 72a5151403f107c445e20cf548ca2e7309c88ce7)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/text/qtextdocument.cpp | 29 | ||||
-rw-r--r-- | src/gui/text/qtextdocument_p.h | 1 |
2 files changed, 27 insertions, 3 deletions
diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp index e302a1e170..8f4bca0df8 100644 --- a/src/gui/text/qtextdocument.cpp +++ b/src/gui/text/qtextdocument.cpp @@ -3063,10 +3063,12 @@ void QTextHtmlExporter::emitBlock(const QTextBlock &block) if (fragmentMarkers && block.position() + block.length() == QTextDocumentPrivate::get(doc)->length()) html += QLatin1String("<!--EndFragment-->"); + QString closeTags; + if (pre) html += QLatin1String("</pre>"); else if (list) - html += QLatin1String("</li>"); + closeTags += QLatin1String("</li>"); else { int headingLevel = blockFormat.headingLevel(); if (headingLevel > 0 && headingLevel <= 6) @@ -3078,9 +3080,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("</ol>"); + closeTags += QLatin1String("</ol>"); else - html += QLatin1String("</ul>"); + closeTags += QLatin1String("</ul>"); + } + 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 |