| 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 | // Qt-Security score:critical reason:data-parser |
| 4 | |
| 5 | #include "qtexthtmlparser_p.h" |
| 6 | |
| 7 | #include <qbytearray.h> |
| 8 | #include <qstack.h> |
| 9 | #include <qdebug.h> |
| 10 | #include <qthread.h> |
| 11 | #include <qguiapplication.h> |
| 12 | |
| 13 | #include "qtextdocument.h" |
| 14 | #include "qtextformat_p.h" |
| 15 | #include "qtextdocument_p.h" |
| 16 | #include "qtextcursor.h" |
| 17 | #include "qfont_p.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | |
| 21 | #ifndef QT_NO_TEXTHTMLPARSER |
| 22 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | using namespace Qt::StringLiterals; |
| 26 | |
| 27 | // see also tst_qtextdocumentfragment.cpp |
| 28 | #define MAX_ENTITY 258 |
| 29 | static const struct QTextHtmlEntity { const char name[9]; char16_t code; } entities[]= { |
| 30 | { .name: "AElig" , .code: 0x00c6 }, |
| 31 | { .name: "AMP" , .code: 38 }, |
| 32 | { .name: "Aacute" , .code: 0x00c1 }, |
| 33 | { .name: "Acirc" , .code: 0x00c2 }, |
| 34 | { .name: "Agrave" , .code: 0x00c0 }, |
| 35 | { .name: "Alpha" , .code: 0x0391 }, |
| 36 | { .name: "Aring" , .code: 0x00c5 }, |
| 37 | { .name: "Atilde" , .code: 0x00c3 }, |
| 38 | { .name: "Auml" , .code: 0x00c4 }, |
| 39 | { .name: "Beta" , .code: 0x0392 }, |
| 40 | { .name: "Ccedil" , .code: 0x00c7 }, |
| 41 | { .name: "Chi" , .code: 0x03a7 }, |
| 42 | { .name: "Dagger" , .code: 0x2021 }, |
| 43 | { .name: "Delta" , .code: 0x0394 }, |
| 44 | { .name: "ETH" , .code: 0x00d0 }, |
| 45 | { .name: "Eacute" , .code: 0x00c9 }, |
| 46 | { .name: "Ecirc" , .code: 0x00ca }, |
| 47 | { .name: "Egrave" , .code: 0x00c8 }, |
| 48 | { .name: "Epsilon" , .code: 0x0395 }, |
| 49 | { .name: "Eta" , .code: 0x0397 }, |
| 50 | { .name: "Euml" , .code: 0x00cb }, |
| 51 | { .name: "GT" , .code: 62 }, |
| 52 | { .name: "Gamma" , .code: 0x0393 }, |
| 53 | { .name: "Iacute" , .code: 0x00cd }, |
| 54 | { .name: "Icirc" , .code: 0x00ce }, |
| 55 | { .name: "Igrave" , .code: 0x00cc }, |
| 56 | { .name: "Iota" , .code: 0x0399 }, |
| 57 | { .name: "Iuml" , .code: 0x00cf }, |
| 58 | { .name: "Kappa" , .code: 0x039a }, |
| 59 | { .name: "LT" , .code: 60 }, |
| 60 | { .name: "Lambda" , .code: 0x039b }, |
| 61 | { .name: "Mu" , .code: 0x039c }, |
| 62 | { .name: "Ntilde" , .code: 0x00d1 }, |
| 63 | { .name: "Nu" , .code: 0x039d }, |
| 64 | { .name: "OElig" , .code: 0x0152 }, |
| 65 | { .name: "Oacute" , .code: 0x00d3 }, |
| 66 | { .name: "Ocirc" , .code: 0x00d4 }, |
| 67 | { .name: "Ograve" , .code: 0x00d2 }, |
| 68 | { .name: "Omega" , .code: 0x03a9 }, |
| 69 | { .name: "Omicron" , .code: 0x039f }, |
| 70 | { .name: "Oslash" , .code: 0x00d8 }, |
| 71 | { .name: "Otilde" , .code: 0x00d5 }, |
| 72 | { .name: "Ouml" , .code: 0x00d6 }, |
| 73 | { .name: "Phi" , .code: 0x03a6 }, |
| 74 | { .name: "Pi" , .code: 0x03a0 }, |
| 75 | { .name: "Prime" , .code: 0x2033 }, |
| 76 | { .name: "Psi" , .code: 0x03a8 }, |
| 77 | { .name: "QUOT" , .code: 34 }, |
| 78 | { .name: "Rho" , .code: 0x03a1 }, |
| 79 | { .name: "Scaron" , .code: 0x0160 }, |
| 80 | { .name: "Sigma" , .code: 0x03a3 }, |
| 81 | { .name: "THORN" , .code: 0x00de }, |
| 82 | { .name: "Tau" , .code: 0x03a4 }, |
| 83 | { .name: "Theta" , .code: 0x0398 }, |
| 84 | { .name: "Uacute" , .code: 0x00da }, |
| 85 | { .name: "Ucirc" , .code: 0x00db }, |
| 86 | { .name: "Ugrave" , .code: 0x00d9 }, |
| 87 | { .name: "Upsilon" , .code: 0x03a5 }, |
| 88 | { .name: "Uuml" , .code: 0x00dc }, |
| 89 | { .name: "Xi" , .code: 0x039e }, |
| 90 | { .name: "Yacute" , .code: 0x00dd }, |
| 91 | { .name: "Yuml" , .code: 0x0178 }, |
| 92 | { .name: "Zeta" , .code: 0x0396 }, |
| 93 | { .name: "aacute" , .code: 0x00e1 }, |
| 94 | { .name: "acirc" , .code: 0x00e2 }, |
| 95 | { .name: "acute" , .code: 0x00b4 }, |
| 96 | { .name: "aelig" , .code: 0x00e6 }, |
| 97 | { .name: "agrave" , .code: 0x00e0 }, |
| 98 | { .name: "alefsym" , .code: 0x2135 }, |
| 99 | { .name: "alpha" , .code: 0x03b1 }, |
| 100 | { .name: "amp" , .code: 38 }, |
| 101 | { .name: "and" , .code: 0x22a5 }, |
| 102 | { .name: "ang" , .code: 0x2220 }, |
| 103 | { .name: "apos" , .code: 0x0027 }, |
| 104 | { .name: "aring" , .code: 0x00e5 }, |
| 105 | { .name: "asymp" , .code: 0x2248 }, |
| 106 | { .name: "atilde" , .code: 0x00e3 }, |
| 107 | { .name: "auml" , .code: 0x00e4 }, |
| 108 | { .name: "bdquo" , .code: 0x201e }, |
| 109 | { .name: "beta" , .code: 0x03b2 }, |
| 110 | { .name: "brvbar" , .code: 0x00a6 }, |
| 111 | { .name: "bull" , .code: 0x2022 }, |
| 112 | { .name: "cap" , .code: 0x2229 }, |
| 113 | { .name: "ccedil" , .code: 0x00e7 }, |
| 114 | { .name: "cedil" , .code: 0x00b8 }, |
| 115 | { .name: "cent" , .code: 0x00a2 }, |
| 116 | { .name: "chi" , .code: 0x03c7 }, |
| 117 | { .name: "circ" , .code: 0x02c6 }, |
| 118 | { .name: "clubs" , .code: 0x2663 }, |
| 119 | { .name: "cong" , .code: 0x2245 }, |
| 120 | { .name: "copy" , .code: 0x00a9 }, |
| 121 | { .name: "crarr" , .code: 0x21b5 }, |
| 122 | { .name: "cup" , .code: 0x222a }, |
| 123 | { .name: "curren" , .code: 0x00a4 }, |
| 124 | { .name: "dArr" , .code: 0x21d3 }, |
| 125 | { .name: "dagger" , .code: 0x2020 }, |
| 126 | { .name: "darr" , .code: 0x2193 }, |
| 127 | { .name: "deg" , .code: 0x00b0 }, |
| 128 | { .name: "delta" , .code: 0x03b4 }, |
| 129 | { .name: "diams" , .code: 0x2666 }, |
| 130 | { .name: "divide" , .code: 0x00f7 }, |
| 131 | { .name: "eacute" , .code: 0x00e9 }, |
| 132 | { .name: "ecirc" , .code: 0x00ea }, |
| 133 | { .name: "egrave" , .code: 0x00e8 }, |
| 134 | { .name: "empty" , .code: 0x2205 }, |
| 135 | { .name: "emsp" , .code: 0x2003 }, |
| 136 | { .name: "ensp" , .code: 0x2002 }, |
| 137 | { .name: "epsilon" , .code: 0x03b5 }, |
| 138 | { .name: "equiv" , .code: 0x2261 }, |
| 139 | { .name: "eta" , .code: 0x03b7 }, |
| 140 | { .name: "eth" , .code: 0x00f0 }, |
| 141 | { .name: "euml" , .code: 0x00eb }, |
| 142 | { .name: "euro" , .code: 0x20ac }, |
| 143 | { .name: "exist" , .code: 0x2203 }, |
| 144 | { .name: "fnof" , .code: 0x0192 }, |
| 145 | { .name: "forall" , .code: 0x2200 }, |
| 146 | { .name: "frac12" , .code: 0x00bd }, |
| 147 | { .name: "frac14" , .code: 0x00bc }, |
| 148 | { .name: "frac34" , .code: 0x00be }, |
| 149 | { .name: "frasl" , .code: 0x2044 }, |
| 150 | { .name: "gamma" , .code: 0x03b3 }, |
| 151 | { .name: "ge" , .code: 0x2265 }, |
| 152 | { .name: "gt" , .code: 62 }, |
| 153 | { .name: "hArr" , .code: 0x21d4 }, |
| 154 | { .name: "harr" , .code: 0x2194 }, |
| 155 | { .name: "hearts" , .code: 0x2665 }, |
| 156 | { .name: "hellip" , .code: 0x2026 }, |
| 157 | { .name: "iacute" , .code: 0x00ed }, |
| 158 | { .name: "icirc" , .code: 0x00ee }, |
| 159 | { .name: "iexcl" , .code: 0x00a1 }, |
| 160 | { .name: "igrave" , .code: 0x00ec }, |
| 161 | { .name: "image" , .code: 0x2111 }, |
| 162 | { .name: "infin" , .code: 0x221e }, |
| 163 | { .name: "int" , .code: 0x222b }, |
| 164 | { .name: "iota" , .code: 0x03b9 }, |
| 165 | { .name: "iquest" , .code: 0x00bf }, |
| 166 | { .name: "isin" , .code: 0x2208 }, |
| 167 | { .name: "iuml" , .code: 0x00ef }, |
| 168 | { .name: "kappa" , .code: 0x03ba }, |
| 169 | { .name: "lArr" , .code: 0x21d0 }, |
| 170 | { .name: "lambda" , .code: 0x03bb }, |
| 171 | { .name: "lang" , .code: 0x2329 }, |
| 172 | { .name: "laquo" , .code: 0x00ab }, |
| 173 | { .name: "larr" , .code: 0x2190 }, |
| 174 | { .name: "lceil" , .code: 0x2308 }, |
| 175 | { .name: "ldquo" , .code: 0x201c }, |
| 176 | { .name: "le" , .code: 0x2264 }, |
| 177 | { .name: "lfloor" , .code: 0x230a }, |
| 178 | { .name: "lowast" , .code: 0x2217 }, |
| 179 | { .name: "loz" , .code: 0x25ca }, |
| 180 | { .name: "lrm" , .code: 0x200e }, |
| 181 | { .name: "lsaquo" , .code: 0x2039 }, |
| 182 | { .name: "lsquo" , .code: 0x2018 }, |
| 183 | { .name: "lt" , .code: 60 }, |
| 184 | { .name: "macr" , .code: 0x00af }, |
| 185 | { .name: "mdash" , .code: 0x2014 }, |
| 186 | { .name: "micro" , .code: 0x00b5 }, |
| 187 | { .name: "middot" , .code: 0x00b7 }, |
| 188 | { .name: "minus" , .code: 0x2212 }, |
| 189 | { .name: "mu" , .code: 0x03bc }, |
| 190 | { .name: "nabla" , .code: 0x2207 }, |
| 191 | { .name: "nbsp" , .code: 0x00a0 }, |
| 192 | { .name: "ndash" , .code: 0x2013 }, |
| 193 | { .name: "ne" , .code: 0x2260 }, |
| 194 | { .name: "ni" , .code: 0x220b }, |
| 195 | { .name: "not" , .code: 0x00ac }, |
| 196 | { .name: "notin" , .code: 0x2209 }, |
| 197 | { .name: "nsub" , .code: 0x2284 }, |
| 198 | { .name: "ntilde" , .code: 0x00f1 }, |
| 199 | { .name: "nu" , .code: 0x03bd }, |
| 200 | { .name: "oacute" , .code: 0x00f3 }, |
| 201 | { .name: "ocirc" , .code: 0x00f4 }, |
| 202 | { .name: "oelig" , .code: 0x0153 }, |
| 203 | { .name: "ograve" , .code: 0x00f2 }, |
| 204 | { .name: "oline" , .code: 0x203e }, |
| 205 | { .name: "omega" , .code: 0x03c9 }, |
| 206 | { .name: "omicron" , .code: 0x03bf }, |
| 207 | { .name: "oplus" , .code: 0x2295 }, |
| 208 | { .name: "or" , .code: 0x22a6 }, |
| 209 | { .name: "ordf" , .code: 0x00aa }, |
| 210 | { .name: "ordm" , .code: 0x00ba }, |
| 211 | { .name: "oslash" , .code: 0x00f8 }, |
| 212 | { .name: "otilde" , .code: 0x00f5 }, |
| 213 | { .name: "otimes" , .code: 0x2297 }, |
| 214 | { .name: "ouml" , .code: 0x00f6 }, |
| 215 | { .name: "para" , .code: 0x00b6 }, |
| 216 | { .name: "part" , .code: 0x2202 }, |
| 217 | { .name: "percnt" , .code: 0x0025 }, |
| 218 | { .name: "permil" , .code: 0x2030 }, |
| 219 | { .name: "perp" , .code: 0x22a5 }, |
| 220 | { .name: "phi" , .code: 0x03c6 }, |
| 221 | { .name: "pi" , .code: 0x03c0 }, |
| 222 | { .name: "piv" , .code: 0x03d6 }, |
| 223 | { .name: "plusmn" , .code: 0x00b1 }, |
| 224 | { .name: "pound" , .code: 0x00a3 }, |
| 225 | { .name: "prime" , .code: 0x2032 }, |
| 226 | { .name: "prod" , .code: 0x220f }, |
| 227 | { .name: "prop" , .code: 0x221d }, |
| 228 | { .name: "psi" , .code: 0x03c8 }, |
| 229 | { .name: "quot" , .code: 34 }, |
| 230 | { .name: "rArr" , .code: 0x21d2 }, |
| 231 | { .name: "radic" , .code: 0x221a }, |
| 232 | { .name: "rang" , .code: 0x232a }, |
| 233 | { .name: "raquo" , .code: 0x00bb }, |
| 234 | { .name: "rarr" , .code: 0x2192 }, |
| 235 | { .name: "rceil" , .code: 0x2309 }, |
| 236 | { .name: "rdquo" , .code: 0x201d }, |
| 237 | { .name: "real" , .code: 0x211c }, |
| 238 | { .name: "reg" , .code: 0x00ae }, |
| 239 | { .name: "rfloor" , .code: 0x230b }, |
| 240 | { .name: "rho" , .code: 0x03c1 }, |
| 241 | { .name: "rlm" , .code: 0x200f }, |
| 242 | { .name: "rsaquo" , .code: 0x203a }, |
| 243 | { .name: "rsquo" , .code: 0x2019 }, |
| 244 | { .name: "sbquo" , .code: 0x201a }, |
| 245 | { .name: "scaron" , .code: 0x0161 }, |
| 246 | { .name: "sdot" , .code: 0x22c5 }, |
| 247 | { .name: "sect" , .code: 0x00a7 }, |
| 248 | { .name: "shy" , .code: 0x00ad }, |
| 249 | { .name: "sigma" , .code: 0x03c3 }, |
| 250 | { .name: "sigmaf" , .code: 0x03c2 }, |
| 251 | { .name: "sim" , .code: 0x223c }, |
| 252 | { .name: "spades" , .code: 0x2660 }, |
| 253 | { .name: "sub" , .code: 0x2282 }, |
| 254 | { .name: "sube" , .code: 0x2286 }, |
| 255 | { .name: "sum" , .code: 0x2211 }, |
| 256 | { .name: "sup" , .code: 0x2283 }, |
| 257 | { .name: "sup1" , .code: 0x00b9 }, |
| 258 | { .name: "sup2" , .code: 0x00b2 }, |
| 259 | { .name: "sup3" , .code: 0x00b3 }, |
| 260 | { .name: "supe" , .code: 0x2287 }, |
| 261 | { .name: "szlig" , .code: 0x00df }, |
| 262 | { .name: "tau" , .code: 0x03c4 }, |
| 263 | { .name: "there4" , .code: 0x2234 }, |
| 264 | { .name: "theta" , .code: 0x03b8 }, |
| 265 | { .name: "thetasym" , .code: 0x03d1 }, |
| 266 | { .name: "thinsp" , .code: 0x2009 }, |
| 267 | { .name: "thorn" , .code: 0x00fe }, |
| 268 | { .name: "tilde" , .code: 0x02dc }, |
| 269 | { .name: "times" , .code: 0x00d7 }, |
| 270 | { .name: "trade" , .code: 0x2122 }, |
| 271 | { .name: "uArr" , .code: 0x21d1 }, |
| 272 | { .name: "uacute" , .code: 0x00fa }, |
| 273 | { .name: "uarr" , .code: 0x2191 }, |
| 274 | { .name: "ucirc" , .code: 0x00fb }, |
| 275 | { .name: "ugrave" , .code: 0x00f9 }, |
| 276 | { .name: "uml" , .code: 0x00a8 }, |
| 277 | { .name: "upsih" , .code: 0x03d2 }, |
| 278 | { .name: "upsilon" , .code: 0x03c5 }, |
| 279 | { .name: "uuml" , .code: 0x00fc }, |
| 280 | { .name: "weierp" , .code: 0x2118 }, |
| 281 | { .name: "xi" , .code: 0x03be }, |
| 282 | { .name: "yacute" , .code: 0x00fd }, |
| 283 | { .name: "yen" , .code: 0x00a5 }, |
| 284 | { .name: "yuml" , .code: 0x00ff }, |
| 285 | { .name: "zeta" , .code: 0x03b6 }, |
| 286 | { .name: "zwj" , .code: 0x200d }, |
| 287 | { .name: "zwnj" , .code: 0x200c } |
| 288 | }; |
| 289 | static_assert(MAX_ENTITY == sizeof entities / sizeof *entities); |
| 290 | |
| 291 | #if defined(Q_CC_MSVC_ONLY) && _MSC_VER < 1600 |
| 292 | bool operator<(const QTextHtmlEntity &entity1, const QTextHtmlEntity &entity2) |
| 293 | { |
| 294 | return QLatin1StringView(entity1.name) < QLatin1StringView(entity2.name); |
| 295 | } |
| 296 | #endif |
| 297 | |
| 298 | static bool operator<(QStringView entityStr, const QTextHtmlEntity &entity) |
| 299 | { |
| 300 | return entityStr < QLatin1StringView(entity.name); |
| 301 | } |
| 302 | |
| 303 | static bool operator<(const QTextHtmlEntity &entity, QStringView entityStr) |
| 304 | { |
| 305 | return QLatin1StringView(entity.name) < entityStr; |
| 306 | } |
| 307 | |
| 308 | static QChar resolveEntity(QStringView entity) |
| 309 | { |
| 310 | const QTextHtmlEntity *start = &entities[0]; |
| 311 | const QTextHtmlEntity *end = &entities[MAX_ENTITY]; |
| 312 | const QTextHtmlEntity *e = std::lower_bound(first: start, last: end, val: entity); |
| 313 | if (e == end || (entity < *e)) |
| 314 | return QChar(); |
| 315 | return e->code; |
| 316 | } |
| 317 | |
| 318 | static const ushort windowsLatin1ExtendedCharacters[0xA0 - 0x80] = { |
| 319 | 0x20ac, // 0x80 |
| 320 | 0x0081, // 0x81 direct mapping |
| 321 | 0x201a, // 0x82 |
| 322 | 0x0192, // 0x83 |
| 323 | 0x201e, // 0x84 |
| 324 | 0x2026, // 0x85 |
| 325 | 0x2020, // 0x86 |
| 326 | 0x2021, // 0x87 |
| 327 | 0x02C6, // 0x88 |
| 328 | 0x2030, // 0x89 |
| 329 | 0x0160, // 0x8A |
| 330 | 0x2039, // 0x8B |
| 331 | 0x0152, // 0x8C |
| 332 | 0x008D, // 0x8D direct mapping |
| 333 | 0x017D, // 0x8E |
| 334 | 0x008F, // 0x8F directmapping |
| 335 | 0x0090, // 0x90 directmapping |
| 336 | 0x2018, // 0x91 |
| 337 | 0x2019, // 0x92 |
| 338 | 0x201C, // 0x93 |
| 339 | 0X201D, // 0x94 |
| 340 | 0x2022, // 0x95 |
| 341 | 0x2013, // 0x96 |
| 342 | 0x2014, // 0x97 |
| 343 | 0x02DC, // 0x98 |
| 344 | 0x2122, // 0x99 |
| 345 | 0x0161, // 0x9A |
| 346 | 0x203A, // 0x9B |
| 347 | 0x0153, // 0x9C |
| 348 | 0x009D, // 0x9D direct mapping |
| 349 | 0x017E, // 0x9E |
| 350 | 0x0178 // 0x9F |
| 351 | }; |
| 352 | |
| 353 | // the displayMode value is according to the what are blocks in the piecetable, not |
| 354 | // what the w3c defines. |
| 355 | static const QTextHtmlElement elements[Html_NumElements]= { |
| 356 | { .name: "a" , .id: Html_a, .displayMode: QTextHtmlElement::DisplayInline }, |
| 357 | { .name: "address" , .id: Html_address, .displayMode: QTextHtmlElement::DisplayInline }, |
| 358 | { .name: "b" , .id: Html_b, .displayMode: QTextHtmlElement::DisplayInline }, |
| 359 | { .name: "big" , .id: Html_big, .displayMode: QTextHtmlElement::DisplayInline }, |
| 360 | { .name: "blockquote" , .id: Html_blockquote, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 361 | { .name: "body" , .id: Html_body, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 362 | { .name: "br" , .id: Html_br, .displayMode: QTextHtmlElement::DisplayInline }, |
| 363 | { .name: "caption" , .id: Html_caption, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 364 | { .name: "center" , .id: Html_center, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 365 | { .name: "cite" , .id: Html_cite, .displayMode: QTextHtmlElement::DisplayInline }, |
| 366 | { .name: "code" , .id: Html_code, .displayMode: QTextHtmlElement::DisplayInline }, |
| 367 | { .name: "dd" , .id: Html_dd, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 368 | { .name: "dfn" , .id: Html_dfn, .displayMode: QTextHtmlElement::DisplayInline }, |
| 369 | { .name: "div" , .id: Html_div, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 370 | { .name: "dl" , .id: Html_dl, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 371 | { .name: "dt" , .id: Html_dt, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 372 | { .name: "em" , .id: Html_em, .displayMode: QTextHtmlElement::DisplayInline }, |
| 373 | { .name: "font" , .id: Html_font, .displayMode: QTextHtmlElement::DisplayInline }, |
| 374 | { .name: "h1" , .id: Html_h1, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 375 | { .name: "h2" , .id: Html_h2, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 376 | { .name: "h3" , .id: Html_h3, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 377 | { .name: "h4" , .id: Html_h4, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 378 | { .name: "h5" , .id: Html_h5, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 379 | { .name: "h6" , .id: Html_h6, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 380 | { .name: "head" , .id: Html_head, .displayMode: QTextHtmlElement::DisplayNone }, |
| 381 | { .name: "hr" , .id: Html_hr, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 382 | { .name: "html" , .id: Html_html, .displayMode: QTextHtmlElement::DisplayInline }, |
| 383 | { .name: "i" , .id: Html_i, .displayMode: QTextHtmlElement::DisplayInline }, |
| 384 | { .name: "img" , .id: Html_img, .displayMode: QTextHtmlElement::DisplayInline }, |
| 385 | { .name: "kbd" , .id: Html_kbd, .displayMode: QTextHtmlElement::DisplayInline }, |
| 386 | { .name: "li" , .id: Html_li, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 387 | { .name: "link" , .id: Html_link, .displayMode: QTextHtmlElement::DisplayNone }, |
| 388 | { .name: "meta" , .id: Html_meta, .displayMode: QTextHtmlElement::DisplayNone }, |
| 389 | { .name: "nobr" , .id: Html_nobr, .displayMode: QTextHtmlElement::DisplayInline }, |
| 390 | { .name: "ol" , .id: Html_ol, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 391 | { .name: "p" , .id: Html_p, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 392 | { .name: "pre" , .id: Html_pre, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 393 | { .name: "qt" , .id: Html_body /*deliberate mapping*/, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 394 | { .name: "s" , .id: Html_s, .displayMode: QTextHtmlElement::DisplayInline }, |
| 395 | { .name: "samp" , .id: Html_samp, .displayMode: QTextHtmlElement::DisplayInline }, |
| 396 | { .name: "script" , .id: Html_script, .displayMode: QTextHtmlElement::DisplayNone }, |
| 397 | { .name: "small" , .id: Html_small, .displayMode: QTextHtmlElement::DisplayInline }, |
| 398 | { .name: "span" , .id: Html_span, .displayMode: QTextHtmlElement::DisplayInline }, |
| 399 | { .name: "strong" , .id: Html_strong, .displayMode: QTextHtmlElement::DisplayInline }, |
| 400 | { .name: "style" , .id: Html_style, .displayMode: QTextHtmlElement::DisplayNone }, |
| 401 | { .name: "sub" , .id: Html_sub, .displayMode: QTextHtmlElement::DisplayInline }, |
| 402 | { .name: "sup" , .id: Html_sup, .displayMode: QTextHtmlElement::DisplayInline }, |
| 403 | { .name: "table" , .id: Html_table, .displayMode: QTextHtmlElement::DisplayTable }, |
| 404 | { .name: "tbody" , .id: Html_tbody, .displayMode: QTextHtmlElement::DisplayTable }, |
| 405 | { .name: "td" , .id: Html_td, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 406 | { .name: "tfoot" , .id: Html_tfoot, .displayMode: QTextHtmlElement::DisplayTable }, |
| 407 | { .name: "th" , .id: Html_th, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 408 | { .name: "thead" , .id: Html_thead, .displayMode: QTextHtmlElement::DisplayTable }, |
| 409 | { .name: "title" , .id: Html_title, .displayMode: QTextHtmlElement::DisplayNone }, |
| 410 | { .name: "tr" , .id: Html_tr, .displayMode: QTextHtmlElement::DisplayTable }, |
| 411 | { .name: "tt" , .id: Html_tt, .displayMode: QTextHtmlElement::DisplayInline }, |
| 412 | { .name: "u" , .id: Html_u, .displayMode: QTextHtmlElement::DisplayInline }, |
| 413 | { .name: "ul" , .id: Html_ul, .displayMode: QTextHtmlElement::DisplayBlock }, |
| 414 | { .name: "var" , .id: Html_var, .displayMode: QTextHtmlElement::DisplayInline }, |
| 415 | }; |
| 416 | |
| 417 | static bool operator<(QStringView str, const QTextHtmlElement &e) |
| 418 | { |
| 419 | return str < QLatin1StringView(e.name); |
| 420 | } |
| 421 | |
| 422 | static bool operator<(const QTextHtmlElement &e, QStringView str) |
| 423 | { |
| 424 | return QLatin1StringView(e.name) < str; |
| 425 | } |
| 426 | |
| 427 | static const QTextHtmlElement *lookupElementHelper(QStringView element) |
| 428 | { |
| 429 | const QTextHtmlElement *start = &elements[0]; |
| 430 | const QTextHtmlElement *end = &elements[Html_NumElements]; |
| 431 | const QTextHtmlElement *e = std::lower_bound(first: start, last: end, val: element); |
| 432 | if ((e == end) || (element < *e)) |
| 433 | return nullptr; |
| 434 | return e; |
| 435 | } |
| 436 | |
| 437 | int QTextHtmlParser::lookupElement(QStringView element) |
| 438 | { |
| 439 | const QTextHtmlElement *e = lookupElementHelper(element); |
| 440 | if (!e) |
| 441 | return -1; |
| 442 | return e->id; |
| 443 | } |
| 444 | |
| 445 | // quotes newlines as "\\n" |
| 446 | static QString quoteNewline(const QString &s) |
| 447 | { |
| 448 | QString n = s; |
| 449 | n.replace(c: u'\n', after: "\\n"_L1 ); |
| 450 | return n; |
| 451 | } |
| 452 | |
| 453 | QTextHtmlParserNode::QTextHtmlParserNode() |
| 454 | : parent(0), id(Html_unknown), |
| 455 | cssFloat(QTextFrameFormat::InFlow), hasOwnListStyle(false), hasOwnLineHeightType(false), hasLineHeightMultiplier(false), |
| 456 | hasCssListIndent(false), isEmptyParagraph(false), isTextFrame(false), isRootFrame(false), |
| 457 | displayMode(QTextHtmlElement::DisplayInline), hasHref(false), |
| 458 | listStyle(QTextListFormat::ListStyleUndefined), imageWidth(-1), imageHeight(-1), tableBorder(0), |
| 459 | tableCellRowSpan(1), tableCellColSpan(1), tableCellSpacing(2), tableCellPadding(0), |
| 460 | borderBrush(Qt::darkGray), borderStyle(QTextFrameFormat::BorderStyle_Outset), |
| 461 | borderCollapse(false), |
| 462 | userState(-1), cssListIndent(0), wsm(WhiteSpaceModeUndefined) |
| 463 | { |
| 464 | margin[QTextHtmlParser::MarginLeft] = 0; |
| 465 | margin[QTextHtmlParser::MarginRight] = 0; |
| 466 | margin[QTextHtmlParser::MarginTop] = 0; |
| 467 | margin[QTextHtmlParser::MarginBottom] = 0; |
| 468 | |
| 469 | for (int i = 0; i < 4; ++i) { |
| 470 | tableCellBorderStyle[i] = QTextFrameFormat::BorderStyle_None; |
| 471 | tableCellBorder[i] = 0; |
| 472 | tableCellBorderBrush[i] = Qt::NoBrush; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | void QTextHtmlParser::dumpHtml() |
| 477 | { |
| 478 | for (int i = 0; i < count(); ++i) { |
| 479 | qDebug().nospace() << qPrintable(QString(depth(i) * 4, u' ')) |
| 480 | << qPrintable(at(i).tag) << ':' |
| 481 | << quoteNewline(s: at(i).text); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | QTextHtmlParserNode *QTextHtmlParser::newNode(int parent) |
| 486 | { |
| 487 | QTextHtmlParserNode *lastNode = nodes.last(); |
| 488 | QTextHtmlParserNode *newNode = nullptr; |
| 489 | |
| 490 | bool reuseLastNode = true; |
| 491 | |
| 492 | if (nodes.size() == 1) { |
| 493 | reuseLastNode = false; |
| 494 | } else if (lastNode->tag.isEmpty()) { |
| 495 | |
| 496 | if (lastNode->text.isEmpty()) { |
| 497 | reuseLastNode = true; |
| 498 | } else { // last node is a text node (empty tag) with some text |
| 499 | |
| 500 | if (lastNode->text.size() == 1 && lastNode->text.at(i: 0).isSpace()) { |
| 501 | |
| 502 | int lastSibling = count() - 2; |
| 503 | while (lastSibling |
| 504 | && at(i: lastSibling).parent != lastNode->parent |
| 505 | && at(i: lastSibling).displayMode == QTextHtmlElement::DisplayInline) { |
| 506 | lastSibling = at(i: lastSibling).parent; |
| 507 | } |
| 508 | |
| 509 | if (at(i: lastSibling).displayMode == QTextHtmlElement::DisplayInline) { |
| 510 | reuseLastNode = false; |
| 511 | } else { |
| 512 | reuseLastNode = true; |
| 513 | } |
| 514 | } else { |
| 515 | // text node with real (non-whitespace) text -> nothing to re-use |
| 516 | reuseLastNode = false; |
| 517 | } |
| 518 | |
| 519 | } |
| 520 | |
| 521 | } else { |
| 522 | // last node had a proper tag -> nothing to re-use |
| 523 | reuseLastNode = false; |
| 524 | } |
| 525 | |
| 526 | if (reuseLastNode) { |
| 527 | newNode = lastNode; |
| 528 | newNode->tag.clear(); |
| 529 | newNode->text.clear(); |
| 530 | newNode->id = Html_unknown; |
| 531 | } else { |
| 532 | nodes.append(t: new QTextHtmlParserNode); |
| 533 | newNode = nodes.last(); |
| 534 | } |
| 535 | |
| 536 | newNode->parent = parent; |
| 537 | return newNode; |
| 538 | } |
| 539 | |
| 540 | void QTextHtmlParser::parse(const QString &text, const QTextDocument *_resourceProvider) |
| 541 | { |
| 542 | qDeleteAll(c: nodes); |
| 543 | nodes.clear(); |
| 544 | nodes.append(t: new QTextHtmlParserNode); |
| 545 | txt = text; |
| 546 | pos = 0; |
| 547 | len = txt.size(); |
| 548 | textEditMode = false; |
| 549 | resourceProvider = _resourceProvider; |
| 550 | parse(); |
| 551 | //dumpHtml(); |
| 552 | } |
| 553 | |
| 554 | int QTextHtmlParser::depth(int i) const |
| 555 | { |
| 556 | int depth = 0; |
| 557 | while (i) { |
| 558 | i = at(i).parent; |
| 559 | ++depth; |
| 560 | } |
| 561 | return depth; |
| 562 | } |
| 563 | |
| 564 | int QTextHtmlParser::margin(int i, int mar) const { |
| 565 | int m = 0; |
| 566 | const QTextHtmlParserNode *node; |
| 567 | if (mar == MarginLeft |
| 568 | || mar == MarginRight) { |
| 569 | while (i) { |
| 570 | node = &at(i); |
| 571 | if (!node->isBlock() && node->id != Html_table) |
| 572 | break; |
| 573 | if (node->isTableCell()) |
| 574 | break; |
| 575 | m += node->margin[mar]; |
| 576 | i = node->parent; |
| 577 | } |
| 578 | } |
| 579 | return m; |
| 580 | } |
| 581 | |
| 582 | int QTextHtmlParser::topMargin(int i) const |
| 583 | { |
| 584 | if (!i) |
| 585 | return 0; |
| 586 | return at(i).margin[MarginTop]; |
| 587 | } |
| 588 | |
| 589 | int QTextHtmlParser::bottomMargin(int i) const |
| 590 | { |
| 591 | if (!i) |
| 592 | return 0; |
| 593 | return at(i).margin[MarginBottom]; |
| 594 | } |
| 595 | |
| 596 | void QTextHtmlParser::eatSpace() |
| 597 | { |
| 598 | while (pos < len && txt.at(i: pos).isSpace() && txt.at(i: pos) != QChar::ParagraphSeparator) |
| 599 | pos++; |
| 600 | } |
| 601 | |
| 602 | void QTextHtmlParser::parse() |
| 603 | { |
| 604 | while (pos < len) { |
| 605 | QChar c = txt.at(i: pos++); |
| 606 | if (c == u'<') { |
| 607 | parseTag(); |
| 608 | } else if (c == u'&') { |
| 609 | nodes.last()->text += parseEntity(); |
| 610 | } else { |
| 611 | nodes.last()->text += c; |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | // parses a tag after "<" |
| 617 | void QTextHtmlParser::parseTag() |
| 618 | { |
| 619 | eatSpace(); |
| 620 | |
| 621 | // handle comments and other exclamation mark declarations |
| 622 | if (hasPrefix(c: u'!')) { |
| 623 | parseExclamationTag(); |
| 624 | if (nodes.last()->wsm != QTextHtmlParserNode::WhiteSpacePre |
| 625 | && nodes.last()->wsm != QTextHtmlParserNode::WhiteSpacePreWrap |
| 626 | && !textEditMode) |
| 627 | eatSpace(); |
| 628 | return; |
| 629 | } |
| 630 | |
| 631 | // if close tag just close |
| 632 | if (hasPrefix(c: u'/')) { |
| 633 | if (nodes.last()->id == Html_style) { |
| 634 | #ifndef QT_NO_CSSPARSER |
| 635 | QCss::Parser parser(nodes.constLast()->text); |
| 636 | QCss::StyleSheet sheet; |
| 637 | sheet.origin = QCss::StyleSheetOrigin_Author; |
| 638 | parser.parse(styleSheet: &sheet, nameCaseSensitivity: Qt::CaseInsensitive); |
| 639 | inlineStyleSheets.append(t: sheet); |
| 640 | resolveStyleSheetImports(sheet); |
| 641 | #endif |
| 642 | } |
| 643 | parseCloseTag(); |
| 644 | return; |
| 645 | } |
| 646 | |
| 647 | int p = last(); |
| 648 | while (p && at(i: p).tag.size() == 0) |
| 649 | p = at(i: p).parent; |
| 650 | |
| 651 | QTextHtmlParserNode *node = newNode(parent: p); |
| 652 | |
| 653 | // parse tag name |
| 654 | node->tag = parseWord().toLower(); |
| 655 | |
| 656 | const QTextHtmlElement *elem = lookupElementHelper(element: node->tag); |
| 657 | if (elem) { |
| 658 | node->id = elem->id; |
| 659 | node->displayMode = elem->displayMode; |
| 660 | } else { |
| 661 | node->id = Html_unknown; |
| 662 | } |
| 663 | |
| 664 | node->attributes.clear(); |
| 665 | // _need_ at least one space after the tag name, otherwise there can't be attributes |
| 666 | if (pos < len && txt.at(i: pos).isSpace()) |
| 667 | node->attributes = parseAttributes(); |
| 668 | |
| 669 | // resolveParent() may have to change the order in the tree and |
| 670 | // insert intermediate nodes for buggy HTML, so re-initialize the 'node' |
| 671 | // pointer through the return value |
| 672 | node = resolveParent(); |
| 673 | resolveNode(); |
| 674 | |
| 675 | #ifndef QT_NO_CSSPARSER |
| 676 | const int nodeIndex = nodes.size() - 1; // this new node is always the last |
| 677 | node->applyCssDeclarations(declarations: declarationsForNode(node: nodeIndex), resourceProvider); |
| 678 | #endif |
| 679 | applyAttributes(attributes: node->attributes); |
| 680 | |
| 681 | // finish tag |
| 682 | bool tagClosed = false; |
| 683 | while (pos < len && txt.at(i: pos) != u'>') { |
| 684 | if (txt.at(i: pos) == u'/') |
| 685 | tagClosed = true; |
| 686 | |
| 687 | pos++; |
| 688 | } |
| 689 | pos++; |
| 690 | |
| 691 | // in a white-space preserving environment strip off a initial newline |
| 692 | // since the element itself already generates a newline |
| 693 | if ((node->wsm == QTextHtmlParserNode::WhiteSpacePre |
| 694 | || node->wsm == QTextHtmlParserNode::WhiteSpacePreWrap |
| 695 | || node->wsm == QTextHtmlParserNode::WhiteSpacePreLine) |
| 696 | && node->isBlock()) { |
| 697 | if (pos < len - 1 && txt.at(i: pos) == u'\n') |
| 698 | ++pos; |
| 699 | } |
| 700 | |
| 701 | if (node->mayNotHaveChildren() || tagClosed) { |
| 702 | newNode(parent: node->parent); |
| 703 | resolveNode(); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | // parses a tag beginning with "/" |
| 708 | void QTextHtmlParser::parseCloseTag() |
| 709 | { |
| 710 | ++pos; |
| 711 | QString tag = parseWord().toLower().trimmed(); |
| 712 | while (pos < len) { |
| 713 | QChar c = txt.at(i: pos++); |
| 714 | if (c == u'>') |
| 715 | break; |
| 716 | } |
| 717 | |
| 718 | // find corresponding open node |
| 719 | int p = last(); |
| 720 | if (p > 0 |
| 721 | && at(i: p - 1).tag == tag |
| 722 | && at(i: p - 1).mayNotHaveChildren()) |
| 723 | p--; |
| 724 | |
| 725 | while (p && at(i: p).tag != tag) |
| 726 | p = at(i: p).parent; |
| 727 | |
| 728 | // simply ignore the tag if we can't find |
| 729 | // a corresponding open node, for broken |
| 730 | // html such as <font>blah</font></font> |
| 731 | if (!p) |
| 732 | return; |
| 733 | |
| 734 | // in a white-space preserving environment strip off a trailing newline |
| 735 | // since the closing of the opening block element will automatically result |
| 736 | // in a new block for elements following the <pre> |
| 737 | // ...foo\n</pre><p>blah -> foo</pre><p>blah |
| 738 | if ((at(i: p).wsm == QTextHtmlParserNode::WhiteSpacePre |
| 739 | || at(i: p).wsm == QTextHtmlParserNode::WhiteSpacePreWrap |
| 740 | || at(i: p).wsm == QTextHtmlParserNode::WhiteSpacePreLine) |
| 741 | && at(i: p).isBlock()) { |
| 742 | if (at(i: last()).text.endsWith(c: u'\n')) |
| 743 | nodes[last()]->text.chop(n: 1); |
| 744 | } |
| 745 | |
| 746 | newNode(parent: at(i: p).parent); |
| 747 | resolveNode(); |
| 748 | } |
| 749 | |
| 750 | // parses a tag beginning with "!" |
| 751 | void QTextHtmlParser::parseExclamationTag() |
| 752 | { |
| 753 | ++pos; |
| 754 | if (hasPrefix(c: u'-') && hasPrefix(c: u'-', lookahead: 1)) { |
| 755 | pos += 2; |
| 756 | // eat comments |
| 757 | int end = txt.indexOf(s: "-->"_L1 , from: pos); |
| 758 | pos = (end >= 0 ? end + 3 : len); |
| 759 | } else { |
| 760 | // eat internal tags |
| 761 | while (pos < len) { |
| 762 | QChar c = txt.at(i: pos++); |
| 763 | if (c == u'>') |
| 764 | break; |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | QString QTextHtmlParser::parseEntity(QStringView entity) |
| 770 | { |
| 771 | QChar resolved = resolveEntity(entity); |
| 772 | if (!resolved.isNull()) |
| 773 | return QString(resolved); |
| 774 | |
| 775 | if (entity.size() > 1 && entity.at(n: 0) == u'#') { |
| 776 | entity = entity.mid(pos: 1); // removing leading # |
| 777 | |
| 778 | int base = 10; |
| 779 | bool ok = false; |
| 780 | |
| 781 | if (entity.at(n: 0).toLower() == u'x') { // hex entity? |
| 782 | entity = entity.mid(pos: 1); |
| 783 | base = 16; |
| 784 | } |
| 785 | |
| 786 | uint uc = entity.toUInt(ok: &ok, base); |
| 787 | if (ok) { |
| 788 | if (uc >= 0x80 && uc < 0x80 + (sizeof(windowsLatin1ExtendedCharacters)/sizeof(windowsLatin1ExtendedCharacters[0]))) |
| 789 | uc = windowsLatin1ExtendedCharacters[uc - 0x80]; |
| 790 | return QStringView{QChar::fromUcs4(c: uc)}.toString(); |
| 791 | } |
| 792 | } |
| 793 | return {}; |
| 794 | } |
| 795 | |
| 796 | // parses an entity after "&", and returns it |
| 797 | QString QTextHtmlParser::parseEntity() |
| 798 | { |
| 799 | const int recover = pos; |
| 800 | int entityLen = 0; |
| 801 | while (pos < len) { |
| 802 | QChar c = txt.at(i: pos++); |
| 803 | if (c.isSpace() || pos - recover > 9) { |
| 804 | goto error; |
| 805 | } |
| 806 | if (c == u';') |
| 807 | break; |
| 808 | ++entityLen; |
| 809 | } |
| 810 | if (entityLen) { |
| 811 | const QStringView entity = QStringView(txt).mid(pos: recover, n: entityLen); |
| 812 | QString parsedEntity = parseEntity(entity); |
| 813 | if (!parsedEntity.isNull()) { |
| 814 | return parsedEntity; |
| 815 | } |
| 816 | } |
| 817 | error: |
| 818 | pos = recover; |
| 819 | return "&"_L1 ; |
| 820 | } |
| 821 | |
| 822 | // parses one word, possibly quoted, and returns it |
| 823 | QString QTextHtmlParser::parseWord() |
| 824 | { |
| 825 | QString word; |
| 826 | if (hasPrefix(c: u'\"')) { // double quotes |
| 827 | ++pos; |
| 828 | while (pos < len) { |
| 829 | QChar c = txt.at(i: pos++); |
| 830 | if (c == u'\"') |
| 831 | break; |
| 832 | else if (c == u'&') |
| 833 | word += parseEntity(); |
| 834 | else |
| 835 | word += c; |
| 836 | } |
| 837 | } else if (hasPrefix(c: u'\'')) { // single quotes |
| 838 | ++pos; |
| 839 | while (pos < len) { |
| 840 | QChar c = txt.at(i: pos++); |
| 841 | // Allow for escaped single quotes as they may be part of the string |
| 842 | if (c == u'\'' && (txt.size() > 1 && txt.at(i: pos - 2) != u'\\')) |
| 843 | break; |
| 844 | else |
| 845 | word += c; |
| 846 | } |
| 847 | } else { // normal text |
| 848 | while (pos < len) { |
| 849 | QChar c = txt.at(i: pos++); |
| 850 | if (c == u'>' || (c == u'/' && hasPrefix(c: u'>')) |
| 851 | || c == u'<' || c == u'=' || c.isSpace()) { |
| 852 | --pos; |
| 853 | break; |
| 854 | } |
| 855 | if (c == u'&') |
| 856 | word += parseEntity(); |
| 857 | else |
| 858 | word += c; |
| 859 | } |
| 860 | } |
| 861 | return word; |
| 862 | } |
| 863 | |
| 864 | // gives the new node the right parent |
| 865 | QTextHtmlParserNode *QTextHtmlParser::resolveParent() |
| 866 | { |
| 867 | QTextHtmlParserNode *node = nodes.last(); |
| 868 | |
| 869 | int p = node->parent; |
| 870 | |
| 871 | // Excel gives us buggy HTML with just tr without surrounding table tags |
| 872 | // or with just td tags |
| 873 | |
| 874 | if (node->id == Html_td) { |
| 875 | int n = p; |
| 876 | while (n && at(i: n).id != Html_tr) |
| 877 | n = at(i: n).parent; |
| 878 | |
| 879 | if (!n) { |
| 880 | nodes.insert(i: nodes.size() - 1, t: new QTextHtmlParserNode); |
| 881 | nodes.insert(i: nodes.size() - 1, t: new QTextHtmlParserNode); |
| 882 | |
| 883 | QTextHtmlParserNode *table = nodes[nodes.size() - 3]; |
| 884 | table->parent = p; |
| 885 | table->id = Html_table; |
| 886 | table->tag = "table"_L1 ; |
| 887 | table->children.append(t: nodes.size() - 2); // add row as child |
| 888 | |
| 889 | QTextHtmlParserNode *row = nodes[nodes.size() - 2]; |
| 890 | row->parent = nodes.size() - 3; // table as parent |
| 891 | row->id = Html_tr; |
| 892 | row->tag = "tr"_L1 ; |
| 893 | |
| 894 | p = nodes.size() - 2; |
| 895 | node = nodes.last(); // re-initialize pointer |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | if (node->id == Html_tr) { |
| 900 | int n = p; |
| 901 | while (n && at(i: n).id != Html_table) |
| 902 | n = at(i: n).parent; |
| 903 | |
| 904 | if (!n) { |
| 905 | nodes.insert(i: nodes.size() - 1, t: new QTextHtmlParserNode); |
| 906 | QTextHtmlParserNode *table = nodes[nodes.size() - 2]; |
| 907 | table->parent = p; |
| 908 | table->id = Html_table; |
| 909 | table->tag = "table"_L1 ; |
| 910 | p = nodes.size() - 2; |
| 911 | node = nodes.last(); // re-initialize pointer |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | // permit invalid html by letting block elements be children |
| 916 | // of inline elements with the exception of paragraphs: |
| 917 | // |
| 918 | // a new paragraph closes parent inline elements (while loop), |
| 919 | // unless they themselves are children of a non-paragraph block |
| 920 | // element (if statement) |
| 921 | // |
| 922 | // For example: |
| 923 | // |
| 924 | // <body><p><b>Foo<p>Bar <-- second <p> implicitly closes <b> that |
| 925 | // belongs to the first <p>. The self-nesting |
| 926 | // check further down prevents the second <p> |
| 927 | // from nesting into the first one then. |
| 928 | // so Bar is not bold. |
| 929 | // |
| 930 | // <body><b><p>Foo <-- Foo should be bold. |
| 931 | // |
| 932 | // <body><b><p>Foo<p>Bar <-- Foo and Bar should be bold. |
| 933 | // |
| 934 | if (node->id == Html_p) { |
| 935 | while (p && !at(i: p).isBlock()) |
| 936 | p = at(i: p).parent; |
| 937 | |
| 938 | if (!p || at(i: p).id != Html_p) |
| 939 | p = node->parent; |
| 940 | } |
| 941 | |
| 942 | // some elements are not self nesting |
| 943 | if (node->id == at(i: p).id |
| 944 | && node->isNotSelfNesting()) |
| 945 | p = at(i: p).parent; |
| 946 | |
| 947 | // some elements are not allowed in certain contexts |
| 948 | while ((p && !node->allowedInContext(parentId: at(i: p).id)) |
| 949 | // ### make new styles aware of empty tags |
| 950 | || at(i: p).mayNotHaveChildren() |
| 951 | ) { |
| 952 | p = at(i: p).parent; |
| 953 | } |
| 954 | |
| 955 | node->parent = p; |
| 956 | |
| 957 | // makes it easier to traverse the tree, later |
| 958 | nodes[p]->children.append(t: nodes.size() - 1); |
| 959 | return node; |
| 960 | } |
| 961 | |
| 962 | // sets all properties on the new node |
| 963 | void QTextHtmlParser::resolveNode() |
| 964 | { |
| 965 | QTextHtmlParserNode *node = nodes.last(); |
| 966 | const QTextHtmlParserNode *parent = nodes.at(i: node->parent); |
| 967 | node->initializeProperties(parent, parser: this); |
| 968 | } |
| 969 | |
| 970 | bool QTextHtmlParserNode::isNestedList(const QTextHtmlParser *parser) const |
| 971 | { |
| 972 | if (!isListStart()) |
| 973 | return false; |
| 974 | |
| 975 | int p = parent; |
| 976 | while (p) { |
| 977 | if (parser->at(i: p).isListStart()) |
| 978 | return true; |
| 979 | p = parser->at(i: p).parent; |
| 980 | } |
| 981 | return false; |
| 982 | } |
| 983 | |
| 984 | void QTextHtmlParserNode::initializeProperties(const QTextHtmlParserNode *parent, const QTextHtmlParser *parser) |
| 985 | { |
| 986 | // inherit properties from parent element |
| 987 | charFormat = parent->charFormat; |
| 988 | |
| 989 | if (id == Html_html) |
| 990 | blockFormat.setLayoutDirection(Qt::LeftToRight); // HTML default |
| 991 | else if (parent->blockFormat.hasProperty(propertyId: QTextFormat::LayoutDirection)) |
| 992 | blockFormat.setLayoutDirection(parent->blockFormat.layoutDirection()); |
| 993 | |
| 994 | if (parent->displayMode == QTextHtmlElement::DisplayNone) |
| 995 | displayMode = QTextHtmlElement::DisplayNone; |
| 996 | |
| 997 | if (parent->id != Html_table || id == Html_caption) { |
| 998 | if (parent->blockFormat.hasProperty(propertyId: QTextFormat::BlockAlignment)) |
| 999 | blockFormat.setAlignment(parent->blockFormat.alignment()); |
| 1000 | else |
| 1001 | blockFormat.clearProperty(propertyId: QTextFormat::BlockAlignment); |
| 1002 | } |
| 1003 | // we don't paint per-row background colors, yet. so as an |
| 1004 | // exception inherit the background color here |
| 1005 | // we also inherit the background between inline elements |
| 1006 | // we also inherit from non-body block elements since we merge them together |
| 1007 | if ((parent->id != Html_tr || !isTableCell()) |
| 1008 | && (displayMode != QTextHtmlElement::DisplayInline || parent->displayMode != QTextHtmlElement::DisplayInline) |
| 1009 | && (parent->id == Html_body || displayMode != QTextHtmlElement::DisplayBlock || parent->displayMode != QTextHtmlElement::DisplayBlock) |
| 1010 | ) { |
| 1011 | charFormat.clearProperty(propertyId: QTextFormat::BackgroundBrush); |
| 1012 | } |
| 1013 | |
| 1014 | listStyle = parent->listStyle; |
| 1015 | // makes no sense to inherit that property, a named anchor is a single point |
| 1016 | // in the document, which is set by the DocumentFragment |
| 1017 | charFormat.clearProperty(propertyId: QTextFormat::AnchorName); |
| 1018 | wsm = parent->wsm; |
| 1019 | |
| 1020 | // initialize remaining properties |
| 1021 | margin[QTextHtmlParser::MarginLeft] = 0; |
| 1022 | margin[QTextHtmlParser::MarginRight] = 0; |
| 1023 | margin[QTextHtmlParser::MarginTop] = 0; |
| 1024 | margin[QTextHtmlParser::MarginBottom] = 0; |
| 1025 | cssFloat = QTextFrameFormat::InFlow; |
| 1026 | |
| 1027 | for (int i = 0; i < 4; ++i) |
| 1028 | padding[i] = -1; |
| 1029 | |
| 1030 | // set element specific attributes |
| 1031 | switch (id) { |
| 1032 | case Html_a: |
| 1033 | for (int i = 0; i < attributes.size(); i += 2) { |
| 1034 | const QString key = attributes.at(i); |
| 1035 | if (key.compare(other: "href"_L1 , cs: Qt::CaseInsensitive) == 0 |
| 1036 | && !attributes.at(i: i + 1).isEmpty()) { |
| 1037 | hasHref = true; |
| 1038 | } |
| 1039 | } |
| 1040 | charFormat.setAnchor(true); |
| 1041 | break; |
| 1042 | case Html_big: |
| 1043 | charFormat.setProperty(propertyId: QTextFormat::FontSizeAdjustment, value: int(1)); |
| 1044 | break; |
| 1045 | case Html_small: |
| 1046 | charFormat.setProperty(propertyId: QTextFormat::FontSizeAdjustment, value: int(-1)); |
| 1047 | break; |
| 1048 | case Html_h1: |
| 1049 | charFormat.setProperty(propertyId: QTextFormat::FontSizeAdjustment, value: int(3)); |
| 1050 | margin[QTextHtmlParser::MarginTop] = 18; |
| 1051 | margin[QTextHtmlParser::MarginBottom] = 12; |
| 1052 | break; |
| 1053 | case Html_h2: |
| 1054 | charFormat.setProperty(propertyId: QTextFormat::FontSizeAdjustment, value: int(2)); |
| 1055 | margin[QTextHtmlParser::MarginTop] = 16; |
| 1056 | margin[QTextHtmlParser::MarginBottom] = 12; |
| 1057 | break; |
| 1058 | case Html_h3: |
| 1059 | charFormat.setProperty(propertyId: QTextFormat::FontSizeAdjustment, value: int(1)); |
| 1060 | margin[QTextHtmlParser::MarginTop] = 14; |
| 1061 | margin[QTextHtmlParser::MarginBottom] = 12; |
| 1062 | break; |
| 1063 | case Html_h4: |
| 1064 | charFormat.setProperty(propertyId: QTextFormat::FontSizeAdjustment, value: int(0)); |
| 1065 | margin[QTextHtmlParser::MarginTop] = 12; |
| 1066 | margin[QTextHtmlParser::MarginBottom] = 12; |
| 1067 | break; |
| 1068 | case Html_h5: |
| 1069 | charFormat.setProperty(propertyId: QTextFormat::FontSizeAdjustment, value: int(-1)); |
| 1070 | margin[QTextHtmlParser::MarginTop] = 12; |
| 1071 | margin[QTextHtmlParser::MarginBottom] = 4; |
| 1072 | break; |
| 1073 | case Html_p: |
| 1074 | margin[QTextHtmlParser::MarginTop] = 12; |
| 1075 | margin[QTextHtmlParser::MarginBottom] = 12; |
| 1076 | break; |
| 1077 | case Html_ul: |
| 1078 | // nested lists don't have margins, except for the toplevel one |
| 1079 | if (!isNestedList(parser)) { |
| 1080 | margin[QTextHtmlParser::MarginTop] = 12; |
| 1081 | margin[QTextHtmlParser::MarginBottom] = 12; |
| 1082 | } |
| 1083 | // no left margin as we use indenting instead |
| 1084 | break; |
| 1085 | case Html_ol: |
| 1086 | // nested lists don't have margins, except for the toplevel one |
| 1087 | if (!isNestedList(parser)) { |
| 1088 | margin[QTextHtmlParser::MarginTop] = 12; |
| 1089 | margin[QTextHtmlParser::MarginBottom] = 12; |
| 1090 | } |
| 1091 | // no left margin as we use indenting instead |
| 1092 | break; |
| 1093 | case Html_br: |
| 1094 | text = QChar(QChar::LineSeparator); |
| 1095 | break; |
| 1096 | case Html_pre: |
| 1097 | margin[QTextHtmlParser::MarginTop] = 12; |
| 1098 | margin[QTextHtmlParser::MarginBottom] = 12; |
| 1099 | break; |
| 1100 | case Html_blockquote: |
| 1101 | margin[QTextHtmlParser::MarginTop] = 12; |
| 1102 | margin[QTextHtmlParser::MarginBottom] = 12; |
| 1103 | margin[QTextHtmlParser::MarginLeft] = 40; |
| 1104 | margin[QTextHtmlParser::MarginRight] = 40; |
| 1105 | blockFormat.setProperty(propertyId: QTextFormat::BlockQuoteLevel, value: 1); |
| 1106 | break; |
| 1107 | case Html_dl: |
| 1108 | margin[QTextHtmlParser::MarginTop] = 8; |
| 1109 | margin[QTextHtmlParser::MarginBottom] = 8; |
| 1110 | break; |
| 1111 | case Html_dd: |
| 1112 | margin[QTextHtmlParser::MarginLeft] = 30; |
| 1113 | break; |
| 1114 | default: break; |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | #ifndef QT_NO_CSSPARSER |
| 1119 | void QTextHtmlParserNode::setListStyle(const QList<QCss::Value> &cssValues) |
| 1120 | { |
| 1121 | for (int i = 0; i < cssValues.size(); ++i) { |
| 1122 | if (cssValues.at(i).type == QCss::Value::KnownIdentifier) { |
| 1123 | switch (static_cast<QCss::KnownValue>(cssValues.at(i).variant.toInt())) { |
| 1124 | case QCss::Value_None: hasOwnListStyle = true; listStyle = QTextListFormat::ListStyleUndefined; break; |
| 1125 | case QCss::Value_Disc: hasOwnListStyle = true; listStyle = QTextListFormat::ListDisc; break; |
| 1126 | case QCss::Value_Square: hasOwnListStyle = true; listStyle = QTextListFormat::ListSquare; break; |
| 1127 | case QCss::Value_Circle: hasOwnListStyle = true; listStyle = QTextListFormat::ListCircle; break; |
| 1128 | case QCss::Value_Decimal: hasOwnListStyle = true; listStyle = QTextListFormat::ListDecimal; break; |
| 1129 | case QCss::Value_LowerAlpha: hasOwnListStyle = true; listStyle = QTextListFormat::ListLowerAlpha; break; |
| 1130 | case QCss::Value_UpperAlpha: hasOwnListStyle = true; listStyle = QTextListFormat::ListUpperAlpha; break; |
| 1131 | case QCss::Value_LowerRoman: hasOwnListStyle = true; listStyle = QTextListFormat::ListLowerRoman; break; |
| 1132 | case QCss::Value_UpperRoman: hasOwnListStyle = true; listStyle = QTextListFormat::ListUpperRoman; break; |
| 1133 | default: break; |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | // allow individual list items to override the style |
| 1138 | if (id == Html_li && hasOwnListStyle) |
| 1139 | blockFormat.setProperty(propertyId: QTextFormat::ListStyle, value: listStyle); |
| 1140 | } |
| 1141 | |
| 1142 | static QTextFrameFormat::BorderStyle toQTextFrameFormat(QCss::BorderStyle cssStyle) |
| 1143 | { |
| 1144 | switch (cssStyle) { |
| 1145 | case QCss::BorderStyle::BorderStyle_Dotted: |
| 1146 | return QTextFrameFormat::BorderStyle::BorderStyle_Dotted; |
| 1147 | case QCss::BorderStyle::BorderStyle_Dashed: |
| 1148 | return QTextFrameFormat::BorderStyle::BorderStyle_Dashed; |
| 1149 | case QCss::BorderStyle::BorderStyle_Solid: |
| 1150 | return QTextFrameFormat::BorderStyle::BorderStyle_Solid; |
| 1151 | case QCss::BorderStyle::BorderStyle_Double: |
| 1152 | return QTextFrameFormat::BorderStyle::BorderStyle_Double; |
| 1153 | case QCss::BorderStyle::BorderStyle_DotDash: |
| 1154 | return QTextFrameFormat::BorderStyle::BorderStyle_DotDash; |
| 1155 | case QCss::BorderStyle::BorderStyle_DotDotDash: |
| 1156 | return QTextFrameFormat::BorderStyle::BorderStyle_DotDotDash; |
| 1157 | case QCss::BorderStyle::BorderStyle_Groove: |
| 1158 | return QTextFrameFormat::BorderStyle::BorderStyle_Groove; |
| 1159 | case QCss::BorderStyle::BorderStyle_Ridge: |
| 1160 | return QTextFrameFormat::BorderStyle::BorderStyle_Ridge; |
| 1161 | case QCss::BorderStyle::BorderStyle_Inset: |
| 1162 | return QTextFrameFormat::BorderStyle::BorderStyle_Inset; |
| 1163 | case QCss::BorderStyle::BorderStyle_Outset: |
| 1164 | return QTextFrameFormat::BorderStyle::BorderStyle_Outset; |
| 1165 | case QCss::BorderStyle::BorderStyle_Unknown: |
| 1166 | case QCss::BorderStyle::BorderStyle_None: |
| 1167 | case QCss::BorderStyle::BorderStyle_Native: |
| 1168 | return QTextFrameFormat::BorderStyle::BorderStyle_None; |
| 1169 | case QCss::BorderStyle::NumKnownBorderStyles: |
| 1170 | break; |
| 1171 | // Intentionally no "default" to allow a compiler warning when extending the enum |
| 1172 | // without updating this here. clang gives such a warning. |
| 1173 | } |
| 1174 | // Must not happen, intentionally trigger undefined behavior which sanitizers will detect. |
| 1175 | // Having all cases covered in switch is not sufficient: |
| 1176 | // MSVC would warn when there is no "default". |
| 1177 | return static_cast<QTextFrameFormat::BorderStyle>(-1); |
| 1178 | } |
| 1179 | |
| 1180 | void QTextHtmlParserNode::applyCssDeclarations(const QList<QCss::Declaration> &declarations, const QTextDocument *resourceProvider) |
| 1181 | { |
| 1182 | QCss::ValueExtractor (declarations); |
| 1183 | extractor.extractBox(margins: margin, paddings: padding); |
| 1184 | |
| 1185 | auto getBorderValues = [&extractor](qreal *borderWidth, QBrush *borderBrush, QTextFrameFormat::BorderStyle *borderStyles) { |
| 1186 | QCss::BorderStyle cssStyles[4]; |
| 1187 | int cssBorder[4]; |
| 1188 | QSize cssRadii[4]; // unused |
| 1189 | for (int i = 0; i < 4; ++i) { |
| 1190 | cssStyles[i] = QCss::BorderStyle_None; |
| 1191 | cssBorder[i] = 0; |
| 1192 | } |
| 1193 | // this will parse (and cache) "border-width" as a list so the |
| 1194 | // QCss::BorderWidth parsing below which expects a single value |
| 1195 | // will not work as expected - which in this case does not matter |
| 1196 | // because tableBorder is not relevant for cells. |
| 1197 | bool hit = extractor.extractBorder(borders: cssBorder, colors: borderBrush, Styles: cssStyles, radii: cssRadii); |
| 1198 | for (int i = 0; i < 4; ++i) { |
| 1199 | borderStyles[i] = toQTextFrameFormat(cssStyle: cssStyles[i]); |
| 1200 | borderWidth[i] = static_cast<qreal>(cssBorder[i]); |
| 1201 | } |
| 1202 | return hit; |
| 1203 | }; |
| 1204 | |
| 1205 | if (id == Html_td || id == Html_th) |
| 1206 | getBorderValues(tableCellBorder, tableCellBorderBrush, tableCellBorderStyle); |
| 1207 | |
| 1208 | for (int i = 0; i < declarations.size(); ++i) { |
| 1209 | const QCss::Declaration &decl = declarations.at(i); |
| 1210 | if (decl.d->values.isEmpty()) continue; |
| 1211 | |
| 1212 | QCss::KnownValue identifier = QCss::UnknownValue; |
| 1213 | if (decl.d->values.constFirst().type == QCss::Value::KnownIdentifier) |
| 1214 | identifier = static_cast<QCss::KnownValue>(decl.d->values.constFirst().variant.toInt()); |
| 1215 | |
| 1216 | switch (decl.d->propertyId) { |
| 1217 | case QCss::BorderColor: { |
| 1218 | QBrush bordersBrush[4]; |
| 1219 | decl.brushValues(c: bordersBrush); |
| 1220 | if (bordersBrush[0].color().isValid()) |
| 1221 | borderBrush = bordersBrush[0]; |
| 1222 | break; |
| 1223 | } |
| 1224 | case QCss::BorderStyles: |
| 1225 | if (decl.styleValue() != QCss::BorderStyle_Unknown && decl.styleValue() != QCss::BorderStyle_Native) |
| 1226 | borderStyle = static_cast<QTextFrameFormat::BorderStyle>(decl.styleValue() - 1); |
| 1227 | break; |
| 1228 | case QCss::BorderWidth: { |
| 1229 | int borders[4]; |
| 1230 | extractor.lengthValues(decl, m: borders); |
| 1231 | tableBorder = borders[0]; |
| 1232 | } |
| 1233 | break; |
| 1234 | case QCss::Border: { |
| 1235 | qreal tblBorder[4]; |
| 1236 | QBrush tblBorderBrush[4]; |
| 1237 | QTextFrameFormat::BorderStyle tblBorderStyle[4]; |
| 1238 | if (getBorderValues(tblBorder, tblBorderBrush, tblBorderStyle)) { |
| 1239 | tableBorder = tblBorder[0]; |
| 1240 | if (tblBorderBrush[0].color().isValid()) |
| 1241 | borderBrush = tblBorderBrush[0]; |
| 1242 | if (tblBorderStyle[0] != static_cast<QTextFrameFormat::BorderStyle>(-1)) |
| 1243 | borderStyle = tblBorderStyle[0]; |
| 1244 | } |
| 1245 | } |
| 1246 | break; |
| 1247 | case QCss::BorderCollapse: |
| 1248 | borderCollapse = decl.borderCollapseValue(); |
| 1249 | break; |
| 1250 | case QCss::Color: charFormat.setForeground(decl.colorValue()); break; |
| 1251 | case QCss::Float: |
| 1252 | cssFloat = QTextFrameFormat::InFlow; |
| 1253 | switch (identifier) { |
| 1254 | case QCss::Value_Left: cssFloat = QTextFrameFormat::FloatLeft; break; |
| 1255 | case QCss::Value_Right: cssFloat = QTextFrameFormat::FloatRight; break; |
| 1256 | default: break; |
| 1257 | } |
| 1258 | break; |
| 1259 | case QCss::QtBlockIndent: |
| 1260 | blockFormat.setIndent(decl.d->values.constFirst().variant.toInt()); |
| 1261 | break; |
| 1262 | case QCss::QtLineHeightType: { |
| 1263 | QString lineHeightTypeName = decl.d->values.constFirst().variant.toString(); |
| 1264 | QTextBlockFormat::LineHeightTypes lineHeightType; |
| 1265 | if (lineHeightTypeName.compare(other: "proportional"_L1 , cs: Qt::CaseInsensitive) == 0) |
| 1266 | lineHeightType = QTextBlockFormat::ProportionalHeight; |
| 1267 | else if (lineHeightTypeName.compare(other: "fixed"_L1 , cs: Qt::CaseInsensitive) == 0) |
| 1268 | lineHeightType = QTextBlockFormat::FixedHeight; |
| 1269 | else if (lineHeightTypeName.compare(other: "minimum"_L1 , cs: Qt::CaseInsensitive) == 0) |
| 1270 | lineHeightType = QTextBlockFormat::MinimumHeight; |
| 1271 | else if (lineHeightTypeName.compare(other: "line-distance"_L1 , cs: Qt::CaseInsensitive) == 0) |
| 1272 | lineHeightType = QTextBlockFormat::LineDistanceHeight; |
| 1273 | else |
| 1274 | lineHeightType = QTextBlockFormat::SingleHeight; |
| 1275 | |
| 1276 | if (hasLineHeightMultiplier) { |
| 1277 | qreal lineHeight = blockFormat.lineHeight() / 100.0; |
| 1278 | blockFormat.setProperty(propertyId: QTextBlockFormat::LineHeight, value: lineHeight); |
| 1279 | } |
| 1280 | |
| 1281 | blockFormat.setProperty(propertyId: QTextBlockFormat::LineHeightType, value: lineHeightType); |
| 1282 | hasOwnLineHeightType = true; |
| 1283 | } |
| 1284 | break; |
| 1285 | case QCss::LineHeight: { |
| 1286 | qreal lineHeight; |
| 1287 | QTextBlockFormat::LineHeightTypes lineHeightType; |
| 1288 | if (decl.realValue(r: &lineHeight, unit: "px" )) { |
| 1289 | lineHeightType = QTextBlockFormat::MinimumHeight; |
| 1290 | } else { |
| 1291 | bool ok; |
| 1292 | QCss::Value cssValue = decl.d->values.constFirst(); |
| 1293 | QString value = cssValue.toString(); |
| 1294 | lineHeight = value.toDouble(ok: &ok); |
| 1295 | if (ok) { |
| 1296 | if (!hasOwnLineHeightType && cssValue.type == QCss::Value::Number) { |
| 1297 | lineHeight *= 100.0; |
| 1298 | hasLineHeightMultiplier = true; |
| 1299 | } |
| 1300 | lineHeightType = QTextBlockFormat::ProportionalHeight; |
| 1301 | } else { |
| 1302 | lineHeight = 0.0; |
| 1303 | lineHeightType = QTextBlockFormat::SingleHeight; |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | // Only override line height type if specified in same node |
| 1308 | if (hasOwnLineHeightType) |
| 1309 | lineHeightType = QTextBlockFormat::LineHeightTypes(blockFormat.lineHeightType()); |
| 1310 | |
| 1311 | blockFormat.setLineHeight(height: lineHeight, heightType: lineHeightType); |
| 1312 | break; |
| 1313 | } |
| 1314 | case QCss::TextIndent: { |
| 1315 | qreal indent = 0; |
| 1316 | if (decl.realValue(r: &indent, unit: "px" )) |
| 1317 | blockFormat.setTextIndent(indent); |
| 1318 | break; } |
| 1319 | case QCss::QtListIndent: |
| 1320 | if (decl.intValue(i: &cssListIndent)) |
| 1321 | hasCssListIndent = true; |
| 1322 | break; |
| 1323 | case QCss::QtParagraphType: |
| 1324 | if (decl.d->values.constFirst().variant.toString().compare(other: "empty"_L1 , cs: Qt::CaseInsensitive) == 0) |
| 1325 | isEmptyParagraph = true; |
| 1326 | break; |
| 1327 | case QCss::QtTableType: |
| 1328 | if (decl.d->values.constFirst().variant.toString().compare(other: "frame"_L1 , cs: Qt::CaseInsensitive) == 0) |
| 1329 | isTextFrame = true; |
| 1330 | else if (decl.d->values.constFirst().variant.toString().compare(other: "root"_L1 , cs: Qt::CaseInsensitive) == 0) { |
| 1331 | isTextFrame = true; |
| 1332 | isRootFrame = true; |
| 1333 | } |
| 1334 | break; |
| 1335 | case QCss::QtUserState: |
| 1336 | userState = decl.d->values.constFirst().variant.toInt(); |
| 1337 | break; |
| 1338 | case QCss::Whitespace: |
| 1339 | switch (identifier) { |
| 1340 | case QCss::Value_Normal: wsm = QTextHtmlParserNode::WhiteSpaceNormal; break; |
| 1341 | case QCss::Value_Pre: wsm = QTextHtmlParserNode::WhiteSpacePre; break; |
| 1342 | case QCss::Value_NoWrap: wsm = QTextHtmlParserNode::WhiteSpaceNoWrap; break; |
| 1343 | case QCss::Value_PreWrap: wsm = QTextHtmlParserNode::WhiteSpacePreWrap; break; |
| 1344 | case QCss::Value_PreLine: wsm = QTextHtmlParserNode::WhiteSpacePreLine; break; |
| 1345 | default: break; |
| 1346 | } |
| 1347 | break; |
| 1348 | case QCss::VerticalAlignment: |
| 1349 | switch (identifier) { |
| 1350 | case QCss::Value_Sub: charFormat.setVerticalAlignment(QTextCharFormat::AlignSubScript); break; |
| 1351 | case QCss::Value_Super: charFormat.setVerticalAlignment(QTextCharFormat::AlignSuperScript); break; |
| 1352 | case QCss::Value_Middle: charFormat.setVerticalAlignment(QTextCharFormat::AlignMiddle); break; |
| 1353 | case QCss::Value_Top: charFormat.setVerticalAlignment(QTextCharFormat::AlignTop); break; |
| 1354 | case QCss::Value_Bottom: charFormat.setVerticalAlignment(QTextCharFormat::AlignBottom); break; |
| 1355 | default: charFormat.setVerticalAlignment(QTextCharFormat::AlignNormal); break; |
| 1356 | } |
| 1357 | break; |
| 1358 | case QCss::PageBreakBefore: |
| 1359 | switch (identifier) { |
| 1360 | case QCss::Value_Always: blockFormat.setPageBreakPolicy(blockFormat.pageBreakPolicy() | QTextFormat::PageBreak_AlwaysBefore); break; |
| 1361 | case QCss::Value_Auto: blockFormat.setPageBreakPolicy(blockFormat.pageBreakPolicy() & ~QTextFormat::PageBreak_AlwaysBefore); break; |
| 1362 | default: break; |
| 1363 | } |
| 1364 | break; |
| 1365 | case QCss::PageBreakAfter: |
| 1366 | switch (identifier) { |
| 1367 | case QCss::Value_Always: blockFormat.setPageBreakPolicy(blockFormat.pageBreakPolicy() | QTextFormat::PageBreak_AlwaysAfter); break; |
| 1368 | case QCss::Value_Auto: blockFormat.setPageBreakPolicy(blockFormat.pageBreakPolicy() & ~QTextFormat::PageBreak_AlwaysAfter); break; |
| 1369 | default: break; |
| 1370 | } |
| 1371 | break; |
| 1372 | case QCss::TextUnderlineStyle: |
| 1373 | switch (identifier) { |
| 1374 | case QCss::Value_None: charFormat.setUnderlineStyle(QTextCharFormat::NoUnderline); break; |
| 1375 | case QCss::Value_Solid: charFormat.setUnderlineStyle(QTextCharFormat::SingleUnderline); break; |
| 1376 | case QCss::Value_Dashed: charFormat.setUnderlineStyle(QTextCharFormat::DashUnderline); break; |
| 1377 | case QCss::Value_Dotted: charFormat.setUnderlineStyle(QTextCharFormat::DotLine); break; |
| 1378 | case QCss::Value_DotDash: charFormat.setUnderlineStyle(QTextCharFormat::DashDotLine); break; |
| 1379 | case QCss::Value_DotDotDash: charFormat.setUnderlineStyle(QTextCharFormat::DashDotDotLine); break; |
| 1380 | case QCss::Value_Wave: charFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline); break; |
| 1381 | default: break; |
| 1382 | } |
| 1383 | break; |
| 1384 | case QCss::TextDecorationColor: charFormat.setUnderlineColor(decl.colorValue()); break; |
| 1385 | case QCss::ListStyleType: |
| 1386 | case QCss::ListStyle: |
| 1387 | setListStyle(decl.d->values); |
| 1388 | break; |
| 1389 | case QCss::QtListNumberPrefix: |
| 1390 | textListNumberPrefix = decl.d->values.constFirst().variant.toString(); |
| 1391 | break; |
| 1392 | case QCss::QtListNumberSuffix: |
| 1393 | textListNumberSuffix = decl.d->values.constFirst().variant.toString(); |
| 1394 | break; |
| 1395 | case QCss::TextAlignment: |
| 1396 | switch (identifier) { |
| 1397 | case QCss::Value_Left: blockFormat.setAlignment(Qt::AlignLeft); break; |
| 1398 | case QCss::Value_Center: blockFormat.setAlignment(Qt::AlignCenter); break; |
| 1399 | case QCss::Value_Right: blockFormat.setAlignment(Qt::AlignRight); break; |
| 1400 | default: break; |
| 1401 | } |
| 1402 | break; |
| 1403 | |
| 1404 | case QCss::QtForegroundTextureCacheKey: |
| 1405 | { |
| 1406 | if (resourceProvider != nullptr && QTextDocumentPrivate::get(document: resourceProvider) != nullptr) { |
| 1407 | bool ok; |
| 1408 | qint64 searchKey = decl.d->values.constFirst().variant.toLongLong(ok: &ok); |
| 1409 | if (ok) |
| 1410 | applyForegroundImage(cacheKey: searchKey, resourceProvider); |
| 1411 | } |
| 1412 | break; |
| 1413 | } |
| 1414 | case QCss::QtStrokeColor: |
| 1415 | { |
| 1416 | QPen pen = charFormat.textOutline(); |
| 1417 | pen.setStyle(Qt::SolidLine); |
| 1418 | pen.setColor(decl.colorValue()); |
| 1419 | charFormat.setTextOutline(pen); |
| 1420 | break; |
| 1421 | } |
| 1422 | case QCss::QtStrokeWidth: |
| 1423 | { |
| 1424 | qreal width; |
| 1425 | if (decl.realValue(r: &width, unit: "px" )) { |
| 1426 | QPen pen = charFormat.textOutline(); |
| 1427 | pen.setWidthF(width); |
| 1428 | charFormat.setTextOutline(pen); |
| 1429 | } |
| 1430 | break; |
| 1431 | } |
| 1432 | case QCss::QtStrokeLineCap: |
| 1433 | { |
| 1434 | QPen pen = charFormat.textOutline(); |
| 1435 | switch (identifier) { |
| 1436 | case QCss::Value_SquareCap: pen.setCapStyle(Qt::SquareCap); break; |
| 1437 | case QCss::Value_FlatCap: pen.setCapStyle(Qt::FlatCap); break; |
| 1438 | case QCss::Value_RoundCap: pen.setCapStyle(Qt::RoundCap); break; |
| 1439 | default: break; |
| 1440 | } |
| 1441 | charFormat.setTextOutline(pen); |
| 1442 | break; |
| 1443 | } |
| 1444 | case QCss::QtStrokeLineJoin: |
| 1445 | { |
| 1446 | QPen pen = charFormat.textOutline(); |
| 1447 | switch (identifier) { |
| 1448 | case QCss::Value_MiterJoin: pen.setJoinStyle(Qt::MiterJoin); break; |
| 1449 | case QCss::Value_BevelJoin: pen.setJoinStyle(Qt::BevelJoin); break; |
| 1450 | case QCss::Value_RoundJoin: pen.setJoinStyle(Qt::RoundJoin); break; |
| 1451 | case QCss::Value_SvgMiterJoin: pen.setJoinStyle(Qt::SvgMiterJoin); break; |
| 1452 | default: break; |
| 1453 | } |
| 1454 | charFormat.setTextOutline(pen); |
| 1455 | break; |
| 1456 | } |
| 1457 | case QCss::QtStrokeMiterLimit: |
| 1458 | { |
| 1459 | qreal miterLimit; |
| 1460 | if (decl.realValue(r: &miterLimit)) { |
| 1461 | QPen pen = charFormat.textOutline(); |
| 1462 | pen.setMiterLimit(miterLimit); |
| 1463 | charFormat.setTextOutline(pen); |
| 1464 | } |
| 1465 | break; |
| 1466 | } |
| 1467 | case QCss::QtStrokeDashArray: |
| 1468 | { |
| 1469 | QList<qreal> dashes = decl.dashArray(); |
| 1470 | if (!dashes.empty()) { |
| 1471 | QPen pen = charFormat.textOutline(); |
| 1472 | pen.setDashPattern(dashes); |
| 1473 | charFormat.setTextOutline(pen); |
| 1474 | } |
| 1475 | break; |
| 1476 | } |
| 1477 | case QCss::QtStrokeDashOffset: |
| 1478 | { |
| 1479 | qreal dashOffset; |
| 1480 | if (decl.realValue(r: &dashOffset)) { |
| 1481 | QPen pen = charFormat.textOutline(); |
| 1482 | pen.setDashOffset(dashOffset); |
| 1483 | charFormat.setTextOutline(pen); |
| 1484 | } |
| 1485 | break; |
| 1486 | } |
| 1487 | case QCss::QtForeground: |
| 1488 | { |
| 1489 | QBrush brush = decl.brushValue(); |
| 1490 | charFormat.setForeground(brush); |
| 1491 | break; |
| 1492 | } |
| 1493 | case QCss::MaximumWidth: |
| 1494 | if (id == Html_img) { |
| 1495 | auto imageFormat = charFormat.toImageFormat(); |
| 1496 | imageFormat.setMaximumWidth(extractor.textLength(decl)); |
| 1497 | charFormat = imageFormat; |
| 1498 | } |
| 1499 | break; |
| 1500 | default: break; |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | QFont f; |
| 1505 | int adjustment = -255; |
| 1506 | extractor.extractFont(font: &f, fontSizeAdjustment: &adjustment); |
| 1507 | if (f.pixelSize() > INT32_MAX / 2) |
| 1508 | f.setPixelSize(INT32_MAX / 2); // avoid even more extreme values |
| 1509 | charFormat.setFont(font: f, behavior: QTextCharFormat::FontPropertiesSpecifiedOnly); |
| 1510 | |
| 1511 | if (adjustment >= -1) |
| 1512 | charFormat.setProperty(propertyId: QTextFormat::FontSizeAdjustment, value: adjustment); |
| 1513 | |
| 1514 | { |
| 1515 | Qt::Alignment ignoredAlignment; |
| 1516 | QCss::Repeat ignoredRepeat; |
| 1517 | QString bgImage; |
| 1518 | QBrush bgBrush; |
| 1519 | QCss::Origin ignoredOrigin, ignoredClip; |
| 1520 | QCss::Attachment ignoredAttachment; |
| 1521 | extractor.extractBackground(&bgBrush, &bgImage, &ignoredRepeat, &ignoredAlignment, |
| 1522 | &ignoredOrigin, &ignoredAttachment, &ignoredClip); |
| 1523 | |
| 1524 | if (!bgImage.isEmpty() && resourceProvider) { |
| 1525 | applyBackgroundImage(url: bgImage, resourceProvider); |
| 1526 | } else if (bgBrush.style() != Qt::NoBrush) { |
| 1527 | charFormat.setBackground(bgBrush); |
| 1528 | if (id == Html_hr) |
| 1529 | blockFormat.setProperty(propertyId: QTextFormat::BackgroundBrush, value: bgBrush); |
| 1530 | } |
| 1531 | } |
| 1532 | } |
| 1533 | |
| 1534 | #endif // QT_NO_CSSPARSER |
| 1535 | |
| 1536 | void QTextHtmlParserNode::applyForegroundImage(qint64 searchKey, const QTextDocument *resourceProvider) |
| 1537 | { |
| 1538 | const QTextDocumentPrivate *priv = QTextDocumentPrivate::get(document: resourceProvider); |
| 1539 | for (int i = 0; i < priv->formats.numFormats(); ++i) { |
| 1540 | QTextCharFormat format = priv->formats.charFormat(index: i); |
| 1541 | if (format.isValid()) { |
| 1542 | QBrush brush = format.foreground(); |
| 1543 | if (brush.style() == Qt::TexturePattern) { |
| 1544 | const bool isPixmap = qHasPixmapTexture(brush); |
| 1545 | |
| 1546 | if (isPixmap && QCoreApplication::instance()->thread() != QThread::currentThread()) { |
| 1547 | qWarning(msg: "Can't apply QPixmap outside of GUI thread" ); |
| 1548 | return; |
| 1549 | } |
| 1550 | |
| 1551 | const qint64 cacheKey = isPixmap ? brush.texture().cacheKey() : brush.textureImage().cacheKey(); |
| 1552 | if (cacheKey == searchKey) { |
| 1553 | QBrush b; |
| 1554 | if (isPixmap) |
| 1555 | b.setTexture(brush.texture()); |
| 1556 | else |
| 1557 | b.setTextureImage(brush.textureImage()); |
| 1558 | b.setStyle(Qt::TexturePattern); |
| 1559 | charFormat.setForeground(b); |
| 1560 | } |
| 1561 | } |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | } |
| 1566 | |
| 1567 | void QTextHtmlParserNode::applyBackgroundImage(const QString &url, const QTextDocument *resourceProvider) |
| 1568 | { |
| 1569 | if (!url.isEmpty() && resourceProvider) { |
| 1570 | QVariant val = resourceProvider->resource(type: QTextDocument::ImageResource, name: url); |
| 1571 | |
| 1572 | if (QCoreApplication::instance()->thread() != QThread::currentThread()) { |
| 1573 | // must use images in non-GUI threads |
| 1574 | if (val.userType() == QMetaType::QImage) { |
| 1575 | QImage image = qvariant_cast<QImage>(v: val); |
| 1576 | charFormat.setBackground(image); |
| 1577 | } else if (val.userType() == QMetaType::QByteArray) { |
| 1578 | QImage image; |
| 1579 | if (image.loadFromData(data: val.toByteArray())) { |
| 1580 | charFormat.setBackground(image); |
| 1581 | } |
| 1582 | } |
| 1583 | } else { |
| 1584 | if (val.userType() == QMetaType::QImage || val.userType() == QMetaType::QPixmap) { |
| 1585 | charFormat.setBackground(qvariant_cast<QPixmap>(v: val)); |
| 1586 | } else if (val.userType() == QMetaType::QByteArray) { |
| 1587 | QPixmap pm; |
| 1588 | if (pm.loadFromData(buf: val.toByteArray())) { |
| 1589 | charFormat.setBackground(pm); |
| 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | } |
| 1594 | if (!url.isEmpty()) |
| 1595 | charFormat.setProperty(propertyId: QTextFormat::BackgroundImageUrl, value: url); |
| 1596 | } |
| 1597 | |
| 1598 | bool QTextHtmlParserNode::hasOnlyWhitespace() const |
| 1599 | { |
| 1600 | for (int i = 0; i < text.size(); ++i) |
| 1601 | if (!text.at(i).isSpace() || text.at(i) == QChar::LineSeparator) |
| 1602 | return false; |
| 1603 | return true; |
| 1604 | } |
| 1605 | |
| 1606 | static bool setIntAttribute(int *destination, const QString &value) |
| 1607 | { |
| 1608 | bool ok = false; |
| 1609 | int val = value.toInt(ok: &ok); |
| 1610 | if (ok) |
| 1611 | *destination = val; |
| 1612 | |
| 1613 | return ok; |
| 1614 | } |
| 1615 | |
| 1616 | static bool setFloatAttribute(qreal *destination, const QString &value) |
| 1617 | { |
| 1618 | bool ok = false; |
| 1619 | qreal val = value.toDouble(ok: &ok); |
| 1620 | if (ok) |
| 1621 | *destination = val; |
| 1622 | |
| 1623 | return ok; |
| 1624 | } |
| 1625 | |
| 1626 | static void setWidthAttribute(QTextLength *width, const QString &valueStr) |
| 1627 | { |
| 1628 | bool ok = false; |
| 1629 | qreal realVal = valueStr.toDouble(ok: &ok); |
| 1630 | if (ok) { |
| 1631 | *width = QTextLength(QTextLength::FixedLength, realVal); |
| 1632 | } else { |
| 1633 | auto value = QStringView(valueStr).trimmed(); |
| 1634 | if (!value.isEmpty() && value.endsWith(c: u'%')) { |
| 1635 | value.truncate(n: value.size() - 1); |
| 1636 | realVal = value.toDouble(ok: &ok); |
| 1637 | if (ok) |
| 1638 | *width = QTextLength(QTextLength::PercentageLength, realVal); |
| 1639 | } |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | #ifndef QT_NO_CSSPARSER |
| 1644 | void QTextHtmlParserNode::parseStyleAttribute(const QString &value, const QTextDocument *resourceProvider) |
| 1645 | { |
| 1646 | const QString css = "* {"_L1 + value + u'}'; |
| 1647 | QCss::Parser parser(css); |
| 1648 | QCss::StyleSheet sheet; |
| 1649 | parser.parse(styleSheet: &sheet, nameCaseSensitivity: Qt::CaseInsensitive); |
| 1650 | if (sheet.styleRules.size() != 1) return; |
| 1651 | applyCssDeclarations(declarations: sheet.styleRules.at(i: 0).declarations, resourceProvider); |
| 1652 | } |
| 1653 | #endif |
| 1654 | |
| 1655 | QStringList QTextHtmlParser::parseAttributes() |
| 1656 | { |
| 1657 | QStringList attrs; |
| 1658 | |
| 1659 | while (pos < len) { |
| 1660 | eatSpace(); |
| 1661 | if (hasPrefix(c: u'>') || hasPrefix(c: u'/')) |
| 1662 | break; |
| 1663 | QString key = parseWord().toLower(); |
| 1664 | QString value = "1"_L1 ; |
| 1665 | if (key.size() == 0) |
| 1666 | break; |
| 1667 | eatSpace(); |
| 1668 | if (hasPrefix(c: u'=')){ |
| 1669 | pos++; |
| 1670 | eatSpace(); |
| 1671 | value = parseWord(); |
| 1672 | } |
| 1673 | if (value.size() == 0) |
| 1674 | continue; |
| 1675 | attrs << key << value; |
| 1676 | } |
| 1677 | |
| 1678 | return attrs; |
| 1679 | } |
| 1680 | |
| 1681 | void QTextHtmlParser::applyAttributes(const QStringList &attributes) |
| 1682 | { |
| 1683 | // local state variable for qt3 textedit mode |
| 1684 | bool seenQt3Richtext = false; |
| 1685 | QString linkHref; |
| 1686 | QString linkType; |
| 1687 | |
| 1688 | if (attributes.size() % 2 == 1) |
| 1689 | return; |
| 1690 | |
| 1691 | QTextHtmlParserNode *node = nodes.last(); |
| 1692 | |
| 1693 | for (int i = 0; i < attributes.size(); i += 2) { |
| 1694 | QString key = attributes.at(i); |
| 1695 | QString value = attributes.at(i: i + 1); |
| 1696 | |
| 1697 | switch (node->id) { |
| 1698 | case Html_font: |
| 1699 | // the infamous font tag |
| 1700 | if (key == "size"_L1 && value.size()) { |
| 1701 | int n = value.toInt(); |
| 1702 | if (value.at(i: 0) != u'+' && value.at(i: 0) != u'-') |
| 1703 | n -= 3; |
| 1704 | node->charFormat.setProperty(propertyId: QTextFormat::FontSizeAdjustment, value: n); |
| 1705 | } else if (key == "face"_L1 ) { |
| 1706 | if (value.contains(c: u',')) { |
| 1707 | QStringList families; |
| 1708 | for (auto family : value.tokenize(needle: u',')) |
| 1709 | families << family.trimmed().toString(); |
| 1710 | node->charFormat.setFontFamilies(families); |
| 1711 | } else { |
| 1712 | node->charFormat.setFontFamilies(QStringList(value)); |
| 1713 | } |
| 1714 | } else if (key == "color"_L1 ) { |
| 1715 | QColor c = QColor::fromString(name: value); |
| 1716 | if (!c.isValid()) |
| 1717 | qWarning(msg: "QTextHtmlParser::applyAttributes: Unknown color name '%s'" ,value.toLatin1().constData()); |
| 1718 | node->charFormat.setForeground(c); |
| 1719 | } |
| 1720 | break; |
| 1721 | case Html_ol: |
| 1722 | case Html_ul: |
| 1723 | if (key == "type"_L1 ) { |
| 1724 | node->hasOwnListStyle = true; |
| 1725 | if (value == "1"_L1 ) { |
| 1726 | node->listStyle = QTextListFormat::ListDecimal; |
| 1727 | } else if (value == "a"_L1 ) { |
| 1728 | node->listStyle = QTextListFormat::ListLowerAlpha; |
| 1729 | } else if (value == "A"_L1 ) { |
| 1730 | node->listStyle = QTextListFormat::ListUpperAlpha; |
| 1731 | } else if (value == "i"_L1 ) { |
| 1732 | node->listStyle = QTextListFormat::ListLowerRoman; |
| 1733 | } else if (value == "I"_L1 ) { |
| 1734 | node->listStyle = QTextListFormat::ListUpperRoman; |
| 1735 | } else { |
| 1736 | value = std::move(value).toLower(); |
| 1737 | if (value == "square"_L1 ) |
| 1738 | node->listStyle = QTextListFormat::ListSquare; |
| 1739 | else if (value == "disc"_L1 ) |
| 1740 | node->listStyle = QTextListFormat::ListDisc; |
| 1741 | else if (value == "circle"_L1 ) |
| 1742 | node->listStyle = QTextListFormat::ListCircle; |
| 1743 | else if (value == "none"_L1 ) |
| 1744 | node->listStyle = QTextListFormat::ListStyleUndefined; |
| 1745 | } |
| 1746 | } else if (key == "start"_L1 ) { |
| 1747 | setIntAttribute(destination: &node->listStart, value); |
| 1748 | } |
| 1749 | break; |
| 1750 | case Html_li: |
| 1751 | if (key == "class"_L1 ) { |
| 1752 | if (value == "unchecked"_L1 ) |
| 1753 | node->blockFormat.setMarker(QTextBlockFormat::MarkerType::Unchecked); |
| 1754 | else if (value == "checked"_L1 ) |
| 1755 | node->blockFormat.setMarker(QTextBlockFormat::MarkerType::Checked); |
| 1756 | } |
| 1757 | break; |
| 1758 | case Html_a: |
| 1759 | if (key == "href"_L1 ) |
| 1760 | node->charFormat.setAnchorHref(value); |
| 1761 | else if (key == "name"_L1 ) |
| 1762 | node->charFormat.setAnchorNames({value}); |
| 1763 | break; |
| 1764 | case Html_img: |
| 1765 | if (key == "src"_L1 || key == "source"_L1 ) { |
| 1766 | node->imageName = value; |
| 1767 | } else if (key == "width"_L1 ) { |
| 1768 | node->imageWidth = -2; // register that there is a value for it. |
| 1769 | setFloatAttribute(destination: &node->imageWidth, value); |
| 1770 | } else if (key == "height"_L1 ) { |
| 1771 | node->imageHeight = -2; // register that there is a value for it. |
| 1772 | setFloatAttribute(destination: &node->imageHeight, value); |
| 1773 | } else if (key == "alt"_L1 ) { |
| 1774 | node->imageAlt = value; |
| 1775 | } else if (key == "title"_L1 ) { |
| 1776 | node->text = value; |
| 1777 | } |
| 1778 | break; |
| 1779 | case Html_tr: |
| 1780 | case Html_body: |
| 1781 | if (key == "bgcolor"_L1 ) { |
| 1782 | QColor c = QColor::fromString(name: value); |
| 1783 | if (!c.isValid()) |
| 1784 | qWarning(msg: "QTextHtmlParser::applyAttributes: Unknown color name '%s'" ,value.toLatin1().constData()); |
| 1785 | node->charFormat.setBackground(c); |
| 1786 | } else if (key == "background"_L1 ) { |
| 1787 | node->applyBackgroundImage(url: value, resourceProvider); |
| 1788 | } |
| 1789 | break; |
| 1790 | case Html_th: |
| 1791 | case Html_td: |
| 1792 | if (key == "width"_L1 ) { |
| 1793 | setWidthAttribute(width: &node->width, valueStr: value); |
| 1794 | } else if (key == "bgcolor"_L1 ) { |
| 1795 | QColor c = QColor::fromString(name: value); |
| 1796 | if (!c.isValid()) |
| 1797 | qWarning(msg: "QTextHtmlParser::applyAttributes: Unknown color name '%s'" ,value.toLatin1().constData()); |
| 1798 | node->charFormat.setBackground(c); |
| 1799 | } else if (key == "background"_L1 ) { |
| 1800 | node->applyBackgroundImage(url: value, resourceProvider); |
| 1801 | } else if (key == "rowspan"_L1 ) { |
| 1802 | if (setIntAttribute(destination: &node->tableCellRowSpan, value)) |
| 1803 | node->tableCellRowSpan = qMax(a: 1, b: node->tableCellRowSpan); |
| 1804 | } else if (key == "colspan"_L1 ) { |
| 1805 | if (setIntAttribute(destination: &node->tableCellColSpan, value)) |
| 1806 | node->tableCellColSpan = qBound(min: 1, val: node->tableCellColSpan, max: 20480); |
| 1807 | } |
| 1808 | break; |
| 1809 | case Html_table: |
| 1810 | // If table border already set through css style, prefer that one otherwise consider this value |
| 1811 | if (key == "border"_L1 && !node->tableBorder) { |
| 1812 | setFloatAttribute(destination: &node->tableBorder, value); |
| 1813 | } else if (key == "bgcolor"_L1 ) { |
| 1814 | QColor c = QColor::fromString(name: value); |
| 1815 | if (!c.isValid()) |
| 1816 | qWarning(msg: "QTextHtmlParser::applyAttributes: Unknown color name '%s'" ,value.toLatin1().constData()); |
| 1817 | node->charFormat.setBackground(c); |
| 1818 | } else if (key == "bordercolor"_L1 ) { |
| 1819 | QColor c = QColor::fromString(name: value); |
| 1820 | if (!c.isValid()) |
| 1821 | qWarning(msg: "QTextHtmlParser::applyAttributes: Unknown color name '%s'" ,value.toLatin1().constData()); |
| 1822 | node->borderBrush = c; |
| 1823 | } else if (key == "background"_L1 ) { |
| 1824 | node->applyBackgroundImage(url: value, resourceProvider); |
| 1825 | } else if (key == "cellspacing"_L1 ) { |
| 1826 | setFloatAttribute(destination: &node->tableCellSpacing, value); |
| 1827 | } else if (key == "cellpadding"_L1 ) { |
| 1828 | setFloatAttribute(destination: &node->tableCellPadding, value); |
| 1829 | } else if (key == "width"_L1 ) { |
| 1830 | setWidthAttribute(width: &node->width, valueStr: value); |
| 1831 | } else if (key == "height"_L1 ) { |
| 1832 | setWidthAttribute(width: &node->height, valueStr: value); |
| 1833 | } |
| 1834 | break; |
| 1835 | case Html_meta: |
| 1836 | if (key == "name"_L1 && value == "qrichtext"_L1 ) |
| 1837 | seenQt3Richtext = true; |
| 1838 | |
| 1839 | if (key == "content"_L1 && value == "1"_L1 && seenQt3Richtext) |
| 1840 | textEditMode = true; |
| 1841 | break; |
| 1842 | case Html_hr: |
| 1843 | if (key == "width"_L1 ) |
| 1844 | setWidthAttribute(width: &node->width, valueStr: value); |
| 1845 | break; |
| 1846 | case Html_link: |
| 1847 | if (key == "href"_L1 ) |
| 1848 | linkHref = value; |
| 1849 | else if (key == "type"_L1 ) |
| 1850 | linkType = value; |
| 1851 | break; |
| 1852 | case Html_pre: |
| 1853 | if (key == "class"_L1 && value.startsWith(s: "language-"_L1 )) |
| 1854 | node->blockFormat.setProperty(propertyId: QTextFormat::BlockCodeLanguage, value: value.mid(position: 9)); |
| 1855 | break; |
| 1856 | default: |
| 1857 | break; |
| 1858 | } |
| 1859 | |
| 1860 | if (key == "style"_L1 ) { |
| 1861 | #ifndef QT_NO_CSSPARSER |
| 1862 | node->parseStyleAttribute(value, resourceProvider); |
| 1863 | #endif |
| 1864 | } else if (key == "align"_L1 ) { |
| 1865 | value = std::move(value).toLower(); |
| 1866 | bool alignmentSet = true; |
| 1867 | |
| 1868 | if (value == "left"_L1 ) |
| 1869 | node->blockFormat.setAlignment(Qt::AlignLeft|Qt::AlignAbsolute); |
| 1870 | else if (value == "right"_L1 ) |
| 1871 | node->blockFormat.setAlignment(Qt::AlignRight|Qt::AlignAbsolute); |
| 1872 | else if (value == "center"_L1 ) |
| 1873 | node->blockFormat.setAlignment(Qt::AlignHCenter); |
| 1874 | else if (value == "justify"_L1 ) |
| 1875 | node->blockFormat.setAlignment(Qt::AlignJustify); |
| 1876 | else |
| 1877 | alignmentSet = false; |
| 1878 | |
| 1879 | if (node->id == Html_img) { |
| 1880 | // HTML4 compat |
| 1881 | if (alignmentSet) { |
| 1882 | if (node->blockFormat.alignment() & Qt::AlignLeft) |
| 1883 | node->cssFloat = QTextFrameFormat::FloatLeft; |
| 1884 | else if (node->blockFormat.alignment() & Qt::AlignRight) |
| 1885 | node->cssFloat = QTextFrameFormat::FloatRight; |
| 1886 | } else if (value == "middle"_L1 ) { |
| 1887 | node->charFormat.setVerticalAlignment(QTextCharFormat::AlignMiddle); |
| 1888 | } else if (value == "top"_L1 ) { |
| 1889 | node->charFormat.setVerticalAlignment(QTextCharFormat::AlignTop); |
| 1890 | } |
| 1891 | } |
| 1892 | } else if (key == "valign"_L1 ) { |
| 1893 | value = std::move(value).toLower(); |
| 1894 | if (value == "top"_L1 ) |
| 1895 | node->charFormat.setVerticalAlignment(QTextCharFormat::AlignTop); |
| 1896 | else if (value == "middle"_L1 ) |
| 1897 | node->charFormat.setVerticalAlignment(QTextCharFormat::AlignMiddle); |
| 1898 | else if (value == "bottom"_L1 ) |
| 1899 | node->charFormat.setVerticalAlignment(QTextCharFormat::AlignBottom); |
| 1900 | } else if (key == "dir"_L1 ) { |
| 1901 | value = std::move(value).toLower(); |
| 1902 | if (value == "ltr"_L1 ) |
| 1903 | node->blockFormat.setLayoutDirection(Qt::LeftToRight); |
| 1904 | else if (value == "rtl"_L1 ) |
| 1905 | node->blockFormat.setLayoutDirection(Qt::RightToLeft); |
| 1906 | } else if (key == "title"_L1 ) { |
| 1907 | node->charFormat.setToolTip(value); |
| 1908 | } else if (key == "id"_L1 ) { |
| 1909 | node->charFormat.setAnchor(true); |
| 1910 | node->charFormat.setAnchorNames({value}); |
| 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | #ifndef QT_NO_CSSPARSER |
| 1915 | if (resourceProvider && !linkHref.isEmpty() && linkType == "text/css"_L1 ) |
| 1916 | importStyleSheet(href: linkHref); |
| 1917 | #endif |
| 1918 | } |
| 1919 | |
| 1920 | #ifndef QT_NO_CSSPARSER |
| 1921 | class QTextHtmlStyleSelector : public QCss::StyleSelector |
| 1922 | { |
| 1923 | public: |
| 1924 | inline QTextHtmlStyleSelector(const QTextHtmlParser *parser) |
| 1925 | : parser(parser) { nameCaseSensitivity = Qt::CaseInsensitive; } |
| 1926 | |
| 1927 | QStringList nodeNames(NodePtr node) const override; |
| 1928 | QString attributeValue(NodePtr node, const QCss::AttributeSelector &aSelector) const override; |
| 1929 | bool hasAttributes(NodePtr node) const override; |
| 1930 | bool isNullNode(NodePtr node) const override; |
| 1931 | NodePtr parentNode(NodePtr node) const override; |
| 1932 | NodePtr previousSiblingNode(NodePtr node) const override; |
| 1933 | NodePtr duplicateNode(NodePtr node) const override; |
| 1934 | void freeNode(NodePtr node) const override; |
| 1935 | |
| 1936 | private: |
| 1937 | const QTextHtmlParser *parser; |
| 1938 | }; |
| 1939 | |
| 1940 | QStringList QTextHtmlStyleSelector::nodeNames(NodePtr node) const |
| 1941 | { |
| 1942 | return QStringList(parser->at(i: node.id).tag.toLower()); |
| 1943 | } |
| 1944 | |
| 1945 | #endif // QT_NO_CSSPARSER |
| 1946 | |
| 1947 | #ifndef QT_NO_CSSPARSER |
| 1948 | |
| 1949 | static inline int findAttribute(const QStringList &attributes, const QString &name) |
| 1950 | { |
| 1951 | int idx = -1; |
| 1952 | do { |
| 1953 | idx = attributes.indexOf(str: name, from: idx + 1); |
| 1954 | } while (idx != -1 && (idx % 2 == 1)); |
| 1955 | return idx; |
| 1956 | } |
| 1957 | |
| 1958 | QString QTextHtmlStyleSelector::attributeValue(NodePtr node, const QCss::AttributeSelector &aSelector) const |
| 1959 | { |
| 1960 | const QStringList &attributes = parser->at(i: node.id).attributes; |
| 1961 | const int idx = findAttribute(attributes, name: aSelector.name); |
| 1962 | if (idx == -1) |
| 1963 | return QString(); |
| 1964 | return attributes.at(i: idx + 1); |
| 1965 | } |
| 1966 | |
| 1967 | bool QTextHtmlStyleSelector::hasAttributes(NodePtr node) const |
| 1968 | { |
| 1969 | const QStringList &attributes = parser->at(i: node.id).attributes; |
| 1970 | return !attributes.isEmpty(); |
| 1971 | } |
| 1972 | |
| 1973 | bool QTextHtmlStyleSelector::isNullNode(NodePtr node) const |
| 1974 | { |
| 1975 | return node.id == 0; |
| 1976 | } |
| 1977 | |
| 1978 | QCss::StyleSelector::NodePtr QTextHtmlStyleSelector::parentNode(NodePtr node) const |
| 1979 | { |
| 1980 | NodePtr parent; |
| 1981 | parent.id = 0; |
| 1982 | if (node.id) { |
| 1983 | parent.id = parser->at(i: node.id).parent; |
| 1984 | } |
| 1985 | return parent; |
| 1986 | } |
| 1987 | |
| 1988 | QCss::StyleSelector::NodePtr QTextHtmlStyleSelector::duplicateNode(NodePtr node) const |
| 1989 | { |
| 1990 | return node; |
| 1991 | } |
| 1992 | |
| 1993 | QCss::StyleSelector::NodePtr QTextHtmlStyleSelector::previousSiblingNode(NodePtr node) const |
| 1994 | { |
| 1995 | NodePtr sibling; |
| 1996 | sibling.id = 0; |
| 1997 | if (!node.id) |
| 1998 | return sibling; |
| 1999 | int parent = parser->at(i: node.id).parent; |
| 2000 | if (!parent) |
| 2001 | return sibling; |
| 2002 | const int childIdx = parser->at(i: parent).children.indexOf(t: node.id); |
| 2003 | if (childIdx <= 0) |
| 2004 | return sibling; |
| 2005 | sibling.id = parser->at(i: parent).children.at(i: childIdx - 1); |
| 2006 | return sibling; |
| 2007 | } |
| 2008 | |
| 2009 | void QTextHtmlStyleSelector::freeNode(NodePtr) const |
| 2010 | { |
| 2011 | } |
| 2012 | |
| 2013 | void QTextHtmlParser::resolveStyleSheetImports(const QCss::StyleSheet &sheet) |
| 2014 | { |
| 2015 | for (int i = 0; i < sheet.importRules.size(); ++i) { |
| 2016 | const QCss::ImportRule &rule = sheet.importRules.at(i); |
| 2017 | if (rule.media.isEmpty() || rule.media.contains(str: "screen"_L1 , cs: Qt::CaseInsensitive)) |
| 2018 | importStyleSheet(href: rule.href); |
| 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | void QTextHtmlParser::importStyleSheet(const QString &href) |
| 2023 | { |
| 2024 | if (!resourceProvider) |
| 2025 | return; |
| 2026 | for (int i = 0; i < externalStyleSheets.size(); ++i) |
| 2027 | if (externalStyleSheets.at(i).url == href) |
| 2028 | return; |
| 2029 | |
| 2030 | QVariant res = resourceProvider->resource(type: QTextDocument::StyleSheetResource, name: href); |
| 2031 | QString css; |
| 2032 | if (res.userType() == QMetaType::QString) { |
| 2033 | css = res.toString(); |
| 2034 | } else if (res.userType() == QMetaType::QByteArray) { |
| 2035 | // #### detect @charset |
| 2036 | css = QString::fromUtf8(ba: res.toByteArray()); |
| 2037 | } |
| 2038 | if (!css.isEmpty()) { |
| 2039 | QCss::Parser parser(css); |
| 2040 | QCss::StyleSheet sheet; |
| 2041 | parser.parse(styleSheet: &sheet, nameCaseSensitivity: Qt::CaseInsensitive); |
| 2042 | externalStyleSheets.append(t: ExternalStyleSheet(href, sheet)); |
| 2043 | resolveStyleSheetImports(sheet); |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | QList<QCss::Declaration> standardDeclarationForNode(const QTextHtmlParserNode &node) |
| 2048 | { |
| 2049 | QList<QCss::Declaration> decls; |
| 2050 | QCss::Declaration decl; |
| 2051 | QCss::Value val; |
| 2052 | switch (node.id) { |
| 2053 | case Html_a: |
| 2054 | case Html_u: { |
| 2055 | bool needsUnderline = (node.id == Html_u) ? true : false; |
| 2056 | if (node.id == Html_a) { |
| 2057 | for (int i = 0; i < node.attributes.size(); i += 2) { |
| 2058 | const QString key = node.attributes.at(i); |
| 2059 | if (key.compare(other: "href"_L1 , cs: Qt::CaseInsensitive) == 0 |
| 2060 | && !node.attributes.at(i: i + 1).isEmpty()) { |
| 2061 | needsUnderline = true; |
| 2062 | decl.d->property = "color"_L1 ; |
| 2063 | decl.d->propertyId = QCss::Color; |
| 2064 | val.type = QCss::Value::Function; |
| 2065 | val.variant = QStringList() << "palette"_L1 << "link"_L1 ; |
| 2066 | decl.d->values = QList<QCss::Value> { val }; |
| 2067 | decl.d->inheritable = true; |
| 2068 | decls << decl; |
| 2069 | break; |
| 2070 | } |
| 2071 | } |
| 2072 | } |
| 2073 | if (needsUnderline) { |
| 2074 | decl = QCss::Declaration(); |
| 2075 | decl.d->property = "text-decoration"_L1 ; |
| 2076 | decl.d->propertyId = QCss::TextDecoration; |
| 2077 | val.type = QCss::Value::KnownIdentifier; |
| 2078 | val.variant = QVariant(QCss::Value_Underline); |
| 2079 | decl.d->values = QList<QCss::Value> { val }; |
| 2080 | decl.d->inheritable = true; |
| 2081 | decls << decl; |
| 2082 | } |
| 2083 | break; |
| 2084 | } |
| 2085 | case Html_b: |
| 2086 | case Html_strong: |
| 2087 | case Html_h1: |
| 2088 | case Html_h2: |
| 2089 | case Html_h3: |
| 2090 | case Html_h4: |
| 2091 | case Html_h5: |
| 2092 | case Html_th: |
| 2093 | decl = QCss::Declaration(); |
| 2094 | decl.d->property = "font-weight"_L1 ; |
| 2095 | decl.d->propertyId = QCss::FontWeight; |
| 2096 | val.type = QCss::Value::KnownIdentifier; |
| 2097 | val.variant = QVariant(QCss::Value_Bold); |
| 2098 | decl.d->values = QList<QCss::Value> { val }; |
| 2099 | decl.d->inheritable = true; |
| 2100 | decls << decl; |
| 2101 | if (node.id == Html_b || node.id == Html_strong) |
| 2102 | break; |
| 2103 | Q_FALLTHROUGH(); |
| 2104 | case Html_big: |
| 2105 | case Html_small: |
| 2106 | if (node.id != Html_th) { |
| 2107 | decl = QCss::Declaration(); |
| 2108 | decl.d->property = "font-size"_L1 ; |
| 2109 | decl.d->propertyId = QCss::FontSize; |
| 2110 | decl.d->inheritable = false; |
| 2111 | val.type = QCss::Value::KnownIdentifier; |
| 2112 | switch (node.id) { |
| 2113 | case Html_h1: val.variant = QVariant(QCss::Value_XXLarge); break; |
| 2114 | case Html_h2: val.variant = QVariant(QCss::Value_XLarge); break; |
| 2115 | case Html_h3: case Html_big: val.variant = QVariant(QCss::Value_Large); break; |
| 2116 | case Html_h4: val.variant = QVariant(QCss::Value_Medium); break; |
| 2117 | case Html_h5: case Html_small: val.variant = QVariant(QCss::Value_Small); break; |
| 2118 | default: break; |
| 2119 | } |
| 2120 | decl.d->values = QList<QCss::Value> { val }; |
| 2121 | decls << decl; |
| 2122 | break; |
| 2123 | } |
| 2124 | Q_FALLTHROUGH(); |
| 2125 | case Html_center: |
| 2126 | case Html_td: |
| 2127 | decl = QCss::Declaration(); |
| 2128 | decl.d->property = "text-align"_L1 ; |
| 2129 | decl.d->propertyId = QCss::TextAlignment; |
| 2130 | val.type = QCss::Value::KnownIdentifier; |
| 2131 | val.variant = (node.id == Html_td) ? QVariant(QCss::Value_Left) : QVariant(QCss::Value_Center); |
| 2132 | decl.d->values = QList<QCss::Value> { val }; |
| 2133 | decl.d->inheritable = true; |
| 2134 | decls << decl; |
| 2135 | break; |
| 2136 | case Html_s: |
| 2137 | decl = QCss::Declaration(); |
| 2138 | decl.d->property = "text-decoration"_L1 ; |
| 2139 | decl.d->propertyId = QCss::TextDecoration; |
| 2140 | val.type = QCss::Value::KnownIdentifier; |
| 2141 | val.variant = QVariant(QCss::Value_LineThrough); |
| 2142 | decl.d->values = QList<QCss::Value> { val }; |
| 2143 | decl.d->inheritable = true; |
| 2144 | decls << decl; |
| 2145 | break; |
| 2146 | case Html_em: |
| 2147 | case Html_i: |
| 2148 | case Html_cite: |
| 2149 | case Html_address: |
| 2150 | case Html_var: |
| 2151 | case Html_dfn: |
| 2152 | decl = QCss::Declaration(); |
| 2153 | decl.d->property = "font-style"_L1 ; |
| 2154 | decl.d->propertyId = QCss::FontStyle; |
| 2155 | val.type = QCss::Value::KnownIdentifier; |
| 2156 | val.variant = QVariant(QCss::Value_Italic); |
| 2157 | decl.d->values = QList<QCss::Value> { val }; |
| 2158 | decl.d->inheritable = true; |
| 2159 | decls << decl; |
| 2160 | break; |
| 2161 | case Html_sub: |
| 2162 | case Html_sup: |
| 2163 | decl = QCss::Declaration(); |
| 2164 | decl.d->property = "vertical-align"_L1 ; |
| 2165 | decl.d->propertyId = QCss::VerticalAlignment; |
| 2166 | val.type = QCss::Value::KnownIdentifier; |
| 2167 | val.variant = (node.id == Html_sub) ? QVariant(QCss::Value_Sub) : QVariant(QCss::Value_Super); |
| 2168 | decl.d->values = QList<QCss::Value> { val }; |
| 2169 | decl.d->inheritable = true; |
| 2170 | decls << decl; |
| 2171 | break; |
| 2172 | case Html_ul: |
| 2173 | case Html_ol: |
| 2174 | decl = QCss::Declaration(); |
| 2175 | decl.d->property = "list-style"_L1 ; |
| 2176 | decl.d->propertyId = QCss::ListStyle; |
| 2177 | val.type = QCss::Value::KnownIdentifier; |
| 2178 | val.variant = (node.id == Html_ul) ? QVariant(QCss::Value_Disc) : QVariant(QCss::Value_Decimal); |
| 2179 | decl.d->values = QList<QCss::Value> { val }; |
| 2180 | decl.d->inheritable = true; |
| 2181 | decls << decl; |
| 2182 | break; |
| 2183 | case Html_code: |
| 2184 | case Html_tt: |
| 2185 | case Html_kbd: |
| 2186 | case Html_samp: |
| 2187 | case Html_pre: { |
| 2188 | decl = QCss::Declaration(); |
| 2189 | decl.d->property = "font-family"_L1 ; |
| 2190 | decl.d->propertyId = QCss::FontFamily; |
| 2191 | QList<QCss::Value> values; |
| 2192 | val.type = QCss::Value::String; |
| 2193 | val.variant = QFontDatabase::systemFont(type: QFontDatabase::FixedFont).families().constFirst(); |
| 2194 | values << val; |
| 2195 | decl.d->values = values; |
| 2196 | decl.d->inheritable = true; |
| 2197 | decls << decl; |
| 2198 | } |
| 2199 | if (node.id != Html_pre) |
| 2200 | break; |
| 2201 | Q_FALLTHROUGH(); |
| 2202 | case Html_br: |
| 2203 | case Html_nobr: |
| 2204 | decl = QCss::Declaration(); |
| 2205 | decl.d->property = "whitespace"_L1 ; |
| 2206 | decl.d->propertyId = QCss::Whitespace; |
| 2207 | val.type = QCss::Value::KnownIdentifier; |
| 2208 | switch (node.id) { |
| 2209 | case Html_br: val.variant = QVariant(QCss::Value_PreWrap); break; |
| 2210 | case Html_nobr: val.variant = QVariant(QCss::Value_NoWrap); break; |
| 2211 | case Html_pre: val.variant = QVariant(QCss::Value_Pre); break; |
| 2212 | default: break; |
| 2213 | } |
| 2214 | decl.d->values = QList<QCss::Value> { val }; |
| 2215 | decl.d->inheritable = true; |
| 2216 | decls << decl; |
| 2217 | break; |
| 2218 | default: |
| 2219 | break; |
| 2220 | } |
| 2221 | return decls; |
| 2222 | } |
| 2223 | |
| 2224 | QList<QCss::Declaration> QTextHtmlParser::declarationsForNode(int node) const |
| 2225 | { |
| 2226 | QList<QCss::Declaration> decls; |
| 2227 | |
| 2228 | QTextHtmlStyleSelector selector(this); |
| 2229 | |
| 2230 | int idx = 0; |
| 2231 | selector.styleSheets.resize(size: (resourceProvider ? 1 : 0) |
| 2232 | + externalStyleSheets.size() |
| 2233 | + inlineStyleSheets.size()); |
| 2234 | if (resourceProvider) |
| 2235 | selector.styleSheets[idx++] = QTextDocumentPrivate::get(document: resourceProvider)->parsedDefaultStyleSheet; |
| 2236 | |
| 2237 | for (int i = 0; i < externalStyleSheets.size(); ++i, ++idx) |
| 2238 | selector.styleSheets[idx] = externalStyleSheets.at(i).sheet; |
| 2239 | |
| 2240 | for (int i = 0; i < inlineStyleSheets.size(); ++i, ++idx) |
| 2241 | selector.styleSheets[idx] = inlineStyleSheets.at(i); |
| 2242 | |
| 2243 | selector.medium = resourceProvider ? resourceProvider->metaInformation(info: QTextDocument::CssMedia) : "screen"_L1 ; |
| 2244 | |
| 2245 | QCss::StyleSelector::NodePtr n; |
| 2246 | n.id = node; |
| 2247 | |
| 2248 | const char * = nullptr; |
| 2249 | if (nodes.at(i: node)->id == Html_a && nodes.at(i: node)->hasHref) |
| 2250 | extraPseudo = "link" ; |
| 2251 | // Ensure that our own style is taken into consideration |
| 2252 | decls = standardDeclarationForNode(node: *nodes.at(i: node)); |
| 2253 | decls += selector.declarationsForNode(node: n, extraPseudo); |
| 2254 | n = selector.parentNode(node: n); |
| 2255 | while (!selector.isNullNode(node: n)) { |
| 2256 | QList<QCss::Declaration> inheritedDecls; |
| 2257 | inheritedDecls = selector.declarationsForNode(node: n, extraPseudo); |
| 2258 | for (int i = 0; i < inheritedDecls.size(); ++i) { |
| 2259 | const QCss::Declaration &decl = inheritedDecls.at(i); |
| 2260 | if (decl.d->inheritable) |
| 2261 | decls.prepend(t: decl); |
| 2262 | } |
| 2263 | n = selector.parentNode(node: n); |
| 2264 | } |
| 2265 | return decls; |
| 2266 | } |
| 2267 | |
| 2268 | bool QTextHtmlParser::nodeIsChildOf(int i, QTextHTMLElements id) const |
| 2269 | { |
| 2270 | while (i) { |
| 2271 | if (at(i).id == id) |
| 2272 | return true; |
| 2273 | i = at(i).parent; |
| 2274 | } |
| 2275 | return false; |
| 2276 | } |
| 2277 | |
| 2278 | #endif // QT_NO_CSSPARSER |
| 2279 | |
| 2280 | QT_END_NAMESPACE |
| 2281 | |
| 2282 | #endif // QT_NO_TEXTHTMLPARSER |
| 2283 | |