23 #include "htmlhighlighter.h"
25 namespace KPIMTextEdit {
27 class HtmlHighlighterPrivate
52 HtmlHighlighterPrivate()
54 colors[DocType] = QColor( 192, 192, 192 );
55 colors[Entity] = QColor( 128, 128, 128 );
56 colors[Tag] = QColor( 136, 18, 128 );
57 colors[Comment] = QColor( 35, 110, 37 );
58 colors[AttributeName] = QColor( 153, 69, 0 );
59 colors[AttributeValue] = QColor( 36, 36, 170 );
61 QHash<int, QColor> colors;
64 HtmlHighlighter::HtmlHighlighter( QTextDocument *document )
65 : QSyntaxHighlighter( document ), d( new HtmlHighlighterPrivate )
69 HtmlHighlighter::~HtmlHighlighter()
74 void HtmlHighlighter::highlightBlock(
const QString &text )
76 int state = previousBlockState();
77 int len = text.length();
83 case HtmlHighlighterPrivate::State_Text:
86 QChar ch = text.at( pos );
87 if ( ch == QLatin1Char(
'<' ) ) {
88 if ( text.mid( pos, 4 ) == QLatin1String(
"<!--" ) ) {
89 state = HtmlHighlighterPrivate::State_Comment;
91 if ( text.mid( pos, 9 ).toUpper() == QLatin1String(
"<!DOCTYPE" ) ) {
92 state = HtmlHighlighterPrivate::State_DocType;
94 state = HtmlHighlighterPrivate::State_TagStart;
98 }
else if ( ch == QLatin1Char(
'&' ) ) {
100 while ( pos < len && text.at( pos++ ) != QLatin1Char(
';' ) )
102 setFormat( start, pos - start, d->colors[HtmlHighlighterPrivate::Entity] );
109 case HtmlHighlighterPrivate::State_Comment:
111 while ( pos < len ) {
112 if ( text.mid( pos, 3 ) == QLatin1String(
"-->" ) ) {
114 state = HtmlHighlighterPrivate::State_Text;
120 setFormat( start, pos - start, d->colors[HtmlHighlighterPrivate::Comment] );
123 case HtmlHighlighterPrivate::State_DocType:
125 while ( pos < len ) {
126 QChar ch = text.at( pos );
128 if ( ch == QLatin1Char(
'>' ) ) {
129 state = HtmlHighlighterPrivate::State_Text;
133 setFormat( start, pos - start, d->colors[HtmlHighlighterPrivate::DocType] );
137 case HtmlHighlighterPrivate::State_TagStart:
139 while ( pos < len ) {
140 QChar ch = text.at( pos );
142 if ( ch == QLatin1Char(
'>' ) ) {
143 state = HtmlHighlighterPrivate::State_Text;
146 if ( !ch.isSpace() ) {
148 state = HtmlHighlighterPrivate::State_TagName;
155 case HtmlHighlighterPrivate::State_TagName:
157 while ( pos < len ) {
158 QChar ch = text.at( pos );
160 if ( ch.isSpace() ) {
162 state = HtmlHighlighterPrivate::State_InsideTag;
165 if ( ch == QLatin1Char(
'>' ) ) {
166 state = HtmlHighlighterPrivate::State_Text;
170 setFormat( start, pos - start, d->colors[HtmlHighlighterPrivate::Tag] );
174 case HtmlHighlighterPrivate::State_InsideTag:
177 while ( pos < len ) {
178 QChar ch = text.at( pos );
181 if ( ch == QLatin1Char(
'/' ) ) {
185 if ( ch == QLatin1Char(
'>' ) ) {
186 state = HtmlHighlighterPrivate::State_Text;
190 if ( !ch.isSpace() ) {
192 state = HtmlHighlighterPrivate::State_AttributeName;
201 case HtmlHighlighterPrivate::State_AttributeName:
204 while ( pos < len ) {
205 QChar ch = text.at( pos );
208 if ( ch == QLatin1Char(
'=' ) ) {
209 state = HtmlHighlighterPrivate::State_AttributeValue;
213 if ( ch == QLatin1Char(
'>' ) || ch == QLatin1Char(
'/' ) ) {
214 state = HtmlHighlighterPrivate::State_InsideTag;
219 setFormat( start, pos - start, d->colors[HtmlHighlighterPrivate::AttributeName] );
223 case HtmlHighlighterPrivate::State_AttributeValue:
227 while ( pos < len ) {
228 QChar ch = text.at( pos );
232 if ( ch == QLatin1Char(
'\'' ) ) {
233 state = HtmlHighlighterPrivate::State_SingleQuote;
238 if ( ch == QLatin1Char(
'"' ) ) {
239 state = HtmlHighlighterPrivate::State_DoubleQuote;
243 if ( !ch.isSpace() ) {
248 if ( state == HtmlHighlighterPrivate::State_AttributeValue ) {
252 while ( pos < len ) {
253 QChar ch = text.at( pos );
254 if ( ch.isSpace() ) {
257 if ( ch == QLatin1Char(
'>' ) || ch == QLatin1Char(
'/' ) ) {
262 state = HtmlHighlighterPrivate::State_InsideTag;
263 setFormat( start, pos - start, d->colors[HtmlHighlighterPrivate::AttributeValue] );
269 case HtmlHighlighterPrivate::State_SingleQuote:
272 while ( pos < len ) {
273 QChar ch = text.at(pos);
275 if ( ch == QLatin1Char(
'\'' ) ) {
280 state = HtmlHighlighterPrivate::State_InsideTag;
282 setFormat( start, pos - start, d->colors[HtmlHighlighterPrivate::AttributeValue] );
286 case HtmlHighlighterPrivate::State_DoubleQuote:
289 while ( pos < len ) {
290 QChar ch = text.at(pos);
292 if ( ch == QLatin1Char(
'"' ) ) {
297 state = HtmlHighlighterPrivate::State_InsideTag;
299 setFormat( start, pos - start, d->colors[HtmlHighlighterPrivate::AttributeValue] );
305 setCurrentBlockState( state );