"""CSS reset/grammar parity, graphical roundtrips and ancestor suppression.""" import pytest from PySide6.QtCore import Qt from mdhelper.style_sync import normalize_payload, analyze_css, patch_core from mdhelper.style_dialog import DocumentStyleEditorDialog from mdhelper.docx_exporter import export_markdown_to_docx from mdhelper.docx_style import W, NS from test_style_regressions import browser, parts from docx_assertions import effective_document @pytest.mark.parametrize('declaration', ['text-decoration:unset','text-decoration-line:UNSET','text-decoration:initial']) def test_resets_disable_link_and_strike_core_styles(qt_application,tmp_path,declaration): payload=normalize_payload({'css':f'a{{{declaration}}} s{{{declaration}}}'}) for key in ('link','strike'): assert payload['rules'][key]['underline'] is False and payload['rules'][key]['strike'] is False assert normalize_payload(payload)==payload assert browser(qt_application,tmp_path,payload,'[Link](#x) ~~Strike~~', "[...document.querySelectorAll('a,s')].map(e=>getComputedStyle(e.querySelector('[data-mdh-text]')||e).textDecorationLine)")==['none','none'] @pytest.mark.parametrize('engine', ['builtin','python-docx','pandoc']) def test_css_resets_and_uppercase_reach_word(tmp_path,engine): payload=normalize_payload({'css':'a{text-decoration:unset}s{text-decoration:unset}strong{text-decoration:UNDERLINE}p{line-height:NORMAL}'}) doc,styles=parts(export_markdown_to_docx('[Link](#x) ~~Strike~~ **Bold**',tmp_path/'reset.docx',engine=engine,style_rules=payload['rules'])) rendered=effective_document(doc,styles) for r in rendered.iter(W+'r'): text=''.join(r.itertext()) if text in ('Link','Strike','Bold'): assert r.find('w:rPr/w:u',NS).get(W+'val')==('single' if text=='Bold' else 'none') if text=='Strike': assert r.find('w:rPr/w:strike',NS).get(W+'val')=='0' assert rendered.find('.//w:pPr/w:spacing',NS).get(W+'line')=='240' @pytest.mark.parametrize('invalid', ['italic 30pt inherit','italic 30pt Arial, unset','oblique 200deg 30pt Arial','oblique -200deg 30pt Arial']) def test_invalid_font_keeps_previous_declarations(qt_application,tmp_path,invalid): payload=normalize_payload({'css':f'p{{font-size:20pt;font-style:normal;font:{invalid}}}'}) assert payload['rules']['paragraph']['font_size']==20 and payload['rules']['paragraph']['italic'] is False assert any('已忽略无效' in note for note in analyze_css(payload['css']).notes) result=browser(qt_application,tmp_path,payload,'Text',"[getComputedStyle(document.querySelector('p')).fontSize,getComputedStyle(document.querySelector('p')).fontStyle]") assert abs(float(result[0][:-2])-20*96/72)<.01 and result[1]=='normal' def test_uppercase_and_reset_controls_roundtrip(qt_application): dialog=DocumentStyleEditorDialog({'css':'p{text-decoration:UNDERLINE;line-height:NORMAL;letter-spacing:NORMAL}'}) try: assert dialog.bool_boxes['underline'].checkState()==Qt.CheckState.Checked dialog.bool_boxes['underline'].setCheckState(Qt.CheckState.Unchecked) assert analyze_css(dialog.style_payload()['css']).rules['paragraph']['underline'] is False dialog.extra_css_edit.setPlainText('p{text-decoration:UNSET}') assert dialog.style_payload()['rules']['paragraph']['underline'] is False assert dialog.bool_boxes['underline'].checkState()==Qt.CheckState.Unchecked finally:dialog.close() @pytest.mark.parametrize('selector', ['#mdhelper-reader a','#mdhelper-reader.reader a',':is(#mdhelper-reader) a']) def test_high_specificity_important_ancestor_does_not_paint_over_inner_toggle(qt_application,tmp_path,selector): css=selector+'{text-decoration:underline line-through!important}' css=patch_core(css,'emphasis',{}, {'underline':False}) payload=normalize_payload({'css':css}) result=browser(qt_application,tmp_path,payload,'[Outer *Inner*](#x)', "[getComputedStyle(document.querySelector('a')).textDecorationLine,...[...document.querySelectorAll('[data-mdh-text]')].map(e=>getComputedStyle(e).textDecorationLine)]") assert result==['none','underline line-through','line-through']