|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtGui module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #ifndef QTEXTHTMLPARSER_P_H |
|
43 #define QTEXTHTMLPARSER_P_H |
|
44 |
|
45 // |
|
46 // W A R N I N G |
|
47 // ------------- |
|
48 // |
|
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. |
|
52 // |
|
53 // We mean it. |
|
54 // |
|
55 |
|
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" |
|
65 |
|
66 #ifndef QT_NO_TEXTHTMLPARSER |
|
67 |
|
68 QT_BEGIN_NAMESPACE |
|
69 |
|
70 enum QTextHTMLElements { |
|
71 Html_unknown = -1, |
|
72 Html_qt = 0, |
|
73 Html_body, |
|
74 |
|
75 Html_a, |
|
76 Html_em, |
|
77 Html_i, |
|
78 Html_big, |
|
79 Html_small, |
|
80 Html_strong, |
|
81 Html_b, |
|
82 Html_cite, |
|
83 Html_address, |
|
84 Html_var, |
|
85 Html_dfn, |
|
86 |
|
87 Html_h1, |
|
88 Html_h2, |
|
89 Html_h3, |
|
90 Html_h4, |
|
91 Html_h5, |
|
92 Html_h6, |
|
93 Html_p, |
|
94 Html_center, |
|
95 |
|
96 Html_font, |
|
97 |
|
98 Html_ul, |
|
99 Html_ol, |
|
100 Html_li, |
|
101 |
|
102 Html_code, |
|
103 Html_tt, |
|
104 Html_kbd, |
|
105 Html_samp, |
|
106 |
|
107 Html_img, |
|
108 Html_br, |
|
109 Html_hr, |
|
110 |
|
111 Html_sub, |
|
112 Html_sup, |
|
113 |
|
114 Html_pre, |
|
115 Html_blockquote, |
|
116 Html_head, |
|
117 Html_div, |
|
118 Html_span, |
|
119 Html_dl, |
|
120 Html_dt, |
|
121 Html_dd, |
|
122 Html_u, |
|
123 Html_s, |
|
124 Html_nobr, |
|
125 |
|
126 // tables |
|
127 Html_table, |
|
128 Html_tr, |
|
129 Html_td, |
|
130 Html_th, |
|
131 Html_thead, |
|
132 Html_tbody, |
|
133 Html_tfoot, |
|
134 Html_caption, |
|
135 |
|
136 // misc... |
|
137 Html_html, |
|
138 Html_style, |
|
139 Html_title, |
|
140 Html_meta, |
|
141 Html_link, |
|
142 Html_script, |
|
143 |
|
144 Html_NumElements |
|
145 }; |
|
146 |
|
147 struct QTextHtmlElement |
|
148 { |
|
149 const char *name; |
|
150 QTextHTMLElements id; |
|
151 enum DisplayMode { DisplayBlock, DisplayInline, DisplayTable, DisplayNone } displayMode; |
|
152 }; |
|
153 |
|
154 class QTextHtmlParser; |
|
155 |
|
156 struct QTextHtmlParserNode { |
|
157 enum WhiteSpaceMode { |
|
158 WhiteSpaceNormal, |
|
159 WhiteSpacePre, |
|
160 WhiteSpaceNoWrap, |
|
161 WhiteSpacePreWrap, |
|
162 WhiteSpaceModeUndefined = -1 |
|
163 }; |
|
164 |
|
165 QTextHtmlParserNode(); |
|
166 QString tag; |
|
167 QString text; |
|
168 QStringList attributes; |
|
169 int parent; |
|
170 QVector<int> children; |
|
171 QTextHTMLElements id; |
|
172 QTextCharFormat charFormat; |
|
173 QTextBlockFormat blockFormat; |
|
174 uint cssFloat : 2; |
|
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 |
|
181 uint hasHref : 1; |
|
182 QTextListFormat::Style listStyle; |
|
183 QString imageName; |
|
184 qreal imageWidth; |
|
185 qreal imageHeight; |
|
186 QTextLength width; |
|
187 QTextLength height; |
|
188 qreal tableBorder; |
|
189 int tableCellRowSpan; |
|
190 int tableCellColSpan; |
|
191 qreal tableCellSpacing; |
|
192 qreal tableCellPadding; |
|
193 QBrush borderBrush; |
|
194 QTextFrameFormat::BorderStyle borderStyle; |
|
195 int userState; |
|
196 |
|
197 int cssListIndent; |
|
198 |
|
199 WhiteSpaceMode wsm; |
|
200 |
|
201 inline bool isListStart() const |
|
202 { return id == Html_ol || id == Html_ul; } |
|
203 inline bool isTableCell() const |
|
204 { return id == Html_td || id == Html_th; } |
|
205 inline bool isBlock() const |
|
206 { return displayMode == QTextHtmlElement::DisplayBlock; } |
|
207 |
|
208 inline bool isNotSelfNesting() const |
|
209 { return id == Html_p || id == Html_li; } |
|
210 |
|
211 inline bool allowedInContext(int parentId) const |
|
212 { |
|
213 switch (id) { |
|
214 case Html_dd: |
|
215 case Html_dt: return (parentId == Html_dl); |
|
216 case Html_tr: return (parentId == Html_table |
|
217 || parentId == Html_thead |
|
218 || parentId == Html_tbody |
|
219 || parentId == Html_tfoot |
|
220 ); |
|
221 case Html_th: |
|
222 case Html_td: return (parentId == Html_tr); |
|
223 case Html_thead: |
|
224 case Html_tbody: |
|
225 case Html_tfoot: return (parentId == Html_table); |
|
226 case Html_caption: return (parentId == Html_table); |
|
227 case Html_body: return parentId != Html_head; |
|
228 default: break; |
|
229 } |
|
230 return true; |
|
231 } |
|
232 |
|
233 inline bool mayNotHaveChildren() const |
|
234 { return id == Html_img || id == Html_hr || id == Html_br || id == Html_meta; } |
|
235 |
|
236 void initializeProperties(const QTextHtmlParserNode *parent, const QTextHtmlParser *parser); |
|
237 |
|
238 inline int uncollapsedMargin(int mar) const { return margin[mar]; } |
|
239 |
|
240 bool isNestedList(const QTextHtmlParser *parser) const; |
|
241 |
|
242 void parseStyleAttribute(const QString &value, const QTextDocument *resourceProvider); |
|
243 |
|
244 #ifndef QT_NO_CSSPARSER |
|
245 void applyCssDeclarations(const QVector<QCss::Declaration> &declarations, const QTextDocument *resourceProvider); |
|
246 |
|
247 void setListStyle(const QVector<QCss::Value> &cssValues); |
|
248 #endif |
|
249 |
|
250 void applyBackgroundImage(const QString &url, const QTextDocument *resourceProvider); |
|
251 |
|
252 bool hasOnlyWhitespace() const; |
|
253 |
|
254 int margin[4]; |
|
255 int padding[4]; |
|
256 |
|
257 friend class QTextHtmlParser; |
|
258 }; |
|
259 Q_DECLARE_TYPEINFO(QTextHtmlParserNode, Q_MOVABLE_TYPE); |
|
260 |
|
261 |
|
262 class QTextHtmlParser |
|
263 { |
|
264 public: |
|
265 enum Margin { |
|
266 MarginTop, |
|
267 MarginRight, |
|
268 MarginBottom, |
|
269 MarginLeft |
|
270 }; |
|
271 |
|
272 inline const QTextHtmlParserNode &at(int i) const { return nodes.at(i); } |
|
273 inline QTextHtmlParserNode &operator[](int i) { return nodes[i]; } |
|
274 inline int count() const { return nodes.count(); } |
|
275 inline int last() const { return nodes.count()-1; } |
|
276 int depth(int i) const; |
|
277 int topMargin(int i) const; |
|
278 int bottomMargin(int i) const; |
|
279 inline int leftMargin(int i) const { return margin(i, MarginLeft); } |
|
280 inline int rightMargin(int i) const { return margin(i, MarginRight); } |
|
281 |
|
282 inline int topPadding(int i) const { return at(i).padding[MarginTop]; } |
|
283 inline int bottomPadding(int i) const { return at(i).padding[MarginBottom]; } |
|
284 inline int leftPadding(int i) const { return at(i).padding[MarginLeft]; } |
|
285 inline int rightPadding(int i) const { return at(i).padding[MarginRight]; } |
|
286 |
|
287 void dumpHtml(); |
|
288 |
|
289 void parse(const QString &text, const QTextDocument *resourceProvider); |
|
290 |
|
291 static int lookupElement(const QString &element); |
|
292 protected: |
|
293 QTextHtmlParserNode *newNode(int parent); |
|
294 QVector<QTextHtmlParserNode> nodes; |
|
295 QString txt; |
|
296 int pos, len; |
|
297 |
|
298 bool textEditMode; |
|
299 |
|
300 void parse(); |
|
301 void parseTag(); |
|
302 void parseCloseTag(); |
|
303 void parseExclamationTag(); |
|
304 QString parseEntity(); |
|
305 QString parseWord(); |
|
306 QTextHtmlParserNode *resolveParent(); |
|
307 void resolveNode(); |
|
308 QStringList parseAttributes(); |
|
309 void applyAttributes(const QStringList &attributes); |
|
310 void eatSpace(); |
|
311 inline bool hasPrefix(QChar c, int lookahead = 0) const |
|
312 {return pos + lookahead < len && txt.at(pos) == c; } |
|
313 int margin(int i, int mar) const; |
|
314 |
|
315 bool nodeIsChildOf(int i, QTextHTMLElements id) const; |
|
316 |
|
317 |
|
318 #ifndef QT_NO_CSSPARSER |
|
319 QVector<QCss::Declaration> declarationsForNode(int node) const; |
|
320 void resolveStyleSheetImports(const QCss::StyleSheet &sheet); |
|
321 void importStyleSheet(const QString &href); |
|
322 |
|
323 struct ExternalStyleSheet |
|
324 { |
|
325 inline ExternalStyleSheet() {} |
|
326 inline ExternalStyleSheet(const QString &_url, const QCss::StyleSheet &_sheet) |
|
327 : url(_url), sheet(_sheet) {} |
|
328 QString url; |
|
329 QCss::StyleSheet sheet; |
|
330 }; |
|
331 QVector<ExternalStyleSheet> externalStyleSheets; |
|
332 QVector<QCss::StyleSheet> inlineStyleSheets; |
|
333 #endif |
|
334 |
|
335 const QTextDocument *resourceProvider; |
|
336 }; |
|
337 |
|
338 QT_END_NAMESPACE |
|
339 |
|
340 #endif // QT_NO_TEXTHTMLPARSER |
|
341 |
|
342 #endif // QTEXTHTMLPARSER_P_H |