Схемы javascript:/data:/vbscript: в ссылках не выводятся; имя вложения и текст ссылки экранируются (скобки, @, переводы строк); fill_ids меняет только ](attachment:@N) и поднимает ValueError вне диапазона; «<» в тексте — <; attachments/links = None трактуются как пустые списки. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
164 lines
6.2 KiB
Python
164 lines
6.2 KiB
Python
"""Перевод HTML Eva в Markdown (wiki-module, spec wiki «Markdown-копия вики», design 10)."""
|
|
import re
|
|
from pathlib import Path
|
|
|
|
from jira_hub.modules.wiki import convert
|
|
|
|
FIX = Path(__file__).parent / "fixtures" / "wiki"
|
|
FILE_URL = "https://eva.example.test/files/obj/CmfDocument/CmfDocument%3Aa08/CmfDocument%3A3f2a9c1e-0b7d-4e8a-9c21-5d6e7f8a9b0c/model.xlsx?version=1"
|
|
IMG_REF = "/files/obj/CmfDocument/CmfDocument%3Aa08/CmfDocument%3A3f2a9c1e-0b7d-4e8a-9c21-5d6e7f8a9b0c/image2026-7-27_14-20-6.png?version=1"
|
|
THUMB_REF = "/files/obj/CmfDocument/CmfDocument%3Aa08/CmfDocument%3A3f2a9c1e-0b7d-4e8a-9c21-5d6e7f8a9b0c/model.xlsx.meta/thumbnail.jpg?version=1"
|
|
FILE_REF = "/files/obj/CmfDocument/CmfDocument%3Aa08/CmfDocument%3A3f2a9c1e-0b7d-4e8a-9c21-5d6e7f8a9b0c/model.xlsx?version=1"
|
|
|
|
|
|
def _html(name):
|
|
return (FIX / name).read_text(encoding="utf-8")
|
|
|
|
|
|
def test_image_becomes_attachment():
|
|
md, atts = convert.to_markdown(
|
|
_html("page_image.html"),
|
|
[{"ref": IMG_REF, "url": "https://eva.example.test" + IMG_REF, "name": "image2026-7-27_14-20-6.png"}],
|
|
[],
|
|
)
|
|
assert "[📎 image2026-7-27_14-20-6.png](attachment:@0)" in md.splitlines()
|
|
assert atts == [{"url": "https://eva.example.test" + IMG_REF, "name": "image2026-7-27_14-20-6.png"}]
|
|
|
|
|
|
def test_thumbnail_and_file_one_link():
|
|
md, atts = convert.to_markdown(
|
|
_html("page_file.html"),
|
|
[
|
|
{"ref": THUMB_REF, "url": FILE_URL, "name": "model.xlsx"},
|
|
{"ref": FILE_REF, "url": FILE_URL, "name": "model.xlsx"},
|
|
],
|
|
[],
|
|
)
|
|
assert [a["name"] for a in atts] == ["model.xlsx"] and len(atts) == 1
|
|
assert md.count("](attachment:@0)") == 1
|
|
assert "[📎 model.xlsx](attachment:@0)" in md
|
|
assert "attach-file" not in md
|
|
|
|
|
|
def test_page_link():
|
|
md, _ = convert.to_markdown(
|
|
_html("page_links.html"),
|
|
[],
|
|
[{"ref": "project/Document/DOC-019834", "code": "DOC-019834"}],
|
|
)
|
|
assert "[Модель данных](page:DOC-019834)" in md
|
|
|
|
|
|
def test_external_link_kept():
|
|
md, _ = convert.to_markdown(_html("page_links.html"), [], [])
|
|
assert "(https://example.org)" in md
|
|
|
|
|
|
def test_no_raw_html():
|
|
for name in ("page_image.html", "page_file.html", "page_links.html"):
|
|
md, _ = convert.to_markdown(
|
|
_html(name),
|
|
[{"ref": IMG_REF, "url": "https://eva.example.test" + IMG_REF, "name": "image.png"},
|
|
{"ref": THUMB_REF, "url": FILE_URL, "name": "model.xlsx"},
|
|
{"ref": FILE_REF, "url": FILE_URL, "name": "model.xlsx"}],
|
|
[{"ref": "project/Document/DOC-019834", "code": "DOC-019834"}],
|
|
)
|
|
assert re.search(r"<[a-zA-Z/]", md) is None, name
|
|
|
|
|
|
def test_table_kept():
|
|
md, _ = convert.to_markdown(_html("page_links.html"), [], [])
|
|
table_lines = [line for line in md.splitlines() if "|" in line]
|
|
assert table_lines and any("---" in line for line in table_lines)
|
|
|
|
|
|
def test_headings_atx():
|
|
md, _ = convert.to_markdown(_html("page_links.html"), [], [])
|
|
assert "# Требования" in md.splitlines()
|
|
|
|
|
|
def test_static_links_removed():
|
|
html = '<p>Текст <img src="/static/x.png" alt=""> и <a href="static/y.html">ссылка</a></p>'
|
|
md, atts = convert.to_markdown(html, [], [])
|
|
assert "static" not in md and atts == []
|
|
|
|
|
|
def test_fill_ids():
|
|
assert convert.fill_ids("[x](attachment:@1)", [7, 9]) == "[x](attachment:9)"
|
|
|
|
|
|
# --- fix round 1 (review Task 6: I1, I2, I3, M3, M4) ---
|
|
|
|
def test_unsafe_schemes_dropped_from_links():
|
|
html = ('<p><a href="javascript:alert(1)">x1</a> <a href="JaVaScript:alert(2)">x2</a> '
|
|
'<a href="data:text/html,<b>x</b>">x3</a> <a href=" java\tscript:alert(3)">x4</a> '
|
|
'<a href="vbscript:msgbox">x5</a></p>')
|
|
md, _ = convert.to_markdown(html, [], [])
|
|
for text in ("x1", "x2", "x3", "x4", "x5"):
|
|
assert text in md
|
|
assert "(javascript" not in md.lower() and "(data:" not in md and "(vbscript" not in md.lower()
|
|
assert "java" not in md.lower().replace("x4", "") # схема не осталась ни в каком виде
|
|
|
|
|
|
def test_unsafe_scheme_img_dropped():
|
|
md, atts = convert.to_markdown('<p>a<img src="javascript:x" alt="z">b</p>', [], [])
|
|
assert md.strip() == "ab" and atts == []
|
|
|
|
|
|
def test_attachment_name_and_link_text_escaped():
|
|
md, atts = convert.to_markdown(
|
|
'<p><a href="/files/a">x</a></p>',
|
|
[{"ref": "/files/a", "url": "https://eva.example.test/files/a", "name": "a]b[c).png"}],
|
|
[],
|
|
)
|
|
assert "[📎 a\\]b\\[c).png](attachment:@0)" in md.splitlines()
|
|
assert atts[0]["name"] == "a]b[c).png"
|
|
|
|
md, _ = convert.to_markdown(
|
|
'<p><a href="project/Document/DOC-1">a ] b [x](http://evil)</a></p>',
|
|
[],
|
|
[{"ref": "project/Document/DOC-1", "code": "DOC-1"}],
|
|
)
|
|
assert "[a \\] b \\[x\\](http://evil)](page:DOC-1)" in md.splitlines()
|
|
|
|
|
|
def test_newlines_in_link_text_collapsed():
|
|
md, _ = convert.to_markdown(
|
|
'<p><a href="project/Document/DOC-1">первая\nвторая</a></p>', [],
|
|
[{"ref": "project/Document/DOC-1", "code": "DOC-1"}],
|
|
)
|
|
assert "[первая вторая](page:DOC-1)" in md.splitlines()
|
|
|
|
|
|
def test_literal_attachment_marker_in_text_is_safe():
|
|
md, atts = convert.to_markdown("<p>[x](attachment:@5) и attachment:@0</p>", [], [])
|
|
assert atts == []
|
|
assert convert.fill_ids(md, []) == md
|
|
assert "(attachment:@5)" not in md.replace("\\(attachment:\\@5)", "")
|
|
|
|
|
|
def test_fill_ids_by_index_not_prefix():
|
|
ids = list(range(100, 111))
|
|
assert convert.fill_ids("[a](attachment:@1) [b](attachment:@10)", ids) == \
|
|
"[a](attachment:101) [b](attachment:110)"
|
|
|
|
|
|
def test_fill_ids_out_of_range_raises():
|
|
try:
|
|
convert.fill_ids("[a](attachment:@3)", [1, 2])
|
|
except ValueError as exc:
|
|
assert "@3" in str(exc)
|
|
else:
|
|
raise AssertionError("ожидалась ValueError")
|
|
|
|
|
|
def test_none_lists_are_empty():
|
|
md, atts = convert.to_markdown("<p>текст</p>", None, None)
|
|
assert md == "текст\n" and atts == []
|
|
|
|
|
|
def test_literal_tag_text_has_no_raw_html():
|
|
md, _ = convert.to_markdown("<p><tag> и <script>alert(1)</script></p>", [], [])
|
|
assert re.search(r"<[a-zA-Z/]", md) is None
|
|
assert "<tag>" in md or "<tag" in md
|