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
+35 -27
View File
@@ -13,7 +13,7 @@ from lxml import etree as ET
from .document_style import resolved_rules, normalize_rule, font_points, length_points, number
from .style_catalog import CATALOG, CHARACTER_DEFAULTS, explicit_rules, parent_style, style_name, legacy_base
from .docx_style import W, NS, child, val, properties, _run_properties, _paragraph_properties, _order_properties
from .docx_inline import source_style_order, ordered_styles, LIST_PARAGRAPH_STYLE, QUOTED_LIST_PARAGRAPH_STYLE, SOURCE_PARAGRAPH_STYLES, CONTINUATION, INNER_QUOTE_STYLE
from .docx_inline import source_style_order, ordered_styles, LIST_PARAGRAPH_STYLE, QUOTED_LIST_PARAGRAPH_STYLE, SOURCE_PARAGRAPH_STYLES, CONTINUATION, INNER_QUOTE_STYLE, LIST_TARGET, LIST_TARGETS, SOURCE_LIST_STYLES
from .table_style import HEADER_COLOR, STRIPE_COLOR, BORDER_COLOR, VERTICAL_BORDER_COLOR
RUN_FIELDS = 'rFonts sz szCs b bCs i iCs color spacing u strike shd'.split()
@@ -327,6 +327,29 @@ class WordStyles:
child(style, 'pPr').append(ind)
return identifier
def contextual_list_rule(self, target, own, level):
"""Compose a real inner paragraph with its list container geometry."""
base = self.effective[target]
rule = {**base, **own}
for side in ('left', 'right'):
field, unit = f'{side}_indent_value', f'{side}_indent_unit'
if field not in own and base[unit] == 'em' and font_points(base) == font_points(rule):
if side == 'left': rule[field] += level * number(base.get('level_indent', 36)) / font_points(base)
continue
rule[field] = length_points(base[field], base[unit], font_points(base))
if field in own:
rule[field] += length_points(own[field], own.get(unit, 'pt'), font_points(rule))
if side == 'left': rule[field] += level * number(base.get('level_indent', 36))
rule[unit] = 'pt'
# text-indent is inherited as a computed length. An inner p's font
# must not resize an em indent declared on li. Absolute hanging units
# also stop Word's character indent from flattening nested levels.
if rule['indent_mode'] in ('hanging', 'first') and rule.get('indent_unit', 'em') == 'em' and (rule['left_indent_unit'] != 'em' or font_points(rule) != font_points(base)):
origin = rule if 'indent_mode' in own else base
rule['indent_value'] = length_points(rule['indent_value'], 'em', font_points(origin))
rule['indent_unit'] = 'pt'
return rule
def list_paragraph(self, target, base_id, level=0):
"""A loose Markdown list contains a real p element; a tight one does not."""
own = normalize_rule(self.rules.get('_list_paragraph'))
@@ -335,25 +358,14 @@ class WordStyles:
if identifier not in self.created:
style = self.new(identifier, 'paragraph', 'MDHelper ' + style_name(target, self.rules) + '(段落)' + base_id.removeprefix(style_id(target)), base_id)
base = self.effective[target]
rule = {**base, **own}
# A p's margins are inside its li, so they add to the list's
# indentation rather than replacing it (including nested levels).
for side in ('left', 'right'):
field, unit = f'{side}_indent_value', f'{side}_indent_unit'
if field in own:
rule[field] = length_points(base[field], base[unit], font_points(base)) + length_points(own[field], own.get(unit, 'pt'), font_points(rule))
if side == 'left': rule[field] += level * number(base.get('level_indent', 36))
rule[unit] = 'pt'
if 'left_indent_value' in own and rule['indent_mode'] == 'hanging' and rule.get('indent_unit', 'em') == 'em':
# Word otherwise substitutes the character hanging width for
# this absolute left margin, flattening nested list levels.
rule['indent_value'] = length_points(rule['indent_value'], 'em', font_points(rule))
rule['indent_unit'] = 'pt'
rule = self.contextual_list_rule(target, own, level)
authored = {**own, **{field: rule[field] for field in ('left_indent_unit', 'right_indent_unit', 'indent_unit')
if rule.get(field) != base.get(field)}}
for tag, writer in (('pPr', _paragraph_properties), ('rPr', _run_properties)):
props, old = child(style, tag), ET.Element(W + tag)
writer(props, rule); writer(old, base)
if tag == 'rPr': remove_fields(props, ('shd',)); remove_fields(old, ('shd',))
_delta(props, old, own)
_delta(props, old, authored)
return identifier
def quoted_list(self, target, base_id, level):
@@ -363,22 +375,12 @@ class WordStyles:
quote_id = self.paragraph('blockquote')
style = self.new(identifier, 'paragraph', 'MDHelper ' + style_name(target, self.rules) + '(引用内段落)' + (str(level + 1) if level else ''), quote_id)
own = self.explicit['blockquote']
rule = {**self.effective[target], **normalize_rule(self.rules.get('_list_paragraph')), **own}
# Container typography is inherited by li; a declaration on li
# wins over that inherited value. A direct blockquote p rule wins
# again. Preserve this distinction from the original CSS cascade.
overrides = {field: value for field, value in self.explicit[target].items()
if field in self.rules.get('_quote_container', {})}
rule.update(overrides)
# Keep the list's marker layout as context-specific paragraph
# properties; quote typography remains inherited from its style.
if level:
points = font_points(rule)
offset = level * number(rule.get('level_indent', 36))
if rule['left_indent_unit'] == 'em': rule['left_indent_value'] += offset / points
else:
rule['left_indent_value'] = length_points(rule['left_indent_value'], rule['left_indent_unit'], points) + offset
rule['left_indent_unit'] = 'pt'
rule = self.contextual_list_rule(target, {**normalize_rule(self.rules.get('_list_paragraph')), **own, **overrides}, level)
for tag, writer in (('pPr', _paragraph_properties), ('rPr', _run_properties)):
props, old = child(style, tag), ET.Element(W + tag)
writer(props, rule); writer(old, self.effective['blockquote'])
@@ -486,11 +488,17 @@ class WordStyles:
def apply(self, document):
for paragraph in document.iter(W + 'p'):
continuation = paragraph.attrib.pop(CONTINUATION, '') == '1'
list_target = paragraph.attrib.pop(LIST_TARGET, '')
ppr = properties(paragraph, 'pPr')
original_id = reference(ppr, 'pStyle') or 'Normal'
names = {reference(style, 'name') for style in self.chain(original_id)}
for name in tuple(names):
if name in SOURCE_LIST_STYLES:
base, list_target = SOURCE_LIST_STYLES[name]
names.add(base)
loose = bool(names & set(SOURCE_PARAGRAPH_STYLES))
target = self.classify(paragraph, original_id)
if target.startswith('list_') and list_target in LIST_TARGETS: target = list_target
identifier = self.paragraph(target)
if continuation and target == 'blockquote': remove_fields(ppr, ('numPr',))
if target.startswith('list_'):