fix: protect editor content and harden document exports
Protect pending edits on window close, keep save-as paths and drafts consistent, copy relative image resources, reject oversized table imports without truncation, and preserve independent preview scrolling during refresh. Archive rendered MHTML resources with portable references and embedded local fonts. Preserve existing outputs on export cancellation or failure. Convert editor match positions and filter directory results in linear passes. Include the previously completed editor image/table/history improvements, undoable replacement, export snapshots, shared page settings, and cancellable PDF/Word export workflow. Update documentation and version to 0.3.1. Validation: 142 pytest cases passed; GUI smoke, improvements integration, and offline MHTML/scroll reliability smoke checks passed.
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
||||
os.environ.setdefault("QTWEBENGINE_DISABLE_SANDBOX", "1")
|
||||
os.environ.setdefault("QTWEBENGINE_CHROMIUM_FLAGS", "--disable-gpu --no-sandbox --log-level=3")
|
||||
|
||||
from pathlib import Path
|
||||
from urllib.parse import unquote
|
||||
import pytest
|
||||
from PySide6.QtCore import QMimeData, QUrl
|
||||
from PySide6.QtGui import QColor, QImage, QTextCursor
|
||||
from PySide6.QtWidgets import QApplication
|
||||
|
||||
from mdhelper.editor_assets import image_markdown, store_image
|
||||
from mdhelper.file_versions import VersionStore
|
||||
from mdhelper.markdown_editor import MarkdownEditorDialog
|
||||
from mdhelper.markdown_renderer import MarkdownRenderer
|
||||
from mdhelper.table_editor import format_table, parse_tsv, table_at_line
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def app():
|
||||
return QApplication.instance() or QApplication([])
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def editor(app, tmp_path):
|
||||
widget = MarkdownEditorDialog(tmp_path / "note.md", "😀 old old", MarkdownRenderer(tmp_path), "light", 1, "", 16)
|
||||
yield widget
|
||||
widget.preview_refresh_timer.stop(); widget.draft_save_timer.stop()
|
||||
widget.deleteLater(); app.processEvents()
|
||||
|
||||
|
||||
def test_replace_all_is_single_undo_and_keeps_earlier_edits(editor):
|
||||
cursor = editor.editor.textCursor(); cursor.movePosition(QTextCursor.MoveOperation.End)
|
||||
cursor.insertText(" earlier")
|
||||
editor.editor_find_box.setText("old")
|
||||
editor.editor_replace_box.setText(r"C:\new\test")
|
||||
editor.replace_all_in_editor()
|
||||
assert editor.source_text() == "😀 C:\\new\\test C:\\new\\test earlier"
|
||||
editor.editor.undo()
|
||||
assert editor.source_text() == "😀 old old earlier"
|
||||
editor.editor.undo()
|
||||
assert editor.source_text() == "😀 old old"
|
||||
editor.editor.redo(); editor.editor.redo()
|
||||
assert r"C:\new\test" in editor.source_text()
|
||||
|
||||
|
||||
def test_find_uses_utf16_positions(editor):
|
||||
editor.editor_find_box.setText("old")
|
||||
editor.find_next_in_editor()
|
||||
assert editor.editor.textCursor().selectedText() == "old"
|
||||
|
||||
|
||||
def test_restore_history_is_undoable(editor):
|
||||
editor.replace_source_undoable("# 历史内容")
|
||||
assert editor.has_unsaved_changes()
|
||||
editor.editor.undo()
|
||||
assert editor.source_text() == "😀 old old"
|
||||
|
||||
|
||||
def test_version_retention_and_unchanged_save(tmp_path):
|
||||
path = tmp_path / "note.md"
|
||||
path.write_text("zero", encoding="utf-8")
|
||||
store = VersionStore(tmp_path / "history", limit=3)
|
||||
for number in range(1, 6):
|
||||
store.save(path, str(number))
|
||||
assert [v.read_text() for v in store.versions(path)] == ["4", "3", "2"]
|
||||
previous = store.versions(path)
|
||||
store.save(path, "5")
|
||||
assert store.versions(path) == previous
|
||||
|
||||
|
||||
def test_failed_atomic_save_keeps_original_and_history(tmp_path, monkeypatch):
|
||||
from mdhelper import file_versions
|
||||
path = tmp_path / "note.md"; path.write_bytes(b"original\r\n")
|
||||
store = VersionStore(tmp_path / "history")
|
||||
replace = file_versions.os.replace
|
||||
def failure(source, destination):
|
||||
if Path(destination) == path:
|
||||
raise OSError("simulated sharing violation")
|
||||
replace(source, destination)
|
||||
monkeypatch.setattr(file_versions.os, "replace", failure)
|
||||
with pytest.raises(OSError):
|
||||
store.save(path, "changed")
|
||||
assert path.read_bytes() == b"original\r\n"
|
||||
assert store.versions(path)[0].read_bytes() == b"original\r\n"
|
||||
assert not list(tmp_path.glob("*.tmp"))
|
||||
|
||||
|
||||
def test_clipboard_image_and_file_drop_use_relative_assets(app, tmp_path):
|
||||
document = tmp_path / "报告 空格.md"
|
||||
image = QImage(16, 12, QImage.Format.Format_ARGB32); image.fill(QColor("red"))
|
||||
mime = QMimeData(); mime.setImageData(image)
|
||||
markdown = image_markdown(mime, document)
|
||||
url = markdown.split("](")[1][:-1]
|
||||
target = tmp_path / unquote(url)
|
||||
assert target.exists() and target.suffix == ".png"
|
||||
assert image_markdown(mime, document) == markdown
|
||||
assert len(list(target.parent.iterdir())) == 1
|
||||
files = QMimeData(); files.setUrls([QUrl.fromLocalFile(str(target))])
|
||||
assert image_markdown(files, document).endswith("](" + url + ")")
|
||||
assert "file:" not in markdown and "%20" in markdown
|
||||
|
||||
|
||||
def test_table_excel_multiline_escaped_pipe_and_alignment():
|
||||
rows = parse_tsv('Name\tValue\r\n"two\nlines"\t"a|b"\r\n')
|
||||
output = format_table(rows, ["left", "right"])
|
||||
assert "two<br>lines" in output and "a\\|b" in output
|
||||
assert "| :--- | ---: |" in output
|
||||
selected = table_at_line("before\n\n" + output + "\n\nafter", 3)
|
||||
assert selected is not None and len(selected.rows) == 2
|
||||
assert selected.alignments == ["left", "right"]
|
||||
assert format_table(selected.rows, selected.alignments) == output
|
||||
|
||||
|
||||
def test_source_map_is_only_in_editor_preview(tmp_path):
|
||||
renderer = MarkdownRenderer(tmp_path)
|
||||
source = "# Heading\n\nparagraph\n\n```python\nx = 1\n```\n"
|
||||
normal = renderer.render_source(source, tmp_path / "x.md")
|
||||
mapped = renderer.render_source(source, tmp_path / "x.md", source_map=True)
|
||||
assert '<h1 id="heading">' in normal
|
||||
assert 'data-source-line="2" data-source-end="3"' in mapped
|
||||
assert 'data-source-line="4" data-source-end="7"' in mapped
|
||||
Reference in New Issue
Block a user