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