23 static constexpr QLatin1Char tab(
'\t');
24 static constexpr QLatin1Char space(
' ');
25 static constexpr QLatin1Char amp(
'&');
26 static constexpr QLatin1Char endTag(
'>');
27 static constexpr QLatin1Char quot(
'"');
28 static constexpr QLatin1Char apos(
'\'');
29 static constexpr QLatin1Char semicolon(
';');
30 static constexpr QLatin1Char equals(
'=');
31 static constexpr QLatin1Char percent(
'%');
32 static constexpr auto startComment =
"<!--"_L1;
33 static constexpr auto endComment =
"-->"_L1;
34 static constexpr auto endElement =
"/>"_L1;
36 int state = previousBlockState();
37 int len = text.size();
46 QChar ch = text.at(pos);
48 if (text.mid(pos, 4) == startComment) {
53 while (pos < len && text.at(pos) != space
54 && text.at(pos) != endTag
55 && text.at(pos) != tab
56 && text.mid(pos, 2) != endElement)
58 if (text.mid(pos, 2) == endElement)
60 setFormat(start, pos - start,
65 }
else if (ch == amp && pos + 1 < len) {
67 if (text.at(pos + 1).isLetterOrNumber())
68 setFormat(pos + 1, 1, m_formats[Accelerator]);
73 while (pos + 1 < len && ch != semicolon && ch.isLetterOrNumber())
76 setFormat(start, pos - start + 1, m_formats[Entity]);
77 }
else if (ch == percent) {
80 for (++pos; pos < len && text.at(pos).isDigit(); ++pos) {}
82 if (pos < len && pos == start + 1 && text.at(pos) == u'n')
84 setFormat(start, pos - start, m_formats[Variable]);
94 if (text.mid(pos, 3) == endComment) {
102 setFormat(start, pos - start, m_formats[Comment]);
105 QChar quote = QChar::Null;
107 QChar ch = text.at(pos);
108 if (quote.isNull()) {
110 if (ch == apos || ch == quot) {
112 }
else if (ch == endTag) {
114 setFormat(start, pos - start, m_formats[Tag]);
117 }
else if (text.mid(pos, 2) == endElement) {
119 setFormat(start, pos - start, m_formats[Tag]);
122 }
else if (ch != space && text.at(pos) != tab) {
126 while (pos < len && text.at(pos) != space
127 && text.at(pos) != tab
128 && text.at(pos) != equals)
130 setFormat(start, pos - start, m_formats[Attribute]);
133 }
else if (ch == quote) {
137 setFormat(start, pos - start, m_formats[Value]);
144 setCurrentBlockState(state);
149 QTextCharFormat entityFormat;
150 QTextCharFormat tagFormat;
151 QTextCharFormat commentFormat;
152 QTextCharFormat attributeFormat;
153 QTextCharFormat valueFormat;
154 QTextCharFormat acceleratorFormat;
155 QTextCharFormat variableFormat;
158 entityFormat.setForeground(Qt::red);
159 tagFormat.setForeground(QColor(Qt::darkMagenta).lighter());
160 commentFormat.setForeground(Qt::gray);
161 attributeFormat.setForeground(QColor(Qt::darkGray).lighter());
162 valueFormat.setForeground(QColor(Qt::darkGreen).lighter());
163 variableFormat.setForeground(QColor(Qt::darkGreen).lighter());
165 entityFormat.setForeground(Qt::red);
166 tagFormat.setForeground(Qt::darkMagenta);
167 commentFormat.setForeground(Qt::gray);
168 attributeFormat.setForeground(Qt::black);
169 valueFormat.setForeground(Qt::darkGreen);
170 variableFormat.setForeground(Qt::darkGreen);
173 commentFormat.setFontItalic(
true);
174 attributeFormat.setFontItalic(
true);
175 acceleratorFormat.setFontUnderline(
true);
177 m_formats[Entity] = entityFormat;
178 m_formats[Tag] = tagFormat;
179 m_formats[Comment] = commentFormat;
180 m_formats[Attribute] = attributeFormat;
181 m_formats[Value] = valueFormat;
182 m_formats[Accelerator] = acceleratorFormat;
183 m_formats[Variable] = variableFormat;