check-templates: Remove prevent_dangling_tags.

This was a misfeature--it occasionally flagged
code that was ugly but necessarily ugly to
ensure proper rendering.
This commit is contained in:
Steve Howell
2021-12-02 12:02:06 +00:00
committed by Tim Abbott
parent 6152bed378
commit 0decfa8da0

View File

@@ -338,7 +338,6 @@ def validate(fn: Optional[str] = None, text: Optional[str] = None) -> None:
)
prevent_whitespace_violations(fn, tokens)
prevent_dangling_tags(fn, tokens)
class State:
def __init__(self, func: Callable[[Token], None]) -> None:
@@ -555,48 +554,6 @@ def prevent_whitespace_violations(fn: str, tokens: List[Token]) -> None:
)
def prevent_dangling_tags(fn: str, tokens: List[Token]) -> None:
"""
Prevent this kind of HTML:
<div attr attr
attr attr>Stuff</div>
We prefer:
<div attr attr
attr attr>
Stuff
</div>
We may eventually have the pretty_printer code do this
automatically, but there are some complications with
legacy code.
"""
min_row: Optional[int] = None
for token in tokens:
# We only apply this validation for a couple tag types, because
# our existing templates may have some funny edge cases. We eventually
# want to be more aggressive here. We may need to be extra careful
# with tags like <pre> that have whitespace sensitivities.
if token.tag not in ("div", "button", "p"):
continue
if min_row and token.line < min_row:
raise TemplateParserException(
f"""
Please fix line {token.line} at {fn} (col {token.col})
by moving this tag so that it closes the block at the
same indentation level as its start tag:
{token.s}
"""
)
else:
min_row = None
if token.line_span > 1:
min_row = token.line + token.line_span
def is_django_block_tag(tag: str) -> bool:
return tag in [
"autoescape",