Skip to content

Commit

Permalink
Merge pull request lesscpy#117 from lephe/master
Browse files Browse the repository at this point in the history
  • Loading branch information
saschpe authored Jul 13, 2021
2 parents 47742f2 + 5070104 commit 4944323
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
3 changes: 3 additions & 0 deletions lesscpy/less.ast
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@

property : css_property
| css_vendor_property
| css_user_property
| css_ident

style_list : style_group
Expand All @@ -89,6 +90,7 @@
| css_string
| istring
| css_vendor_property
| css_user_property
| css_property
| css_ident
| '~' istring
Expand Down Expand Up @@ -136,6 +138,7 @@ factor : color
| css_ident
| css_id
| css_uri
| css_user_property
| '='

istring : less_string
Expand Down
23 changes: 13 additions & 10 deletions lesscpy/lessc/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ class LessLexer:
literals = '<>=%!/*-+&'
tokens = [
'css_ident', 'css_dom', 'css_class', 'css_id', 'css_property',
'css_vendor_property', 'css_comment', 'css_string', 'css_color',
'css_filter', 'css_number', 'css_important', 'css_vendor_hack',
'css_uri', 'css_ms_filter', 'css_keyframe_selector', 'css_media_type',
'css_media_feature', 't_and', 't_not', 't_only', 'less_variable',
'less_comment', 'less_open_format', 'less_when', 'less_and',
'less_not', 't_ws', 't_popen', 't_pclose', 't_semicolon', 't_tilde',
't_colon', 't_comma', 't_eopen', 't_eclose', 't_isopen', 't_isclose',
't_bopen', 't_bclose'
'css_vendor_property', 'css_user_property', 'css_comment',
'css_string', 'css_color', 'css_filter', 'css_number', 'css_important',
'css_vendor_hack', 'css_uri', 'css_ms_filter', 'css_keyframe_selector',
'css_media_type', 'css_media_feature', 't_and', 't_not', 't_only',
'less_variable', 'less_comment', 'less_open_format', 'less_when',
'less_and', 'less_not', 't_ws', 't_popen', 't_pclose', 't_semicolon',
't_tilde', 't_colon', 't_comma', 't_eopen', 't_eclose', 't_isopen',
't_isclose', 't_bopen', 't_bclose'
]
tokens += list(set(reserved.tokens.values()))
# Tokens with significant following whitespace
significant_ws = {'css_class', 'css_id', 'css_dom', 'css_property', 'css_vendor_property', 'css_ident',
significant_ws = {'css_class', 'css_id', 'css_dom', 'css_property', 'css_vendor_property', 'css_user_property', 'css_ident',
'css_number', 'css_color', 'css_media_type', 'css_filter', 'less_variable', 't_and', 't_not',
't_only', '&'}
significant_ws.update(reserved.tokens.values())
Expand Down Expand Up @@ -87,7 +87,7 @@ def t_css_number(self, t):
return t

def t_css_ident(self, t):
(r'([\-\.\#]?'
(r'((\-|\.|\#|\-\-)?'
'([_a-z]'
'|[\200-\377]'
'|\\\[0-9a-f]{1,6}'
Expand Down Expand Up @@ -132,6 +132,9 @@ def t_css_ident(self, t):
# DOM elements can't be part of property declarations, avoids ambiguity between 'rect' DOM
# element and rect() CSS function.
t.type = 'css_dom'
elif v.startswith("--"):
t.type = 'css_user_property'
t.lexer.in_property_decl = True
elif c == '-':
t.type = 'css_vendor_property'
t.lexer.in_property_decl = True
Expand Down
8 changes: 8 additions & 0 deletions lesscpy/lessc/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ def p_prop_open_ie_hack(self, p):
def p_prop_open(self, p):
""" prop_open : property t_colon
| vendor_property t_colon
| user_property t_colon
| word t_colon
"""
p[0] = (p[1][0], '')
Expand Down Expand Up @@ -771,6 +772,7 @@ def p_argument(self, p):
| word
| id
| css_uri
| css_user_property
| '='
| fcall
"""
Expand Down Expand Up @@ -967,6 +969,12 @@ def p_vendor_property(self, p):
"""
p[0] = tuple(list(p)[1:])

def p_user_property(self, p):
""" user_property : css_user_property
| css_user_property t_ws
"""
p[0] = tuple(list(p)[1:])

def p_media_type(self, p):
""" media_type : css_media_type
| css_media_type t_ws
Expand Down
17 changes: 17 additions & 0 deletions test/css/css-variables.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
:root {
--bg: #e0e0e0;
}
p {
--bg: white;
}
* {
background: var(--bg);
color: var(--fg,var(--text-fg,black));
}
div {
--bg: red;
}
div span {
--bg: purple;
background: var(--bg);
}
5 changes: 5 additions & 0 deletions test/css/css-variables.min.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:root{--bg:#e0e0e0;}
p{--bg:white;}
*{background:var(--bg);color:var(--fg,var(--text-fg,black));}
div{--bg:red;}
div span{--bg:purple;background:var(--bg);}
21 changes: 21 additions & 0 deletions test/less/css-variables.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
:root {
--bg: #e0e0e0;
}

p {
--bg: white;
}

* {
background: var(--bg);
color: var(--fg, var(--text-fg, black));
}

div {
--bg: red;

span {
--bg: purple;
background: var(--bg);
}
}

0 comments on commit 4944323

Please sign in to comment.