diff options
Diffstat (limited to 'tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp')
-rw-r--r-- | tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp index fdc7997d35..de12862383 100644 --- a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp +++ b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp @@ -197,6 +197,12 @@ private slots: void clearUndoRedoStacks(); void mergeFontFamilies(); + void contentsChangeIndices_data(); + void contentsChangeIndices(); + + void insertHtmlWithComments_data(); + void insertHtmlWithComments(); + private: void backgroundImage_checkExpectedHtml(const QTextDocument &doc); void buildRegExpData(); @@ -1772,6 +1778,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 +1845,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); @@ -3658,5 +3722,100 @@ void tst_QTextDocument::clearUndoRedoStacks() } +void tst_QTextDocument::contentsChangeIndices_data() +{ + QTest::addColumn<QString>("html"); + // adding list entries change the entire block, so change position is + // not the same as the cursor position if this value is >= 0 + QTest::addColumn<int>("expectedBegin"); + + QTest::addRow("text") << "Test" << -1; + QTest::addRow("unnumbered list") << "<ul><li>Test</li></ul>" << 0; + QTest::addRow("numbered list") << "<ol><li>Test</li></ol>" << 0; + QTest::addRow("table") << "<table><tr><td>Test</td></tr></table>" << -1; +} + +void tst_QTextDocument::contentsChangeIndices() +{ + QFETCH(QString, html); + QFETCH(int, expectedBegin); + + QTextDocument doc; + QTestDocumentLayout *layout = new QTestDocumentLayout(&doc); + doc.setDocumentLayout(layout); + doc.setHtml(QString("<html><body>%1</body></html>").arg(html)); + + int documentLength = 0; + int cursorLength = 0; + int changeBegin = 0; + int changeRemoved = 0; + int changeAdded = 0; + connect(&doc, &QTextDocument::contentsChange, this, [&](int pos, int removed, int added){ + documentLength = doc.characterCount(); + + QTextCursor cursor(&doc); + cursor.movePosition(QTextCursor::End); + // includes end-of-paragraph character + cursorLength = cursor.position() + 1; + + changeBegin = pos; + changeRemoved = removed; + changeAdded = added; + }); + + QTextCursor cursor(&doc); + cursor.movePosition(QTextCursor::End); + if (expectedBegin < 0) + expectedBegin = cursor.position(); + cursor.insertBlock(); + + const int changeEnd = changeBegin + changeAdded; + + QVERIFY(documentLength > 0); + QCOMPARE(documentLength, cursorLength); + QVERIFY(documentLength >= changeEnd); + QCOMPARE(changeBegin, expectedBegin); + QCOMPARE(changeAdded - changeRemoved, 1); +} + +void tst_QTextDocument::insertHtmlWithComments_data() +{ + QTest::addColumn<QString>("html"); + QTest::addColumn<QStringList>("expectedBlocks"); + + QTest::newRow("commentless") << "<p>first</p><p>second</p><p>third</p>" + << QStringList { "first", "second", "third" }; + QTest::newRow("normal") << "<p>first</p><!--<p>second</p>--><p>third</p>" + << QStringList { "first", "third" }; + QTest::newRow("nonClosing") << "<p>first</p><!--<p>second</p><p>third</p>" + << QStringList { "first" }; + QTest::newRow("immediatelyClosing") << "<p>first</p><!----><p>second</p><p>third</p>" + << QStringList { "first", "second", "third" }; + QTest::newRow("fake") << "<p>first</p><!-<p>second</p><p>third</p>" + << QStringList { "first", "second", "third" }; + QTest::newRow("endingNonExistant") << "<p>first</p>--><p>second</p><p>third</p>" + << QStringList { "first", "-->", "second", "third" }; +} + +void tst_QTextDocument::insertHtmlWithComments() +{ + QFETCH(QString, html); + QFETCH(QStringList, expectedBlocks); + + QTextDocument doc; + doc.setHtml(html); + + QCOMPARE(doc.blockCount(), expectedBlocks.count()); + + QStringList blockContent; + auto currentBlock = doc.begin(); + while (currentBlock != doc.end()) { + blockContent.append(currentBlock.text()); + currentBlock = currentBlock.next(); + } + + QCOMPARE(blockContent, expectedBlocks); +} + QTEST_MAIN(tst_QTextDocument) #include "tst_qtextdocument.moc" |