fix: preserve CSS resets and independent variable decorations

This commit is contained in:
2026-11-05 11:10:56 +08:00
parent f36bd3199c
commit 0a9aeb6aa3
5 changed files with 238 additions and 22 deletions
+37 -12
View File
@@ -174,6 +174,7 @@ def _declaration(name, value, important=False):
def _expand(declaration):
"""Expand margins while keeping individual tokens (including calc/var)."""
if declaration.type == "declaration" and declaration.lower_name == "font":
if _invalid_core_value('font', declaration.value): return [declaration]
tokens = [t for t in declaration.value if t.type not in ("whitespace", "comment")]
size_index = next((i for i, t in enumerate(tokens) if t.type == "dimension" and t.lower_unit in ("pt", "px")), None)
if size_index is None:
@@ -274,11 +275,22 @@ def _invalid_core_value(prop, tokens):
prefix_names = {'normal','italic','oblique','small-caps','bold','bolder','lighter',
'ultra-condensed','extra-condensed','condensed','semi-condensed',
'semi-expanded','expanded','extra-expanded','ultra-expanded'}
for token in tokens[:size_index]:
if token.type=='ident' and token.lower_value in prefix_names: continue
if token.type=='number' and 1 <= token.value <= 1000: continue
if token.type=='dimension' and token.lower_unit in ('deg','grad','rad','turn'): continue
return True
groups, normal = set(), 0
for index, token in enumerate(tokens[:size_index]):
if token.type == 'ident' and token.lower_value == 'normal':
normal += 1; continue
if token.type == 'ident' and token.lower_value in prefix_names:
name = token.lower_value
group = 'style' if name in ('italic','oblique') else 'variant' if name == 'small-caps' else 'weight' if name in ('bold','bolder','lighter') else 'stretch'
elif token.type == 'number' and 1 <= token.value <= 1000:
group = 'weight'
elif token.type == 'dimension' and token.lower_unit in ('deg','grad','rad','turn'):
if not index or getattr(tokens[index-1], 'lower_value', '') != 'oblique': return True
continue
else: return True
if group in groups: return True
groups.add(group)
if len(groups) + normal > 4: return True
end = size_index + 1
if end < len(tokens) and tokens[end].type=='literal' and tokens[end].value=='/':
end += 2
@@ -297,14 +309,21 @@ def _invalid_core_value(prop, tokens):
selected = [name for name in names if name in lines]
if 'none' in selected and len(selected)>1 or len(selected)!=len(set(selected)): return True
if prop == 'text-decoration-line': return any(t.type!='ident' or t.lower_value not in lines for t in tokens)
groups = set()
for token in tokens:
if token.type=='ident' and token.lower_value in lines | {'solid','double','dotted','dashed','wavy','auto','from-font'}: continue
if token.type=='function':
if _invalid_core_value('color', [token]): return True
continue # Includes variable colors/thickness.
if parse_color(token) is not None: continue
if token.type in ('dimension','percentage','number') and not _invalid_core_value('font-size',[token]): continue
return True
if token.type=='ident' and token.lower_value in lines: continue
if token.type=='ident' and token.lower_value in {'solid','double','dotted','dashed','wavy'}:
group = 'style'
elif token.type=='ident' and token.lower_value in {'auto','from-font'}:
group = 'thickness'
elif token.type=='function':
group = 'thickness' if token.lower_name in ('calc','min','max','clamp') else 'color'
if _invalid_core_value('font-size' if group == 'thickness' else 'color', [token]): return True
elif parse_color(token) is not None: group = 'color'
elif token.type in ('dimension','percentage','number') and not _invalid_core_value('font-size',[token]): group = 'thickness'
else: return True
if group in groups: return True
groups.add(group)
if len(tokens) != 1: return False
token = tokens[0]
if token.type == 'string' and prop != 'font-family' and not prop.startswith('--'): return True
@@ -455,6 +474,12 @@ class _CoreRuleReader:
return self.rule
def decoration(self, value):
if value.lower() in ('initial', 'unset'):
self.rule.update(underline=False, strike=False)
return
if value.lower() == 'revert':
self.rule.update(underline=self.target == 'link', strike=self.target == 'strike')
return
if re.sub(r'\s+', '', value) == re.sub(r'\s+', '', DECORATION_EXPRESSION):
for key in ('underline', 'strike'):
self.choice('--mdhelper-' + key, key, {'true': True, 'false': False})