First version
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
"""Tests for :mod:`genshin_tcg.markup`."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from genshin_tcg.markup import clean_markup, inline_symbols, sanitize, strip_markup
|
||||
|
||||
|
||||
def test_clean_markup_converts_color_tags() -> None:
|
||||
assert clean_markup("<color=#FFD700>Or</color>") == '<span style="color: #FFD700">Or</span>'
|
||||
|
||||
|
||||
def test_clean_markup_converts_escaped_newlines() -> None:
|
||||
assert clean_markup("a\\nb") == "a<br/>b"
|
||||
|
||||
|
||||
def test_clean_markup_keeps_colour_and_underline_spans() -> None:
|
||||
raw = '<span style="color: #80C0FFFF; text-decoration: underline;">DGT</span>'
|
||||
expected = '<span style="color: #80C0FFFF; text-decoration: underline">DGT</span>'
|
||||
assert clean_markup(raw) == expected
|
||||
|
||||
|
||||
def test_sanitize_removes_script_and_event_handlers() -> None:
|
||||
dirty = '<script>alert(1)</script><span onclick="x()" style="color: red;">ok</span>'
|
||||
cleaned = sanitize(dirty)
|
||||
assert "script" not in cleaned
|
||||
assert "alert" not in cleaned
|
||||
assert "onclick" not in cleaned
|
||||
assert cleaned == '<span style="color: red">ok</span>'
|
||||
|
||||
|
||||
def test_sanitize_unwraps_unknown_tags_but_keeps_text() -> None:
|
||||
assert sanitize("<div>hello <em>world</em></div>") == "hello <em>world</em>"
|
||||
|
||||
|
||||
def test_sanitize_drops_non_allowlisted_styles() -> None:
|
||||
cleaned = sanitize('<span style="position: fixed; color: blue;">x</span>')
|
||||
assert cleaned == '<span style="color: blue">x</span>'
|
||||
|
||||
|
||||
def test_strip_markup_returns_plain_text() -> None:
|
||||
assert strip_markup("<span>a</span>\\nb") == "a b"
|
||||
|
||||
|
||||
def test_inline_symbols_prefixes_keyword_only_in_text() -> None:
|
||||
html = 'DGT <span style="color: red;">Pyro</span>'
|
||||
result = inline_symbols(html, {"Pyro": "[ICON]"})
|
||||
assert result == 'DGT <span style="color: red;">[ICON]Pyro</span>'
|
||||
|
||||
|
||||
def test_inline_symbols_respects_word_boundaries_and_attributes() -> None:
|
||||
html = '<span title="Pyro">Pyromane</span>'
|
||||
result = inline_symbols(html, {"Pyro": "[ICON]"})
|
||||
# "Pyro" inside the attribute and inside "Pyromane" must not match.
|
||||
assert result == html
|
||||
|
||||
|
||||
def test_inline_symbols_no_symbols_is_identity() -> None:
|
||||
assert inline_symbols("<b>x</b>", {}) == "<b>x</b>"
|
||||
Reference in New Issue
Block a user