Coverage Report

Created: 2025-09-08 07:52

/src/qtbase/src/gui/text/qtextmarkdownimporter.cpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (C) 2019 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:critical reason:data-parser
4
5
#include "qtextmarkdownimporter_p.h"
6
#include "qtextdocumentfragment_p.h"
7
#include <QLoggingCategory>
8
#if QT_CONFIG(regularexpression)
9
#include <QRegularExpression>
10
#endif
11
#include <QTextCursor>
12
#include <QTextDocument>
13
#include <QTextDocumentFragment>
14
#include <QTextList>
15
#include <QTextTable>
16
#if QT_CONFIG(system_textmarkdownreader)
17
#include <md4c.h>
18
#else
19
#include "../../3rdparty/md4c/md4c.h"
20
#endif
21
22
QT_BEGIN_NAMESPACE
23
24
using namespace Qt::StringLiterals;
25
26
Q_STATIC_LOGGING_CATEGORY(lcMD, "qt.text.markdown")
27
28
static const QChar qtmi_Newline = u'\n';
29
static const QChar qtmi_Space = u' ';
30
31
0
static constexpr auto lfMarkerString() noexcept { return "---\n"_L1; }
32
0
static constexpr auto crlfMarkerString() noexcept { return "---\r\n"_L1; }
33
34
// TODO maybe eliminate the margins after all views recognize BlockQuoteLevel, CSS can format it, etc.
35
static const int qtmi_BlockQuoteIndent =
36
        40; // pixels, same as in QTextHtmlParserNode::initializeProperties
37
38
static_assert(int(QTextMarkdownImporter::FeatureCollapseWhitespace) == MD_FLAG_COLLAPSEWHITESPACE);
39
static_assert(int(QTextMarkdownImporter::FeaturePermissiveATXHeaders) == MD_FLAG_PERMISSIVEATXHEADERS);
40
static_assert(int(QTextMarkdownImporter::FeaturePermissiveURLAutoLinks) == MD_FLAG_PERMISSIVEURLAUTOLINKS);
41
static_assert(int(QTextMarkdownImporter::FeaturePermissiveMailAutoLinks) == MD_FLAG_PERMISSIVEEMAILAUTOLINKS);
42
static_assert(int(QTextMarkdownImporter::FeatureNoIndentedCodeBlocks) == MD_FLAG_NOINDENTEDCODEBLOCKS);
43
static_assert(int(QTextMarkdownImporter::FeatureNoHTMLBlocks) == MD_FLAG_NOHTMLBLOCKS);
44
static_assert(int(QTextMarkdownImporter::FeatureNoHTMLSpans) == MD_FLAG_NOHTMLSPANS);
45
static_assert(int(QTextMarkdownImporter::FeatureTables) == MD_FLAG_TABLES);
46
static_assert(int(QTextMarkdownImporter::FeatureStrikeThrough) == MD_FLAG_STRIKETHROUGH);
47
static_assert(int(QTextMarkdownImporter::FeatureUnderline) == MD_FLAG_UNDERLINE);
48
static_assert(int(QTextMarkdownImporter::FeaturePermissiveWWWAutoLinks) == MD_FLAG_PERMISSIVEWWWAUTOLINKS);
49
static_assert(int(QTextMarkdownImporter::FeaturePermissiveAutoLinks) == MD_FLAG_PERMISSIVEAUTOLINKS);
50
static_assert(int(QTextMarkdownImporter::FeatureTasklists) == MD_FLAG_TASKLISTS);
51
static_assert(int(QTextMarkdownImporter::FeatureNoHTML) == MD_FLAG_NOHTML);
52
static_assert(int(QTextMarkdownImporter::DialectCommonMark) == MD_DIALECT_COMMONMARK);
53
static_assert(int(QTextMarkdownImporter::DialectGitHub) ==
54
              (MD_DIALECT_GITHUB | MD_FLAG_UNDERLINE | QTextMarkdownImporter::FeatureFrontMatter));
