from django.test import TestCase from djc_core_html_parser import set_html_attributes from .django_test_setup 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 class TestHTMLParser(TestCase): def test_basic_transformation(self): html = "

Hello

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

Hello

""", ) def test_multiple_roots(self): html = "
First
Second" result, _ = set_html_attributes(html, root_attributes=["data-root"], all_attributes=["data-all"]) self.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 self.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"]) self.assertHTMLEqual(result, expected) def test_html_head_with_meta(self): html = """ Test Page """ result, _ = set_html_attributes(html, ["data-root"], ["data-v-123"]) self.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") self.assertHTMLEqual( result, """

Regular element

Nested element
""", ) # Verify attribute capturing self.assertEqual(len(captured), 3) # Root element should have both root and all attributes self.assertEqual(captured["123"], ["data-root", "data-v-123"]) # Non-root elements should only have all attributes self.assertEqual(captured["456"], ["data-v-123"]) self.assertEqual(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
""" self.assertEqual(result, expected)