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
csshighlighter.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
"csshighlighter_p.h"
6
7
QT_BEGIN_NAMESPACE
8
9
using
namespace
Qt::StringLiterals;
10
11
namespace
qdesigner_internal
{
12
13
CssHighlighter
::
CssHighlighter
(
const
CssHighlightColors
&
colors
,
14
QTextDocument
*
document
)
15
:
QSyntaxHighlighter
(
document
),
m_colors
(
colors
)
16
{
17
}
18
19
void
CssHighlighter
::
highlightBlock
(
const
QString
&
text
)
20
{
21
enum
Token
{
ALNUM
,
LBRACE
,
RBRACE
,
COLON
,
SEMICOLON
,
COMMA
,
QUOTE
,
SLASH
,
STAR
};
22
static
const
int
transitions
[10][9] = {
23
{
Selector
,
Property
,
Selector
,
Pseudo
,
Property
,
Selector
,
Quote
,
MaybeComment
,
Selector
},
// Selector
24
{
Property
,
Property
,
Selector
,
Value
,
Property
,
Property
,
Quote
,
MaybeComment
,
Property
},
// Property
25
{
Value
,
Property
,
Selector
,
Value
,
Property
,
Value
,
Quote
,
MaybeComment
,
Value
},
// Value
26
{
Pseudo1
,
Property
,
Selector
,
Pseudo2
,
Selector
,
Selector
,
Quote
,
MaybeComment
,
Pseudo
},
// Pseudo
27
{
Pseudo1
,
Property
,
Selector
,
Pseudo
,
Selector
,
Selector
,
Quote
,
MaybeComment
,
Pseudo1
},
// Pseudo1
28
{
Pseudo2
,
Property
,
Selector
,
Pseudo
,
Selector
,
Selector
,
Quote
,
MaybeComment
,
Pseudo2
},
// Pseudo2
29
{
Quote
,
Quote
,
Quote
,
Quote
,
Quote
,
Quote
, -1,
Quote
,
Quote
},
// Quote
30
{ -1, -1, -1, -1, -1, -1, -1, -1,
Comment
},
// MaybeComment
31
{
Comment
,
Comment
,
Comment
,
Comment
,
Comment
,
Comment
,
Comment
,
Comment
,
MaybeCommentEnd
},
// Comment
32
{
Comment
,
Comment
,
Comment
,
Comment
,
Comment
,
Comment
,
Comment
, -1,
MaybeCommentEnd
}
// MaybeCommentEnd
33
};
34
35
qsizetype
lastIndex
= 0;
36
bool
lastWasSlash
=
false
;
37
int
state
=
previousBlockState
(),
save_state
;
38
if
(
state
== -1) {
39
// As long as the text is empty, leave the state undetermined
40
if
(
text
.
isEmpty
()) {
41
setCurrentBlockState
(-1);
42
return
;
43
}
44
// The initial state is based on the precense of a : and the absense of a {.
45
// This is because Qt style sheets support both a full stylesheet as well as
46
// an inline form with just properties.
47
state
=
save_state
= (
text
.
indexOf
(u':') > -1 &&
48
text
.
indexOf
(u'{') == -1) ?
Property
:
Selector
;
49
}
else
{
50
save_state
=
state
>>16;
51
state
&= 0x00ff;
52
}
53
54
if
(
state
==
MaybeCommentEnd
) {
55
state
=
Comment
;
56
}
else
if
(
state
==
MaybeComment
) {
57
state
=
save_state
;
58
}
59
60
for
(
qsizetype
i
= 0;
i
<
text
.
size
(); ++
i
) {
61
int
token
=
ALNUM
;
62
const
QChar
c
=
text
.
at
(
i
);
63
const
char
a
=
c
.
toLatin1
();
64
65
if
(
state
==
Quote
) {
66
if
(
a
==
'\\'
) {
67
lastWasSlash
=
true
;
68
}
else
{
69
if
(
a
==
'\"'
&& !
lastWasSlash
) {
70
token
=
QUOTE
;
71
}
72
lastWasSlash
=
false
;
73
}
74
}
else
{
75
switch
(
a
) {
76
case
'{'
:
token
=
LBRACE
;
break
;
77
case
'}'
:
token
=
RBRACE
;
break
;
78
case
':'
:
token
=
COLON
;
break
;
79
case
';'
:
token
=
SEMICOLON
;
break
;
80
case
','
:
token
=
COMMA
;
break
;
81
case
'\"'
:
token
=
QUOTE
;
break
;
82
case
'/'
:
token
=
SLASH
;
break
;
83
case
'*'
:
token
=
STAR
;
break
;
84
default
:
break
;
85
}
86
}
87
88
int
new_state
=
transitions
[
state
][
token
];
89
90
if
(
new_state
!=
state
) {
91
bool
include_token
=
new_state
==
MaybeCommentEnd
|| (
state
==
MaybeCommentEnd
&&
new_state
!=
Comment
)
92
||
state
==
Quote
;
93
highlight
(
text
,
lastIndex
,
i
-
lastIndex
+
include_token
,
state
);
94
95
if
(
new_state
==
Comment
) {
96
lastIndex
=
i
-1;
// include the slash and star
97
}
else
{
98
lastIndex
=
i
+ ((
token
==
ALNUM
||
new_state
==
Quote
) ? 0 : 1);
99
}
100
}
101
102
if
(
new_state
== -1) {
103
state
=
save_state
;
104
}
else
if
(
state
<=
Pseudo2
) {
105
save_state
=
state
;
106
state
=
new_state
;
107
}
else
{
108
state
=
new_state
;
109
}
110
}
111
112
highlight
(
text
,
lastIndex
,
text
.
size
() -
lastIndex
,
state
);
113
setCurrentBlockState
(
state
+ (
save_state
<<16));
114
}
115
116
void
CssHighlighter
::
highlight
(
const
QString
&
text
,
int
start
,
int
length
,
int
state
)
117
{
118
if
(
start
>=
text
.
size
() ||
length
<= 0)
119
return
;
120
121
QTextCharFormat
format
;
122
123
switch
(
state
) {
124
case
Selector
:
125
setFormat
(
start
,
length
,
m_colors
.
selector
);
126
break
;
127
case
Property
:
128
setFormat
(
start
,
length
,
m_colors
.
property
);
129
break
;
130
case
Value
:
131
setFormat
(
start
,
length
,
m_colors
.
value
);
132
break
;
133
case
Pseudo1
:
134
setFormat
(
start
,
length
,
m_colors
.
pseudo1
);
135
break
;
136
case
Pseudo2
:
137
setFormat
(
start
,
length
,
m_colors
.
pseudo2
);
138
break
;
139
case
Quote
:
140
setFormat
(
start
,
length
,
m_colors
.
quote
);
141
break
;
142
case
Comment
:
143
case
MaybeCommentEnd
:
144
format
.
setForeground
(
m_colors
.
comment
);
145
setFormat
(
start
,
length
,
format
);
146
break
;
147
default
:
148
break
;
149
}
150
}
151
152
}
// namespace qdesigner_internal
153
154
QT_END_NAMESPACE
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qdesigner_internal
Auxiliary methods to store/retrieve settings.
Definition
buddyeditor.cpp:67
qttools
src
designer
src
lib
shared
csshighlighter.cpp
Generated on
for Qt by
1.16.1