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