diff --git a/src/gui/text/qtextdocument_p.cpp b/src/gui/text/qtextdocument_p.cpp index 69730256cb4..f73475226fd 100644 --- a/src/gui/text/qtextdocument_p.cpp +++ b/src/gui/text/qtextdocument_p.cpp @@ -382,8 +382,11 @@ int QTextDocumentPrivate::insert_block(int pos, uint strPos, int format, int blo Q_ASSERT(blocks.length() == fragments.length()); QTextBlockGroup *group = qobject_cast(objectForFormat(blockFormat)); - if (group) + if (group) { group->blockInserted(QTextBlock(this, b)); + docChangeOldLength--; + docChangeLength--; + } QTextFrame *frame = qobject_cast(objectForFormat(formats.format(format))); if (frame) { diff --git a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp index c028ae1b9c6..922226a3a3c 100644 --- a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp +++ b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp @@ -198,6 +198,9 @@ private slots: void resourceProvider(); + void contentsChangeIndices_data(); + void contentsChangeIndices(); + private: void backgroundImage_checkExpectedHtml(const QTextDocument &doc); void buildRegExpData(); @@ -3820,5 +3823,62 @@ void tst_QTextDocument::resourceProvider() QCOMPARE(providerCalled, 2); } +void tst_QTextDocument::contentsChangeIndices_data() +{ + QTest::addColumn("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("expectedBegin"); + + QTest::addRow("text") << "Test" << -1; + QTest::addRow("unnumbered list") << "
  • Test
" << 0; + QTest::addRow("numbered list") << "
  1. Test
" << 0; + QTest::addRow("table") << "
Test
" << -1; +} + +void tst_QTextDocument::contentsChangeIndices() +{ + QFETCH(QString, html); + QFETCH(int, expectedBegin); + + QTextDocument doc; + QTestDocumentLayout *layout = new QTestDocumentLayout(&doc); + doc.setDocumentLayout(layout); + doc.setHtml(QString("%1").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); +} + + QTEST_MAIN(tst_QTextDocument) #include "tst_qtextdocument.moc"