diff --git a/src/gui/text/qtextmarkdownimporter.cpp b/src/gui/text/qtextmarkdownimporter.cpp index 75b5a324c8..35feb0b3b1 100644 --- a/src/gui/text/qtextmarkdownimporter.cpp +++ b/src/gui/text/qtextmarkdownimporter.cpp @@ -490,7 +490,10 @@ int QTextMarkdownImporter::cbText(int textType, const char *text, unsigned size) break; #if QT_CONFIG(texthtmlparser) case MD_TEXT_ENTITY: - m_cursor->insertHtml(s); + if (m_htmlTagDepth) + m_htmlAccumulator += s; + else + m_cursor->insertHtml(s); s = QString(); break; #endif diff --git a/tests/auto/gui/text/qtextmarkdownimporter/tst_qtextmarkdownimporter.cpp b/tests/auto/gui/text/qtextmarkdownimporter/tst_qtextmarkdownimporter.cpp index 5a8df45d99..9b376c41f4 100644 --- a/tests/auto/gui/text/qtextmarkdownimporter/tst_qtextmarkdownimporter.cpp +++ b/tests/auto/gui/text/qtextmarkdownimporter/tst_qtextmarkdownimporter.cpp @@ -62,6 +62,8 @@ private slots: void nestedSpans(); void avoidBlankLineAtBeginning_data(); void avoidBlankLineAtBeginning(); + void fragmentsAndProperties_data(); + void fragmentsAndProperties(); void pathological_data(); void pathological(); @@ -378,6 +380,70 @@ void tst_QTextMarkdownImporter::avoidBlankLineAtBeginning() // QTBUG-81060 QCOMPARE(i, expectedNumberOfParagraphs); } +void tst_QTextMarkdownImporter::fragmentsAndProperties_data() +{ + QTest::addColumn("input"); + QTest::addColumn("fragmentToCheck"); + QTest::addColumn("expectedText"); + QTest::addColumn("propertyToCheck"); + QTest::addColumn("expectedPropertyValue"); + QTest::addColumn("expectedNumberOfBlocks"); + QTest::addColumn("expectedNumberOfFragments"); + + QTest::newRow("entitiesInHtmlFontBlock") // QTBUG-94245 + << QString("<123 test> test") + << 0 << "<123 test>" << QTextFormat::ForegroundBrush << QVariant(QBrush(QColor("red"))) + << 1 << 2; + QTest::newRow("entitiesInHtmlBoldBlock") // QTBUG-91222 + << QString("x&lt;") + << 0 << "x<" << QTextFormat::FontWeight << QVariant(700) + << 1 << 1; +} + +void tst_QTextMarkdownImporter::fragmentsAndProperties() +{ + QFETCH(QString, input); + QFETCH(int, fragmentToCheck); + QFETCH(QString, expectedText); + QFETCH(QTextFormat::Property, propertyToCheck); + QFETCH(QVariant, expectedPropertyValue); + QFETCH(int, expectedNumberOfBlocks); + QFETCH(int, expectedNumberOfFragments); + + QTextDocument doc; + QTextMarkdownImporter(QTextMarkdownImporter::DialectGitHub).import(&doc, input); +#ifdef DEBUG_WRITE_HTML + { + QFile out("/tmp/" + QLatin1String(QTest::currentDataTag()) + ".html"); + out.open(QFile::WriteOnly); + out.write(doc.toHtml().toLatin1()); + out.close(); + } +#endif + QTextFrame::iterator blockIter = doc.rootFrame()->begin(); + int blockCount = 0; + int fragCount = 0; + while (!blockIter.atEnd()) { + QTextBlock block = blockIter.currentBlock(); + auto fragIter = block.begin(); + while (!fragIter.atEnd()) { + auto frag = fragIter.fragment(); + qCDebug(lcTests) << "fragment" << fragCount << ':' << frag.text() << Qt::hex << frag.charFormat().properties(); + if (fragCount == fragmentToCheck) { + QVariant prop = frag.charFormat().property(propertyToCheck); + QCOMPARE(prop, expectedPropertyValue); + QCOMPARE(frag.text(), expectedText); + } + ++fragIter; + ++fragCount; + } + ++blockIter; + ++blockCount; + } + QCOMPARE(blockCount, expectedNumberOfBlocks); + QCOMPARE(fragCount, expectedNumberOfFragments); +} + void tst_QTextMarkdownImporter::pathological_data() { QTest::addColumn("warning");