Skip to content

Commit 5070104

Browse files
committed
Basic support for CSS4 variables
Fixes #113.
1 parent 47742f2 commit 5070104

File tree

6 files changed

+67
-10
lines changed

6 files changed

+67
-10
lines changed

lesscpy/less.ast

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575

7676
property : css_property
7777
| css_vendor_property
78+
| css_user_property
7879
| css_ident
7980

8081
style_list : style_group
@@ -89,6 +90,7 @@
8990
| css_string
9091
| istring
9192
| css_vendor_property
93+
| css_user_property
9294
| css_property
9395
| css_ident
9496
| '~' istring
@@ -136,6 +138,7 @@ factor : color
136138
| css_ident
137139
| css_id
138140
| css_uri
141+
| css_user_property
139142
| '='
140143

141144
istring : less_string

lesscpy/lessc/lexer.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ class LessLexer:
3232
literals = '<>=%!/*-+&'
3333
tokens = [
3434
'css_ident', 'css_dom', 'css_class', 'css_id', 'css_property',
35-
'css_vendor_property', 'css_comment', 'css_string', 'css_color',
36-
'css_filter', 'css_number', 'css_important', 'css_vendor_hack',
37-
'css_uri', 'css_ms_filter', 'css_keyframe_selector', 'css_media_type',
38-
'css_media_feature', 't_and', 't_not', 't_only', 'less_variable',
39-
'less_comment', 'less_open_format', 'less_when', 'less_and',
40-
'less_not', 't_ws', 't_popen', 't_pclose', 't_semicolon', 't_tilde',
41-
't_colon', 't_comma', 't_eopen', 't_eclose', 't_isopen', 't_isclose',
42-
't_bopen', 't_bclose'
35+
'css_vendor_property', 'css_user_property', 'css_comment',
36+
'css_string', 'css_color', 'css_filter', 'css_number', 'css_important',
37+
'css_vendor_hack', 'css_uri', 'css_ms_filter', 'css_keyframe_selector',
38+
'css_media_type', 'css_media_feature', 't_and', 't_not', 't_only',
39+
'less_variable', 'less_comment', 'less_open_format', 'less_when',
40+
'less_and', 'less_not', 't_ws', 't_popen', 't_pclose', 't_semicolon',
41+
't_tilde', 't_colon', 't_comma', 't_eopen', 't_eclose', 't_isopen',
42+
't_isclose', 't_bopen', 't_bclose'
4343
]
4444
tokens += list(set(reserved.tokens.values()))
4545
# Tokens with significant following whitespace
46-
significant_ws = {'css_class', 'css_id', 'css_dom', 'css_property', 'css_vendor_property', 'css_ident',
46+
significant_ws = {'css_class', 'css_id', 'css_dom', 'css_property', 'css_vendor_property', 'css_user_property', 'css_ident',
4747
'css_number', 'css_color', 'css_media_type', 'css_filter', 'less_variable', 't_and', 't_not',
4848
't_only', '&'}
4949
significant_ws.update(reserved.tokens.values())
@@ -87,7 +87,7 @@ def t_css_number(self, t):
8787
return t
8888

8989
def t_css_ident(self, t):
90-
(r'([\-\.\#]?'
90+
(r'((\-|\.|\#|\-\-)?'
9191
'([_a-z]'
9292
'|[\200-\377]'
9393
'|\\\[0-9a-f]{1,6}'
@@ -132,6 +132,9 @@ def t_css_ident(self, t):
132132
# DOM elements can't be part of property declarations, avoids ambiguity between 'rect' DOM
133133
# element and rect() CSS function.
134134
t.type = 'css_dom'
135+
elif v.startswith("--"):
136+
t.type = 'css_user_property'
137+
t.lexer.in_property_decl = True
135138
elif c == '-':
136139
t.type = 'css_vendor_property'
137140
t.lexer.in_property_decl = True

lesscpy/lessc/parser.py

+8
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ def p_prop_open_ie_hack(self, p):
494494
def p_prop_open(self, p):
495495
""" prop_open : property t_colon
496496
| vendor_property t_colon
497+
| user_property t_colon
497498
| word t_colon
498499
"""
499500
p[0] = (p[1][0], '')
@@ -771,6 +772,7 @@ def p_argument(self, p):
771772
| word
772773
| id
773774
| css_uri
775+
| css_user_property
774776
| '='
775777
| fcall
776778
"""
@@ -967,6 +969,12 @@ def p_vendor_property(self, p):
967969
"""
968970
p[0] = tuple(list(p)[1:])
969971

972+
def p_user_property(self, p):
973+
""" user_property : css_user_property
974+
| css_user_property t_ws
975+
"""
976+
p[0] = tuple(list(p)[1:])
977+
970978
def p_media_type(self, p):
971979
""" media_type : css_media_type
972980
| css_media_type t_ws

test/css/css-variables.css

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
:root {
2+
--bg: #e0e0e0;
3+
}
4+
p {
5+
--bg: white;
6+
}
7+
* {
8+
background: var(--bg);
9+
color: var(--fg,var(--text-fg,black));
10+
}
11+
div {
12+
--bg: red;
13+
}
14+
div span {
15+
--bg: purple;
16+
background: var(--bg);
17+
}

test/css/css-variables.min.css

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:root{--bg:#e0e0e0;}
2+
p{--bg:white;}
3+
*{background:var(--bg);color:var(--fg,var(--text-fg,black));}
4+
div{--bg:red;}
5+
div span{--bg:purple;background:var(--bg);}

test/less/css-variables.less

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
:root {
2+
--bg: #e0e0e0;
3+
}
4+
5+
p {
6+
--bg: white;
7+
}
8+
9+
* {
10+
background: var(--bg);
11+
color: var(--fg, var(--text-fg, black));
12+
}
13+
14+
div {
15+
--bg: red;
16+
17+
span {
18+
--bg: purple;
19+
background: var(--bg);
20+
}
21+
}

0 commit comments

Comments
 (0)