Files
MDHelper/tests/test_style_revision_054.py
T

77 lines
4.3 KiB
Python

"""Regressions from the 0.5.3 style and table audit."""
import pytest
import zipfile
from lxml import etree as ET
from mdhelper.docx_exporter import export_markdown_to_docx
from mdhelper.docx_style import W, NS
from mdhelper.style_sync import normalize_payload
from test_style_regressions import browser, parts
from docx_assertions import effective_document
ENGINES = ('builtin', 'python-docx', 'pandoc')
TABLE = '| Head | Description |\n|---|---|\n| Alpha | First |\n| Beta | Second |'
BACKGROUND_RULES = {'h1': {'background_color': '#ff0000'},
'table_head': {'background_color': '#0000ff'}, 'table_cell': {'background_color': '#00ff00'},
'_metadata': {'table_head': {'based_on': 'h1'}, 'table_cell': {'based_on': 'h1'}}}
CONTINUATION_SOURCE = '1. First\n\n Continuation\n\n2. Second'
NESTED_SOURCE = '- Outer\n\n - Inner\n\n - NestedSecond\n\n- Second'
NESTED_RULES = normalize_payload({'css': 'p {margin-left:12pt;} li {margin-left:24pt;}'})['rules']
CODE_SOURCE = '`Inline`\n\n```text\nBlock\n```'
CODE_RULES = {'code': {'font_color': '#ff0000', 'font_size': 20, 'font_size_unit': 'pt'}}
def test_copy_button_has_continuous_pointer_path(qt_application, tmp_path):
assert browser(qt_application, tmp_path, normalize_payload({}), TABLE, """(()=>{
const button=document.querySelector('.copy-table-button');button.focus();
const b=button.getBoundingClientRect(),t=document.querySelector('table').getBoundingClientRect();
const tools=document.querySelector('.table-tools').getBoundingClientRect();
return tools.bottom >= t.top && tools.top <= b.top;
})()""")
@pytest.mark.parametrize('engine', ENGINES)
def test_table_background_cancels_paragraph_parent_shading(tmp_path, engine):
doc, styles = parts(export_markdown_to_docx(TABLE,tmp_path/'shade.docx',engine=engine,style_rules=BACKGROUND_RULES))
for p in effective_document(doc,styles).iter(W+'p'):
assert p.find('w:pPr/w:shd',NS).get(W+'fill') == 'auto'
for key, color in (('firstRow','0000ff'),('band1Horz','00ff00')):
assert styles.find(f"w:style[@w:styleId='MDHelperTable']/w:tblStylePr[@w:type='{key}']/w:tcPr/w:shd",NS).get(W+'fill') == color
assert not doc.findall('.//w:tcPr/w:shd',NS)
@pytest.mark.parametrize('engine', ENGINES)
def test_list_continuation_does_not_consume_a_number(tmp_path, engine):
path=export_markdown_to_docx(CONTINUATION_SOURCE,tmp_path/'list.docx',engine=engine)
doc, styles = parts(path)
with zipfile.ZipFile(path) as z: numbering=ET.fromstring(z.read('word/numbering.xml'))
numbered=[]
for p in effective_document(doc,styles).iter(W+'p'):
num=p.find('w:pPr/w:numPr',NS)
if num is None: continue
nid=num.find(W+'numId').get(W+'val')
if nid=='0': continue
aid=numbering.find(f"w:num[@w:numId='{nid}']/w:abstractNumId",NS).get(W+'val')
level=num.find(W+'ilvl').get(W+'val')
lvl=numbering.find(f"w:abstractNum[@w:abstractNumId='{aid}']/w:lvl[@w:ilvl='{level}']",NS)
if lvl.find(W+'numFmt').get(W+'val')!='none' and lvl.find(W+'lvlText').get(W+'val').strip():
numbered.append(''.join(p.itertext()))
assert numbered == ['First','Second']
assert not any('{urn:mdhelper:source}continuation' in p.attrib for p in doc.iter(W+'p'))
@pytest.mark.parametrize('engine', ENGINES)
def test_loose_nested_list_adds_inner_paragraph_margin(tmp_path, engine):
doc, styles = parts(export_markdown_to_docx(NESTED_SOURCE,tmp_path/'nested.docx',engine=engine,style_rules=NESTED_RULES))
indents = {''.join(p.itertext()): int(p.find('w:pPr/w:ind',NS).get(W+'left'))/20 for p in effective_document(doc,styles).iter(W+'p')}
assert indents['Outer'] == 36 and indents['Inner'] == 72
@pytest.mark.parametrize('engine', ENGINES)
def test_legacy_code_uses_linked_editable_word_base(tmp_path, engine):
_, styles = parts(export_markdown_to_docx(CODE_SOURCE,tmp_path/'code.docx',engine=engine,style_rules=CODE_RULES))
assert styles.find("w:style[@w:styleId='MDHelperCode']/w:link",NS).get(W+'val') == 'MDHelperCodeChar'
assert styles.find("w:style[@w:styleId='MDHelperCodeChar']/w:link",NS).get(W+'val') == 'MDHelperCode'
inline = styles.find("w:style[@w:styleId='MDHelperInlineCode']",NS)
assert inline.find(W+'basedOn').get(W+'val') == 'MDHelperCodeChar'
assert inline.find('w:rPr/w:color',NS) is None