1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QTEXTHTMLPARSER_P_H
43 #define QTEXTHTMLPARSER_P_H
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
56 #include "QtCore/qvector.h"
57 #include "QtGui/qbrush.h"
58 #include "QtGui/qcolor.h"
59 #include "QtGui/qfont.h"
60 #include "QtGui/qtextdocument.h"
61 #include "QtGui/qtextcursor.h"
62 #include "private/qtextformat_p.h"
63 #include "private/qtextdocument_p.h"
64 #include "private/qcssparser_p.h"
66 #ifndef QT_NO_TEXTHTMLPARSER
70 enum QTextHTMLElements {
147 struct QTextHtmlElement
150 QTextHTMLElements id;
151 enum DisplayMode { DisplayBlock, DisplayInline, DisplayTable, DisplayNone } displayMode;
154 class QTextHtmlParser;
156 struct QTextHtmlParserNode {
157 enum WhiteSpaceMode {
162 WhiteSpaceModeUndefined = -1
165 QTextHtmlParserNode();
168 QStringList attributes;
170 QVector<int> children;
171 QTextHTMLElements id;
172 QTextCharFormat charFormat;
173 QTextBlockFormat blockFormat;
175 uint hasOwnListStyle : 1;
176 uint hasCssListIndent : 1;
177 uint isEmptyParagraph : 1;
178 uint isTextFrame : 1;
179 uint isRootFrame : 1;
180 uint displayMode : 3; // QTextHtmlElement::DisplayMode
182 QTextListFormat::Style listStyle;
183 QString textListNumberPrefix;
184 QString textListNumberSuffix;
191 int tableCellRowSpan;
192 int tableCellColSpan;
193 qreal tableCellSpacing;
194 qreal tableCellPadding;
196 QTextFrameFormat::BorderStyle borderStyle;
203 inline bool isListStart() const
204 { return id == Html_ol || id == Html_ul; }
205 inline bool isTableCell() const
206 { return id == Html_td || id == Html_th; }
207 inline bool isBlock() const
208 { return displayMode == QTextHtmlElement::DisplayBlock; }
210 inline bool isNotSelfNesting() const
211 { return id == Html_p || id == Html_li; }
213 inline bool allowedInContext(int parentId) const
217 case Html_dt: return (parentId == Html_dl);
218 case Html_tr: return (parentId == Html_table
219 || parentId == Html_thead
220 || parentId == Html_tbody
221 || parentId == Html_tfoot
224 case Html_td: return (parentId == Html_tr);
227 case Html_tfoot: return (parentId == Html_table);
228 case Html_caption: return (parentId == Html_table);
229 case Html_body: return parentId != Html_head;
235 inline bool mayNotHaveChildren() const
236 { return id == Html_img || id == Html_hr || id == Html_br || id == Html_meta; }
238 void initializeProperties(const QTextHtmlParserNode *parent, const QTextHtmlParser *parser);
240 inline int uncollapsedMargin(int mar) const { return margin[mar]; }
242 bool isNestedList(const QTextHtmlParser *parser) const;
244 void parseStyleAttribute(const QString &value, const QTextDocument *resourceProvider);
246 #ifndef QT_NO_CSSPARSER
247 void applyCssDeclarations(const QVector<QCss::Declaration> &declarations, const QTextDocument *resourceProvider);
249 void setListStyle(const QVector<QCss::Value> &cssValues);
252 void applyBackgroundImage(const QString &url, const QTextDocument *resourceProvider);
254 bool hasOnlyWhitespace() const;
259 friend class QTextHtmlParser;
261 Q_DECLARE_TYPEINFO(QTextHtmlParserNode, Q_MOVABLE_TYPE);
264 class QTextHtmlParser
274 inline const QTextHtmlParserNode &at(int i) const { return nodes.at(i); }
275 inline QTextHtmlParserNode &operator[](int i) { return nodes[i]; }
276 inline int count() const { return nodes.count(); }
277 inline int last() const { return nodes.count()-1; }
278 int depth(int i) const;
279 int topMargin(int i) const;
280 int bottomMargin(int i) const;
281 inline int leftMargin(int i) const { return margin(i, MarginLeft); }
282 inline int rightMargin(int i) const { return margin(i, MarginRight); }
284 inline int topPadding(int i) const { return at(i).padding[MarginTop]; }
285 inline int bottomPadding(int i) const { return at(i).padding[MarginBottom]; }
286 inline int leftPadding(int i) const { return at(i).padding[MarginLeft]; }
287 inline int rightPadding(int i) const { return at(i).padding[MarginRight]; }
291 void parse(const QString &text, const QTextDocument *resourceProvider);
293 static int lookupElement(const QString &element);
295 QTextHtmlParserNode *newNode(int parent);
296 QVector<QTextHtmlParserNode> nodes;
304 void parseCloseTag();
305 void parseExclamationTag();
306 QString parseEntity();
308 QTextHtmlParserNode *resolveParent();
310 QStringList parseAttributes();
311 void applyAttributes(const QStringList &attributes);
313 inline bool hasPrefix(QChar c, int lookahead = 0) const
314 {return pos + lookahead < len && txt.at(pos) == c; }
315 int margin(int i, int mar) const;
317 bool nodeIsChildOf(int i, QTextHTMLElements id) const;
320 #ifndef QT_NO_CSSPARSER
321 QVector<QCss::Declaration> declarationsForNode(int node) const;
322 void resolveStyleSheetImports(const QCss::StyleSheet &sheet);
323 void importStyleSheet(const QString &href);
325 struct ExternalStyleSheet
327 inline ExternalStyleSheet() {}
328 inline ExternalStyleSheet(const QString &_url, const QCss::StyleSheet &_sheet)
329 : url(_url), sheet(_sheet) {}
331 QCss::StyleSheet sheet;
333 QVector<ExternalStyleSheet> externalStyleSheets;
334 QVector<QCss::StyleSheet> inlineStyleSheets;
337 const QTextDocument *resourceProvider;
342 #endif // QT_NO_TEXTHTMLPARSER
344 #endif // QTEXTHTMLPARSER_P_H