Skip to content

Commit c237eee

Browse files
committed
Rename replace_attributes to replace_conditionals.
1 parent 032c7f8 commit c237eee

4 files changed

Lines changed: 108 additions & 8 deletions

File tree

src/dj_angles/replacers/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22

3-
from dj_angles.replacers.attributes import replace_attributes, replace_values
3+
from dj_angles.replacers.attributes import replace_conditionals, replace_values
44
from dj_angles.replacers.comments import mask_comments
55
from dj_angles.replacers.tags import replace_tags
66
from dj_angles.replacers.variables import replace_variables
@@ -25,8 +25,8 @@ def convert_template(html: str, *, origin=None) -> str:
2525

2626
(html, comments) = mask_comments(html, initial_tag_regex=initial_tag_regex)
2727

28-
# 1. Replace attributes, e.g. `<div dj-if="condition">`
29-
html = replace_attributes(html)
28+
# 1. Replace conditionals, e.g. `<div dj-if="condition">`
29+
html = replace_conditionals(html)
3030

3131
# 2. Replace variables, e.g. `{{ foo or bar }}`
3232
html = replace_variables(html)

src/dj_angles/replacers/attributes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def condition(self) -> str:
7979
return self.value
8080

8181

82-
def replace_attributes(html: str) -> str:
82+
def replace_conditionals(html: str) -> str:
8383
"""Convert dj-if/elif/else attributes to Django template tags.
8484
8585
Args:
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import re
2+
3+
from dj_angles.replacers.attributes import _find_element
4+
5+
6+
def test_find_element_basic():
7+
html = '<div dj-value="x">content</div>'
8+
match = re.search(r"dj-value=\"x\"", html)
9+
10+
element = _find_element(html, match, "value")
11+
12+
assert element.tag_name == "div"
13+
assert element.tag_start == 0
14+
assert element.tag_end == 18
15+
assert element.full_end == 31
16+
assert element.original_tag == '<div dj-value="x">'
17+
assert element.original_full == '<div dj-value="x">content</div>'
18+
assert element.is_closing is False
19+
20+
21+
def test_find_element_self_closing():
22+
html = '<img dj-value="x" />'
23+
match = re.search(r"dj-value=\"x\"", html)
24+
25+
element = _find_element(html, match, "value")
26+
27+
assert element.tag_name == "img"
28+
assert element.original_tag == '<img dj-value="x" />'
29+
assert element.full_end == element.tag_end
30+
assert element.is_closing is False
31+
32+
33+
def test_find_element_void():
34+
html = '<input dj-value="x">'
35+
match = re.search(r"dj-value=\"x\"", html)
36+
37+
element = _find_element(html, match, "value")
38+
39+
assert element.tag_name == "input"
40+
assert element.original_tag == '<input dj-value="x">'
41+
assert element.full_end == element.tag_end
42+
43+
44+
def test_find_element_closing_tag():
45+
html = '</div dj-value="x">'
46+
match = re.search(r"dj-value=\"x\"", html)
47+
48+
element = _find_element(html, match, "value")
49+
50+
assert element.tag_name == "div"
51+
assert element.is_closing is True
52+
53+
54+
def test_find_element_nested():
55+
html = '<div dj-value="outer"><span dj-value="inner"></span></div>'
56+
matches = list(re.finditer(r"dj-value=\"[^\"]+\"", html))
57+
58+
outer = _find_element(html, matches[0], "value")
59+
inner = _find_element(html, matches[1], "value")
60+
61+
assert outer.contains(inner) is True
62+
assert inner.contains(outer) is False
63+
64+
65+
def test_element_remove_attribute():
66+
html = '<div class="foo" dj-value="x">content</div>'
67+
match = re.search(r"dj-value=\"x\"", html)
68+
69+
element = _find_element(html, match, "value")
70+
71+
assert element.remove_attribute() == '<div class="foo">'
72+
73+
74+
def test_element_closing_tag():
75+
html = '<div dj-value="x">content</div>'
76+
match = re.search(r"dj-value=\"x\"", html)
77+
78+
element = _find_element(html, match, "value")
79+
80+
assert element.closing_tag() == "</div>"
81+
82+
83+
def test_element_closing_tag_generated():
84+
html = '<img dj-value="x" />'
85+
match = re.search(r"dj-value=\"x\"", html)
86+
87+
element = _find_element(html, match, "value")
88+
89+
assert element.closing_tag() == "</img>"
90+
91+
92+
def test_element_type_and_value_fields():
93+
html = '<div dj-value="x">content</div>'
94+
match = re.search(r"dj-value=\"x\"", html)
95+
96+
element = _find_element(html, match, "value")
97+
element.value = "x"
98+
99+
assert element.type == "value"
100+
assert element.value == "x"

tests/dj_angles/replacers/attributes/test_replace_attributes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from dj_angles.replacers.attributes import replace_attributes
5+
from dj_angles.replacers.attributes import replace_conditionals
66

77
# Structure to store parameterize data
88
Params = namedtuple(
@@ -72,13 +72,13 @@
7272
),
7373
)
7474
def test_attributes(original, replacement):
75-
actual = replace_attributes(original)
75+
actual = replace_conditionals(original)
7676
assert actual == replacement
7777

7878

7979
def test_orphaned_else():
8080
with pytest.raises(AssertionError, match="Invalid use of dj-else attribute"):
81-
replace_attributes("<div dj-else>orphaned</div>")
81+
replace_conditionals("<div dj-else>orphaned</div>")
8282

8383
with pytest.raises(AssertionError, match="Invalid use of dj-elif attribute"):
84-
replace_attributes("<div dj-elif='c'>orphaned</div>")
84+
replace_conditionals("<div dj-elif='c'>orphaned</div>")

0 commit comments

Comments
 (0)