QTextMarkdownWriter: preserve empty lists

You can save a "skeletal" document with list items to fill in later,
the same as you can do in HTML or ODF format.  Reading them back via
QTextDocument::fromMarkdown() isn't always perfect though.

Fixes: QTBUG-79217
Change-Id: Iacdb3e6792250ebdead05f314c9e3d00546eeb9f
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
bb10
Shawn Rutledge 2019-10-14 17:59:16 +02:00
parent 524ab7b535
commit d1c6f7e5a2
3 changed files with 16 additions and 6 deletions

View File

@ -173,7 +173,8 @@ void QTextMarkdownWriter::writeFrame(const QTextFrame *frame)
if (lastWasList)
m_stream << Newline;
}
int endingCol = writeBlock(block, !table, table && tableRow == 0, nextIsDifferent);
int endingCol = writeBlock(block, !table, table && tableRow == 0,
nextIsDifferent && !block.textList());
m_doubleNewlineWritten = false;
if (table) {
QTextTableCell cell = table->cellAt(block.position());

View File

@ -171,14 +171,14 @@ void tst_QTextMarkdownImporter::lists_data()
// Some of these cases show odd behavior, which is subject to change
// as the importer and the writer are tweaked to fix bugs over time.
QTest::newRow("dot newline") << ".\n" << 0 << true << ".\n\n";
QTest::newRow("number dot newline") << "1.\n" << 1 << true << "";
QTest::newRow("star newline") << "*\n" << 1 << true << "";
QTest::newRow("hyphen newline") << "-\n" << 1 << true << "";
QTest::newRow("hyphen space newline") << "- \n" << 1 << true << "";
QTest::newRow("number dot newline") << "1.\n" << 1 << true << "1. \n";
QTest::newRow("star newline") << "*\n" << 1 << true << "* \n";
QTest::newRow("hyphen newline") << "-\n" << 1 << true << "- \n";
QTest::newRow("hyphen space newline") << "- \n" << 1 << true << "- \n";
QTest::newRow("hyphen space letter newline") << "- a\n" << 1 << false << "- a\n";
QTest::newRow("hyphen nbsp newline") <<
QString::fromUtf8("-\u00A0\n") << 0 << true << "-\u00A0\n\n";
QTest::newRow("nested empty lists") << "*\n *\n *\n" << 1 << true << "";
QTest::newRow("nested empty lists") << "*\n *\n *\n" << 1 << true << " * \n";
QTest::newRow("list nested in empty list") << "-\n * a\n" << 2 << false << "- \n * a\n";
QTest::newRow("lists nested in empty lists")
<< "-\n * a\n * b\n- c\n *\n + d\n" << 5 << false

View File

@ -50,6 +50,7 @@ private slots:
void testWriteParagraph_data();
void testWriteParagraph();
void testWriteList();
void testWriteEmptyList();
void testWriteNestedBulletLists_data();
void testWriteNestedBulletLists();
void testWriteNestedNumericLists();
@ -124,6 +125,14 @@ void tst_QTextMarkdownWriter::testWriteList()
"- ListItem 1\n- ListItem 2\n"));
}
void tst_QTextMarkdownWriter::testWriteEmptyList()
{
QTextCursor cursor(document);
cursor.createList(QTextListFormat::ListDisc);
QCOMPARE(documentToUnixMarkdown(), QString::fromLatin1("- \n"));
}
void tst_QTextMarkdownWriter::testWriteNestedBulletLists_data()
{
QTest::addColumn<bool>("checkbox");