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