1"""
2 pygments.lexers.css
3 ~~~~~~~~~~~~~~~~~~~
4
5 Lexers for CSS and related stylesheet formats.
6
7 :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
10
11import re
12import copy
13
14from pygments.lexer import ExtendedRegexLexer, RegexLexer, include, bygroups, \
15 default, words, inherit
16from pygments.token import Comment, Operator, Keyword, Name, String, Number, \
17 Punctuation, Whitespace
18from pygments.lexers._css_builtins import _css_properties
19
20__all__ = ['CssLexer', 'SassLexer', 'ScssLexer', 'LessCssLexer']
21
22
23# List of vendor prefixes obtained from:
24# https://www.w3.org/TR/CSS21/syndata.html#vendor-keyword-history
25_vendor_prefixes = (
26 '-ms-', 'mso-', '-moz-', '-o-', '-xv-', '-atsc-', '-wap-', '-khtml-',
27 '-webkit-', 'prince-', '-ah-', '-hp-', '-ro-', '-rim-', '-tc-',
28)
29
30# List of extended color keywords obtained from:
31# https://drafts.csswg.org/css-color/#named-colors
32_color_keywords = (
33 'aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'beige',
34 'bisque', 'black', 'blanchedalmond', 'blue', 'blueviolet', 'brown',
35 'burlywood', 'cadetblue', 'chartreuse', 'chocolate', 'coral',
36 'cornflowerblue', 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan',
37 'darkgoldenrod', 'darkgray', 'darkgreen', 'darkgrey', 'darkkhaki',
38 'darkmagenta', 'darkolivegreen', 'darkorange', 'darkorchid', 'darkred',
39 'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray',
40 'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink', 'deepskyblue',
41 'dimgray', 'dimgrey', 'dodgerblue', 'firebrick', 'floralwhite',
42 'forestgreen', 'fuchsia', 'gainsboro', 'ghostwhite', 'gold', 'goldenrod',
43 'gray', 'green', 'greenyellow', 'grey', 'honeydew', 'hotpink', 'indianred',
44 'indigo', 'ivory', 'khaki', 'lavender', 'lavenderblush', 'lawngreen',
45 'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan',
46 'lightgoldenrodyellow', 'lightgray', 'lightgreen', 'lightgrey',
47 'lightpink', 'lightsalmon', 'lightseagreen', 'lightskyblue',
48 'lightslategray', 'lightslategrey', 'lightsteelblue', 'lightyellow',
49 'lime', 'limegreen', 'linen', 'magenta', 'maroon', 'mediumaquamarine',
50 'mediumblue', 'mediumorchid', 'mediumpurple', 'mediumseagreen',
51 'mediumslateblue', 'mediumspringgreen', 'mediumturquoise',
52 'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose', 'moccasin',
53 'navajowhite', 'navy', 'oldlace', 'olive', 'olivedrab', 'orange',
54 'orangered', 'orchid', 'palegoldenrod', 'palegreen', 'paleturquoise',
55 'palevioletred', 'papayawhip', 'peachpuff', 'peru', 'pink', 'plum',
56 'powderblue', 'purple', 'rebeccapurple', 'red', 'rosybrown', 'royalblue',
57 'saddlebrown', 'salmon', 'sandybrown', 'seagreen', 'seashell', 'sienna',
58 'silver', 'skyblue', 'slateblue', 'slategray', 'slategrey', 'snow',
59 'springgreen', 'steelblue', 'tan', 'teal', 'thistle', 'tomato', 'turquoise',
60 'violet', 'wheat', 'white', 'whitesmoke', 'yellow', 'yellowgreen',
61) + ('transparent',)
62
63# List of keyword values obtained from:
64# http://cssvalues.com/
65_keyword_values = (
66 'absolute', 'alias', 'all', 'all-petite-caps', 'all-scroll',
67 'all-small-caps', 'allow-end', 'alpha', 'alternate', 'alternate-reverse',
68 'always', 'armenian', 'auto', 'avoid', 'avoid-column', 'avoid-page',
69 'backwards', 'balance', 'baseline', 'below', 'blink', 'block', 'bold',
70 'bolder', 'border-box', 'both', 'bottom', 'box-decoration', 'break-word',
71 'capitalize', 'cell', 'center', 'circle', 'clip', 'clone', 'close-quote',
72 'col-resize', 'collapse', 'color', 'color-burn', 'color-dodge', 'column',
73 'column-reverse', 'compact', 'condensed', 'contain', 'container',
74 'content-box', 'context-menu', 'copy', 'cover', 'crisp-edges', 'crosshair',
75 'currentColor', 'cursive', 'darken', 'dashed', 'decimal',
76 'decimal-leading-zero', 'default', 'descendants', 'difference', 'digits',
77 'disc', 'distribute', 'dot', 'dotted', 'double', 'double-circle', 'e-resize',
78 'each-line', 'ease', 'ease-in', 'ease-in-out', 'ease-out', 'edges',
79 'ellipsis', 'end', 'ew-resize', 'exclusion', 'expanded', 'extra-condensed',
80 'extra-expanded', 'fantasy', 'fill', 'fill-box', 'filled', 'first', 'fixed',
81 'flat', 'flex', 'flex-end', 'flex-start', 'flip', 'force-end', 'forwards',
82 'from-image', 'full-width', 'geometricPrecision', 'georgian', 'groove',
83 'hanging', 'hard-light', 'help', 'hidden', 'hide', 'horizontal', 'hue',
84 'icon', 'infinite', 'inherit', 'initial', 'ink', 'inline', 'inline-block',
85 'inline-flex', 'inline-table', 'inset', 'inside', 'inter-word', 'invert',
86 'isolate', 'italic', 'justify', 'large', 'larger', 'last', 'left',
87 'lighten', 'lighter', 'line-through', 'linear', 'list-item', 'local',
88 'loose', 'lower-alpha', 'lower-greek', 'lower-latin', 'lower-roman',
89 'lowercase', 'ltr', 'luminance', 'luminosity', 'mandatory', 'manipulation',
90 'manual', 'margin-box', 'match-parent', 'medium', 'mixed', 'monospace',
91 'move', 'multiply', 'n-resize', 'ne-resize', 'nesw-resize',
92 'no-close-quote', 'no-drop', 'no-open-quote', 'no-repeat', 'none', 'normal',
93 'not-allowed', 'nowrap', 'ns-resize', 'nw-resize', 'nwse-resize', 'objects',
94 'oblique', 'off', 'on', 'open', 'open-quote', 'optimizeLegibility',
95 'optimizeSpeed', 'outset', 'outside', 'over', 'overlay', 'overline',
96 'padding-box', 'page', 'pan-down', 'pan-left', 'pan-right', 'pan-up',
97 'pan-x', 'pan-y', 'paused', 'petite-caps', 'pixelated', 'pointer',
98 'preserve-3d', 'progress', 'proximity', 'relative', 'repeat',
99 'repeat no-repeat', 'repeat-x', 'repeat-y', 'reverse', 'revert', 'ridge', 'right',
100 'round', 'row', 'row-resize', 'row-reverse', 'rtl', 'ruby', 'ruby-base',
101 'ruby-base-container', 'ruby-text', 'ruby-text-container', 'run-in',
102 'running', 's-resize', 'sans-serif', 'saturation', 'scale-down', 'screen',
103 'scroll', 'se-resize', 'semi-condensed', 'semi-expanded', 'separate',
104 'serif', 'sesame', 'show', 'sideways', 'sideways-left', 'sideways-right',
105 'slice', 'small', 'small-caps', 'smaller', 'smooth', 'snap', 'soft-light',
106 'solid', 'space', 'space-around', 'space-between', 'spaces', 'square',
107 'start', 'static', 'step-end', 'step-start', 'sticky', 'stretch', 'strict',
108 'stroke-box', 'style', 'sw-resize', 'table', 'table-caption', 'table-cell',
109 'table-column', 'table-column-group', 'table-footer-group',
110 'table-header-group', 'table-row', 'table-row-group', 'text', 'thick',
111 'thin', 'titling-caps', 'to', 'top', 'triangle', 'ultra-condensed',
112 'ultra-expanded', 'under', 'underline', 'unicase', 'unset', 'upper-alpha',
113 'upper-latin', 'upper-roman', 'uppercase', 'upright', 'use-glyph-orientation',
114 'vertical', 'vertical-text', 'view-box', 'visible', 'w-resize', 'wait',
115 'wavy', 'weight', 'weight style', 'wrap', 'wrap-reverse', 'x-large',
116 'x-small', 'xx-large', 'xx-small', 'zoom-in', 'zoom-out',
117)
118
119# List of other keyword values from other sources:
120_other_keyword_values = (
121 'above', 'aural', 'behind', 'bidi-override', 'center-left', 'center-right',
122 'cjk-ideographic', 'continuous', 'crop', 'cross', 'embed', 'far-left',
123 'far-right', 'fast', 'faster', 'hebrew', 'high', 'higher', 'hiragana',
124 'hiragana-iroha', 'katakana', 'katakana-iroha', 'landscape', 'left-side',
125 'leftwards', 'level', 'loud', 'low', 'lower', 'message-box', 'middle',
126 'mix', 'narrower', 'once', 'portrait', 'right-side', 'rightwards', 'silent',
127 'slow', 'slower', 'small-caption', 'soft', 'spell-out', 'status-bar',
128 'super', 'text-bottom', 'text-top', 'wider', 'x-fast', 'x-high', 'x-loud',
129 'x-low', 'x-soft', 'yes', 'pre', 'pre-wrap', 'pre-line',
130)
131
132# List of functional notation and function keyword values:
133_functional_notation_keyword_values = (
134 'attr', 'blackness', 'blend', 'blenda', 'blur', 'brightness', 'calc',
135 'circle', 'color-mod', 'contrast', 'counter', 'cubic-bezier', 'device-cmyk',
136 'drop-shadow', 'ellipse', 'gray', 'grayscale', 'hsl', 'hsla', 'hue',
137 'hue-rotate', 'hwb', 'image', 'inset', 'invert', 'lightness',
138 'linear-gradient', 'matrix', 'matrix3d', 'opacity', 'perspective',
139 'polygon', 'radial-gradient', 'rect', 'repeating-linear-gradient',
140 'repeating-radial-gradient', 'rgb', 'rgba', 'rotate', 'rotate3d', 'rotateX',
141 'rotateY', 'rotateZ', 'saturate', 'saturation', 'scale', 'scale3d',
142 'scaleX', 'scaleY', 'scaleZ', 'sepia', 'shade', 'skewX', 'skewY', 'steps',
143 'tint', 'toggle', 'translate', 'translate3d', 'translateX', 'translateY',
144 'translateZ', 'whiteness',
145)
146# Note! Handle url(...) separately.
147
148# List of units obtained from:
149# https://www.w3.org/TR/css3-values/
150_angle_units = (
151 'deg', 'grad', 'rad', 'turn',
152)
153_frequency_units = (
154 'Hz', 'kHz',
155)
156_length_units = (
157 'em', 'ex', 'ch', 'rem',
158 'vh', 'vw', 'vmin', 'vmax',
159 'px', 'mm', 'cm', 'in', 'pt', 'pc', 'q',
160)
161_resolution_units = (
162 'dpi', 'dpcm', 'dppx',
163)
164_time_units = (
165 's', 'ms',
166)
167_all_units = _angle_units + _frequency_units + _length_units + \
168 _resolution_units + _time_units
169
170
171class CssLexer(RegexLexer):
172 """
173 For CSS (Cascading Style Sheets).
174 """
175
176 name = 'CSS'
177 url = 'https://www.w3.org/TR/CSS/#css'
178 aliases = ['css']
179 filenames = ['*.css']
180 mimetypes = ['text/css']
181 version_added = ''
182
183 tokens = {
184 'root': [
185 include('basics'),
186 ],
187 'basics': [
188 (r'\s+', Whitespace),
189 (r'/\*(?:.|\n)*?\*/', Comment),
190 (r'\{', Punctuation, 'content'),
191 (r'(\:{1,2})([\w-]+)', bygroups(Punctuation, Name.Decorator)),
192 (r'(\.)([\w-]+)', bygroups(Punctuation, Name.Class)),
193 (r'(\#)([\w-]+)', bygroups(Punctuation, Name.Namespace)),
194 (r'(@)([\w-]+)', bygroups(Punctuation, Keyword), 'atrule'),
195 (r'[\w-]+', Name.Tag),
196 (r'[~^*!%&$\[\]()<>|+=@:;,./?-]', Operator),
197 (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
198 (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
199 ],
200 'atrule': [
201 (r'\{', Punctuation, 'atcontent'),
202 (r';', Punctuation, '#pop'),
203 include('basics'),
204 ],
205 'atcontent': [
206 include('basics'),
207 (r'\}', Punctuation, '#pop:2'),
208 ],
209 'content': [
210 (r'\s+', Whitespace),
211 (r'\}', Punctuation, '#pop'),
212 (r';', Punctuation),
213 (r'^@.*?$', Comment.Preproc),
214
215 (words(_vendor_prefixes,), Keyword.Pseudo),
216 (r'('+r'|'.join(_css_properties)+r')(\s*)(\:)',
217 bygroups(Keyword, Whitespace, Punctuation), 'value-start'),
218 (r'([-]+[a-zA-Z_][\w-]*)(\s*)(\:)', bygroups(Name.Variable, Whitespace, Punctuation),
219 'value-start'),
220 (r'([a-zA-Z_][\w-]*)(\s*)(\:)', bygroups(Name, Whitespace, Punctuation),
221 'value-start'),
222
223 (r'/\*(?:.|\n)*?\*/', Comment),
224 ],
225 'value-start': [
226 (r'\s+', Whitespace),
227 (words(_vendor_prefixes,), Name.Builtin.Pseudo),
228 include('urls'),
229 (r'('+r'|'.join(_functional_notation_keyword_values)+r')(\()',
230 bygroups(Name.Builtin, Punctuation), 'function-start'),
231 (r'([a-zA-Z_][\w-]+)(\()',
232 bygroups(Name.Function, Punctuation), 'function-start'),
233 (words(_keyword_values, suffix=r'\b'), Keyword.Constant),
234 (words(_other_keyword_values, suffix=r'\b'), Keyword.Constant),
235 (words(_color_keywords, suffix=r'\b'), Keyword.Constant),
236 # for transition-property etc.
237 (words(_css_properties, suffix=r'\b'), Keyword),
238 (r'\!important', Comment.Preproc),
239 (r'/\*(?:.|\n)*?\*/', Comment),
240
241 include('numeric-values'),
242
243 (r'[~^*!%&<>|+=@:./?-]+', Operator),
244 (r'[\[\](),]+', Punctuation),
245 (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
246 (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
247 (r'[a-zA-Z_][\w-]*', Name),
248 (r';', Punctuation, '#pop'),
249 (r'\}', Punctuation, '#pop:2'),
250 ],
251 'function-start': [
252 (r'\s+', Whitespace),
253 (r'[-]+([A-Za-z][\w+]*[-]*)+', Name.Variable),
254 include('urls'),
255 (words(_vendor_prefixes,), Keyword.Pseudo),
256 (words(_keyword_values, suffix=r'\b'), Keyword.Constant),
257 (words(_other_keyword_values, suffix=r'\b'), Keyword.Constant),
258 (words(_color_keywords, suffix=r'\b'), Keyword.Constant),
259
260 # function-start may be entered recursively
261 (r'(' + r'|'.join(_functional_notation_keyword_values) + r')(\()',
262 bygroups(Name.Builtin, Punctuation), 'function-start'),
263 (r'([a-zA-Z_][\w-]+)(\()',
264 bygroups(Name.Function, Punctuation), 'function-start'),
265
266 (r'/\*(?:.|\n)*?\*/', Comment),
267 include('numeric-values'),
268 (r'[*+/-]', Operator),
269 (r',', Punctuation),
270 (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
271 (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
272 (r'[a-zA-Z_-]\w*', Name),
273 (r'\)', Punctuation, '#pop'),
274 ],
275 'urls': [
276 (r'(url)(\()(".*?")(\))', bygroups(Name.Builtin, Punctuation,
277 String.Double, Punctuation)),
278 (r"(url)(\()('.*?')(\))", bygroups(Name.Builtin, Punctuation,
279 String.Single, Punctuation)),
280 (r'(url)(\()(.*?)(\))', bygroups(Name.Builtin, Punctuation,
281 String.Other, Punctuation)),
282 ],
283 'numeric-values': [
284 (r'\#[a-zA-Z0-9]{1,6}', Number.Hex),
285 (r'[+\-]?[0-9]*[.][0-9]+', Number.Float, 'numeric-end'),
286 (r'[+\-]?[0-9]+', Number.Integer, 'numeric-end'),
287 ],
288 'numeric-end': [
289 (words(_all_units, suffix=r'\b'), Keyword.Type),
290 (r'%', Keyword.Type),
291 default('#pop'),
292 ],
293 }
294
295
296common_sass_tokens = {
297 'value': [
298 (r'[ \t]+', Whitespace),
299 (r'[!$][\w-]+', Name.Variable),
300 (r'url\(', String.Other, 'string-url'),
301 (r'[a-z_-][\w-]*(?=\()', Name.Function),
302 (words(_css_properties + (
303 'above', 'absolute', 'always', 'armenian', 'aural', 'auto', 'avoid', 'baseline',
304 'behind', 'below', 'bidi-override', 'blink', 'block', 'bold', 'bolder', 'both',
305 'capitalize', 'center-left', 'center-right', 'center', 'circle',
306 'cjk-ideographic', 'close-quote', 'collapse', 'condensed', 'continuous',
307 'crosshair', 'cross', 'cursive', 'dashed', 'decimal-leading-zero',
308 'decimal', 'default', 'digits', 'disc', 'dotted', 'double', 'e-resize', 'embed',
309 'extra-condensed', 'extra-expanded', 'expanded', 'fantasy', 'far-left',
310 'far-right', 'faster', 'fast', 'fixed', 'georgian', 'groove', 'hebrew', 'help',
311 'hidden', 'hide', 'higher', 'high', 'hiragana-iroha', 'hiragana', 'icon',
312 'inherit', 'inline-table', 'inline', 'inset', 'inside', 'invert', 'italic',
313 'justify', 'katakana-iroha', 'katakana', 'landscape', 'larger', 'large',
314 'left-side', 'leftwards', 'level', 'lighter', 'line-through', 'list-item',
315 'loud', 'lower-alpha', 'lower-greek', 'lower-roman', 'lowercase', 'ltr',
316 'lower', 'low', 'medium', 'message-box', 'middle', 'mix', 'monospace',
317 'n-resize', 'narrower', 'ne-resize', 'no-close-quote', 'no-open-quote',
318 'no-repeat', 'none', 'normal', 'nowrap', 'nw-resize', 'oblique', 'once',
319 'open-quote', 'outset', 'outside', 'overline', 'pointer', 'portrait', 'px',
320 'relative', 'repeat-x', 'repeat-y', 'repeat', 'rgb', 'ridge', 'right-side',
321 'rightwards', 's-resize', 'sans-serif', 'scroll', 'se-resize',
322 'semi-condensed', 'semi-expanded', 'separate', 'serif', 'show', 'silent',
323 'slow', 'slower', 'small-caps', 'small-caption', 'smaller', 'soft', 'solid',
324 'spell-out', 'square', 'static', 'status-bar', 'super', 'sw-resize',
325 'table-caption', 'table-cell', 'table-column', 'table-column-group',
326 'table-footer-group', 'table-header-group', 'table-row',
327 'table-row-group', 'text', 'text-bottom', 'text-top', 'thick', 'thin',
328 'transparent', 'ultra-condensed', 'ultra-expanded', 'underline',
329 'upper-alpha', 'upper-latin', 'upper-roman', 'uppercase', 'url',
330 'visible', 'w-resize', 'wait', 'wider', 'x-fast', 'x-high', 'x-large', 'x-loud',
331 'x-low', 'x-small', 'x-soft', 'xx-large', 'xx-small', 'yes'), suffix=r'\b'),
332 Name.Constant),
333 (words(_color_keywords, suffix=r'\b'), Name.Entity),
334 (words((
335 'black', 'silver', 'gray', 'white', 'maroon', 'red', 'purple', 'fuchsia', 'green',
336 'lime', 'olive', 'yellow', 'navy', 'blue', 'teal', 'aqua'), suffix=r'\b'),
337 Name.Builtin),
338 (r'\!(important|default)', Name.Exception),
339 (r'(true|false)', Name.Pseudo),
340 (r'(and|or|not)', Operator.Word),
341 (r'/\*', Comment.Multiline, 'inline-comment'),
342 (r'//[^\n]*', Comment.Single),
343 (r'\#[a-z0-9]{1,6}', Number.Hex),
344 (r'(-?\d+)(\%|[a-z]+)?', bygroups(Number.Integer, Keyword.Type)),
345 (r'(-?\d*\.\d+)(\%|[a-z]+)?', bygroups(Number.Float, Keyword.Type)),
346 (r'#\{', String.Interpol, 'interpolation'),
347 (r'[~^*!&%<>|+=@:,./?-]+', Operator),
348 (r'[\[\]()]+', Punctuation),
349 (r'"', String.Double, 'string-double'),
350 (r"'", String.Single, 'string-single'),
351 (r'[a-z_-][\w-]*', Name),
352 ],
353
354 'interpolation': [
355 (r'\}', String.Interpol, '#pop'),
356 include('value'),
357 ],
358
359 'selector': [
360 (r'[ \t]+', Whitespace),
361 (r'\:', Name.Decorator, 'pseudo-class'),
362 (r'\.', Name.Class, 'class'),
363 (r'\#', Name.Namespace, 'id'),
364 (r'[\w-]+', Name.Tag),
365 (r'#\{', String.Interpol, 'interpolation'),
366 (r'&', Keyword),
367 (r'[~^*!&\[\]()<>|+=@:;,./?-]', Operator),
368 (r'"', String.Double, 'string-double'),
369 (r"'", String.Single, 'string-single'),
370 ],
371
372 'string-double': [
373 (r'(\\.|#(?=[^\n{])|[^\n"#])+', String.Double),
374 (r'#\{', String.Interpol, 'interpolation'),
375 (r'"', String.Double, '#pop'),
376 ],
377
378 'string-single': [
379 (r"(\\.|#(?=[^\n{])|[^\n'#])+", String.Single),
380 (r'#\{', String.Interpol, 'interpolation'),
381 (r"'", String.Single, '#pop'),
382 ],
383
384 'string-url': [
385 (r'(\\#|#(?=[^\n{])|[^\n#)])+', String.Other),
386 (r'#\{', String.Interpol, 'interpolation'),
387 (r'\)', String.Other, '#pop'),
388 ],
389
390 'pseudo-class': [
391 (r'[\w-]+', Name.Decorator),
392 (r'#\{', String.Interpol, 'interpolation'),
393 default('#pop'),
394 ],
395
396 'class': [
397 (r'[\w-]+', Name.Class),
398 (r'#\{', String.Interpol, 'interpolation'),
399 default('#pop'),
400 ],
401
402 'id': [
403 (r'[\w-]+', Name.Namespace),
404 (r'#\{', String.Interpol, 'interpolation'),
405 default('#pop'),
406 ],
407
408 'for': [
409 (r'(from|to|through)', Operator.Word),
410 include('value'),
411 ],
412}
413
414
415def _indentation(lexer, match, ctx):
416 indentation = match.group(0)
417 yield match.start(), Whitespace, indentation
418 ctx.last_indentation = indentation
419 ctx.pos = match.end()
420
421 if hasattr(ctx, 'block_state') and ctx.block_state and \
422 indentation.startswith(ctx.block_indentation) and \
423 indentation != ctx.block_indentation:
424 ctx.stack.append(ctx.block_state)
425 else:
426 ctx.block_state = None
427 ctx.block_indentation = None
428 ctx.stack.append('content')
429
430
431def _starts_block(token, state):
432 def callback(lexer, match, ctx):
433 yield match.start(), token, match.group(0)
434
435 if hasattr(ctx, 'last_indentation'):
436 ctx.block_indentation = ctx.last_indentation
437 else:
438 ctx.block_indentation = ''
439
440 ctx.block_state = state
441 ctx.pos = match.end()
442
443 return callback
444
445
446class SassLexer(ExtendedRegexLexer):
447 """
448 For Sass stylesheets.
449 """
450
451 name = 'Sass'
452 url = 'https://sass-lang.com/'
453 aliases = ['sass']
454 filenames = ['*.sass']
455 mimetypes = ['text/x-sass']
456 version_added = '1.3'
457
458 flags = re.IGNORECASE | re.MULTILINE
459
460 tokens = {
461 'root': [
462 (r'[ \t]*\n', Whitespace),
463 (r'[ \t]*', _indentation),
464 ],
465
466 'content': [
467 (r'//[^\n]*', _starts_block(Comment.Single, 'single-comment'),
468 'root'),
469 (r'/\*[^\n]*', _starts_block(Comment.Multiline, 'multi-comment'),
470 'root'),
471 (r'@import', Keyword, 'import'),
472 (r'@for', Keyword, 'for'),
473 (r'@(debug|warn|if|while)', Keyword, 'value'),
474 (r'(@mixin)( )([\w-]+)', bygroups(Keyword, Whitespace, Name.Function), 'value'),
475 (r'(@include)( )([\w-]+)', bygroups(Keyword, Whitespace, Name.Decorator), 'value'),
476 (r'@extend', Keyword, 'selector'),
477 (r'@[\w-]+', Keyword, 'selector'),
478 (r'=[\w-]+', Name.Function, 'value'),
479 (r'\+[\w-]+', Name.Decorator, 'value'),
480 (r'([!$][\w-]\w*)([ \t]*(?:(?:\|\|)?=|:))',
481 bygroups(Name.Variable, Operator), 'value'),
482 (r':', Name.Attribute, 'old-style-attr'),
483 (r'(?=.+?[=:]([^a-z]|$))', Name.Attribute, 'new-style-attr'),
484 default('selector'),
485 ],
486
487 'single-comment': [
488 (r'.+', Comment.Single),
489 (r'\n', Whitespace, 'root'),
490 ],
491
492 'multi-comment': [
493 (r'.+', Comment.Multiline),
494 (r'\n', Whitespace, 'root'),
495 ],
496
497 'import': [
498 (r'[ \t]+', Whitespace),
499 (r'\S+', String),
500 (r'\n', Whitespace, 'root'),
501 ],
502
503 'old-style-attr': [
504 (r'[^\s:="\[]+', Name.Attribute),
505 (r'#\{', String.Interpol, 'interpolation'),
506 (r'([ \t]*)(=)', bygroups(Whitespace, Operator), 'value'),
507 default('value'),
508 ],
509
510 'new-style-attr': [
511 (r'[^\s:="\[]+', Name.Attribute),
512 (r'#\{', String.Interpol, 'interpolation'),
513 (r'([ \t]*)([=:])', bygroups(Whitespace, Operator), 'value'),
514 ],
515
516 'inline-comment': [
517 (r"(\\#|#(?=[^\n{])|\*(?=[^\n/])|[^\n#*])+", Comment.Multiline),
518 (r'#\{', String.Interpol, 'interpolation'),
519 (r"\*/", Comment, '#pop'),
520 ],
521 }
522 for group, common in common_sass_tokens.items():
523 tokens[group] = copy.copy(common)
524 tokens['value'].append((r'\n', Whitespace, 'root'))
525 tokens['selector'].append((r'\n', Whitespace, 'root'))
526
527
528class ScssLexer(RegexLexer):
529 """
530 For SCSS stylesheets.
531 """
532
533 name = 'SCSS'
534 url = 'https://sass-lang.com/'
535 aliases = ['scss']
536 filenames = ['*.scss']
537 mimetypes = ['text/x-scss']
538 version_added = ''
539
540 flags = re.IGNORECASE | re.DOTALL
541 tokens = {
542 'root': [
543 (r'\s+', Whitespace),
544 (r'//.*?\n', Comment.Single),
545 (r'/\*.*?\*/', Comment.Multiline),
546 (r'@import', Keyword, 'value'),
547 (r'@for', Keyword, 'for'),
548 (r'@(debug|warn|if|while)', Keyword, 'value'),
549 (r'(@mixin)( [\w-]+)', bygroups(Keyword, Name.Function), 'value'),
550 (r'(@include)( [\w-]+)', bygroups(Keyword, Name.Decorator), 'value'),
551 (r'@extend', Keyword, 'selector'),
552 (r'(@media)(\s+)', bygroups(Keyword, Whitespace), 'value'),
553 (r'@[\w-]+', Keyword, 'selector'),
554 (r'(\$[\w-]*\w)([ \t]*:)', bygroups(Name.Variable, Operator), 'value'),
555 # TODO: broken, and prone to infinite loops.
556 # (r'(?=[^;{}][;}])', Name.Attribute, 'attr'),
557 # (r'(?=[^;{}:]+:[^a-z])', Name.Attribute, 'attr'),
558 default('selector'),
559 ],
560
561 'attr': [
562 (r'[^\s:="\[]+', Name.Attribute),
563 (r'#\{', String.Interpol, 'interpolation'),
564 (r'[ \t]*:', Operator, 'value'),
565 default('#pop'),
566 ],
567
568 'inline-comment': [
569 (r"(\\#|#(?=[^{])|\*(?=[^/])|[^#*])+", Comment.Multiline),
570 (r'#\{', String.Interpol, 'interpolation'),
571 (r"\*/", Comment, '#pop'),
572 ],
573 }
574 for group, common in common_sass_tokens.items():
575 tokens[group] = copy.copy(common)
576 tokens['value'].extend([(r'\n', Whitespace), (r'[;{}]', Punctuation, '#pop')])
577 tokens['selector'].extend([(r'\n', Whitespace), (r'[;{}]', Punctuation, '#pop')])
578
579
580class LessCssLexer(CssLexer):
581 """
582 For LESS styleshets.
583 """
584
585 name = 'LessCss'
586 url = 'http://lesscss.org/'
587 aliases = ['less']
588 filenames = ['*.less']
589 mimetypes = ['text/x-less-css']
590 version_added = '2.1'
591
592 tokens = {
593 'root': [
594 (r'@\w+', Name.Variable),
595 inherit,
596 ],
597 'content': [
598 (r'\{', Punctuation, '#push'),
599 (r'//.*\n', Comment.Single),
600 inherit,
601 ],
602 }