Files
MDHelper/tests/test_semantic_style_controls.py

74 lines
4.3 KiB
Python

import json
import time
from pathlib import Path
from PySide6.QtCore import Qt
from PySide6.QtWebEngineCore import QWebEnginePage
from mdhelper.style_sync import analyze_css, patch_core, normalize_payload, render_css
from mdhelper.style_dialog import DocumentStyleEditorDialog
from mdhelper.markdown_renderer import MarkdownRenderer
def test_core_character_and_container_fields_roundtrip():
rule = dict(font_color='#123456', underline=True, strike=True, background_color='#ffff00')
source = patch_core('a:hover {color:red} /* retained */', 'link', {}, rule)
assert analyze_css(source).rules['link'] == rule
assert 'a:hover' in source and 'retained' in source
source = patch_core(source, 'table', {}, {'border_color': '#123456', 'border_width': 1, 'cell_padding': 5})
assert analyze_css(source).rules['table']['cell_padding'] == 5
source = patch_core(source, 'list_ordered', {}, {'list_marker': 'upper-roman', 'level_indent': 20})
assert analyze_css(source).rules['list_ordered']['level_indent'] == 20
assert normalize_payload(normalize_payload({'css': source}))['css'] == source
def test_css_for_inapplicable_word_style_types_is_preserved_with_explanation():
source = 'strong {margin-top: 3pt; color:red;} p {border-width:1pt; padding:4pt;}'
payload = normalize_payload({'css': source})
assert payload['css'] == source
assert 'space_before_value' not in payload['rules']['strong']
assert not payload['rules'].get('paragraph')
assert analyze_css(source).notes
def test_character_dialog_hides_paragraph_controls_and_preserves_other_types(qt_application):
dialog = DocumentStyleEditorDialog({'rules': {'paragraph': {'alignment': 'justify'}}})
try:
for index in range(dialog.target_list.count()):
if dialog.target_list.item(index).data(Qt.ItemDataRole.UserRole) == 'link': dialog.target_list.setCurrentRow(index); break
assert not dialog.form.isRowVisible(dialog.paragraph_row_start)
dialog.bool_boxes['underline'].setChecked(True)
dialog.background_edit.setText('#aabbcc'); dialog.background_edit.editingFinished.emit()
rules = dialog.style_payload()['rules']
assert rules['paragraph']['alignment'] == 'justify'
assert rules['link']['underline'] and rules['link']['background_color'] == '#aabbcc'
assert 'alignment' not in rules['link']
finally: dialog.close()
def test_browser_semantic_styles_and_contextual_character_inheritance(qt_application, tmp_path):
renderer = MarkdownRenderer(tmp_path)
payload = normalize_payload({'rules': {'h1': {'font_size': 30, 'font_size_unit': 'px'},
'link': {'font_color': '#123456', 'underline': True}, 'inline_code': {'background_color': '#ffeecc'},
'list_bullet': {'font_color': '#112233'}, 'list_ordered': {'font_color': '#445566'},
'table_head': {'font_color': '#778899'}}})
content = renderer.render_source('# Head **bold** [link](https://example.com)\n\n`code`\n\n- Bullet\n\n1. Number\n\n| Head |\n|---|\n| Cell |',
tmp_path / 'note.md', document_style_css=render_css(payload['css'], payload['rules']))
page = QWebEnginePage(); loaded = []
page.loadFinished.connect(loaded.append); page.setHtml(content)
def wait(condition):
deadline = time.monotonic() + 30
while not condition() and time.monotonic() < deadline: qt_application.processEvents(); time.sleep(.01)
assert condition()
try:
wait(lambda: bool(loaded)); values = []
page.runJavaScript('''JSON.stringify({h:getComputedStyle(document.querySelector('h1')).fontSize,
a:getComputedStyle(document.querySelector('h1 a')).fontSize,
strong:getComputedStyle(document.querySelector('h1 strong')).fontSize,
color:getComputedStyle(document.querySelector('h1 a')).color,
lists:[...document.querySelectorAll('li')].map(e=>getComputedStyle(e).color),
code:document.querySelector('p code').getAttribute('data-mdh-style')})''', values.append)
wait(lambda: bool(values)); result = json.loads(values[0])
assert result['a'] == result['strong'] == result['h'] == '30px'
assert result['color'] == 'rgb(18, 52, 86)' and result['lists'] == ['rgb(17, 34, 51)', 'rgb(68, 85, 102)']
assert result['code'] == 'inline_code'
finally: page.deleteLater(); qt_application.processEvents()