fix: preserve list context and nested CSS decoration behavior

This commit is contained in:
2026-11-05 09:49:52 +08:00
parent d0f95d277a
commit 77ee046463
10 changed files with 284 additions and 79 deletions
+42 -40
View File
@@ -47,7 +47,7 @@ def decoration_css(rules, inherited, selectors, source):
from .style_catalog import CHARACTER_DEFAULTS, CATALOG
if not any(CATALOG.get(key) and CATALOG[key].kind == 'character' and
any(rule.get(field) is False for field in ('underline','strike'))
for key,rule in rules.items() if isinstance(rule,dict)):
for key,rule in {key: {**inherited.get(key, {}), **rules.get(key, {})} for key in CATALOG}.items()):
return source
root='.reader[data-mdh-leaf-decoration]'
parts=[MARKER, root+' { --mdh-line-underline: ; --mdh-line-strike: ; }']
@@ -66,52 +66,54 @@ def decoration_css(rules, inherited, selectors, source):
# Defaults have zero specificity; authored conditional selectors
# can continue to change decorations (including hover effects).
scope=selector if field in own else ':where('+selector+')'
parts.append(scope+' { --mdh-decoration-raw: initial; --mdh-line-'+field+': '+(token if rule[field] else '')+'; }')
parts.append(scope+' { --mdh-decoration-mode: initial; --mdh-decoration-raw: initial; --mdh-line-'+field+': '+(token if rule[field] else '')+'; }')
parts.append(_decoration_variables(source))
parts.append(', '.join(suppress)+' { text-decoration-line: none !important; }')
parts.append(':where('+root+' p, '+root+' strong, '+root+' em, '+root+' a, '+root+' s, '+root+' del, '+root+' code) { text-decoration-color: inherit; text-decoration-style: inherit; text-decoration-thickness: inherit; }')
parts.append(root+' [data-mdh-text] { text-decoration-line: var(--mdh-decoration-raw, var(--mdh-line-underline) var(--mdh-line-strike)); text-decoration-color: inherit; text-decoration-style: inherit; text-decoration-thickness: inherit; }')
# A variable may contain a complete shorthand (including color/style).
# Let the browser resolve it on text leaves; an inner core rule resets
# the inherited mode so it can still cancel decorations independently.
leaf=root+' [data-mdh-text]'
parts.append(leaf+' { text-decoration-line: var(--mdh-decoration-raw, var(--mdh-line-underline) var(--mdh-line-strike)); text-decoration-color: inherit; text-decoration-style: inherit; text-decoration-thickness: inherit; }')
parts.append('@container style(--mdh-decoration-mode: raw) { '+leaf+' { text-decoration: var(--mdh-decoration-shorthand); } }')
return '\n'.join(parts)
def _decoration_variables(source):
"""Keep authored selectors, conditions, priority and decoration appearance."""
import tinycss2 as css
from .style_sync import DECORATION_EXPRESSION
from .style_sync import DECORATION_EXPRESSION, _invalid_core_value
def visit(nodes):
for node in nodes:
if node.type=='at-rule' and node.content is not None and node.lower_at_keyword in ('media','supports','layer','container'):
children=css.parse_blocks_contents(node.content);visit(children)
node.content=css.parse_component_value_list(css.serialize(children))
elif node.type=='qualified-rule':
declarations=css.parse_blocks_contents(node.content,skip_whitespace=False,skip_comments=False)
visit(declarations)
output=[]
for declaration in declarations:
# Invalid declarations have no serializable representation.
# Keep valid siblings, including nested qualified rules.
if declaration.type=='error': continue
output.append(declaration)
if declaration.type!='declaration':continue
value=css.serialize(declaration.value).strip()
suffix=' !important' if declaration.important else ''
if declaration.name in ('--mdhelper-underline','--mdhelper-strike') and value in ('true','false'):
field=declaration.name.removeprefix('--mdhelper-')
token=('underline' if field=='underline' else 'line-through') if value=='true' else ''
output.extend(css.parse_declaration_list(f'--mdh-decoration-raw:initial{suffix};--mdh-line-{field}:{token}{suffix};'))
continue
if declaration.lower_name not in ('text-decoration','text-decoration-line'):continue
if value==DECORATION_EXPRESSION:continue
tokens=[t.lower_value for t in declaration.value if t.type=='ident']
lines=set(tokens)&{'none','underline','line-through','overline'}
if lines and not ('none' in lines and len(lines)>1):
values={'--mdh-decoration-raw':'initial','--mdh-line-underline':'underline' if 'underline' in lines else '',
'--mdh-line-strike':'line-through' if 'line-through' in lines else ''}
if 'overline' in lines:values['--mdh-decoration-raw']=' '.join(x for x in ('underline','line-through','overline') if x in lines)
elif declaration.lower_name=='text-decoration-line' and any(t.type=='function' for t in declaration.value):
values={'--mdh-decoration-raw':value}
else:continue
output.extend(css.parse_declaration_list(''.join(f'{prop}:{val}{suffix};' for prop,val in values.items())))
node.content=css.parse_component_value_list(css.serialize(output))
nodes=css.parse_stylesheet(source);visit(nodes)
return css.serialize(nodes)
output=[]
for declaration in nodes:
if declaration.type=='error': continue
if declaration.type=='qualified-rule' or (declaration.type=='at-rule' and declaration.content is not None
and declaration.lower_at_keyword in ('media','supports','layer','container')):
children=css.parse_blocks_contents(declaration.content,skip_whitespace=False,skip_comments=False)
declaration.content=css.parse_component_value_list(css.serialize(visit(children)))
output.append(declaration)
if declaration.type!='declaration': continue
value=css.serialize(declaration.value).strip()
suffix=' !important' if declaration.important else ''
if declaration.name in ('--mdhelper-underline','--mdhelper-strike') and value in ('true','false'):
field=declaration.name.removeprefix('--mdhelper-')
token=('underline' if field=='underline' else 'line-through') if value=='true' else ''
output.extend(css.parse_declaration_list(f'--mdh-decoration-mode:initial{suffix};--mdh-decoration-raw:initial{suffix};--mdh-line-{field}:{token}{suffix};'))
continue
if declaration.lower_name not in ('text-decoration','text-decoration-line'):continue
if value==DECORATION_EXPRESSION:continue
if _invalid_core_value(declaration.lower_name, declaration.value): continue
tokens=[t.lower_value for t in declaration.value if t.type=='ident']
lines=set(tokens)&{'none','underline','line-through','overline'}
if declaration.lower_name=='text-decoration' and any(t.type=='function' for t in declaration.value):
values={'--mdh-decoration-mode':'raw','--mdh-decoration-shorthand':value}
elif lines and not ('none' in lines and len(lines)>1):
values={'--mdh-decoration-mode':'initial','--mdh-decoration-raw':'initial','--mdh-line-underline':'underline' if 'underline' in lines else '',
'--mdh-line-strike':'line-through' if 'line-through' in lines else ''}
if 'overline' in lines:values['--mdh-decoration-raw']=' '.join(x for x in ('underline','line-through','overline') if x in lines)
elif declaration.lower_name=='text-decoration-line' and any(t.type=='function' for t in declaration.value):
values={'--mdh-decoration-mode':'initial','--mdh-decoration-raw':value}
else:continue
output.extend(css.parse_declaration_list(''.join(f'{prop}:{val}{suffix};' for prop,val in values.items())))
return output
return css.serialize(visit(css.parse_stylesheet(source)))