Add tst_QTextMarkdownImporter::paragraphs

Test how QTextMarkdownImporter reacts to paragraphs ending with
premature LFs (created by shift-Enter in QTextEdit, for example) and
paragraphs broken by Unicode LineSeparator characters. It turns out that
the u2028's are retained and have the desired effect for rendering.
Currently we don't distinguish auto-wrapped paragraphs from paragraphs
that the user broke manually in QTextEdit (because we use a plain
newline for both).

Task-number: QTBUG-121475
Change-Id: Icaca4dba8be03b37ad6faa40ce1f9dfceadc48a8
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
Reviewed-by: Oliver Eftevaag <oliver.eftevaag@qt.io>
bb10
Shawn Rutledge 2024-02-28 15:44:11 -07:00
parent 61c9b8bb28
commit 08dde3ac0c
2 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,9 @@
This paragraph has enough text to auto-wrap when QTextMarkdownWriter writes it
to a markdown file. The wrapping should be around 80 columns.
This paragrah has been broken up into shorter lines.
Each line break is created
by hitting shift-enter in QTextEdit.
But it's treated as one QTextBlock.
This paragraph also has short lines.Each ends with a Unicode LineSeparator.

View File

@ -29,6 +29,7 @@ class tst_QTextMarkdownImporter : public QObject
Q_OBJECT
private slots:
void paragraphs();
void headingBulletsContinuations();
void thematicBreaks();
void lists_data();
@ -77,6 +78,43 @@ bool tst_QTextMarkdownImporter::isMainFontFixed()
return ret;
}
void tst_QTextMarkdownImporter::paragraphs()
{
QFile f(QFINDTESTDATA("data/paragraphs.md"));
QVERIFY(f.open(QFile::ReadOnly | QIODevice::Text));
QString md = QString::fromUtf8(f.readAll());
f.close();
int lineSeparatorCount = 0;
QTextDocument doc;
QTextMarkdownImporter(&doc, QTextMarkdownImporter::DialectGitHub).import(md);
QTextFrame::iterator iterator = doc.rootFrame()->begin();
int i = 0;
while (!iterator.atEnd()) {
QTextBlock block = iterator.currentBlock();
int lineSeparatorPos = block.text().indexOf(QChar::LineSeparator);
qCDebug(lcTests) << i << block.text();
while (lineSeparatorPos > 0) {
++lineSeparatorCount;
qCDebug(lcTests) << " LineSeparator @" << lineSeparatorPos;
lineSeparatorPos = block.text().indexOf(QChar::LineSeparator, lineSeparatorPos + 1);
}
++iterator;
++i;
}
QCOMPARE(doc.blockCount(), 3);
QCOMPARE(lineSeparatorCount, 2);
#ifdef DEBUG_WRITE_HTML
{
QFile out("/tmp/paragraphs.html");
out.open(QFile::WriteOnly);
out.write(doc.toHtml().toLatin1());
out.close();
}
#endif
}
void tst_QTextMarkdownImporter::headingBulletsContinuations()
{
const QStringList expectedBlocks = QStringList() <<