55
56
// --------------------------------------------------------
57
// MD4C callback function wrappers
58
59
static int CbEnterBlock(MD_BLOCKTYPE type, void *detail, void *userdata)
60
0
{
61
0
    QTextMarkdownImporter *mdi = static_cast<QTextMarkdownImporter *>(userdata);
62
0
    return mdi->cbEnterBlock(int(type), detail);
63
0
}
64
65
static int CbLeaveBlock(MD_BLOCKTYPE type, void *detail, void *userdata)
66
0
{
67
0
    QTextMarkdownImporter *mdi = static_cast<QTextMarkdownImporter *>(userdata);
68
0
    return mdi->cbLeaveBlock(int(type), detail);
69
0
}
70
71
static int CbEnterSpan(MD_SPANTYPE type, void *detail, void *userdata)
72
0
{
73
0
    QTextMarkdownImporter *mdi = static_cast<QTextMarkdownImporter *>(userdata);
74
0
    return mdi->cbEnterSpan(int(type), detail);
75
0
}
76
77
static int CbLeaveSpan(MD_SPANTYPE type, void *detail, void *userdata)
78
0
{
79
0
    QTextMarkdownImporter *mdi = static_cast<QTextMarkdownImporter *>(userdata);
80
0
    return mdi->cbLeaveSpan(int(type), detail);
81
0
}
82
83
static int CbText(MD_TEXTTYPE type, const MD_CHAR *text, MD_SIZE size, void *userdata)
84
0
{
85
0
    QTextMarkdownImporter *mdi = static_cast<QTextMarkdownImporter *>(userdata);
86
0
    return mdi->cbText(int(type), text, size);
87
0
}
88
89
static void CbDebugLog(const char *msg, void *userdata)
90
0
{
91
0
    Q_UNUSED(userdata);
92
0
    qCDebug(lcMD) << msg;
93
0
}
94
95
// MD4C callback function wrappers
96
// --------------------------------------------------------
97
98
static Qt::Alignment MdAlignment(MD_ALIGN a, Qt::Alignment defaultAlignment = Qt::AlignLeft | Qt::AlignVCenter)
99
0
{
100
0
    switch (a) {
101
0
    case MD_ALIGN_LEFT:
102
0
        return Qt::AlignLeft | Qt::AlignVCenter;
103
0
    case MD_ALIGN_CENTER:
104
0
        return Qt::AlignHCenter | Qt::AlignVCenter;
105
0
    case MD_ALIGN_RIGHT:
106
0
        return Qt::AlignRight | Qt::AlignVCenter;
107
0
    default: // including MD_ALIGN_DEFAULT
108
0
        return defaultAlignment;
109
0
    }
110
0
}
111
112
QTextMarkdownImporter::QTextMarkdownImporter(QTextDocument *doc, QTextMarkdownImporter::Features features)
113
0
  : m_cursor(doc)
114
0
  , m_monoFont(QFontDatabase::systemFont(QFontDatabase::FixedFont))
115
0
  , m_features(features)
116
0
{
117
0
}
118
119
QTextMarkdownImporter::QTextMarkdownImporter(QTextDocument *doc, QTextDocument::MarkdownFeatures features)
120
0
  : QTextMarkdownImporter(doc, static_cast<QTextMarkdownImporter::Features>(int(features)))
