summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/gui/text/qtextdocument.cpp29
-rw-r--r--src/gui/text/qtextdocument_p.h1
-rw-r--r--tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp58
3 files changed, 85 insertions, 3 deletions
diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp
index 4a7af00a92..ee7b25640c 100644
--- a/src/gui/text/qtextdocument.cpp
+++ b/src/gui/text/qtextdocument.cpp
@@ -3032,10 +3032,12 @@ void QTextHtmlExporter::emitBlock(const QTextBlock &block)
if (fragmentMarkers && block.position() + block.length() == doc->docHandle()->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)
@@ -3047,9 +3049,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 35cedc67c0..d90710586a 100644
--- a/src/gui/text/qtextdocument_p.h
+++ b/src/gui/text/qtextdocument_p.h
@@ -405,6 +405,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 fdc7997d35..7bc9fe47c5 100644
--- a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp
+++ b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp
@@ -1772,6 +1772,59 @@ void tst_QTextDocument::toHtml_data()
<< QString("EMPTYBLOCK") +
QString("<ul style=\"margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;\"><li DEFAULTBLOCKSTYLE>Blah</li></ul>");
}
+ {
+ CREATE_DOC_AND_CURSOR();
+ const QString listHtml = "<ul><li>item-1</li><li>item-2<ul><li>item-2.1</li><li>item-2.2"
+ "<ul><li>item-2.2.1</li></ul></li><li>item-2.3<ul><li>item-2.3.1"
+ "</li></ul></li></ul></li><li>item-3</li></ul>";
+ cursor.insertHtml(listHtml);
+
+ QTest::newRow("nested-lists-one") << QTextDocumentFragment(&doc)
+ << QString("<ul DEFAULTULSTYLE 1;\"><li style=\" margin-top:12px; margin-bottom:0px; "
+ "margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">"
+ "item-1</li>\n<li DEFAULTBLOCKSTYLE>item-2\n<ul DEFAULTULSTYLE 2;\"><li "
+ "DEFAULTBLOCKSTYLE>item-2.1</li>\n<li DEFAULTBLOCKSTYLE>item-2.2\n<ul "
+ "DEFAULTULSTYLE 3;\"><li DEFAULTBLOCKSTYLE>item-2.2.1</li></ul></li>\n"
+ "<li DEFAULTBLOCKSTYLE>item-2.3\n<ul DEFAULTULSTYLE 3;\"><li DEFAULTBLOCKSTYLE>"
+ "item-2.3.1</li></ul></li></ul></li>\n<li DEFAULTLASTLISTYLE>item-3</li></ul>");
+ }
+ {
+ CREATE_DOC_AND_CURSOR();
+ const QString listHtml = "<ul><li>item-1</li><li>item-2<ul><li>item-2.1</li></ul></li></ul>";
+ cursor.insertHtml(listHtml);
+
+ QTest::newRow("nested-lists-two") << QTextDocumentFragment(&doc)
+ << QString("<ul DEFAULTULSTYLE 1;\"><li style=\" margin-top:12px; margin-bottom:0px; "
+ "margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">"
+ "item-1</li>\n<li DEFAULTLASTLISTYLE>item-2\n<ul DEFAULTULSTYLE 2;\"><li "
+ "DEFAULTBLOCKSTYLE>item-2.1</li></ul></li></ul>");
+ }
+ {
+ CREATE_DOC_AND_CURSOR();
+ const QString listHtml = "<ul><li>item-1</li><li>item-2<ul><li>item-2.1</li><li>item-2.2"
+ "</li></ul></li></ul>";
+ cursor.insertHtml(listHtml);
+
+ QTest::newRow("nested-lists-three") << QTextDocumentFragment(&doc)
+ << QString("<ul DEFAULTULSTYLE 1;\"><li style=\" margin-top:12px; margin-bottom:0px; "
+ "margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">"
+ "item-1</li>\n<li DEFAULTLASTLISTYLE>item-2\n<ul DEFAULTULSTYLE 2;\"><li "
+ "DEFAULTBLOCKSTYLE>item-2.1</li>\n<li DEFAULTBLOCKSTYLE>item-2.2</li></ul>"
+ "</li></ul>");
+ }
+ {
+ CREATE_DOC_AND_CURSOR();
+ const QString listHtml = "<ul><li>item-1.1</li><li>item-1.2<li></ul>"
+ "<ul><li>item-2.1</li></ul>";
+ cursor.insertHtml(listHtml);
+
+ QTest::newRow("not-nested-list") << QTextDocumentFragment(&doc)
+ << QString("<ul DEFAULTULSTYLE 1;\"><li style=\" margin-top:12px; margin-bottom:0px; "
+ "margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">"
+ "item-1.1</li>\n<li DEFAULTBLOCKSTYLE>item-1.2</li></ul>\n<ul DEFAULTULSTYLE 1;\">"
+ "<li style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; "
+ "margin-right:0px; -qt-block-indent:0; text-indent:0px;\">item-2.1</li></ul>");
+ }
}
void tst_QTextDocument::toHtml()
@@ -1786,6 +1839,11 @@ 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;\"><br /></p>\n");
+ expectedOutput.replace("DEFAULTULSTYLE", "style=\"margin-top: 0px; margin-bottom: 0px; "
+ "margin-left: 0px; margin-right: 0px; -qt-list-indent:");
+ expectedOutput.replace("DEFAULTLASTLISTYLE", "style=\" margin-top:0px; margin-bottom:12px; "
+ "margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"");
+
if (expectedOutput.endsWith(QLatin1Char('\n')))
expectedOutput.chop(1);
expectedOutput.append(htmlTail);