From 4ebb0413f16ba1cb3cfce2c5e78982450972c745 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Thu, 24 Apr 2025 06:24:47 +0200 Subject: [PATCH] QTextMarkdownImporter: spell CRLF literal correctly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Amends eced22d7250fc7ba4dbafa1694bf149c2259d9ea tst_QTextMarkdownImporter::frontMatter(yaml + markdown with CRLFs) now does explicit replacement of LF with CRLF: git may convert the file, so we can't rely on the CI test system checking out a true copy of yaml-crlf.md; but QFile::open(ReadOnly | Text) ensures that we will have LF line endings, not CRLF. Pick-to: 6.8 Task-number: QTBUG-135284 Change-Id: I7eee5f41e7aea902a59ac06238e591ece016d2d3 Reviewed-by: Jan Arve Sæther (cherry picked from commit 66595c3efe125cc92333d7a71317cce529c52e92) Reviewed-by: Qt Cherry-pick Bot --- src/gui/text/qtextmarkdownimporter.cpp | 2 +- .../qtextmarkdownimporter/data/yaml-crlf.md | 10 ---------- .../tst_qtextmarkdownimporter.cpp | 18 ++++++++++++------ 3 files changed, 13 insertions(+), 17 deletions(-) delete mode 100644 tests/auto/gui/text/qtextmarkdownimporter/data/yaml-crlf.md diff --git a/src/gui/text/qtextmarkdownimporter.cpp b/src/gui/text/qtextmarkdownimporter.cpp index f0845427250..e7bab8be9f3 100644 --- a/src/gui/text/qtextmarkdownimporter.cpp +++ b/src/gui/text/qtextmarkdownimporter.cpp @@ -29,7 +29,7 @@ static const QChar qtmi_Newline = u'\n'; static const QChar qtmi_Space = u' '; static constexpr auto lfMarkerString() noexcept { return "---\n"_L1; } -static constexpr auto crlfMarkerString() noexcept { return "---r\n"_L1; } +static constexpr auto crlfMarkerString() noexcept { return "---\r\n"_L1; } // TODO maybe eliminate the margins after all views recognize BlockQuoteLevel, CSS can format it, etc. static const int qtmi_BlockQuoteIndent = diff --git a/tests/auto/gui/text/qtextmarkdownimporter/data/yaml-crlf.md b/tests/auto/gui/text/qtextmarkdownimporter/data/yaml-crlf.md deleted file mode 100644 index c3e52432b4b..00000000000 --- a/tests/auto/gui/text/qtextmarkdownimporter/data/yaml-crlf.md +++ /dev/null @@ -1,10 +0,0 @@ ---- -name: "Venus" -discoverer: "Galileo Galilei" -title: "A description of the planet Venus" -keywords: - - planets - - solar system - - astronomy ---- -*Venus* is the second planet from the Sun, orbiting it every 224.7 Earth days. diff --git a/tests/auto/gui/text/qtextmarkdownimporter/tst_qtextmarkdownimporter.cpp b/tests/auto/gui/text/qtextmarkdownimporter/tst_qtextmarkdownimporter.cpp index 1a71b48ee6c..ffb7d506cf2 100644 --- a/tests/auto/gui/text/qtextmarkdownimporter/tst_qtextmarkdownimporter.cpp +++ b/tests/auto/gui/text/qtextmarkdownimporter/tst_qtextmarkdownimporter.cpp @@ -645,20 +645,22 @@ void tst_QTextMarkdownImporter::fencedCodeBlocks() void tst_QTextMarkdownImporter::frontMatter_data() { QTest::addColumn("inputFile"); + QTest::addColumn("convertToCrLf"); QTest::addColumn("expectedFrontMatterSize"); QTest::addColumn("expectedBlockCount"); - QTest::newRow("yaml + markdown") << QFINDTESTDATA("data/yaml.md") << 140 << 1; - QTest::newRow("yaml + markdown with CRLFs") << QFINDTESTDATA("data/yaml-crlf.md") << 140 << 1; - QTest::newRow("yaml only") << QFINDTESTDATA("data/yaml-only.md") << 59 << 0; - QTest::newRow("malformed 1") << QFINDTESTDATA("data/front-marker-malformed1.md") << 0 << 1; - QTest::newRow("malformed 2") << QFINDTESTDATA("data/front-marker-malformed2.md") << 0 << 2; - QTest::newRow("malformed 3") << QFINDTESTDATA("data/front-marker-malformed3.md") << 0 << 1; + QTest::newRow("yaml + markdown") << QFINDTESTDATA("data/yaml.md") << false << 140 << 1; + QTest::newRow("yaml + markdown with CRLFs") << QFINDTESTDATA("data/yaml.md") << true << 147 << 1; + QTest::newRow("yaml only") << QFINDTESTDATA("data/yaml-only.md") << false << 59 << 0; + QTest::newRow("malformed 1") << QFINDTESTDATA("data/front-marker-malformed1.md") << false << 0 << 1; + QTest::newRow("malformed 2") << QFINDTESTDATA("data/front-marker-malformed2.md") << false << 0 << 2; + QTest::newRow("malformed 3") << QFINDTESTDATA("data/front-marker-malformed3.md") << false << 0 << 1; } void tst_QTextMarkdownImporter::frontMatter() { QFETCH(QString, inputFile); + QFETCH(bool, convertToCrLf); QFETCH(int, expectedFrontMatterSize); QFETCH(int, expectedBlockCount); @@ -666,6 +668,10 @@ void tst_QTextMarkdownImporter::frontMatter() QVERIFY(f.open(QFile::ReadOnly | QIODevice::Text)); QString md = QString::fromUtf8(f.readAll()); f.close(); + if (convertToCrLf) + md.replace("\n", "\r\n"); + qCDebug(lcTests) << inputFile << "begins with" << md.first(16).toLatin1().toHex(' '); + const int yamlBegin = md.indexOf("name:"); const int yamlEnd = md.indexOf("---", yamlBegin); const QString yaml = md.sliced(yamlBegin, yamlEnd - yamlBegin);