from pytest_django.asserts import assertHTMLEqual from djc_core_html_parser import set_html_attributes from django_components.testing import djc_test from .testutils import setup_test_config setup_test_config({"autodiscover": False}) # This same set of tests is also found in djc_html_parser, to ensure that # this implementation can be replaced with the djc_html_parser's Rust-based implementation @djc_test class TestHTMLParser: def test_basic_transformation(self): html = "

Hello

" result, _ = set_html_attributes(html, root_attributes=["data-root"], all_attributes=["data-all"]) assertHTMLEqual( result, """

Hello

""", ) def test_multiple_roots(self): html = "
First
Second" result, _ = set_html_attributes(html, root_attributes=["data-root"], all_attributes=["data-all"]) assertHTMLEqual( result, """
First
Second """, ) def test_complex_html(self): html = """

Hello & Welcome

Article 1

Some text with bold and emphasis

Test Image
""" result, _ = set_html_attributes(html, ["data-root"], ["data-all", "data-v-123"]) expected = """

Hello & Welcome

Article 1

Some text with bold and emphasis

Test Image
""" # noqa: E501 assertHTMLEqual(result, expected) def test_void_elements(self): test_cases = [ ('', ''), ('', ''), ("


", "


"), ('Test', 'Test'), ] for input_html, expected in test_cases: result, _ = set_html_attributes(input_html, ["data-root"], ["data-v-123"]) assertHTMLEqual(result, expected) def test_html_head_with_meta(self): html = """ Test Page """ result, _ = set_html_attributes(html, ["data-root"], ["data-v-123"]) assertHTMLEqual( result, """ Test Page """, ) def test_watch_attribute(self): html = """

Regular element

Nested element
""" result, captured = set_html_attributes(html, ["data-root"], ["data-v-123"], watch_on_attribute="data-id") assertHTMLEqual( result, """

Regular element

Nested element
""", ) # Verify attribute capturing assert len(captured) == 3 # Root element should have both root and all attributes assert captured["123"] == ["data-root", "data-v-123"] # Non-root elements should only have all attributes assert captured["456"] == ["data-v-123"] assert captured["789"] == ["data-v-123"] def test_whitespace_preservation(self): html = """

Hello World

Text with spaces
""" result, _ = set_html_attributes(html, ["data-root"], ["data-all"]) expected = """

Hello World

Text with spaces
""" assert result == expected