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
+32 -9
View File
@@ -7,6 +7,7 @@ independent decoration to the final text. Raw markup and entities are retained.
from html.parser import HTMLParser
MARKER = '/* mdhelper:leaf-decorations */'
DYNAMIC_MARKER = '/* mdhelper:dynamic-decorations */'
def wrap_decoration_text(body):
@@ -51,6 +52,10 @@ def decoration_css(rules, inherited, selectors, source):
return source
root='.reader[data-mdh-leaf-decoration]'
parts=[MARKER, root+' { --mdh-line-underline: ; --mdh-line-strike: ; }']
# CSS revert uses user-agent decoration defaults, not our authored theme.
parts.extend([':where('+root+' *) { --mdh-default-underline: ; --mdh-default-strike: ; }',
':where('+root+' a:any-link, '+root+' u) { --mdh-default-underline: underline; }',
':where('+root+' s, '+root+' del) { --mdh-default-strike: line-through; }'])
suppress=[]
for key,selector in selectors.items():
if key == 'code_block': continue # Code blocks have no Markdown inline nesting.
@@ -66,13 +71,14 @@ 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-mode: initial; --mdh-decoration-raw: initial; --mdh-line-'+field+': '+(token if rule[field] else '')+'; }')
parts.append(scope+' { --mdh-line-'+field+': '+(token if rule[field] else '')+
'; --mdh-override-'+field+': '+str(rule[field]).lower()+'; }')
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; }')
# 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.
# Let the browser resolve it on text leaves. Independent core overrides
# are applied after the browser parses variable shorthand values.
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); } }')
@@ -83,7 +89,9 @@ def _decoration_variables(source):
"""Keep authored selectors, conditions, priority and decoration appearance."""
import tinycss2 as css
from .style_sync import DECORATION_EXPRESSION, _invalid_core_value
dynamic = False
def visit(nodes):
nonlocal dynamic
output=[]
for declaration in nodes:
if declaration.type=='error': continue
@@ -98,22 +106,37 @@ def _decoration_variables(source):
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};'))
output.extend(css.parse_declaration_list(f'--mdh-line-{field}:{token}{suffix};--mdh-override-{field}:{value}{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):
reset = {'--mdh-decoration-mode':'initial', '--mdh-decoration-raw':'initial',
'--mdh-decoration-shorthand':'initial', '--mdh-override-underline':'initial', '--mdh-override-strike':'initial'}
if value.lower() in ('inherit', 'revert-layer'):
values = {key:value.lower() for key in (*reset, '--mdh-line-underline', '--mdh-line-strike')}
elif value.lower() in ('initial','unset','revert'):
values = {**reset, '--mdh-line-underline':'', '--mdh-line-strike':''}
if value.lower() == 'revert':
values.update({'--mdh-line-'+field:'var(--mdh-default-'+field+')' for field in ('underline','strike')})
elif 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 '',
values={**reset, '--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)
if 'overline' in lines:
values.update({'--mdh-decoration-mode':'raw-line', '--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}
values={'--mdh-decoration-mode':'raw-line','--mdh-decoration-raw':value}
elif declaration.lower_name=='text-decoration':
values={**reset, '--mdh-line-underline':'', '--mdh-line-strike':''}
else:continue
if values.get('--mdh-decoration-mode') in ('raw','raw-line'):
dynamic = True
values.update({'--mdh-override-underline':'initial', '--mdh-override-strike':'initial'})
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)))
result = css.serialize(visit(css.parse_stylesheet(source)))
return (DYNAMIC_MARKER+'\n' if dynamic else '') + result