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