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