Coverage Report

Created: 2026-10-14 08:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qtbase/src/gui/text/qtexthtmlparser_p.h
Line
Count
Source
1
// Copyright (C) 2016 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
4
#ifndef QTEXTHTMLPARSER_P_H
5
#define QTEXTHTMLPARSER_P_H
6
7
//
8
//  W A R N I N G
9
//  -------------
10
//
11
// This file is not part of the Qt API.  It exists purely as an
12
// implementation detail.  This header file may change from version to
13
// version without notice, or even be removed.
14
//
15
// We mean it.
16
//
17
18
#include <QtGui/private/qtguiglobal_p.h>
19
#include "QtGui/qbrush.h"
20
#include "QtGui/qcolor.h"
21
#include "QtGui/qfont.h"
22
#include "QtGui/qtextdocument.h"
23
#include "QtGui/qtextcursor.h"
24
25
#include <QtCore/qbytearrayview.h>
26
#include <QtCore/qlist.h>
27
28
#include "private/qtextformat_p.h"
29
#include "private/qtextdocument_p.h"
30
#if QT_CONFIG(cssparser)
31
#include "private/qcssparser_p.h"
32
#endif
33
34
#ifndef QT_NO_TEXTHTMLPARSER
35
36
QT_BEGIN_NAMESPACE
37
38
enum QTextHTMLElements {
39
    Html_unknown = -1,
40
    Html_qt = 0,
41
    Html_body,
42
43
    Html_a,
44
    Html_em,
45
    Html_i,
46
    Html_big,
47
    Html_small,
48
    Html_strong,
49
    Html_b,
50
    Html_cite,
51
    Html_address,
52
    Html_var,
53
    Html_dfn,
54
55
    Html_h1,
56
    Html_h2,
57
    Html_h3,
58
    Html_h4,
59
    Html_h5,
60
    Html_h6,
61
    Html_p,
62
    Html_center,
63
64
    Html_font,
65
66
    Html_ul,
67
    Html_ol,
68
    Html_li,
69
70
    Html_code,
71
    Html_tt,
72
    Html_kbd,
73
    Html_samp,
74
75
    Html_img,
76
    Html_br,
77
    Html_hr,
78
79
    Html_sub,
80
    Html_sup,
81
82
    Html_pre,
83
    Html_blockquote,
84
    Html_head,
85
    Html_div,
86
    Html_span,
87
    Html_dl,
88
    Html_dt,
89
    Html_dd,
90
    Html_u,
91
    Html_s,
92
    Html_del,
93
    Html_nobr,
94
95
    // tables
96
    Html_table,
97
    Html_tr,
98
    Html_td,
99
    Html_th,
100
    Html_thead,
101
    Html_tbody,
102
    Html_tfoot,
103
    Html_caption,
104
105
    // misc...
106
    Html_html,
107
    Html_style,
108
    Html_title,
109
    Html_meta,
110
    Html_link,
111
    Html_script,
112
};
113
114
struct QTextHtmlElement
115
{
116
    QByteArrayView name; // QLatin1StringView here breaks INTEGRITY
117
    QTextHTMLElements id;
118
    enum DisplayMode { DisplayBlock, DisplayInline, DisplayTable, DisplayNone } displayMode;
119
};
120
121
class QTextHtmlParser;
122
123
struct QTextHtmlParserNode {
124
    enum WhiteSpaceMode {
125
        WhiteSpaceNormal,
126
        WhiteSpacePre,
127
        WhiteSpaceNoWrap,
128
        WhiteSpacePreWrap,
129
        WhiteSpacePreLine,
130
        WhiteSpaceModeUndefined = -1
131
    };
132
133
    QTextHtmlParserNode();
134
    QString tag;
135
    QString text;
136
    QStringList attributes;
137
    int parent;
138
    QList<int> children;
139
    QTextHTMLElements id;
140
    QTextCharFormat charFormat;
141
    QTextBlockFormat blockFormat;
142
    uint cssFloat : 2;
143
    uint hasOwnListStyle : 1;
144
    uint hasOwnLineHeightType : 1;
145
    uint hasLineHeightMultiplier : 1;
146
    uint hasCssListIndent : 1;
147
    uint isEmptyParagraph : 1;
148
    uint isTextFrame : 1;
149
    uint isRootFrame : 1;
150
    uint displayMode : 3; // QTextHtmlElement::DisplayMode
151
    uint hasHref : 1;
152
    QTextListFormat::Style listStyle;
153
    int listStart = 1;
154
    QString textListNumberPrefix;
155
    QString textListNumberSuffix;
156
    QString imageName;
157
    QString imageAlt;
158
    qreal imageWidth;
159
    qreal imageHeight;
160
    QTextLength width;
161
    QTextLength height;
162
    qreal tableBorder;
163
    int tableCellRowSpan;
164
    int tableCellColSpan;
165
    qreal tableCellSpacing;
166
    qreal tableCellPadding;
167
    qreal tableCellBorder[4];
168
    QBrush tableCellBorderBrush[4];
169
    QTextFrameFormat::BorderStyle tableCellBorderStyle[4];
170
    QBrush borderBrush;
171
    QTextFrameFormat::BorderStyle borderStyle;
172
    bool borderCollapse;
173
    int userState;
174
175
    int cssListIndent;
176
177
    WhiteSpaceMode wsm;
178
179
    inline bool isListStart() const
180
0
    { return id == Html_ol || id == Html_ul; }
181
    inline bool isTableCell() const
182
0
    { return id == Html_td || id == Html_th; }
183
    inline bool isBlock() const
184
0
    { return displayMode == QTextHtmlElement::DisplayBlock; }
185
186
    inline bool isNotSelfNesting() const
187
0
    { return id == Html_p || id == Html_li; }
188
189
    inline bool allowedInContext(int parentId) const
190
0
    {
191
0
        switch (id) {
192
0
            case Html_dd:
193
0
            case Html_dt: return (parentId == Html_dl);
194
0
            case Html_tr: return (parentId == Html_table
195
0
                                  || parentId == Html_thead
196
0
                                  || parentId == Html_tbody
197
0
                                  || parentId == Html_tfoot
198
0
                                 );
199
0
            case Html_th:
200
0
            case Html_td: return (parentId == Html_tr);
201
0
            case Html_thead:
202
0
            case Html_tbody:
203
0
            case Html_tfoot: return (parentId == Html_table);
204
0
            case Html_caption: return (parentId == Html_table);
205
0
            case Html_body: return parentId != Html_head;
206
0
            default: break;
207
0
        }
208
0
        return true;
209
0
    }
210
211
    inline bool mayNotHaveChildren() const
212
0
    { return id == Html_img || id == Html_hr || id == Html_br || id == Html_meta; }
213
214
    void initializeProperties(const QTextHtmlParserNode *parent, const QTextHtmlParser *parser);
215
216
0
    inline int uncollapsedMargin(int mar) const { return margin[mar]; }
217
218
    bool isNestedList(const QTextHtmlParser *parser) const;
219
220
    void parseStyleAttribute(const QString &value, const QTextDocument *resourceProvider);
221
222
#if QT_CONFIG(cssparser)
223
    void applyCssDeclarations(const QList<QCss::Declaration> &declarations,
224
                              const QTextDocument *resourceProvider);
225
226
    void setListStyle(const QList<QCss::Value> &cssValues);
227
#    endif
228
229
    void applyForegroundImage(qint64 cacheKey, const QTextDocument *resourceProvider);
230
    void applyBackgroundImage(const QString &url, const QTextDocument *resourceProvider);
231
232
    bool hasOnlyWhitespace() const;
233
234
    int margin[4];
235
    int padding[4];
236
237
    friend class QTextHtmlParser;
238
};
239
Q_DECLARE_TYPEINFO(QTextHtmlParserNode, Q_RELOCATABLE_TYPE);
240
241
242
class QTextHtmlParser
243
{
244
public:
245
    enum Margin {
246
        MarginTop,
247
        MarginRight,
248
        MarginBottom,
249
        MarginLeft
250
    };
251
    ~QTextHtmlParser()
252
0
    {
253
0
        qDeleteAll(nodes);
254
0
    }
255
256
0
    inline const QTextHtmlParserNode &at(int i) const { return *nodes.at(i); }
257
0
    inline QTextHtmlParserNode &operator[](int i) { return *nodes[i]; }
258
0
    inline int count() const { return nodes.size(); }
259
0
    inline int last() const { return nodes.size()-1; }
260
    int depth(int i) const;
261
    int topMargin(int i) const;
262
    int bottomMargin(int i) const;
263
0
    inline int leftMargin(int i) const { return margin(i, MarginLeft); }
264
0
    inline int rightMargin(int i) const { return margin(i, MarginRight); }
265
266
0
    inline int topPadding(int i) const { return at(i).padding[MarginTop]; }
267
0
    inline int bottomPadding(int i) const { return at(i).padding[MarginBottom]; }
268
0
    inline int leftPadding(int i) const { return at(i).padding[MarginLeft]; }
269
0
    inline int rightPadding(int i) const { return at(i).padding[MarginRight]; }
270
271
0
    inline qreal tableCellBorder(int i, int edge) const { return at(i).tableCellBorder[edge]; }
272
0
    inline QTextFrameFormat::BorderStyle tableCellBorderStyle(int i, int edge) const { return at(i).tableCellBorderStyle[edge]; }
273
0
    inline QBrush tableCellBorderBrush(int i, int edge) const { return at(i).tableCellBorderBrush[edge]; }
274
275
    void dumpHtml();
276
277
    void parse(const QString &text, const QTextDocument *resourceProvider);
278
279
    Q_GUI_EXPORT static int lookupElement(QAnyStringView element);
280
281
    Q_GUI_EXPORT static QString parseEntity(QStringView entity);
282
283
protected:
284
    QTextHtmlParserNode *newNode(int parent);
285
    QList<QTextHtmlParserNode *> nodes;
286
    QString txt;
287
    int pos, len;
288
289
    bool textEditMode;
290
291
    void parse();
292
    void parseTag();
293
    void parseCloseTag();
294
    void parseExclamationTag();
295
    QString parseEntity();
296
    QString parseWord();
297
    QTextHtmlParserNode *resolveParent();
298
    void resolveNode();
299
    QStringList parseAttributes();
300
    void applyAttributes(const QStringList &attributes);
301
    void eatSpace();
302
    inline bool hasPrefix(QChar c, int lookahead = 0) const
303
0
    {
304
0
        return pos + lookahead < len && txt.at(pos + lookahead) == c;
305
0
    }
306
    int margin(int i, int mar) const;
307
308
    bool nodeIsChildOf(int i, QTextHTMLElements id) const;
309
310
311
#if QT_CONFIG(cssparser)
312
    QList<QCss::Declaration> declarationsForNode(int node) const;
313
    void resolveStyleSheetImports(const QCss::StyleSheet &sheet);
314
    void importStyleSheet(const QString &href);
315
316
    struct ExternalStyleSheet
317
    {
318
0
        inline ExternalStyleSheet() {}
319
        inline ExternalStyleSheet(const QString &_url, const QCss::StyleSheet &_sheet)
320
0
            : url(_url), sheet(_sheet) {}
321
        QString url;
322
        QCss::StyleSheet sheet;
323
    };
324
    friend class QTypeInfo<ExternalStyleSheet>;
325
    QList<ExternalStyleSheet> externalStyleSheets;
326
    QList<QCss::StyleSheet> inlineStyleSheets;
327
#    endif
328
329
    const QTextDocument *resourceProvider;
330
};
331
#if QT_CONFIG(cssparser)
332
Q_DECLARE_TYPEINFO(QTextHtmlParser::ExternalStyleSheet, Q_RELOCATABLE_TYPE);
333
#endif
334
335
QT_END_NAMESPACE
336
337
#endif // QT_NO_TEXTHTMLPARSER
338
339
#endif // QTEXTHTMLPARSER_P_H