From 08dde3ac0c5005ee48848a6fac9d03fe1fe2f184 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Wed, 28 Feb 2024 15:44:11 -0700 Subject: [PATCH] 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 Reviewed-by: Oliver Eftevaag --- .../qtextmarkdownimporter/data/paragraphs.md | 9 +++++ .../tst_qtextmarkdownimporter.cpp | 38 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 tests/auto/gui/text/qtextmarkdownimporter/data/paragraphs.md diff --git a/tests/auto/gui/text/qtextmarkdownimporter/data/paragraphs.md b/tests/auto/gui/text/qtextmarkdownimporter/data/paragraphs.md new file mode 100644 index 0000000000..3d89536376 --- /dev/null +++ b/tests/auto/gui/text/qtextmarkdownimporter/data/paragraphs.md @@ -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.
 diff --git a/tests/auto/gui/text/qtextmarkdownimporter/tst_qtextmarkdownimporter.cpp b/tests/auto/gui/text/qtextmarkdownimporter/tst_qtextmarkdownimporter.cpp index 5f539b993b..c78a1b29cb 100644 --- a/tests/auto/gui/text/qtextmarkdownimporter/tst_qtextmarkdownimporter.cpp +++ b/tests/auto/gui/text/qtextmarkdownimporter/tst_qtextmarkdownimporter.cpp @@ -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() <<