Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtexthtmlparser.cpp
Go to the documentation of this file.
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
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"
16#include "qtextcursor.h"
17#include "qfont_p.h"
18
19#include <algorithm>
20
21#ifndef QT_NO_TEXTHTMLPARSER
22
23QT_BEGIN_NAMESPACE
24
25using namespace Qt::StringLiterals;
26
27// see also tst_qtextdocumentfragment.cpp
28#define MAX_ENTITY 258
29static const struct QTextHtmlEntity { const char name[9]; char16_t code; } entities[]= {
30 { "AElig", 0x00c6 },
31 { "AMP", 38 },
32 { "Aacute", 0x00c1 },
33 { "Acirc", 0x00c2 },
34 { "Agrave", 0x00c0 },
35 { "Alpha", 0x0391 },
36 { "Aring", 0x00c5 },
37 { "Atilde", 0x00c3 },
38 { "Auml", 0x00c4 },
39 { "Beta", 0x0392 },
40 { "Ccedil", 0x00c7 },
41 { "Chi", 0x03a7 },
42 { "Dagger", 0x2021 },
43 { "Delta", 0x0394 },
44 { "ETH", 0x00d0 },
45 { "Eacute", 0x00c9 },
46 { "Ecirc", 0x00ca },
47 { "Egrave", 0x00c8 },
48 { "Epsilon", 0x0395 },
49 { "Eta", 0x0397 },
50 { "Euml", 0x00cb },
51 { "GT", 62 },
52 { "Gamma", 0x0393 },
53 { "Iacute", 0x00cd },
54 { "Icirc", 0x00ce },
55 { "Igrave", 0x00cc },
56 { "Iota", 0x0399 },
57 { "Iuml", 0x00cf },
58 { "Kappa", 0x039a },
59 { "LT", 60 },
60 { "Lambda", 0x039b },
61 { "Mu", 0x039c },
62 { "Ntilde", 0x00d1 },
63 { "Nu", 0x039d },
64 { "OElig", 0x0152 },
65 { "Oacute", 0x00d3 },
66 { "Ocirc", 0x00d4 },
67 { "Ograve", 0x00d2 },
68 { "Omega", 0x03a9 },
69 { "Omicron", 0x039f },
70 { "Oslash", 0x00d8 },
71 { "Otilde", 0x00d5 },
72 { "Ouml", 0x00d6 },
73 { "Phi", 0x03a6 },
74 { "Pi", 0x03a0 },
75 { "Prime", 0x2033 },
76 { "Psi", 0x03a8 },
77 { "QUOT", 34 },
78 { "Rho", 0x03a1 },
79 { "Scaron", 0x0160 },
80 { "Sigma", 0x03a3 },
81 { "THORN", 0x00de },
82 { "Tau", 0x03a4 },
83 { "Theta", 0x0398 },
84 { "Uacute", 0x00da },
85 { "Ucirc", 0x00db },
86 { "Ugrave", 0x00d9 },
87 { "Upsilon", 0x03a5 },
88 { "Uuml", 0x00dc },
89 { "Xi", 0x039e },
90 { "Yacute", 0x00dd },
91 { "Yuml", 0x0178 },
92 { "Zeta", 0x0396 },
93 { "aacute", 0x00e1 },
94 { "acirc", 0x00e2 },
95 { "acute", 0x00b4 },
96 { "aelig", 0x00e6 },
97 { "agrave", 0x00e0 },
98 { "alefsym", 0x2135 },
99 { "alpha", 0x03b1 },
100 { "amp", 38 },
101 { "and", 0x22a5 },
102 { "ang", 0x2220 },
103 { "apos", 0x0027 },
104 { "aring", 0x00e5 },
105 { "asymp", 0x2248 },
106 { "atilde", 0x00e3 },
107 { "auml", 0x00e4 },
108 { "bdquo", 0x201e },
109 { "beta", 0x03b2 },
110 { "brvbar", 0x00a6 },
111 { "bull", 0x2022 },
112 { "cap", 0x2229 },
113 { "ccedil", 0x00e7 },
114 { "cedil", 0x00b8 },
115 { "cent", 0x00a2 },
116 { "chi", 0x03c7 },
117 { "circ", 0x02c6 },
118 { "clubs", 0x2663 },
119 { "cong", 0x2245 },
120 { "copy", 0x00a9 },
121 { "crarr", 0x21b5 },
122 { "cup", 0x222a },
123 { "curren", 0x00a4 },
124 { "dArr", 0x21d3 },
125 { "dagger", 0x2020 },
126 { "darr", 0x2193 },
127 { "deg", 0x00b0 },
128 { "delta", 0x03b4 },
129 { "diams", 0x2666 },
130 { "divide", 0x00f7 },
131 { "eacute", 0x00e9 },
132 { "ecirc", 0x00ea },
133 { "egrave", 0x00e8 },
134 { "empty", 0x2205 },
135 { "emsp", 0x2003 },
136 { "ensp", 0x2002 },
137 { "epsilon", 0x03b5 },
138 { "equiv", 0x2261 },
139 { "eta", 0x03b7 },
140 { "eth", 0x00f0 },
141 { "euml", 0x00eb },
142 { "euro", 0x20ac },
143 { "exist", 0x2203 },
144 { "fnof", 0x0192 },
145 { "forall", 0x2200 },
146 { "frac12", 0x00bd },
147 { "frac14", 0x00bc },
148 { "frac34", 0x00be },
149 { "frasl", 0x2044 },
150 { "gamma", 0x03b3 },
151 { "ge", 0x2265 },
152 { "gt", 62 },
153 { "hArr", 0x21d4 },
154 { "harr", 0x2194 },
155 { "hearts", 0x2665 },
156 { "hellip", 0x2026 },
157 { "iacute", 0x00ed },
158 { "icirc", 0x00ee },
159 { "iexcl", 0x00a1 },
160 { "igrave", 0x00ec },
161 { "image", 0x2111 },
162 { "infin", 0x221e },
163 { "int", 0x222b },
164 { "iota", 0x03b9 },
165 { "iquest", 0x00bf },
166 { "isin", 0x2208 },
167 { "iuml", 0x00ef },
168 { "kappa", 0x03ba },
169 { "lArr", 0x21d0 },
170 { "lambda", 0x03bb },
171 { "lang", 0x2329 },
172 { "laquo", 0x00ab },
173 { "larr", 0x2190 },
174 { "lceil", 0x2308 },
175 { "ldquo", 0x201c },
176 { "le", 0x2264 },
177 { "lfloor", 0x230a },
178 { "lowast", 0x2217 },
179 { "loz", 0x25ca },
180 { "lrm", 0x200e },
181 { "lsaquo", 0x2039 },
182 { "lsquo", 0x2018 },
183 { "lt", 60 },
184 { "macr", 0x00af },
185 { "mdash", 0x2014 },
186 { "micro", 0x00b5 },
187 { "middot", 0x00b7 },
188 { "minus", 0x2212 },
189 { "mu", 0x03bc },
190 { "nabla", 0x2207 },
191 { "nbsp", 0x00a0 },
192 { "ndash", 0x2013 },
193 { "ne", 0x2260 },
194 { "ni", 0x220b },
195 { "not", 0x00ac },
196 { "notin", 0x2209 },
197 { "nsub", 0x2284 },
198 { "ntilde", 0x00f1 },
199 { "nu", 0x03bd },
200 { "oacute", 0x00f3 },
201 { "ocirc", 0x00f4 },
202 { "oelig", 0x0153 },
203 { "ograve", 0x00f2 },
204 { "oline", 0x203e },
205 { "omega", 0x03c9 },
206 { "omicron", 0x03bf },
207 { "oplus", 0x2295 },
208 { "or", 0x22a6 },
209 { "ordf", 0x00aa },
210 { "ordm", 0x00ba },
211 { "oslash", 0x00f8 },
212 { "otilde", 0x00f5 },
213 { "otimes", 0x2297 },
214 { "ouml", 0x00f6 },
215 { "para", 0x00b6 },
216 { "part", 0x2202 },
217 { "percnt", 0x0025 },
218 { "permil", 0x2030 },
219 { "perp", 0x22a5 },
220 { "phi", 0x03c6 },
221 { "pi", 0x03c0 },
222 { "piv", 0x03d6 },
223 { "plusmn", 0x00b1 },
224 { "pound", 0x00a3 },
225 { "prime", 0x2032 },
226 { "prod", 0x220f },
227 { "prop", 0x221d },
228 { "psi", 0x03c8 },
229 { "quot", 34 },
230 { "rArr", 0x21d2 },
231 { "radic", 0x221a },
232 { "rang", 0x232a },
233 { "raquo", 0x00bb },
234 { "rarr", 0x2192 },
235 { "rceil", 0x2309 },
236 { "rdquo", 0x201d },
237 { "real", 0x211c },
238 { "reg", 0x00ae },
239 { "rfloor", 0x230b },
240 { "rho", 0x03c1 },
241 { "rlm", 0x200f },
242 { "rsaquo", 0x203a },
243 { "rsquo", 0x2019 },
244 { "sbquo", 0x201a },
245 { "scaron", 0x0161 },
246 { "sdot", 0x22c5 },
247 { "sect", 0x00a7 },
248 { "shy", 0x00ad },
249 { "sigma", 0x03c3 },
250 { "sigmaf", 0x03c2 },
251 { "sim", 0x223c },
252 { "spades", 0x2660 },
253 { "sub", 0x2282 },
254 { "sube", 0x2286 },
255 { "sum", 0x2211 },
256 { "sup", 0x2283 },
257 { "sup1", 0x00b9 },
258 { "sup2", 0x00b2 },
259 { "sup3", 0x00b3 },
260 { "supe", 0x2287 },
261 { "szlig", 0x00df },
262 { "tau", 0x03c4 },
263 { "there4", 0x2234 },
264 { "theta", 0x03b8 },
265 { "thetasym", 0x03d1 },
266 { "thinsp", 0x2009 },
267 { "thorn", 0x00fe },
268 { "tilde", 0x02dc },
269 { "times", 0x00d7 },
270 { "trade", 0x2122 },
271 { "uArr", 0x21d1 },
272 { "uacute", 0x00fa },
273 { "uarr", 0x2191 },
274 { "ucirc", 0x00fb },
275 { "ugrave", 0x00f9 },
276 { "uml", 0x00a8 },
277 { "upsih", 0x03d2 },
278 { "upsilon", 0x03c5 },
279 { "uuml", 0x00fc },
280 { "weierp", 0x2118 },
281 { "xi", 0x03be },
282 { "yacute", 0x00fd },
283 { "yen", 0x00a5 },
284 { "yuml", 0x00ff },
285 { "zeta", 0x03b6 },
286 { "zwj", 0x200d },
287 { "zwnj", 0x200c }
289static_assert(MAX_ENTITY == sizeof entities / sizeof *entities);
290
291#if defined(Q_CC_MSVC_ONLY) && _MSC_VER < 1600
292bool operator<(const QTextHtmlEntity &entity1, const QTextHtmlEntity &entity2)
293{
294 return QLatin1StringView(entity1.name) < QLatin1StringView(entity2.name);
295}
296#endif
297
298static bool operator<(QStringView entityStr, const QTextHtmlEntity &entity)
299{
300 return entityStr < QLatin1StringView(entity.name);
301}
302
303static bool operator<(const QTextHtmlEntity &entity, QStringView entityStr)
304{
305 return QLatin1StringView(entity.name) < entityStr;
306}
307
308static 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(start, end, entity);
313 if (e == end || (entity < *e))
314 return QChar();
315 return e->code;
316}
317
318static 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.
393 { "qt", Html_body /*deliberate mapping*/, QTextHtmlElement::DisplayBlock },
415};
416
417static bool operator<(QStringView str, const QTextHtmlElement &e)
418{
419 return str < QLatin1StringView(e.name);
420}
421
422static bool operator<(const QTextHtmlElement &e, QStringView str)
423{
424 return QLatin1StringView(e.name) < str;
425}
426
427static const QTextHtmlElement *lookupElementHelper(QStringView element)
428{
429 const QTextHtmlElement *start = &elements[0];
431 const QTextHtmlElement *e = std::lower_bound(start, end, element);
432 if ((e == end) || (element < *e))
433 return nullptr;
434 return e;
435}
436
437int 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"
446static QString quoteNewline(const QString &s)
447{
448 QString n = s;
449 n.replace(u'\n', "\\n"_L1);
450 return n;
451}
452
454 : parent(0), id(Html_unknown),
456 hasCssListIndent(false), isEmptyParagraph(false), isTextFrame(false), isRootFrame(false),
461 borderCollapse(false),
463{
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
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(at(i).text);
482 }
483}
484
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(0).isSpace()) {
501
502 int lastSibling = count() - 2;
503 while (lastSibling
504 && at(lastSibling).parent != lastNode->parent
505 && at(lastSibling).displayMode == QTextHtmlElement::DisplayInline) {
506 lastSibling = at(lastSibling).parent;
507 }
508
509 if (at(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(new QTextHtmlParserNode);
533 newNode = nodes.last();
534 }
535
536 newNode->parent = parent;
537 return newNode;
538}
539
540void QTextHtmlParser::parse(const QString &text, const QTextDocument *_resourceProvider)
541{
542 qDeleteAll(nodes);
543 nodes.clear();
544 nodes.append(new QTextHtmlParserNode);
545 txt = text;
546 pos = 0;
547 len = txt.size();
548 textEditMode = false;
549 resourceProvider = _resourceProvider;
550 parse();
551 //dumpHtml();
552}
553
554int 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
564int 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
582int QTextHtmlParser::topMargin(int i) const
583{
584 if (!i)
585 return 0;
586 return at(i).margin[MarginTop];
587}
588
590{
591 if (!i)
592 return 0;
593 return at(i).margin[MarginBottom];
594}
595
597{
598 while (pos < len && txt.at(pos).isSpace() && txt.at(pos) != QChar::ParagraphSeparator)
599 pos++;
600}
601
603{
604 while (pos < len) {
605 QChar c = txt.at(pos++);
606 if (c == u'<') {
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 "<"
618{
620
621 // handle comments and other exclamation mark declarations
622 if (hasPrefix(u'!')) {
624 if (nodes.last()->wsm != QTextHtmlParserNode::WhiteSpacePre
625 && nodes.last()->wsm != QTextHtmlParserNode::WhiteSpacePreWrap
626 && !textEditMode)
628 return;
629 }
630
631 // if close tag just close
632 if (hasPrefix(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(&sheet, Qt::CaseInsensitive);
639 inlineStyleSheets.append(sheet);
640 resolveStyleSheetImports(sheet);
641#endif
642 }
644 return;
645 }
646
647 int p = last();
648 while (p && at(p).tag.size() == 0)
649 p = at(p).parent;
650
652
653 // parse tag name
654 node->tag = parseWord().toLower();
655
656 const QTextHtmlElement *elem = lookupElementHelper(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(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();
674
675#ifndef QT_NO_CSSPARSER
676 const int nodeIndex = nodes.size() - 1; // this new node is always the last
677 node->applyCssDeclarations(declarationsForNode(nodeIndex), resourceProvider);
678#endif
679 applyAttributes(node->attributes);
680
681 // finish tag
682 bool tagClosed = false;
683 while (pos < len && txt.at(pos) != u'>') {
684 if (txt.at(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
696 && node->isBlock()) {
697 if (pos < len - 1 && txt.at(pos) == u'\n')
698 ++pos;
699 }
700
701 if (node->mayNotHaveChildren() || tagClosed) {
704 }
705}
706
707// parses a tag beginning with "/"
709{
710 ++pos;
711 QString tag = parseWord().toLower().trimmed();
712 while (pos < len) {
713 QChar c = txt.at(pos++);
714 if (c == u'>')
715 break;
716 }
717
718 // find corresponding open node
719 int p = last();
720 if (p > 0
721 && at(p - 1).tag == tag
723 p--;
724
725 while (p && at(p).tag != tag)
726 p = at(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
741 && at(p).isBlock()) {
742 if (at(last()).text.endsWith(u'\n'))
743 nodes[last()]->text.chop(1);
744 }
745
748}
749
750// parses a tag beginning with "!"
752{
753 ++pos;
754 if (hasPrefix(u'-') && hasPrefix(u'-', 1)) {
755 pos += 2;
756 // eat comments
757 int end = txt.indexOf("-->"_L1, pos);
758 pos = (end >= 0 ? end + 3 : len);
759 } else {
760 // eat internal tags
761 while (pos < len) {
762 QChar c = txt.at(pos++);
763 if (c == u'>')
764 break;
765 }
766 }
767}
768
769QString 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(0) == u'#') {
776 entity = entity.mid(1); // removing leading #
777
778 int base = 10;
779 bool ok = false;
780
781 if (entity.at(0).toLower() == u'x') { // hex entity?
782 entity = entity.mid(1);
783 base = 16;
784 }
785
786 uint uc = entity.toUInt(&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(uc)}.toString();
791 }
792 }
793 return {};
794}
795
796// parses an entity after "&", and returns it
798{
799 const int recover = pos;
800 int entityLen = 0;
801 while (pos < len) {
802 QChar c = txt.at(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(recover, entityLen);
812 QString parsedEntity = parseEntity(entity);
813 if (!parsedEntity.isNull()) {
814 return parsedEntity;
815 }
816 }
817error:
818 pos = recover;
819 return "&"_L1;
820}
821
822// parses one word, possibly quoted, and returns it
824{
825 QString word;
826 if (hasPrefix(u'\"')) { // double quotes
827 ++pos;
828 while (pos < len) {
829 QChar c = txt.at(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(u'\'')) { // single quotes
838 ++pos;
839 while (pos < len) {
840 QChar c = txt.at(pos++);
841 // Allow for escaped single quotes as they may be part of the string
842 if (c == u'\'' && (txt.size() > 1 && txt.at(pos - 2) != u'\\'))
843 break;
844 else
845 word += c;
846 }
847 } else { // normal text
848 while (pos < len) {
849 QChar c = txt.at(pos++);
850 if (c == u'>' || (c == u'/' && hasPrefix(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
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(n).id != Html_tr)
877 n = at(n).parent;
878
879 if (!n) {
880 nodes.insert(nodes.size() - 1, new QTextHtmlParserNode);
881 nodes.insert(nodes.size() - 1, 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(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(n).id != Html_table)
902 n = at(n).parent;
903
904 if (!n) {
905 nodes.insert(nodes.size() - 1, 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(p).isBlock())
936 p = at(p).parent;
937
938 if (!p || at(p).id != Html_p)
939 p = node->parent;
940 }
941
942 // some elements are not self nesting
943 if (node->id == at(p).id
945 p = at(p).parent;
946
947 // some elements are not allowed in certain contexts
948 while ((p && !node->allowedInContext(at(p).id))
949 // ### make new styles aware of empty tags
951 ) {
952 p = at(p).parent;
953 }
954
955 node->parent = p;
956
957 // makes it easier to traverse the tree, later
958 nodes[p]->children.append(nodes.size() - 1);
959 return node;
960}
961
962// sets all properties on the new node
964{
965 QTextHtmlParserNode *node = nodes.last();
966 const QTextHtmlParserNode *parent = nodes.at(node->parent);
967 node->initializeProperties(parent, this);
968}
969
971{
972 if (!isListStart())
973 return false;
974
975 int p = parent;
976 while (p) {
977 if (parser->at(p).isListStart())
978 return true;
979 p = parser->at(p).parent;
980 }
981 return false;
982}
983
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(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(QTextFormat::BlockAlignment))
999 blockFormat.setAlignment(parent->blockFormat.alignment());
1000 else
1001 blockFormat.clearProperty(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(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(QTextFormat::AnchorName);
1018 wsm = parent->wsm;
1019
1020 // initialize remaining properties
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("href"_L1, Qt::CaseInsensitive) == 0
1036 && !attributes.at(i + 1).isEmpty()) {
1037 hasHref = true;
1038 }
1039 }
1040 charFormat.setAnchor(true);
1041 break;
1042 case Html_big:
1043 charFormat.setProperty(QTextFormat::FontSizeAdjustment, int(1));
1044 break;
1045 case Html_small:
1046 charFormat.setProperty(QTextFormat::FontSizeAdjustment, int(-1));
1047 break;
1048 case Html_h1:
1049 charFormat.setProperty(QTextFormat::FontSizeAdjustment, int(3));
1052 break;
1053 case Html_h2:
1054 charFormat.setProperty(QTextFormat::FontSizeAdjustment, int(2));
1057 break;
1058 case Html_h3:
1059 charFormat.setProperty(QTextFormat::FontSizeAdjustment, int(1));
1062 break;
1063 case Html_h4:
1064 charFormat.setProperty(QTextFormat::FontSizeAdjustment, int(0));
1067 break;
1068 case Html_h5:
1069 charFormat.setProperty(QTextFormat::FontSizeAdjustment, int(-1));
1072 break;
1073 case Html_p:
1076 break;
1077 case Html_ul:
1078 // nested lists don't have margins, except for the toplevel one
1079 if (!isNestedList(parser)) {
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)) {
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:
1099 break;
1100 case Html_blockquote:
1105 blockFormat.setProperty(QTextFormat::BlockQuoteLevel, 1);
1106 break;
1107 case Html_dl:
1110 break;
1111 case Html_dd:
1113 break;
1114 default: break;
1115 }
1116}
1117
1118#ifndef QT_NO_CSSPARSER
1119void 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(QTextFormat::ListStyle, listStyle);
1140}
1141
1142static 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
1180void QTextHtmlParserNode::applyCssDeclarations(const QList<QCss::Declaration> &declarations, const QTextDocument *resourceProvider)
1181{
1182 QCss::ValueExtractor extractor(declarations);
1183 extractor.extractBox(margin, 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(cssBorder, borderBrush, cssStyles, cssRadii);
1198 for (int i = 0; i < 4; ++i) {
1199 borderStyles[i] = toQTextFrameFormat(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(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, 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("proportional"_L1, Qt::CaseInsensitive) == 0)
1266 lineHeightType = QTextBlockFormat::ProportionalHeight;
1267 else if (lineHeightTypeName.compare("fixed"_L1, Qt::CaseInsensitive) == 0)
1268 lineHeightType = QTextBlockFormat::FixedHeight;
1269 else if (lineHeightTypeName.compare("minimum"_L1, Qt::CaseInsensitive) == 0)
1270 lineHeightType = QTextBlockFormat::MinimumHeight;
1271 else if (lineHeightTypeName.compare("line-distance"_L1, 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(QTextBlockFormat::LineHeight, lineHeight);
1279 }
1280
1281 blockFormat.setProperty(QTextBlockFormat::LineHeightType, lineHeightType);
1282 hasOwnLineHeightType = true;
1283 }
1284 break;
1285 case QCss::LineHeight: {
1286 qreal lineHeight;
1287 QTextBlockFormat::LineHeightTypes lineHeightType;
1288 if (decl.realValue(&lineHeight, "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);
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(lineHeight, lineHeightType);
1312 break;
1313 }
1314 case QCss::TextIndent: {
1315 qreal indent = 0;
1316 if (decl.realValue(&indent, "px"))
1317 blockFormat.setTextIndent(indent);
1318 break; }
1319 case QCss::QtListIndent:
1320 if (decl.intValue(&cssListIndent))
1321 hasCssListIndent = true;
1322 break;
1323 case QCss::QtParagraphType:
1324 if (decl.d->values.constFirst().variant.toString().compare("empty"_L1, Qt::CaseInsensitive) == 0)
1325 isEmptyParagraph = true;
1326 break;
1327 case QCss::QtTableType:
1328 if (decl.d->values.constFirst().variant.toString().compare("frame"_L1, Qt::CaseInsensitive) == 0)
1329 isTextFrame = true;
1330 else if (decl.d->values.constFirst().variant.toString().compare("root"_L1, 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(resourceProvider) != nullptr) {
1407 bool ok;
1408 qint64 searchKey = decl.d->values.constFirst().variant.toLongLong(&ok);
1409 if (ok)
1410 applyForegroundImage(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(&width, "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(&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(&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(&f, &adjustment);
1507 if (f.pixelSize() > INT32_MAX / 2)
1508 f.setPixelSize(INT32_MAX / 2); // avoid even more extreme values
1509 charFormat.setFont(f, QTextCharFormat::FontPropertiesSpecifiedOnly);
1510
1511 if (adjustment >= -1)
1512 charFormat.setProperty(QTextFormat::FontSizeAdjustment, 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(bgImage, resourceProvider);
1526 } else if (bgBrush.style() != Qt::NoBrush) {
1527 charFormat.setBackground(bgBrush);
1528 if (id == Html_hr)
1529 blockFormat.setProperty(QTextFormat::BackgroundBrush, bgBrush);
1530 }
1531 }
1532}
1533
1534#endif // QT_NO_CSSPARSER
1535
1536void QTextHtmlParserNode::applyForegroundImage(qint64 searchKey, const QTextDocument *resourceProvider)
1537{
1538 const QTextDocumentPrivate *priv = QTextDocumentPrivate::get(resourceProvider);
1539 for (int i = 0; i < priv->formats.numFormats(); ++i) {
1540 QTextCharFormat format = priv->formats.charFormat(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("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
1567void QTextHtmlParserNode::applyBackgroundImage(const QString &url, const QTextDocument *resourceProvider)
1568{
1569 if (!url.isEmpty() && resourceProvider) {
1570 QVariant val = resourceProvider->resource(QTextDocument::ImageResource, 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>(val);
1576 charFormat.setBackground(image);
1577 } else if (val.userType() == QMetaType::QByteArray) {
1578 QImage image;
1579 if (image.loadFromData(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>(val));
1586 } else if (val.userType() == QMetaType::QByteArray) {
1587 QPixmap pm;
1588 if (pm.loadFromData(val.toByteArray())) {
1589 charFormat.setBackground(pm);
1590 }
1591 }
1592 }
1593 }
1594 if (!url.isEmpty())
1595 charFormat.setProperty(QTextFormat::BackgroundImageUrl, url);
1596}
1597
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
1606static bool setIntAttribute(int *destination, const QString &value)
1607{
1608 bool ok = false;
1609 int val = value.toInt(&ok);
1610 if (ok)
1611 *destination = val;
1612
1613 return ok;
1614}
1615
1616static bool setFloatAttribute(qreal *destination, const QString &value)
1617{
1618 bool ok = false;
1619 qreal val = value.toDouble(&ok);
1620 if (ok)
1621 *destination = val;
1622
1623 return ok;
1624}
1625
1626static void setWidthAttribute(QTextLength *width, const QString &valueStr)
1627{
1628 bool ok = false;
1629 qreal realVal = valueStr.toDouble(&ok);
1630 if (ok) {
1631 *width = QTextLength(QTextLength::FixedLength, realVal);
1632 } else {
1633 auto value = QStringView(valueStr).trimmed();
1634 if (!value.isEmpty() && value.endsWith(u'%')) {
1635 value.truncate(value.size() - 1);
1636 realVal = value.toDouble(&ok);
1637 if (ok)
1638 *width = QTextLength(QTextLength::PercentageLength, realVal);
1639 }
1640 }
1641}
1642
1643#ifndef QT_NO_CSSPARSER
1644void 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(&sheet, Qt::CaseInsensitive);
1650 if (sheet.styleRules.size() != 1) return;
1651 applyCssDeclarations(sheet.styleRules.at(0).declarations, resourceProvider);
1652}
1653#endif
1654
1656{
1657 QStringList attrs;
1658
1659 while (pos < len) {
1660 eatSpace();
1661 if (hasPrefix(u'>') || hasPrefix(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(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
1681void 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 + 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(0) != u'+' && value.at(0) != u'-')
1703 n -= 3;
1704 node->charFormat.setProperty(QTextFormat::FontSizeAdjustment, n);
1705 } else if (key == "face"_L1) {
1706 if (value.contains(u',')) {
1707 QStringList families;
1708 for (auto family : value.tokenize(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(value);
1716 if (!c.isValid())
1717 qWarning("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(&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(&node->imageWidth, value);
1770 } else if (key == "height"_L1) {
1771 node->imageHeight = -2; // register that there is a value for it.
1772 setFloatAttribute(&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(value);
1783 if (!c.isValid())
1784 qWarning("QTextHtmlParser::applyAttributes: Unknown color name '%s'",value.toLatin1().constData());
1785 node->charFormat.setBackground(c);
1786 } else if (key == "background"_L1) {
1787 node->applyBackgroundImage(value, resourceProvider);
1788 }
1789 break;
1790 case Html_th:
1791 case Html_td:
1792 if (key == "width"_L1) {
1793 setWidthAttribute(&node->width, value);
1794 } else if (key == "bgcolor"_L1) {
1795 QColor c = QColor::fromString(value);
1796 if (!c.isValid())
1797 qWarning("QTextHtmlParser::applyAttributes: Unknown color name '%s'",value.toLatin1().constData());
1798 node->charFormat.setBackground(c);
1799 } else if (key == "background"_L1) {
1800 node->applyBackgroundImage(value, resourceProvider);
1801 } else if (key == "rowspan"_L1) {
1802 if (setIntAttribute(&node->tableCellRowSpan, value))
1803 node->tableCellRowSpan = qMax(1, node->tableCellRowSpan);
1804 } else if (key == "colspan"_L1) {
1805 if (setIntAttribute(&node->tableCellColSpan, value))
1806 node->tableCellColSpan = qBound(1, node->tableCellColSpan, 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(&node->tableBorder, value);
1813 } else if (key == "bgcolor"_L1) {
1814 QColor c = QColor::fromString(value);
1815 if (!c.isValid())
1816 qWarning("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(value);
1820 if (!c.isValid())
1821 qWarning("QTextHtmlParser::applyAttributes: Unknown color name '%s'",value.toLatin1().constData());
1822 node->borderBrush = c;
1823 } else if (key == "background"_L1) {
1824 node->applyBackgroundImage(value, resourceProvider);
1825 } else if (key == "cellspacing"_L1) {
1826 setFloatAttribute(&node->tableCellSpacing, value);
1827 } else if (key == "cellpadding"_L1) {
1828 setFloatAttribute(&node->tableCellPadding, value);
1829 } else if (key == "width"_L1) {
1830 setWidthAttribute(&node->width, value);
1831 } else if (key == "height"_L1) {
1832 setWidthAttribute(&node->height, 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(&node->width, 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("language-"_L1))
1854 node->blockFormat.setProperty(QTextFormat::BlockCodeLanguage, value.mid(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(linkHref);
1917#endif
1918}
1919
1920#ifndef QT_NO_CSSPARSER
1922{
1923public:
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
1936private:
1937 const QTextHtmlParser *parser;
1938};
1939
1941{
1942 return QStringList(parser->at(node.id).tag.toLower());
1943}
1944
1945#endif // QT_NO_CSSPARSER
1946
1947#ifndef QT_NO_CSSPARSER
1948
1949static inline int findAttribute(const QStringList &attributes, const QString &name)
1950{
1951 int idx = -1;
1952 do {
1953 idx = attributes.indexOf(name, idx + 1);
1954 } while (idx != -1 && (idx % 2 == 1));
1955 return idx;
1956}
1957
1958QString QTextHtmlStyleSelector::attributeValue(NodePtr node, const QCss::AttributeSelector &aSelector) const
1959{
1960 const QStringList &attributes = parser->at(node.id).attributes;
1961 const int idx = findAttribute(attributes, aSelector.name);
1962 if (idx == -1)
1963 return QString();
1964 return attributes.at(idx + 1);
1965}
1966
1967bool QTextHtmlStyleSelector::hasAttributes(NodePtr node) const
1968{
1969 const QStringList &attributes = parser->at(node.id).attributes;
1970 return !attributes.isEmpty();
1971}
1972
1973bool QTextHtmlStyleSelector::isNullNode(NodePtr node) const
1974{
1975 return node.id == 0;
1976}
1977
1979{
1980 NodePtr parent;
1981 parent.id = 0;
1982 if (node.id) {
1983 parent.id = parser->at(node.id).parent;
1984 }
1985 return parent;
1986}
1987
1989{
1990 return node;
1991}
1992
1994{
1995 NodePtr sibling;
1996 sibling.id = 0;
1997 if (!node.id)
1998 return sibling;
1999 int parent = parser->at(node.id).parent;
2000 if (!parent)
2001 return sibling;
2002 const int childIdx = parser->at(parent).children.indexOf(node.id);
2003 if (childIdx <= 0)
2004 return sibling;
2005 sibling.id = parser->at(parent).children.at(childIdx - 1);
2006 return sibling;
2007}
2008
2010{
2011}
2012
2013void 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("screen"_L1, Qt::CaseInsensitive))
2018 importStyleSheet(rule.href);
2019 }
2020}
2021
2022void 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(QTextDocument::StyleSheetResource, 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(res.toByteArray());
2037 }
2038 if (!css.isEmpty()) {
2039 QCss::Parser parser(css);
2040 QCss::StyleSheet sheet;
2041 parser.parse(&sheet, Qt::CaseInsensitive);
2042 externalStyleSheets.append(ExternalStyleSheet(href, sheet));
2043 resolveStyleSheetImports(sheet);
2044 }
2045}
2046
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("href"_L1, Qt::CaseInsensitive) == 0
2060 && !node.attributes.at(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(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
2224QList<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((resourceProvider ? 1 : 0)
2232 + externalStyleSheets.size()
2233 + inlineStyleSheets.size());
2234 if (resourceProvider)
2235 selector.styleSheets[idx++] = QTextDocumentPrivate::get(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(QTextDocument::CssMedia) : "screen"_L1;
2244
2245 QCss::StyleSelector::NodePtr n;
2246 n.id = node;
2247
2248 const char *extraPseudo = nullptr;
2249 if (nodes.at(node)->id == Html_a && nodes.at(node)->hasHref)
2250 extraPseudo = "link";
2251 // Ensure that our own style is taken into consideration
2252 decls = standardDeclarationForNode(*nodes.at(node));
2253 decls += selector.declarationsForNode(n, extraPseudo);
2254 n = selector.parentNode(n);
2255 while (!selector.isNullNode(n)) {
2256 QList<QCss::Declaration> inheritedDecls;
2257 inheritedDecls = selector.declarationsForNode(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(decl);
2262 }
2263 n = selector.parentNode(n);
2264 }
2265 return decls;
2266}
2267
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
2280QT_END_NAMESPACE
2281
2282#endif // QT_NO_TEXTHTMLPARSER
bool nodeIsChildOf(int i, QTextHTMLElements id) const
QTextHtmlParserNode * resolveParent()
void applyAttributes(const QStringList &attributes)
int margin(int i, int mar) const
int topMargin(int i) const
const QTextHtmlParserNode & at(int i) const
int depth(int i) const
QStringList parseAttributes()
int bottomMargin(int i) const
QTextHtmlParserNode * newNode(int parent)
NodePtr previousSiblingNode(NodePtr node) const override
bool isNullNode(NodePtr node) const override
QString attributeValue(NodePtr node, const QCss::AttributeSelector &aSelector) const override
bool hasAttributes(NodePtr node) const override
NodePtr duplicateNode(NodePtr node) const override
QTextHtmlStyleSelector(const QTextHtmlParser *parser)
void freeNode(NodePtr node) const override
NodePtr parentNode(NodePtr node) const override
QStringList nodeNames(NodePtr node) const override
static int findAttribute(const QStringList &attributes, const QString &name)
static bool operator<(QStringView entityStr, const QTextHtmlEntity &entity)
static bool operator<(const QTextHtmlEntity &entity, QStringView entityStr)
QList< QCss::Declaration > standardDeclarationForNode(const QTextHtmlParserNode &node)
static const QTextHtmlElement elements[Html_NumElements]
static bool operator<(QStringView str, const QTextHtmlElement &e)
static bool setFloatAttribute(qreal *destination, const QString &value)
static const QTextHtmlElement * lookupElementHelper(QStringView element)
static QTextFrameFormat::BorderStyle toQTextFrameFormat(QCss::BorderStyle cssStyle)
static void setWidthAttribute(QTextLength *width, const QString &valueStr)
static const ushort windowsLatin1ExtendedCharacters[0xA0 - 0x80]
static bool operator<(const QTextHtmlElement &e, QStringView str)
static bool setIntAttribute(int *destination, const QString &value)
#define MAX_ENTITY
static QChar resolveEntity(QStringView entity)
static QString quoteNewline(const QString &s)
QTextHTMLElements
@ Html_h2
@ Html_dl
@ Html_meta
@ Html_samp
@ Html_em
@ Html_code
@ Html_th
@ Html_dd
@ Html_tr
@ Html_NumElements
@ Html_tbody
@ Html_nobr
@ Html_tfoot
@ Html_b
@ Html_h4
@ Html_a
@ Html_caption
@ Html_h5
@ Html_big
@ Html_title
@ Html_table
@ Html_address
@ Html_div
@ Html_var
@ Html_i
@ Html_u
@ Html_tt
@ Html_font
@ Html_p
@ Html_ol
@ Html_blockquote
@ Html_head
@ Html_ul
@ Html_span
@ Html_br
@ Html_script
@ Html_thead
@ Html_kbd
@ Html_pre
@ Html_body
@ Html_cite
@ Html_link
@ Html_s
@ Html_unknown
@ Html_dfn
@ Html_sub
@ Html_td
@ Html_hr
@ Html_li
@ Html_img
@ Html_html
@ Html_h6
@ Html_h1
@ Html_small
@ Html_sup
@ Html_h3
@ Html_dt
@ Html_center
@ Html_strong
@ Html_style
const char name[11]
QTextHTMLElements id
const char name[9]
bool isNotSelfNesting() const
void initializeProperties(const QTextHtmlParserNode *parent, const QTextHtmlParser *parser)
QTextHTMLElements id
bool isNestedList(const QTextHtmlParser *parser) const
bool allowedInContext(int parentId) const
void applyForegroundImage(qint64 cacheKey, const QTextDocument *resourceProvider)
bool mayNotHaveChildren() const