121
0
{
122
0
}
123
124
/*! \internal
125
    Split any Front Matter from the Markdown document \a md.
126
    Returns a pair of QStringViews: if \a md begins with qualifying Front Matter
127
    (according to the specification at https://jekyllrb.com/docs/front-matter/ ),
128
    put it into the \c frontMatter view, omitting both markers; and put the remaining
129
    Markdown into \c rest. If no Front Matter is found, return all of \a md in \c rest.
130
*/
131
static auto splitFrontMatter(QStringView md)
132
0
{
133
0
    struct R {
134
0
        QStringView frontMatter, rest;
135
0
        explicit operator bool() const noexcept { return !frontMatter.isEmpty(); }
136
0
    };
137
138
0
    const auto NotFound = R{{}, md};
139
140
    /*  Front Matter must start with '---\n' or '---\r\n' on the very first line,
141
        and Front Matter must end with another such line.
142
        If that is not the case, we return NotFound: then the whole document is
143
        to be passed on to the Markdown parser, in which '---\n' is interpreted
144
        as a "thematic break" (like <hr/> in HTML). */
145
0
    QLatin1StringView marker;
146
0
    if (md.startsWith(lfMarkerString()))
147
0
        marker = lfMarkerString();
148
0
    else if (md.startsWith(crlfMarkerString()))
149
0
        marker = crlfMarkerString();
150
0
    else
151
0
        return NotFound;
152
153
0
    const auto frontMatterStart = marker.size();
154
0
    const auto endMarkerPos = md.indexOf(marker, frontMatterStart);
155
156
0
    if (endMarkerPos < 0 || md[endMarkerPos - 1] != QChar::LineFeed)
157
0
        return NotFound;
158
159
0
    Q_ASSERT(frontMatterStart < md.size());
160
0
    Q_ASSERT(endMarkerPos < md.size());
161
0
    const auto frontMatter = md.sliced(frontMatterStart, endMarkerPos - frontMatterStart);
162
0
    return R{frontMatter, md.sliced(endMarkerPos + marker.size())};
163
0
}
164
165
void QTextMarkdownImporter::import(const QString &markdown)
166
0
{
167
0
    MD_PARSER callbacks = {
168
0
        0, // abi_version
169
0
        unsigned(m_features),
170
0
        &CbEnterBlock,
171
0
        &CbLeaveBlock,
172
0
        &CbEnterSpan,
173
0
        &CbLeaveSpan,
174
0
        &CbText,
175
0
        &CbDebugLog,
176
0
        nullptr // syntax
177
0
    };
178
0
    QTextDocument *doc = m_cursor.document();
179
0
    const auto defaultFont = doc->defaultFont();
180
0
    m_paragraphMargin = defaultFont.pointSize() * 2 / 3;
181
0
    doc->clear();
182
0
    if (defaultFont.pointSize() != -1)
183
0
        m_monoFont.setPointSize(defaultFont.pointSize());
184
0
    else
185
0
        m_monoFont.setPixelSize(defaultFont.pixelSize());
186
0
    qCDebug(lcMD) << "default font" << defaultFont << "mono font" << m_monoFont;
187
0
    QStringView md = markdown;
188
189
0
    if (m_features.testFlag(QTextMarkdownImporter::FeatureFrontMatter)) {
190
0
        if (const auto split = splitFrontMatter(md)) {
191
0
            doc->setMetaInformation(QTextDocument::FrontMatter, split.frontMatter.toString());
192
0
            qCDebug(lcMD) << "extracted FrontMatter: size" << split.frontMatter.size();
193
0
            md = split.rest;
194
0
        }
195
0
    }
196
197
0
    const auto mdUtf8 = md.toUtf8();
198
0
    m_cursor.beginEditBlock();
199
0
    md_parse(mdUtf8.constData(), MD_SIZE(mdUtf8.size()), &callbacks, this);
200
0
    m_cursor.endEditBlock();
201
0
}
202
203
int QTextMarkdownImporter::cbEnterBlock(int blockType, void *det)
204
0
{
205
0
    m_blockType = blockType;
206
0
    switch (blockType) {
207
0
    case MD_BLOCK_P:
208
0
        if (!m_listStack.isEmpty())
209
0
            qCDebug(lcMD, m_listItem ? "P of LI at level %d"  : "P continuation inside LI at level %d", int(m_listStack.size()));
210
0
        else
211
0
            qCDebug(lcMD, "P");
212
0
        m_needsInsertBlock = true;
213
0
        break;
214
0
    case MD_BLOCK_QUOTE:
215
0
        ++m_blockQuoteDepth;
216
0
        qCDebug(lcMD, "QUOTE level %d", m_blockQuoteDepth);
217
0
        break;
218
0
    case MD_BLOCK_CODE: {
219
0
        MD_BLOCK_CODE_DETAIL *detail = static_cast<MD_BLOCK_CODE_DETAIL *>(det);
220
0
        m_codeBlock = true;
221
0
        m_blockCodeLanguage = QLatin1StringView(detail->lang.text, int(detail->lang.size));
222
0
        m_blockCodeFence = detail->fence_char;
223
0
        QString info = QLatin1StringView(detail->info.text, int(detail->info.size));
224
0
        m_needsInsertBlock = true;
225
0
        if (m_blockQuoteDepth)
226
0
            qCDebug(lcMD, "CODE lang '%s' info '%s' fenced with '%c' inside QUOTE %d", qPrintable(m_blockCodeLanguage), qPrintable(info), m_blockCodeFence, m_blockQuoteDepth);
227
0
        else
228
0
            qCDebug(lcMD, "CODE lang '%s' info '%s' fenced with '%c'", qPrintable(m_blockCodeLanguage), qPrintable(info), m_blockCodeFence);
229
0
    } break;
230
0
    case MD_BLOCK_H: {
231
0
        MD_BLOCK_H_DETAIL *detail = static_cast<MD_BLOCK_H_DETAIL *>(det);
232
0
        QTextBlockFormat blockFmt;
233
0
        QTextCharFormat charFmt;
234
0
        int sizeAdjustment = 4 - int(detail->level); // H1 to H6: +3 to -2
235
0
        charFmt.setProperty(QTextFormat::FontSizeAdjustment, sizeAdjustment);
236
0
        charFmt.setFontWeight(QFont::Bold);
237
0
        blockFmt.setHeadingLevel(int(detail->level));
238
0
        m_needsInsertBlock = false;
239
0
        if (m_cursor.document()->isEmpty()) {
240
0
            m_cursor.setBlockFormat(blockFmt);
241
0
            m_cursor.setCharFormat(charFmt);
242
0
        } else {
243
0
            m_cursor.insertBlock(blockFmt, charFmt);
244
0
        }
245
0
        qCDebug(lcMD, "H%d", detail->level);
246
0
    } break;
247
0
    case MD_BLOCK_LI: {
248
0
        m_needsInsertBlock = true;
249
0
        m_listItem = true;
250
0
        MD_BLOCK_LI_DETAIL *detail = static_cast<MD_BLOCK_LI_DETAIL *>(det);
251
0
        m_markerType = detail->is_task ?
252
0
                    (detail->task_mark == ' ' ? QTextBlockFormat::MarkerType::Unchecked : QTextBlockFormat::MarkerType::Checked) :
253
0
                    QTextBlockFormat::MarkerType::NoMarker;
254
0
        qCDebug(lcMD) << "LI";
255
0
    } break;
256
0
    case MD_BLOCK_UL: {
257
0
        if (m_needsInsertList) // list nested in an empty list
258
0
            m_listStack.push(m_cursor.insertList(m_listFormat));
259
0
        else
260
0
            m_needsInsertList = true;
261
0
        MD_BLOCK_UL_DETAIL *detail = static_cast<MD_BLOCK_UL_DETAIL *>(det);
262
0
        m_listFormat = QTextListFormat();
263
0
        m_listFormat.setIndent(m_listStack.size() + 1);
264
0
        switch (detail->mark) {
265
0
        case '*':
266
0
            m_listFormat.setStyle(QTextListFormat::ListCircle);
267
0
            break;
268
0
        case '+':
269
0
            m_listFormat.setStyle(QTextListFormat::ListSquare);
270
0
            break;
271
0
        default: // including '-'
272
0
            m_listFormat.setStyle(QTextListFormat::ListDisc);
273
0
            break;
274
0
        }
275
0
        qCDebug(lcMD, "UL %c level %d", detail->mark, int(m_listStack.size()) + 1);
276
0
    } break;
277
0
    case MD_BLOCK_OL: {
278
0
        if (m_needsInsertList) // list nested in an empty list
279
0
            m_listStack.push(m_cursor.insertList(m_listFormat));
280
0
        else
281
0
            m_needsInsertList = true;
282
0
        MD_BLOCK_OL_DETAIL *detail = static_cast<MD_BLOCK_OL_DETAIL *>(det);
283
0
        m_listFormat = QTextListFormat();
284
0
        m_listFormat.setIndent(m_listStack.size() + 1);
285
0
        m_listFormat.setNumberSuffix(QChar::fromLatin1(detail->mark_delimiter));
286
0
        m_listFormat.setStyle(QTextListFormat::ListDecimal);
287
0
        m_listFormat.setStart(detail->start);
288
0
        qCDebug(lcMD, "OL xx%d level %d start %d", detail->mark_delimiter, int(m_listStack.size()) + 1, detail->start);
289
0
    } break;
290
0
    case MD_BLOCK_TD: {
291
0
        MD_BLOCK_TD_DETAIL *detail = static_cast<MD_BLOCK_TD_DETAIL *>(det);
292
0
        ++m_tableCol;
293
        // absolute movement (and storage of m_tableCol) shouldn't be necessary, but
294
        // movePosition(QTextCursor::NextCell) doesn't work
295
0
        QTextTableCell cell = m_currentTable->cellAt(m_tableRowCount - 1, m_tableCol);
296
0
        if (!cell.isValid()) {
297
0
            qWarning("malformed table in Markdown input");
298
0
            return 1;
299
0
        }
300
0
        m_cursor = cell.firstCursorPosition();
301
0
        QTextBlockFormat blockFmt = m_cursor.blockFormat();
302
0
        blockFmt.setAlignment(MdAlignment(detail->align));
303
0
        m_cursor.setBlockFormat(blockFmt);
304
0
        qCDebug(lcMD) << "TD; align" << detail->align << MdAlignment(detail->align) << "col" << m_tableCol;
305
0
    } break;
306
0
    case MD_BLOCK_TH: {
307
0
        ++m_tableColumnCount;
308
0
        ++m_tableCol;
309
0
        if (m_currentTable->columns() < m_tableColumnCount)
310
0
            m_currentTable->appendColumns(1);
311
0
        auto cell = m_currentTable->cellAt(m_tableRowCount - 1, m_tableCol);
312
0
        if (!cell.isValid()) {
313
0
            qWarning("malformed table in Markdown input");
314
0
            return 1;
315
0
        }
316
0
        auto fmt = cell.format();
317
0
        fmt.setFontWeight(QFont::Bold);
318
0
        cell.setFormat(fmt);
319
0
    } break;
320
0
    case MD_BLOCK_TR: {
321
0
        ++m_tableRowCount;
322
0
        m_nonEmptyTableCells.clear();
323
0
        if (m_currentTable->rows() < m_tableRowCount)
324
0
            m_currentTable->appendRows(1);
325
0
        m_tableCol = -1;
326
0
        qCDebug(lcMD) << "TR" << m_currentTable->rows();
327
0
    } break;
328
0
    case MD_BLOCK_TABLE:
329
0
        m_tableColumnCount = 0;
330
0
        m_tableRowCount = 0;
331
0
        m_currentTable = m_cursor.insertTable(1, 1); // we don't know the dimensions yet
332
0
        break;
333
0
    case MD_BLOCK_HR: {
334
0
        qCDebug(lcMD, "HR");
335
0
        QTextBlockFormat blockFmt;
336
0
        blockFmt.setProperty(QTextFormat::BlockTrailingHorizontalRulerWidth, 1);
337
0
        m_cursor.insertBlock(blockFmt, QTextCharFormat());
338
0
    } break;
339
0
    default:
340
0
        break; // nothing to do for now
341
0
    }
342
0
    return 0; // no error
343
0
}
344
345
int QTextMarkdownImporter::cbLeaveBlock(int blockType, void *detail)
346
0
{
347
0
    Q_UNUSED(detail);
348
0
    switch (blockType) {
349
0
    case MD_BLOCK_P:
350
0
        m_listItem = false;
351
0
        break;
352
0
    case MD_BLOCK_UL:
353
0
    case MD_BLOCK_OL:
354
0
        if (Q_UNLIKELY(m_needsInsertList))
355
0
            m_listStack.push(m_cursor.createList(m_listFormat));
356
0
        if (Q_UNLIKELY(m_listStack.isEmpty())) {
357
0
            qCWarning(lcMD, "list ended unexpectedly");
358
0
        } else {
359
0
            qCDebug(lcMD, "list at level %d ended", int(m_listStack.size()));
360
0
            m_listStack.pop();
361
0
        }
362
0
        break;
363
0
    case MD_BLOCK_TR: {
364
        // https://github.com/mity/md4c/issues/29
365
        // MD4C doesn't tell us explicitly which cells are merged, so merge empty cells
366
        // with previous non-empty ones
367
0
        int mergeEnd = -1;
368
0
        int mergeBegin = -1;
369
0
        for (int col = m_tableCol; col >= 0; --col) {
370
0
            if (m_nonEmptyTableCells.contains(col)) {
371
0
                if (mergeEnd >= 0 && mergeBegin >= 0) {
372
0
                    qCDebug(lcMD) << "merging cells" << mergeBegin << "to" << mergeEnd << "inclusive, on row" << m_currentTable->rows() - 1;
373
0
                    m_currentTable->mergeCells(m_currentTable->rows() - 1, mergeBegin - 1, 1, mergeEnd - mergeBegin + 2);
374
0
                }
375
0
                mergeEnd = -1;
376
0
                mergeBegin = -1;
377
0
            } else {
378
0
                if (mergeEnd < 0)
379
0
                    mergeEnd = col;
380
0
                else
381
0
                    mergeBegin = col;
382
0
            }
383
0
        }
384
0
    } break;
385
0
    case MD_BLOCK_QUOTE: {
386
0
        qCDebug(lcMD, "QUOTE level %d ended", m_blockQuoteDepth);
387
0
        --m_blockQuoteDepth;
388
0
        m_needsInsertBlock = true;
389
0
    } break;
390
0
    case MD_BLOCK_TABLE:
391
0
        qCDebug(lcMD) << "table ended with" << m_currentTable->columns() << "cols and" << m_currentTable->rows() << "rows";
392
0
        m_currentTable = nullptr;
393
0
        m_cursor.movePosition(QTextCursor::End);
394
0
        break;
395
0
    case MD_BLOCK_LI:
396
0
        qCDebug(lcMD, "LI at level %d ended", int(m_listStack.size()));
397
0
        m_listItem = false;
398
0
        break;
399
0
    case MD_BLOCK_CODE: {
400
0
        m_codeBlock = false;
401
0
        m_blockCodeLanguage.clear();
402
0
        m_blockCodeFence = 0;
403
0
        if (m_blockQuoteDepth)
404
0
            qCDebug(lcMD, "CODE ended inside QUOTE %d", m_blockQuoteDepth);
405
0
        else
406
0
            qCDebug(lcMD, "CODE ended");
407
0
        m_needsInsertBlock = true;
408
0
    } break;
409
0
    case MD_BLOCK_H:
410
0
        m_cursor.setCharFormat(QTextCharFormat());
411
0
        break;
412
0
    default:
413
0
        break;
414
0
    }
415
0
    return 0; // no error
416
0
}
417
418
int QTextMarkdownImporter::cbEnterSpan(int spanType, void *det)
419
0
{
420
0
    QTextCharFormat charFmt;
421
0
    if (!m_spanFormatStack.isEmpty())
422
0
        charFmt = m_spanFormatStack.top();
423
0
    switch (spanType) {
424
0
    case MD_SPAN_EM:
425
0
        charFmt.setFontItalic(true);
426
0
        break;
427
0
    case MD_SPAN_STRONG:
428
0
        charFmt.setFontWeight(QFont::Bold);
429
0
        break;
430
0
    case MD_SPAN_U:
431
0
        charFmt.setFontUnderline(true);
432
0
        break;
433
0
    case MD_SPAN_A: {
434
0
        MD_SPAN_A_DETAIL *detail = static_cast<MD_SPAN_A_DETAIL *>(det);
435
0
        QString url = QString::fromUtf8(detail->href.text, int(detail->href.size));
436
0
        QString title = QString::fromUtf8(detail->title.text, int(detail->title.size));
437
0
        charFmt.setAnchor(true);
438
0
        charFmt.setAnchorHref(url);
439
0
        if (!title.isEmpty())
440
0
            charFmt.setToolTip(title);
441
0
        charFmt.setForeground(m_palette.link());
442
0
        qCDebug(lcMD) << "anchor" << url << title;
443
0
        } break;
444
0
    case MD_SPAN_IMG: {
445
0
        m_imageSpan = true;
446
0
        m_imageFormat = QTextImageFormat();
447
0
        MD_SPAN_IMG_DETAIL *detail = static_cast<MD_SPAN_IMG_DETAIL *>(det);
448
0
        m_imageFormat.setName(QString::fromUtf8(detail->src.text, int(detail->src.size)));
449
0
        m_imageFormat.setProperty(QTextFormat::ImageTitle, QString::fromUtf8(detail->title.text, int(detail->title.size)));
450
0
        break;
451
0
    }
452
0
    case MD_SPAN_CODE:
453
0
        charFmt.setFont(m_monoFont);
454
0
        charFmt.setFontFixedPitch(true);
455
0
        break;
456
0
    case MD_SPAN_DEL:
457
0
        charFmt.setFontStrikeOut(true);
458
0
        break;
459
0
    }
460
0
    m_spanFormatStack.push(charFmt);
461
0
    qCDebug(lcMD) << spanType << "setCharFormat" << charFmt.font().families().constFirst()
462
0
                  << charFmt.fontWeight() << (charFmt.fontItalic() ? "italic" : "")
463
0
                  << charFmt.foreground().color().name();
464
0
    m_cursor.setCharFormat(charFmt);
465
0
    return 0; // no error
466
0
}
467
468
int QTextMarkdownImporter::cbLeaveSpan(int spanType, void *detail)
469
0
{
470
0
    Q_UNUSED(detail);
471
0
    QTextCharFormat charFmt;
472
0
    if (!m_spanFormatStack.isEmpty()) {
473
0
        m_spanFormatStack.pop();
474
0
        if (!m_spanFormatStack.isEmpty())
475
0
            charFmt = m_spanFormatStack.top();
476
0
    }
477
0
    m_cursor.setCharFormat(charFmt);
478
0
    qCDebug(lcMD) << spanType << "setCharFormat" << charFmt.font().families().constFirst()
479
0
                  << charFmt.fontWeight() << (charFmt.fontItalic() ? "italic" : "")
480
0
                  << charFmt.foreground().color().name();
481
0
    if (spanType == int(MD_SPAN_IMG))
482
0
        m_imageSpan = false;
483
0
    return 0; // no error
484
0
}
485
486
int QTextMarkdownImporter::cbText(int textType, const char *text, unsigned size)
487
0
{
488
0
    if (m_needsInsertBlock)
489
0
        insertBlock();
490
0
#if QT_CONFIG(regularexpression)
491
0
    static const QRegularExpression openingBracket(QStringLiteral("<[a-zA-Z]"));
492
0
    static const QRegularExpression closingBracket(QStringLiteral("(/>|</)"));
493
0
#endif
494
0
    QString s = QString::fromUtf8(text, int(size));
495
496
0
    switch (textType) {
497
0
    case MD_TEXT_NORMAL:
498
0
#if QT_CONFIG(regularexpression)
499
0
        if (m_htmlTagDepth) {
500
0
            m_htmlAccumulator += s;
501
0
            s = QString();
502
0
        }
503
0
#endif
504
0
        break;
505
0
    case MD_TEXT_NULLCHAR:
506
0
        s = QString(QChar(u'\xFFFD')); // CommonMark-required replacement for null
507
0
        break;
508
0
    case MD_TEXT_BR:
509
0
        s = QString(qtmi_Newline);
510
0
        break;
511
0
    case MD_TEXT_SOFTBR:
512
0
        s = QString(qtmi_Space);
513
0
        break;
514
0
    case MD_TEXT_CODE:
515
        // We'll see MD_SPAN_CODE too, which will set the char format, and that's enough.
516
0
        break;
517
0
#if QT_CONFIG(texthtmlparser)
518
0
    case MD_TEXT_ENTITY:
519
0
        if (m_htmlTagDepth)
520
0
            m_htmlAccumulator += s;
521
0
        else
522
0
            m_cursor.insertHtml(s);
523
0
        s = QString();
524
0
        break;
525
0
#endif
526
0
    case MD_TEXT_HTML:
527
        // count how many tags are opened and how many are closed
528
0
#if QT_CONFIG(regularexpression) && QT_CONFIG(texthtmlparser)
529
0
        {
530
0
            int startIdx = 0;
531
0
            while ((startIdx = s.indexOf(openingBracket, startIdx)) >= 0) {
532
0
                ++m_htmlTagDepth;
533
0
                startIdx += 2;
534
0
            }
535
0
            startIdx = 0;
536
0
            while ((startIdx = s.indexOf(closingBracket, startIdx)) >= 0) {
537
0
                --m_htmlTagDepth;
538
0
                startIdx += 2;
539
0
            }
540
0
        }
541
0
        m_htmlAccumulator += s;
542
0
        if (!m_htmlTagDepth) { // all open tags are now closed
543
0
            qCDebug(lcMD) << "HTML" << m_htmlAccumulator;
544
0
            m_cursor.insertHtml(m_htmlAccumulator);
545
0
            if (m_spanFormatStack.isEmpty())
546
0
                m_cursor.setCharFormat(QTextCharFormat());
547
0
            else
548
0
                m_cursor.setCharFormat(m_spanFormatStack.top());
549
0
            m_htmlAccumulator = QString();
550
0
        }
551
0
#endif
552
0
        s = QString();
553
0
        break;
554
0
    }
555
556
0
    switch (m_blockType) {
557
0
    case MD_BLOCK_TD:
558
0
        m_nonEmptyTableCells.append(m_tableCol);
559
0
        break;
560
0
    case MD_BLOCK_CODE:
561
0
        if (s == qtmi_Newline) {
562
            // defer a blank line until we see something else in the code block,
563
            // to avoid ending every code block with a gratuitous blank line
564
0
            m_needsInsertBlock = true;
565
0
            s = QString();
566
0
        }
567
0
        break;
568
0
    default:
569
0
        break;
570
0
    }
571
572
0
    if (m_imageSpan) {
573
        // TODO we don't yet support alt text with formatting, because of the cases where m_cursor
574
        // already inserted the text above.  Rather need to accumulate it in case we need it here.
575
0
        m_imageFormat.setProperty(QTextFormat::ImageAltText, s);
576
0
        qCDebug(lcMD) << "image" << m_imageFormat.name()
577
0
                      << "title" << m_imageFormat.stringProperty(QTextFormat::ImageTitle)
578
0
                      << "alt" << s << "relative to" << m_cursor.document()->baseUrl();
579
0
        m_cursor.insertImage(m_imageFormat);
580
0
        return 0; // no error
581
0
    }
582
583
0
    if (!s.isEmpty())
584
0
        m_cursor.insertText(s);
585
0
    if (m_cursor.currentList()) {
586
        // The list item will indent the list item's text, so we don't need indentation on the block.
587
0
        QTextBlockFormat bfmt = m_cursor.blockFormat();
588
0
        bfmt.setIndent(0);
589
0
        m_cursor.setBlockFormat(bfmt);
590
0
    }
591
0
    if (lcMD().isEnabled(QtDebugMsg)) {
592
0
        QTextBlockFormat bfmt = m_cursor.blockFormat();
593
0
        QString debugInfo;
594
0
        if (m_cursor.currentList())
595
0
            debugInfo = "in list at depth "_L1 + QString::number(m_cursor.currentList()->format().indent());
596
0
        if (bfmt.hasProperty(QTextFormat::BlockQuoteLevel))
597
0
            debugInfo += "in blockquote at depth "_L1 +
598
0
                    QString::number(bfmt.intProperty(QTextFormat::BlockQuoteLevel));
599
0
        if (bfmt.hasProperty(QTextFormat::BlockCodeLanguage))
600
0
            debugInfo += "in a code block"_L1;
601
0
        qCDebug(lcMD) << textType << "in block" << m_blockType << s << qPrintable(debugInfo)
602
0
                      << "bindent" << bfmt.indent() << "tindent" << bfmt.textIndent()
603
0
                      << "margins" << bfmt.leftMargin() << bfmt.topMargin() << bfmt.bottomMargin() << bfmt.rightMargin();
604
0
    }
605
0
    return 0; // no error
606
0
}
607
608
/*!
609
    Insert a new block based on stored state.
610
611
    m_cursor cannot store the state for the _next_ block ahead of time, because
612
    m_cursor.setBlockFormat() controls the format of the block that the cursor
613
    is already in; so cbLeaveBlock() cannot call setBlockFormat() without
614
    altering the block that was just added. Therefore cbLeaveBlock() and the
615
    following cbEnterBlock() set variables to remember what formatting should
616
    come next, and insertBlock() is called just before the actual text
617
    insertion, to create a new block with the right formatting.
618
*/
619
void QTextMarkdownImporter::insertBlock()
620
0
{
621
0
    QTextCharFormat charFormat;
622
0
    if (!m_spanFormatStack.isEmpty())
623
0
        charFormat = m_spanFormatStack.top();
624
0
    QTextBlockFormat blockFormat;
625
0
    if (!m_listStack.isEmpty() && !m_needsInsertList && m_listItem) {
626
0
        QTextList *list = m_listStack.top();
627
0
        if (list)
628
0
            blockFormat = list->item(list->count() - 1).blockFormat();
629
0
        else
630
0
            qWarning() << "attempted to insert into a list that no longer exists";
631
0
    }
632
0
    if (m_blockQuoteDepth) {
633
0
        blockFormat.setProperty(QTextFormat::BlockQuoteLevel, m_blockQuoteDepth);
634
0
        blockFormat.setLeftMargin(qtmi_BlockQuoteIndent * m_blockQuoteDepth);
635
0
        blockFormat.setRightMargin(qtmi_BlockQuoteIndent);
636
0
    }
637
0
    if (m_codeBlock) {
638
0
        blockFormat.setProperty(QTextFormat::BlockCodeLanguage, m_blockCodeLanguage);
639
0
        if (m_blockCodeFence) {
640
0
            blockFormat.setNonBreakableLines(true);
641
0
            blockFormat.setProperty(QTextFormat::BlockCodeFence, QString(QLatin1Char(m_blockCodeFence)));
642
0
        }
643
0
        charFormat.setFont(m_monoFont);
644
0
    } else {
645
0
        blockFormat.setTopMargin(m_paragraphMargin);
646
0
        blockFormat.setBottomMargin(m_paragraphMargin);
647
0
    }
648
0
    if (m_markerType == QTextBlockFormat::MarkerType::NoMarker)
649
0
        blockFormat.clearProperty(QTextFormat::BlockMarker);
650
0
    else
651
0
        blockFormat.setMarker(m_markerType);
652
0
    if (!m_listStack.isEmpty())
653
0
        blockFormat.setIndent(m_listStack.size());
654
0
    if (m_cursor.document()->isEmpty()) {
655
0
        m_cursor.setBlockFormat(blockFormat);
656
0
        m_cursor.setCharFormat(charFormat);
657
0
    } else if (m_listItem) {
658
0
        m_cursor.insertBlock(blockFormat, QTextCharFormat());
659
0
        m_cursor.setCharFormat(charFormat);
660
0
    } else {
661
0
        m_cursor.insertBlock(blockFormat, charFormat);
662
0
    }
663
0
    if (m_needsInsertList) {
664
0
        m_listStack.push(m_cursor.createList(m_listFormat));
665
0
    } else if (!m_listStack.isEmpty() && m_listItem && m_listStack.top()) {
666
0
        m_listStack.top()->add(m_cursor.block());
667
0
    }
668
0
    m_needsInsertList = false;
669
0
    m_needsInsertBlock = false;
670
0
}
671
672
QT_END_NAMESPACE