Files
mdpolish/tests/test_html_table_entities.py

76 lines
2.7 KiB
Python

from __future__ import annotations
import pytest
from mdpolish import Pipeline, RunStatus
from mdpolish.modifiers import html_table_entity_unescape
def transform(markdown: str): # type: ignore[no-untyped-def]
return Pipeline([html_table_entity_unescape()]).transform(markdown)
def test_unescapes_one_layer_only_in_strict_cell_text() -> None:
markdown = (
'<table data-note="&amp;lt;"><tr><td>&amp;lt;5</td><td>&amp;gt;2 &amp;amp; x &lt;</td></tr></table>'
"\noutside &amp;lt;"
)
result = transform(markdown)
assert result.status is RunStatus.SUCCESS
assert result.output_markdown == (
'<table data-note="&amp;lt;"><tr><td>&lt;5</td><td>&gt;2 &amp; x &lt;</td></tr></table>'
"\noutside &amp;lt;"
)
assert len(result.changes) == 3
assert result.modifiers[0].version == "1.0.1"
def test_multiple_tables_and_cells_report_source_order() -> None:
markdown = "<table><tr><td>&amp;gt;</td></tr></table> x <table><tr><th>&amp;lt;</th></tr></table>"
result = transform(markdown)
assert result.output_markdown == "<table><tr><td>&gt;</td></tr></table> x <table><tr><th>&lt;</th></tr></table>"
assert [change.span.start for change in result.changes] == sorted(change.span.start for change in result.changes)
@pytest.mark.parametrize(
"markdown",
[
"outside &amp;lt;",
"<table><tr><td>&amp;lt;</tr></table>",
"<table><tr><td><em>&amp;lt;</em></td></tr></table>",
"<table><tr><td><table><tr><td>&amp;lt;</td></tr></table></td></tr></table>",
"<table><tbody><tr><td>&amp;lt;</td></tr></tbody></table>",
],
)
def test_non_strict_or_outside_content_is_preserved(markdown: str) -> None:
assert transform(markdown).output_markdown == markdown
@pytest.mark.parametrize(
"markdown",
[
"<table>broken<table><tr><td>&amp;lt;</td></tr></table>",
"<table broken <table><tr><td>&amp;lt;</td></tr></table>",
],
)
def test_damaged_outer_table_does_not_expose_complete_inner_table(markdown: str) -> None:
result = transform(markdown)
assert result.status is RunStatus.SUCCESS
assert result.output_markdown == markdown
assert result.changes == ()
def test_fenced_table_is_not_protected_by_the_lexical_subset() -> None:
markdown = "```html\n<table><tr><td>&amp;lt;</td></tr></table>\n```"
assert transform(markdown).output_markdown == "```html\n<table><tr><td>&lt;</td></tr></table>\n```"
def test_successful_output_is_idempotent() -> None:
pipeline = Pipeline([html_table_entity_unescape()])
first = pipeline.transform("<table><tr><td>&amp;lt;</td></tr></table>")
assert first.output_markdown is not None
assert pipeline.transform(first.output_markdown).changes == ()