1 // Scintilla source code edit control
2 // Encoding: UTF-8
3 /** @file LexCSS.cxx
4  ** Lexer for Cascading Style Sheets
5  ** Written by Jakub Vrána
6  ** Improved by Philippe Lhoste (CSS2)
7  ** Improved by Ross McKay (SCSS mode; see http://sass-lang.com/ )
8  **/
9 // Copyright 1998-2002 by Neil Hodgson <neilh@scintilla.org>
10 // The License.txt file describes the conditions under which this software may be distributed.
11 
12 // TODO: handle SCSS nested properties like font: { weight: bold; size: 1em; }
13 // TODO: handle SCSS interpolation: #{}
14 // TODO: add features for Less if somebody feels like contributing; http://lesscss.org/
15 // TODO: refactor this monster so that the next poor slob can read it!
16 
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stdio.h>
20 #include <stdarg.h>
21 #include <assert.h>
22 #include <ctype.h>
23 
24 #include "ILexer.h"
25 #include "Scintilla.h"
26 #include "SciLexer.h"
27 
28 #include "WordList.h"
29 #include "LexAccessor.h"
30 #include "Accessor.h"
31 #include "StyleContext.h"
32 #include "CharacterSet.h"
33 #include "LexerModule.h"
34 
35 using namespace Scintilla;
36 
37 
IsAWordChar(const unsigned int ch)38 static inline bool IsAWordChar(const unsigned int ch) {
39 	/* FIXME:
40 	 * The CSS spec allows "ISO 10646 characters U+00A1 and higher" to be treated as word chars.
41 	 * Unfortunately, we are only getting string bytes here, and not full unicode characters. We cannot guarantee
42 	 * that our byte is between U+0080 - U+00A0 (to return false), so we have to allow all characters U+0080 and higher
43 	 */
44 	return ch >= 0x80 || isalnum(ch) || ch == '-' || ch == '_';
45 }
46 
IsCssOperator(const int ch)47 inline bool IsCssOperator(const int ch) {
48 	if (!((ch < 0x80) && isalnum(ch)) &&
49 		(ch == '{' || ch == '}' || ch == ':' || ch == ',' || ch == ';' ||
50 		 ch == '.' || ch == '#' || ch == '!' || ch == '@' ||
51 		 /* CSS2 */
52 		 ch == '*' || ch == '>' || ch == '+' || ch == '=' || ch == '~' || ch == '|' ||
53 		 ch == '[' || ch == ']' || ch == '(' || ch == ')')) {
54 		return true;
55 	}
56 	return false;
57 }
58 
59 // look behind (from start of document to our start position) to determine current nesting level
NestingLevelLookBehind(Sci_PositionU startPos,Accessor & styler)60 inline int NestingLevelLookBehind(Sci_PositionU startPos, Accessor &styler) {
61 	int ch;
62 	int nestingLevel = 0;
63 
64 	for (Sci_PositionU i = 0; i < startPos; i++) {
65 		ch = styler.SafeGetCharAt(i);
66 		if (ch == '{')
67 			nestingLevel++;
68 		else if (ch == '}')
69 			nestingLevel--;
70 	}
71 
72 	return nestingLevel;
73 }
74 
ColouriseCssDoc(Sci_PositionU startPos,Sci_Position length,int initStyle,WordList * keywordlists[],Accessor & styler)75 static void ColouriseCssDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[], Accessor &styler) {
76 	WordList &css1Props = *keywordlists[0];
77 	WordList &pseudoClasses = *keywordlists[1];
78 	WordList &css2Props = *keywordlists[2];
79 	WordList &css3Props = *keywordlists[3];
80 	WordList &pseudoElements = *keywordlists[4];
81 	WordList &exProps = *keywordlists[5];
82 	WordList &exPseudoClasses = *keywordlists[6];
83 	WordList &exPseudoElements = *keywordlists[7];
84 
85 	StyleContext sc(startPos, length, initStyle, styler);
86 
87 	int lastState = -1; // before operator
88 	int lastStateC = -1; // before comment
89 	int lastStateS = -1; // before single-quoted/double-quoted string
90 	int lastStateVar = -1; // before variable (SCSS)
91 	int lastStateVal = -1; // before value (SCSS)
92 	int op = ' '; // last operator
93 	int opPrev = ' '; // last operator
94 	bool insideParentheses = false; // true if currently in a CSS url() or similar construct
95 
96 	// property lexer.css.scss.language
97 	//	Set to 1 for Sassy CSS (.scss)
98 	bool isScssDocument = styler.GetPropertyInt("lexer.css.scss.language") != 0;
99 
100 	// property lexer.css.less.language
101 	// Set to 1 for Less CSS (.less)
102 	bool isLessDocument = styler.GetPropertyInt("lexer.css.less.language") != 0;
103 
104 	// property lexer.css.hss.language
105 	// Set to 1 for HSS (.hss)
106 	bool isHssDocument = styler.GetPropertyInt("lexer.css.hss.language") != 0;
107 
108 	// SCSS/LESS/HSS have the concept of variable
109 	bool hasVariables = isScssDocument || isLessDocument || isHssDocument;
110 	char varPrefix = 0;
111 	if (hasVariables)
112 		varPrefix = isLessDocument ? '@' : '$';
113 
114 	// SCSS/LESS/HSS support single-line comments
115 	typedef enum _CommentModes { eCommentBlock = 0, eCommentLine = 1} CommentMode;
116 	CommentMode comment_mode = eCommentBlock;
117 	bool hasSingleLineComments = isScssDocument || isLessDocument || isHssDocument;
118 
119 	// must keep track of nesting level in document types that support it (SCSS/LESS/HSS)
120 	bool hasNesting = false;
121 	int nestingLevel = 0;
122 	if (isScssDocument || isLessDocument || isHssDocument) {
123 		hasNesting = true;
124 		nestingLevel = NestingLevelLookBehind(startPos, styler);
125 	}
126 
127 	// "the loop"
128 	for (; sc.More(); sc.Forward()) {
129 		if (sc.state == SCE_CSS_COMMENT && ((comment_mode == eCommentBlock && sc.Match('*', '/')) || (comment_mode == eCommentLine && sc.atLineEnd))) {
130 			if (lastStateC == -1) {
131 				// backtrack to get last state:
132 				// comments are like whitespace, so we must return to the previous state
133 				Sci_PositionU i = startPos;
134 				for (; i > 0; i--) {
135 					if ((lastStateC = styler.StyleAt(i-1)) != SCE_CSS_COMMENT) {
136 						if (lastStateC == SCE_CSS_OPERATOR) {
137 							op = styler.SafeGetCharAt(i-1);
138 							opPrev = styler.SafeGetCharAt(i-2);
139 							while (--i) {
140 								lastState = styler.StyleAt(i-1);
141 								if (lastState != SCE_CSS_OPERATOR && lastState != SCE_CSS_COMMENT)
142 									break;
143 							}
144 							if (i == 0)
145 								lastState = SCE_CSS_DEFAULT;
146 						}
147 						break;
148 					}
149 				}
150 				if (i == 0)
151 					lastStateC = SCE_CSS_DEFAULT;
152 			}
153 			if (comment_mode == eCommentBlock) {
154 				sc.Forward();
155 				sc.ForwardSetState(lastStateC);
156 			} else /* eCommentLine */ {
157 				sc.SetState(lastStateC);
158 			}
159 		}
160 
161 		if (sc.state == SCE_CSS_COMMENT)
162 			continue;
163 
164 		if (sc.state == SCE_CSS_DOUBLESTRING || sc.state == SCE_CSS_SINGLESTRING) {
165 			if (sc.ch != (sc.state == SCE_CSS_DOUBLESTRING ? '\"' : '\''))
166 				continue;
167 			Sci_PositionU i = sc.currentPos;
168 			while (i && styler[i-1] == '\\')
169 				i--;
170 			if ((sc.currentPos - i) % 2 == 1)
171 				continue;
172 			sc.ForwardSetState(lastStateS);
173 		}
174 
175 		if (sc.state == SCE_CSS_OPERATOR) {
176 			if (op == ' ') {
177 				Sci_PositionU i = startPos;
178 				op = styler.SafeGetCharAt(i-1);
179 				opPrev = styler.SafeGetCharAt(i-2);
180 				while (--i) {
181 					lastState = styler.StyleAt(i-1);
182 					if (lastState != SCE_CSS_OPERATOR && lastState != SCE_CSS_COMMENT)
183 						break;
184 				}
185 			}
186 			switch (op) {
187 			case '@':
188 				if (lastState == SCE_CSS_DEFAULT || hasNesting)
189 					sc.SetState(SCE_CSS_DIRECTIVE);
190 				break;
191 			case '>':
192 			case '+':
193 				if (lastState == SCE_CSS_TAG || lastState == SCE_CSS_CLASS || lastState == SCE_CSS_ID ||
194 					lastState == SCE_CSS_PSEUDOCLASS || lastState == SCE_CSS_EXTENDED_PSEUDOCLASS || lastState == SCE_CSS_UNKNOWN_PSEUDOCLASS)
195 					sc.SetState(SCE_CSS_DEFAULT);
196 				break;
197 			case '[':
198 				if (lastState == SCE_CSS_TAG || lastState == SCE_CSS_DEFAULT || lastState == SCE_CSS_CLASS || lastState == SCE_CSS_ID ||
199 					lastState == SCE_CSS_PSEUDOCLASS || lastState == SCE_CSS_EXTENDED_PSEUDOCLASS || lastState == SCE_CSS_UNKNOWN_PSEUDOCLASS)
200 					sc.SetState(SCE_CSS_ATTRIBUTE);
201 				break;
202 			case ']':
203 				if (lastState == SCE_CSS_ATTRIBUTE)
204 					sc.SetState(SCE_CSS_TAG);
205 				break;
206 			case '{':
207 				nestingLevel++;
208 				switch (lastState) {
209 				case SCE_CSS_MEDIA:
210 					sc.SetState(SCE_CSS_DEFAULT);
211 					break;
212 				case SCE_CSS_TAG:
213 				case SCE_CSS_DIRECTIVE:
214 					sc.SetState(SCE_CSS_IDENTIFIER);
215 					break;
216 				}
217 				break;
218 			case '}':
219 				if (--nestingLevel < 0)
220 					nestingLevel = 0;
221 				switch (lastState) {
222 				case SCE_CSS_DEFAULT:
223 				case SCE_CSS_VALUE:
224 				case SCE_CSS_IMPORTANT:
225 				case SCE_CSS_IDENTIFIER:
226 				case SCE_CSS_IDENTIFIER2:
227 				case SCE_CSS_IDENTIFIER3:
228 					if (hasNesting)
229 						sc.SetState(nestingLevel > 0 ? SCE_CSS_IDENTIFIER : SCE_CSS_DEFAULT);
230 					else
231 						sc.SetState(SCE_CSS_DEFAULT);
232 					break;
233 				}
234 				break;
235 			case '(':
236 				if (lastState == SCE_CSS_PSEUDOCLASS)
237 					sc.SetState(SCE_CSS_TAG);
238 				else if (lastState == SCE_CSS_EXTENDED_PSEUDOCLASS)
239 					sc.SetState(SCE_CSS_EXTENDED_PSEUDOCLASS);
240 				break;
241 			case ')':
242 				if (lastState == SCE_CSS_TAG || lastState == SCE_CSS_DEFAULT || lastState == SCE_CSS_CLASS || lastState == SCE_CSS_ID ||
243 					lastState == SCE_CSS_PSEUDOCLASS || lastState == SCE_CSS_EXTENDED_PSEUDOCLASS || lastState == SCE_CSS_UNKNOWN_PSEUDOCLASS ||
244 					lastState == SCE_CSS_PSEUDOELEMENT || lastState == SCE_CSS_EXTENDED_PSEUDOELEMENT)
245 					sc.SetState(SCE_CSS_TAG);
246 				break;
247 			case ':':
248 				switch (lastState) {
249 				case SCE_CSS_TAG:
250 				case SCE_CSS_DEFAULT:
251 				case SCE_CSS_CLASS:
252 				case SCE_CSS_ID:
253 				case SCE_CSS_PSEUDOCLASS:
254 				case SCE_CSS_EXTENDED_PSEUDOCLASS:
255 				case SCE_CSS_UNKNOWN_PSEUDOCLASS:
256 				case SCE_CSS_PSEUDOELEMENT:
257 				case SCE_CSS_EXTENDED_PSEUDOELEMENT:
258 					sc.SetState(SCE_CSS_PSEUDOCLASS);
259 					break;
260 				case SCE_CSS_IDENTIFIER:
261 				case SCE_CSS_IDENTIFIER2:
262 				case SCE_CSS_IDENTIFIER3:
263 				case SCE_CSS_EXTENDED_IDENTIFIER:
264 				case SCE_CSS_UNKNOWN_IDENTIFIER:
265 				case SCE_CSS_VARIABLE:
266 					sc.SetState(SCE_CSS_VALUE);
267 					lastStateVal = lastState;
268 					break;
269 				}
270 				break;
271 			case '.':
272 				if (lastState == SCE_CSS_TAG || lastState == SCE_CSS_DEFAULT || lastState == SCE_CSS_CLASS || lastState == SCE_CSS_ID ||
273 					lastState == SCE_CSS_PSEUDOCLASS || lastState == SCE_CSS_EXTENDED_PSEUDOCLASS || lastState == SCE_CSS_UNKNOWN_PSEUDOCLASS)
274 					sc.SetState(SCE_CSS_CLASS);
275 				break;
276 			case '#':
277 				if (lastState == SCE_CSS_TAG || lastState == SCE_CSS_DEFAULT || lastState == SCE_CSS_CLASS || lastState == SCE_CSS_ID ||
278 					lastState == SCE_CSS_PSEUDOCLASS || lastState == SCE_CSS_EXTENDED_PSEUDOCLASS || lastState == SCE_CSS_UNKNOWN_PSEUDOCLASS)
279 					sc.SetState(SCE_CSS_ID);
280 				break;
281 			case ',':
282 			case '|':
283 			case '~':
284 				if (lastState == SCE_CSS_TAG)
285 					sc.SetState(SCE_CSS_DEFAULT);
286 				break;
287 			case ';':
288 				switch (lastState) {
289 				case SCE_CSS_DIRECTIVE:
290 					if (hasNesting) {
291 						sc.SetState(nestingLevel > 0 ? SCE_CSS_IDENTIFIER : SCE_CSS_DEFAULT);
292 					} else {
293 						sc.SetState(SCE_CSS_DEFAULT);
294 					}
295 					break;
296 				case SCE_CSS_VALUE:
297 				case SCE_CSS_IMPORTANT:
298 					// data URLs can have semicolons; simplistically check for wrapping parentheses and move along
299 					if (insideParentheses) {
300 						sc.SetState(lastState);
301 					} else {
302 						if (lastStateVal == SCE_CSS_VARIABLE) {
303 							sc.SetState(SCE_CSS_DEFAULT);
304 						} else {
305 							sc.SetState(SCE_CSS_IDENTIFIER);
306 						}
307 					}
308 					break;
309 				case SCE_CSS_VARIABLE:
310 					if (lastStateVar == SCE_CSS_VALUE) {
311 						// data URLs can have semicolons; simplistically check for wrapping parentheses and move along
312 						if (insideParentheses) {
313 							sc.SetState(SCE_CSS_VALUE);
314 						} else {
315 							sc.SetState(SCE_CSS_IDENTIFIER);
316 						}
317 					} else {
318 						sc.SetState(SCE_CSS_DEFAULT);
319 					}
320 					break;
321 				}
322 				break;
323 			case '!':
324 				if (lastState == SCE_CSS_VALUE)
325 					sc.SetState(SCE_CSS_IMPORTANT);
326 				break;
327 			}
328 		}
329 
330 		if (sc.ch == '*' && sc.state == SCE_CSS_DEFAULT) {
331 			sc.SetState(SCE_CSS_TAG);
332 			continue;
333 		}
334 
335 		// check for inside parentheses (whether part of an "operator" or not)
336 		if (sc.ch == '(')
337 			insideParentheses = true;
338 		else if (sc.ch == ')')
339 			insideParentheses = false;
340 
341 		// SCSS special modes
342 		if (hasVariables) {
343 			// variable name
344 			if (sc.ch == varPrefix) {
345 				switch (sc.state) {
346 				case SCE_CSS_DEFAULT:
347 					if (isLessDocument) // give priority to pseudo elements
348 						break;
349 					// Falls through.
350 				case SCE_CSS_VALUE:
351 					lastStateVar = sc.state;
352 					sc.SetState(SCE_CSS_VARIABLE);
353 					continue;
354 				}
355 			}
356 			if (sc.state == SCE_CSS_VARIABLE) {
357 				if (IsAWordChar(sc.ch)) {
358 					// still looking at the variable name
359 					continue;
360 				}
361 				if (lastStateVar == SCE_CSS_VALUE) {
362 					// not looking at the variable name any more, and it was part of a value
363 					sc.SetState(SCE_CSS_VALUE);
364 				}
365 			}
366 
367 			// nested rule parent selector
368 			if (sc.ch == '&') {
369 				switch (sc.state) {
370 				case SCE_CSS_DEFAULT:
371 				case SCE_CSS_IDENTIFIER:
372 					sc.SetState(SCE_CSS_TAG);
373 					continue;
374 				}
375 			}
376 		}
377 
378 		// nesting rules that apply to SCSS and Less
379 		if (hasNesting) {
380 			// check for nested rule selector
381 			if (sc.state == SCE_CSS_IDENTIFIER && (IsAWordChar(sc.ch) || sc.ch == ':' || sc.ch == '.' || sc.ch == '#')) {
382 				// look ahead to see whether { comes before next ; and }
383 				Sci_PositionU endPos = startPos + length;
384 				int ch;
385 
386 				for (Sci_PositionU i = sc.currentPos; i < endPos; i++) {
387 					ch = styler.SafeGetCharAt(i);
388 					if (ch == ';' || ch == '}')
389 						break;
390 					if (ch == '{') {
391 						sc.SetState(SCE_CSS_DEFAULT);
392 						continue;
393 					}
394 				}
395 			}
396 
397 		}
398 
399 		if (IsAWordChar(sc.ch)) {
400 			if (sc.state == SCE_CSS_DEFAULT)
401 				sc.SetState(SCE_CSS_TAG);
402 			continue;
403 		}
404 
405 		if (IsAWordChar(sc.chPrev) && (
406 			sc.state == SCE_CSS_IDENTIFIER || sc.state == SCE_CSS_IDENTIFIER2 ||
407 			sc.state == SCE_CSS_IDENTIFIER3 || sc.state == SCE_CSS_EXTENDED_IDENTIFIER ||
408 			sc.state == SCE_CSS_UNKNOWN_IDENTIFIER ||
409 			sc.state == SCE_CSS_PSEUDOCLASS || sc.state == SCE_CSS_PSEUDOELEMENT ||
410 			sc.state == SCE_CSS_EXTENDED_PSEUDOCLASS || sc.state == SCE_CSS_EXTENDED_PSEUDOELEMENT ||
411 			sc.state == SCE_CSS_UNKNOWN_PSEUDOCLASS ||
412 			sc.state == SCE_CSS_IMPORTANT ||
413 			sc.state == SCE_CSS_DIRECTIVE
414 		)) {
415 			char s[100];
416 			sc.GetCurrentLowered(s, sizeof(s));
417 			char *s2 = s;
418 			while (*s2 && !IsAWordChar(*s2))
419 				s2++;
420 			switch (sc.state) {
421 			case SCE_CSS_IDENTIFIER:
422 			case SCE_CSS_IDENTIFIER2:
423 			case SCE_CSS_IDENTIFIER3:
424 			case SCE_CSS_EXTENDED_IDENTIFIER:
425 			case SCE_CSS_UNKNOWN_IDENTIFIER:
426 				if (css1Props.InList(s2))
427 					sc.ChangeState(SCE_CSS_IDENTIFIER);
428 				else if (css2Props.InList(s2))
429 					sc.ChangeState(SCE_CSS_IDENTIFIER2);
430 				else if (css3Props.InList(s2))
431 					sc.ChangeState(SCE_CSS_IDENTIFIER3);
432 				else if (exProps.InList(s2))
433 					sc.ChangeState(SCE_CSS_EXTENDED_IDENTIFIER);
434 				else
435 					sc.ChangeState(SCE_CSS_UNKNOWN_IDENTIFIER);
436 				break;
437 			case SCE_CSS_PSEUDOCLASS:
438 			case SCE_CSS_PSEUDOELEMENT:
439 			case SCE_CSS_EXTENDED_PSEUDOCLASS:
440 			case SCE_CSS_EXTENDED_PSEUDOELEMENT:
441 			case SCE_CSS_UNKNOWN_PSEUDOCLASS:
442 				if (op == ':' && opPrev != ':' && pseudoClasses.InList(s2))
443 					sc.ChangeState(SCE_CSS_PSEUDOCLASS);
444 				else if (opPrev == ':' && pseudoElements.InList(s2))
445 					sc.ChangeState(SCE_CSS_PSEUDOELEMENT);
446 				else if ((op == ':' || (op == '(' && lastState == SCE_CSS_EXTENDED_PSEUDOCLASS)) && opPrev != ':' && exPseudoClasses.InList(s2))
447 					sc.ChangeState(SCE_CSS_EXTENDED_PSEUDOCLASS);
448 				else if (opPrev == ':' && exPseudoElements.InList(s2))
449 					sc.ChangeState(SCE_CSS_EXTENDED_PSEUDOELEMENT);
450 				else
451 					sc.ChangeState(SCE_CSS_UNKNOWN_PSEUDOCLASS);
452 				break;
453 			case SCE_CSS_IMPORTANT:
454 				if (strcmp(s2, "important") != 0)
455 					sc.ChangeState(SCE_CSS_VALUE);
456 				break;
457 			case SCE_CSS_DIRECTIVE:
458 				if (op == '@' && strcmp(s2, "media") == 0)
459 					sc.ChangeState(SCE_CSS_MEDIA);
460 				break;
461 			}
462 		}
463 
464 		if (sc.ch != '.' && sc.ch != ':' && sc.ch != '#' && (
465 			sc.state == SCE_CSS_CLASS || sc.state == SCE_CSS_ID ||
466 			(sc.ch != '(' && sc.ch != ')' && ( /* This line of the condition makes it possible to extend pseudo-classes with parentheses */
467 				sc.state == SCE_CSS_PSEUDOCLASS || sc.state == SCE_CSS_PSEUDOELEMENT ||
468 				sc.state == SCE_CSS_EXTENDED_PSEUDOCLASS || sc.state == SCE_CSS_EXTENDED_PSEUDOELEMENT ||
469 				sc.state == SCE_CSS_UNKNOWN_PSEUDOCLASS
470 			))
471 		))
472 			sc.SetState(SCE_CSS_TAG);
473 
474 		if (sc.Match('/', '*')) {
475 			lastStateC = sc.state;
476 			comment_mode = eCommentBlock;
477 			sc.SetState(SCE_CSS_COMMENT);
478 			sc.Forward();
479 		} else if (hasSingleLineComments && sc.Match('/', '/') && !insideParentheses) {
480 			// note that we've had to treat ([...]// as the start of a URL not a comment, e.g. url(http://example.com), url(//example.com)
481 			lastStateC = sc.state;
482 			comment_mode = eCommentLine;
483 			sc.SetState(SCE_CSS_COMMENT);
484 			sc.Forward();
485 		} else if ((sc.state == SCE_CSS_VALUE || sc.state == SCE_CSS_ATTRIBUTE)
486 			&& (sc.ch == '\"' || sc.ch == '\'')) {
487 			lastStateS = sc.state;
488 			sc.SetState((sc.ch == '\"' ? SCE_CSS_DOUBLESTRING : SCE_CSS_SINGLESTRING));
489 		} else if (IsCssOperator(sc.ch)
490 			&& (sc.state != SCE_CSS_ATTRIBUTE || sc.ch == ']')
491 			&& (sc.state != SCE_CSS_VALUE || sc.ch == ';' || sc.ch == '}' || sc.ch == '!')
492 			&& ((sc.state != SCE_CSS_DIRECTIVE && sc.state != SCE_CSS_MEDIA) || sc.ch == ';' || sc.ch == '{')
493 		) {
494 			if (sc.state != SCE_CSS_OPERATOR)
495 				lastState = sc.state;
496 			sc.SetState(SCE_CSS_OPERATOR);
497 			op = sc.ch;
498 			opPrev = sc.chPrev;
499 		}
500 	}
501 
502 	sc.Complete();
503 }
504 
FoldCSSDoc(Sci_PositionU startPos,Sci_Position length,int,WordList * [],Accessor & styler)505 static void FoldCSSDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler) {
506 	bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
507 	bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
508 	Sci_PositionU endPos = startPos + length;
509 	int visibleChars = 0;
510 	Sci_Position lineCurrent = styler.GetLine(startPos);
511 	int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
512 	int levelCurrent = levelPrev;
513 	char chNext = styler[startPos];
514 	bool inComment = (styler.StyleAt(startPos-1) == SCE_CSS_COMMENT);
515 	for (Sci_PositionU i = startPos; i < endPos; i++) {
516 		char ch = chNext;
517 		chNext = styler.SafeGetCharAt(i + 1);
518 		int style = styler.StyleAt(i);
519 		bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
520 		if (foldComment) {
521 			if (!inComment && (style == SCE_CSS_COMMENT))
522 				levelCurrent++;
523 			else if (inComment && (style != SCE_CSS_COMMENT))
524 				levelCurrent--;
525 			inComment = (style == SCE_CSS_COMMENT);
526 		}
527 		if (style == SCE_CSS_OPERATOR) {
528 			if (ch == '{') {
529 				levelCurrent++;
530 			} else if (ch == '}') {
531 				levelCurrent--;
532 			}
533 		}
534 		if (atEOL) {
535 			int lev = levelPrev;
536 			if (visibleChars == 0 && foldCompact)
537 				lev |= SC_FOLDLEVELWHITEFLAG;
538 			if ((levelCurrent > levelPrev) && (visibleChars > 0))
539 				lev |= SC_FOLDLEVELHEADERFLAG;
540 			if (lev != styler.LevelAt(lineCurrent)) {
541 				styler.SetLevel(lineCurrent, lev);
542 			}
543 			lineCurrent++;
544 			levelPrev = levelCurrent;
545 			visibleChars = 0;
546 		}
547 		if (!isspacechar(ch))
548 			visibleChars++;
549 	}
550 	// Fill in the real level of the next line, keeping the current flags as they will be filled in later
551 	int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
552 	styler.SetLevel(lineCurrent, levelPrev | flagsNext);
553 }
554 
555 static const char * const cssWordListDesc[] = {
556 	"CSS1 Properties",
557 	"Pseudo-classes",
558 	"CSS2 Properties",
559 	"CSS3 Properties",
560 	"Pseudo-elements",
561 	"Browser-Specific CSS Properties",
562 	"Browser-Specific Pseudo-classes",
563 	"Browser-Specific Pseudo-elements",
564 	0
565 };
566 
567 LexerModule lmCss(SCLEX_CSS, ColouriseCssDoc, "css", FoldCSSDoc, cssWordListDesc);
568