Skip to content

Commit

Permalink
More testcases for spaces in Django template tags
Browse files Browse the repository at this point in the history
  • Loading branch information
JaapJoris committed May 16, 2021
1 parent 30f2e51 commit 1a190bd
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
6 changes: 4 additions & 2 deletions djhtml/modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Mode:
"""

TAG = r"\{%.*?%\}"
COMMENT = r"{% comment .*? endcomment %}"
COMMENT = r"{% *comment.*?endcomment *%}"
TOKEN = re.compile(f"(?s)({COMMENT})|({TAG})")

DJANGO_OPENING_TAGS = [
Expand Down Expand Up @@ -55,6 +55,8 @@ class Mode:
]

def __init__(self, source):
if "\t" in source:
raise SyntaxError("This file contains TAB characters.")
self.source = source

def indent(self, tabwidth, level=0):
Expand Down Expand Up @@ -132,7 +134,7 @@ def get_token_type(self, raw_token):
Given the raw token string, determine what type it is.
"""
if raw_token.startswith("{% comment"):
if re.match(r"{% *comment", raw_token):
return Comment
if raw_token.startswith("{%"):
if tag_name := re.search(r"(\w+)", raw_token):
Expand Down
1 change: 1 addition & 0 deletions test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hoi
27 changes: 27 additions & 0 deletions tests/suite/django.in
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,30 @@ console.log("Hi mom!");
jrei `- | `::_; `\ `' /
`--'
{% endcomment %}


<!-- Django comment with optional comment -->
{% comment "Yo dawg" %}
I heard you like comments
{% endcomment %}


<!-- Various spacings -->
{%comment %}
%}
{% endcomment%}
{%if%}
{%block content %}
Yuck!
{% endblock%}
{% endif %}


<!-- Inconsistent nesting -->
{% block %}
1
{% with %}
2
{% endblock %}
1
{% endwith %}
27 changes: 27 additions & 0 deletions tests/suite/django.out
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,30 @@
jrei `- | `::_; `\ `' /
`--'
{% endcomment %}


<!-- Django comment with optional comment -->
{% comment "Yo dawg" %}
I heard you like comments
{% endcomment %}


<!-- Various spacings -->
{%comment %}
%}
{% endcomment%}
{%if%}
{%block content %}
Yuck!
{% endblock%}
{% endif %}


<!-- Inconsistent nesting -->
{% block %}
1
{% with %}
2
{% endblock %}
1
{% endwith %}

0 comments on commit 1a190bd

Please sign in to comment.