|
| 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" |
0 commit comments