summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRainer Keller <Rainer.Keller@qt.io>2020-01-07 09:13:21 +0100
committerRainer Keller <Rainer.Keller@qt.io>2020-01-13 09:25:39 +0100
commit76fbe75abee7d77911467d56630176f777e8ed78 (patch)
tree3f04e6aa0203fca2166d89ef9e32eab3fe261c25
parent18e06c37e10943f4b4f6d57b7044b9bce3a23202 (diff)
Remove empty block at beginning of imported markdown
An empty QTextDocument already contains a block; so when the formatting is fully determined, if the document is still empty, then instead of inserting a new block, we can set formatting on the cursor, which affects the pre-existing block, before inserting text. This avoids leaving a blank line (the default block) above the inserted content. Fixes: QTBUG-81060 Change-Id: I14e45e300a602493aa59680417d74d4c2b25862d Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
-rw-r--r--src/gui/text/qtextmarkdownimporter.cpp14
-rw-r--r--tests/auto/gui/text/qtextmarkdownimporter/tst_qtextmarkdownimporter.cpp36
2 files changed, 47 insertions, 3 deletions
diff --git a/src/gui/text/qtextmarkdownimporter.cpp b/src/gui/text/qtextmarkdownimporter.cpp
index 78d18a714b..88965046ce 100644
--- a/src/gui/text/qtextmarkdownimporter.cpp
+++ b/src/gui/text/qtextmarkdownimporter.cpp
@@ -207,7 +207,12 @@ int QTextMarkdownImporter::cbEnterBlock(int blockType, void *det)
charFmt.setFontWeight(QFont::Bold);
blockFmt.setHeadingLevel(int(detail->level));
m_needsInsertBlock = false;
- m_cursor->insertBlock(blockFmt, charFmt);
+ if (m_doc->isEmpty()) {
+ m_cursor->setBlockFormat(blockFmt);
+ m_cursor->setCharFormat(charFmt);
+ } else {
+ m_cursor->insertBlock(blockFmt, charFmt);
+ }
qCDebug(lcMD, "H%d", detail->level);
} break;
case MD_BLOCK_LI: {
@@ -592,7 +597,12 @@ void QTextMarkdownImporter::insertBlock()
blockFormat.setMarker(m_markerType);
if (!m_listStack.isEmpty())
blockFormat.setIndent(m_listStack.count());
- m_cursor->insertBlock(blockFormat, charFormat);
+ if (m_doc->isEmpty()) {
+ m_cursor->setBlockFormat(blockFormat);
+ m_cursor->setCharFormat(charFormat);
+ } else {
+ m_cursor->insertBlock(blockFormat, charFormat);
+ }
if (m_needsInsertList) {
m_listStack.push(m_cursor->createList(m_listFormat));
} else if (!m_listStack.isEmpty() && m_listItem) {
diff --git a/tests/auto/gui/text/qtextmarkdownimporter/tst_qtextmarkdownimporter.cpp b/tests/auto/gui/text/qtextmarkdownimporter/tst_qtextmarkdownimporter.cpp
index 2f0b877799..39a1370f6f 100644
--- a/tests/auto/gui/text/qtextmarkdownimporter/tst_qtextmarkdownimporter.cpp
+++ b/tests/auto/gui/text/qtextmarkdownimporter/tst_qtextmarkdownimporter.cpp
@@ -55,12 +55,13 @@ private slots:
void thematicBreaks();
void lists_data();
void lists();
+ void avoidBlankLineAtBeginning_data();
+ void avoidBlankLineAtBeginning();
};
void tst_QTextMarkdownImporter::headingBulletsContinuations()
{
const QStringList expectedBlocks = QStringList() <<
- "" << // we could do without this blank line before the heading, but currently it happens
"heading" <<
"bullet 1 continuation line 1, indented via tab" <<
"bullet 2 continuation line 2, indented via 4 spaces" <<
@@ -222,5 +223,38 @@ void tst_QTextMarkdownImporter::lists()
QCOMPARE(doc.toMarkdown(), rewrite);
}
+void tst_QTextMarkdownImporter::avoidBlankLineAtBeginning_data()
+{
+ QTest::addColumn<QString>("input");
+ QTest::addColumn<int>("expectedNumberOfParagraphs");
+
+ QTest::newRow("Text block") << QString("Markdown text") << 1;
+ QTest::newRow("Headline") << QString("Markdown text\n============") << 1;
+ QTest::newRow("Code block") << QString(" Markdown text") << 2;
+ QTest::newRow("Unordered list") << QString("* Markdown text") << 1;
+ QTest::newRow("Ordered list") << QString("1. Markdown text") << 1;
+ QTest::newRow("Blockquote") << QString("> Markdown text") << 1;
+}
+
+void tst_QTextMarkdownImporter::avoidBlankLineAtBeginning() // QTBUG-81060
+{
+ QFETCH(QString, input);
+ QFETCH(int, expectedNumberOfParagraphs);
+
+ QTextDocument doc;
+ QTextMarkdownImporter(QTextMarkdownImporter::DialectGitHub).import(&doc, input);
+ QTextFrame::iterator iterator = doc.rootFrame()->begin();
+ int i = 0;
+ while (!iterator.atEnd()) {
+ QTextBlock block = iterator.currentBlock();
+ // Make sure there is no empty paragraph at the beginning of the document
+ if (i == 0)
+ QVERIFY(!block.text().isEmpty());
+ ++iterator;
+ ++i;
+ }
+ QCOMPARE(i, expectedNumberOfParagraphs);
+}
+
QTEST_MAIN(tst_QTextMarkdownImporter)
#include "tst_qtextmarkdownimporter.moc"