diff options
author | Nicolas Werner <nicolas.werner@hotmail.de> | 2023-02-15 01:34:54 +0100 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2023-03-22 10:50:58 +0100 |
commit | 3f40a8b5b1f7d83130d6ec2b85a28a782072b6a9 (patch) | |
tree | 6968953bf1b6388f8bdcfb0a63dc26b7ee8a7dea /tests/auto/gui/text/qtextmarkdownwriter/tst_qtextmarkdownwriter.cpp | |
parent | b05540bd00d051c74e67d138f5148eb23e018996 (diff) |
Add QTextListFormat::start: html and markdown ordered list index offset
This is useful for a lot of applications that render text coming from
sources which already support arbitrarily numbered lists (like chat
applications for example). Application-side workarounds usually have
significant overhead; and this feature has been requested multiple times.
It should be possible to both read and write HTML with the <ol start="x">
attribute, and read and write Markdown with arbitrary numbers for the
first item in a list.
[ChangeLog][QtGui] QTextList now supports specifying a start index using
the new QTextList::{setStart, start} functions. The HTML start attribute
on ordered lists in rich text documents is now parsed and used when
rendering a text list. Non-negative indices in markdown lists are now
also parsed and written properly. This allows starting a list with a
different number than 1.
Fixes: QTBUG-30407
Fixes: QTBUG-65384
Task-number: QTBUG-107562
Change-Id: Ib35b9378d9134ffedaa2d92f728b0984793aa7c1
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'tests/auto/gui/text/qtextmarkdownwriter/tst_qtextmarkdownwriter.cpp')
-rw-r--r-- | tests/auto/gui/text/qtextmarkdownwriter/tst_qtextmarkdownwriter.cpp | 107 |
1 files changed, 106 insertions, 1 deletions
diff --git a/tests/auto/gui/text/qtextmarkdownwriter/tst_qtextmarkdownwriter.cpp b/tests/auto/gui/text/qtextmarkdownwriter/tst_qtextmarkdownwriter.cpp index 2b6b1ecca5..36b494e550 100644 --- a/tests/auto/gui/text/qtextmarkdownwriter/tst_qtextmarkdownwriter.cpp +++ b/tests/auto/gui/text/qtextmarkdownwriter/tst_qtextmarkdownwriter.cpp @@ -34,6 +34,7 @@ private slots: void testWriteNestedBulletLists_data(); void testWriteNestedBulletLists(); void testWriteNestedNumericLists(); + void testWriteNumericListWithStart(); void testWriteTable(); void rewriteDocument_data(); void rewriteDocument(); @@ -284,6 +285,7 @@ void tst_QTextMarkdownWriter::testWriteNestedNumericLists() list1->add(cursor.block()); QTextListFormat fmt2; + // Alpha "numbering" is not supported in markdown, so we'll actually get decimal. fmt2.setStyle(QTextListFormat::ListLowerAlpha); fmt2.setNumberSuffix(QLatin1String(")")); fmt2.setIndent(2); @@ -305,7 +307,24 @@ void tst_QTextMarkdownWriter::testWriteNestedNumericLists() list2->add(cursor.block()); const QString output = documentToUnixMarkdown(); - // There's no QTextList API to set the starting number so we hard-coded all lists to start at 1 (QTBUG-65384) + + #ifdef DEBUG_WRITE_OUTPUT + { + QFile out(QDir::temp().filePath(QLatin1String(QTest::currentTestFunction()) + ".md")); + out.open(QFile::WriteOnly); + out.write(output.toUtf8()); + out.close(); + } + { + QFile out(QDir::temp().filePath(QLatin1String(QTest::currentTestFunction()) + ".html")); + out.open(QFile::WriteOnly); + out.write(document->toHtml().toUtf8()); + out.close(); + } +#endif + + // While we can set the start index for a block, if list items intersect each other, they will + // still use the list numbering. const QString expected = QString::fromLatin1( "1. ListItem 1\n 1) ListItem 2\n 1. ListItem 3\n2. ListItem 4\n 2) ListItem 5\n"); if (output != expected && isMainFontFixed()) @@ -313,6 +332,92 @@ void tst_QTextMarkdownWriter::testWriteNestedNumericLists() QCOMPARE(output, expected); } +void tst_QTextMarkdownWriter::testWriteNumericListWithStart() +{ + QTextCursor cursor(document); + + // The first list will start at 2. + QTextListFormat fmt1; + fmt1.setStyle(QTextListFormat::ListDecimal); + fmt1.setStart(2); + QTextList *list1 = cursor.createList(fmt1); + cursor.insertText("ListItem 1"); + list1->add(cursor.block()); + + // This list uses the default start (1) again. + QTextListFormat fmt2; + // Alpha "numbering" is not supported in markdown, so we'll actually get decimal. + fmt2.setStyle(QTextListFormat::ListLowerAlpha); + fmt2.setNumberSuffix(QLatin1String(")")); + fmt2.setIndent(2); + QTextList *list2 = cursor.insertList(fmt2); + cursor.insertText("ListItem 2"); + + // Negative list numbers are disallowed by most Markdown implementations. This list will start + // at 1 for that reason. + QTextListFormat fmt3; + fmt3.setStyle(QTextListFormat::ListDecimal); + fmt3.setIndent(3); + fmt3.setStart(-1); + cursor.insertList(fmt3); + cursor.insertText("ListItem 3"); + + // Continuing list1, so the second item will have the number 3. + cursor.insertBlock(); + cursor.insertText("ListItem 4"); + list1->add(cursor.block()); + + // This will look out of place: it's in a different position than its list would suggest. + // Generates invalid markdown numbering (OK for humans, but md4c will parse it differently than we "meant"). + // TODO QTBUG-111707: the writer needs to add newlines, otherwise ListItem 5 becomes part of the text for ListItem 4. + cursor.insertBlock(); + cursor.insertText("ListItem 5"); + list2->add(cursor.block()); + + // 0 indexed lists are fine. + QTextListFormat fmt4; + fmt4.setStyle(QTextListFormat::ListDecimal); + fmt4.setStart(0); + QTextList *list4 = cursor.insertList(fmt4); + cursor.insertText("SecondList Item 0"); + list4->add(cursor.block()); + + // Ensure list numbers are incremented properly. + cursor.insertBlock(); + cursor.insertText("SecondList Item 1"); + list4->add(cursor.block()); + + const QString output = documentToUnixMarkdown(); + const QString expected = QString::fromLatin1( + R"(2. ListItem 1 + 1) ListItem 2 + 1. ListItem 3 +3. ListItem 4 + 2) ListItem 5 +0. SecondList Item 0 +1. SecondList Item 1 +)"); + +#ifdef DEBUG_WRITE_OUTPUT + { + QFile out(QDir::temp().filePath(QLatin1String(QTest::currentTestFunction()) + ".md")); + out.open(QFile::WriteOnly); + out.write(output.toUtf8()); + out.close(); + } + { + QFile out(QDir::temp().filePath(QLatin1String(QTest::currentTestFunction()) + ".html")); + out.open(QFile::WriteOnly); + out.write(document->toHtml().toUtf8()); + out.close(); + } +#endif + + if (output != expected && isMainFontFixed()) + QEXPECT_FAIL("", "fixed-pitch main font (QTBUG-103484)", Continue); + QCOMPARE(output, expected); +} + void tst_QTextMarkdownWriter::testWriteTable() { QTextCursor cursor(document); |