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
htmlhighlighter.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant reason:default
4
5#include <QtCore/qtextstream.h>
6#include <QtWidgets/qtextedit.h>
7
9
11
12using namespace Qt::StringLiterals;
13
14namespace qdesigner_internal {
15
16HtmlHighlighter::HtmlHighlighter(QTextEdit *textEdit)
18{
19 QTextCharFormat entityFormat;
20 entityFormat.setForeground(Qt::red);
21 setFormatFor(Entity, entityFormat);
22
23 QTextCharFormat tagFormat;
24 tagFormat.setForeground(Qt::darkMagenta);
25 tagFormat.setFontWeight(QFont::Bold);
26 setFormatFor(Tag, tagFormat);
27
28 QTextCharFormat commentFormat;
29 commentFormat.setForeground(Qt::gray);
30 commentFormat.setFontItalic(true);
31 setFormatFor(Comment, commentFormat);
32
33 QTextCharFormat attributeFormat;
34 attributeFormat.setForeground(Qt::black);
35 attributeFormat.setFontWeight(QFont::Bold);
36 setFormatFor(Attribute, attributeFormat);
37
38 QTextCharFormat valueFormat;
39 valueFormat.setForeground(Qt::blue);
40 setFormatFor(Value, valueFormat);
41}
42
43void HtmlHighlighter::setFormatFor(Construct construct,
44 const QTextCharFormat &format)
45{
46 m_formats[construct] = format;
47 rehighlight();
48}
49
50void HtmlHighlighter::highlightBlock(const QString &text)
51{
52 static const QChar tab = u'\t';
53 static const QChar space = u' ';
54
55 int state = previousBlockState();
56 qsizetype len = text.size();
57 qsizetype start = 0;
58 qsizetype pos = 0;
59
60 while (pos < len) {
61 switch (state) {
62 case NormalState:
63 default:
64 while (pos < len) {
65 QChar ch = text.at(pos);
66 if (ch == u'<') {
67 if (QStringView{text}.sliced(pos).startsWith("<!--"_L1)) {
68 state = InComment;
69 } else {
70 state = InTag;
71 start = pos;
72 while (pos < len && text.at(pos) != space
73 && text.at(pos) != u'>'
74 && text.at(pos) != tab
75 && !QStringView{text}.sliced(pos).startsWith("/>"_L1)) {
76 ++pos;
77 }
78 if (QStringView{text}.sliced(pos).startsWith("/>"_L1))
79 ++pos;
80 setFormat(start, pos - start,
81 m_formats[Tag]);
82 break;
83 }
84 break;
85 }
86 if (ch == u'&') {
87 start = pos;
88 while (pos < len && text.at(pos++) != u';')
89 ;
90 setFormat(start, pos - start,
91 m_formats[Entity]);
92 } else {
93 // No tag, comment or entity started, continue...
94 ++pos;
95 }
96 }
97 break;
98 case InComment:
99 start = pos;
100 for ( ; pos < len; ++pos) {
101 if (QStringView{text}.sliced(pos).startsWith("-->"_L1)) {
102 pos += 3;
103 state = NormalState;
104 break;
105 }
106 }
107 setFormat(start, pos - start, m_formats[Comment]);
108 break;
109 case InTag:
110 QChar quote = QChar::Null;
111 while (pos < len) {
112 QChar ch = text.at(pos);
113 if (quote.isNull()) {
114 start = pos;
115 if (ch == '\''_L1 || ch == u'"') {
116 quote = ch;
117 } else if (ch == u'>') {
118 ++pos;
119 setFormat(start, pos - start, m_formats[Tag]);
120 state = NormalState;
121 break;
122 } else if (QStringView{text}.sliced(pos).startsWith("/>"_L1)) {
123 pos += 2;
124 setFormat(start, pos - start, m_formats[Tag]);
125 state = NormalState;
126 break;
127 } else if (ch != space && text.at(pos) != tab) {
128 // Tag not ending, not a quote and no whitespace, so
129 // we must be dealing with an attribute.
130 ++pos;
131 while (pos < len && text.at(pos) != space
132 && text.at(pos) != tab
133 && text.at(pos) != u'=')
134 ++pos;
135 setFormat(start, pos - start, m_formats[Attribute]);
136 start = pos;
137 }
138 } else if (ch == quote) {
139 quote = QChar::Null;
140
141 // Anything quoted is a value
142 setFormat(start, pos - start, m_formats[Value]);
143 }
144 ++pos;
145 }
146 break;
147 }
148 }
149 setCurrentBlockState(state);
150}
151
152} // namespace qdesigner_internal
153
154QT_END_NAMESPACE
void highlightBlock(const QString &text) override
Highlights the given text block.
void setFormatFor(Construct construct, const QTextCharFormat &format)
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.