|
| 1 | +from dj_angles.replacers.comments import mask_comments |
| 2 | + |
| 3 | + |
| 4 | +def test_mask_django_single_line(): |
| 5 | + html = "Hello {# comment #} World" |
| 6 | + masked_html, comments = mask_comments(html) |
| 7 | + assert masked_html == "Hello __DJ_ANGLES_COMMENT_0__ World" |
| 8 | + assert comments == ["{# comment #}"] |
| 9 | + |
| 10 | + |
| 11 | +def test_mask_django_block(): |
| 12 | + html = "Hello {% comment %} block {% endcomment %} World" |
| 13 | + masked_html, comments = mask_comments(html) |
| 14 | + assert masked_html == "Hello __DJ_ANGLES_COMMENT_0__ World" |
| 15 | + assert comments == ["{% comment %} block {% endcomment %}"] |
| 16 | + |
| 17 | + |
| 18 | +def test_mask_dj_comment(): |
| 19 | + html = "Hello <dj-comment> custom </dj-comment> World" |
| 20 | + masked_html, comments = mask_comments(html) |
| 21 | + assert masked_html == "Hello __DJ_ANGLES_COMMENT_0__ World" |
| 22 | + assert comments == ["<dj-comment> custom </dj-comment>"] |
| 23 | + |
| 24 | + |
| 25 | +def test_mask_nested_dj_comment(): |
| 26 | + html = """ |
| 27 | + <dj-comment> |
| 28 | + outer |
| 29 | + <dj-comment> |
| 30 | + inner |
| 31 | + </dj-comment> |
| 32 | + end outer |
| 33 | + </dj-comment> |
| 34 | + """ |
| 35 | + masked_html, comments = mask_comments(html) |
| 36 | + assert "__DJ_ANGLES_COMMENT_0__" in masked_html |
| 37 | + assert len(comments) == 1 |
| 38 | + assert "outer" in comments[0] |
| 39 | + assert "inner" in comments[0] |
| 40 | + |
| 41 | + |
| 42 | +def test_mask_multiple_comments(): |
| 43 | + html = "{# one #} middle <dj-comment> two </dj-comment> end" |
| 44 | + masked_html, comments = mask_comments(html) |
| 45 | + assert masked_html == "__DJ_ANGLES_COMMENT_0__ middle __DJ_ANGLES_COMMENT_1__ end" |
| 46 | + assert len(comments) == 2 |
| 47 | + assert comments[0] == "{# one #}" |
| 48 | + assert comments[1] == "<dj-comment> two </dj-comment>" |
| 49 | + |
| 50 | + |
| 51 | +def test_unclosed_comment(): |
| 52 | + html = "Hello <dj-comment> unclosed" |
| 53 | + masked_html, comments = mask_comments(html) |
| 54 | + # Unclosed comments should be treated as text (not masked) |
| 55 | + assert masked_html == "Hello <dj-comment> unclosed" |
| 56 | + assert len(comments) == 0 |
| 57 | + |
| 58 | + |
| 59 | +def test_django_block_inside_dj_comment(): |
| 60 | + html = "<dj-comment>{% comment %} inner {% endcomment %}</dj-comment>" |
| 61 | + masked_html, comments = mask_comments(html) |
| 62 | + assert masked_html == "__DJ_ANGLES_COMMENT_0__" |
| 63 | + assert len(comments) == 1 |
| 64 | + assert "{% comment %}" in comments[0] |
| 65 | + |
| 66 | + |
| 67 | +def test_mask_custom_prefix(): |
| 68 | + html = "Hello <my-comment> custom </my-comment> World" |
| 69 | + # Use raw string for prefix to avoid escape issues in test |
| 70 | + masked_html, comments = mask_comments(html, initial_tag_regex=r"(my-)") |
| 71 | + assert masked_html == "Hello __DJ_ANGLES_COMMENT_0__ World" |
| 72 | + assert comments == ["<my-comment> custom </my-comment>"] |
| 73 | + |
| 74 | + |
| 75 | +def test_mask_unclosed_tag(): |
| 76 | + html = "Normal <dj-comment> This is unclosed" |
| 77 | + masked_html, comments = mask_comments(html) |
| 78 | + # Unclosed tags are kept as-is, not masked |
| 79 | + assert masked_html == html |
| 80 | + assert len(comments) == 0 |
| 81 | + |
| 82 | + |
| 83 | +def test_mask_orphaned_closing_tag(): |
| 84 | + html = "Orphaned </dj-comment> tag" |
| 85 | + masked_html, comments = mask_comments(html) |
| 86 | + # Orphaned end tags should be masked to prevent downstream crashes |
| 87 | + assert masked_html == "Orphaned __DJ_ANGLES_COMMENT_0__ tag" |
| 88 | + assert comments == ["</dj-comment>"] |
| 89 | + |
| 90 | + |
| 91 | +def test_mask_mixed_orphaned_tags(): |
| 92 | + html = "</dj-comment> {% endcomment %}" |
| 93 | + masked_html, comments = mask_comments(html) |
| 94 | + assert masked_html == "__DJ_ANGLES_COMMENT_0__ __DJ_ANGLES_COMMENT_1__" |
| 95 | + assert comments == ["</dj-comment>", "{% endcomment %}"] |
0 commit comments