From 1a190bdfb704c59e43083038145748be60340350 Mon Sep 17 00:00:00 2001 From: Jaap Joris Vens Date: Sun, 16 May 2021 12:11:57 +0200 Subject: [PATCH] More testcases for spaces in Django template tags --- djhtml/modes.py | 6 ++++-- test.html | 1 + tests/suite/django.in | 27 +++++++++++++++++++++++++++ tests/suite/django.out | 27 +++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 test.html diff --git a/djhtml/modes.py b/djhtml/modes.py index 26151fe..490af15 100644 --- a/djhtml/modes.py +++ b/djhtml/modes.py @@ -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 = [ @@ -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): @@ -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): diff --git a/test.html b/test.html new file mode 100644 index 0000000..2520985 --- /dev/null +++ b/test.html @@ -0,0 +1 @@ + hoi diff --git a/tests/suite/django.in b/tests/suite/django.in index dfe5de0..d699860 100644 --- a/tests/suite/django.in +++ b/tests/suite/django.in @@ -55,3 +55,30 @@ console.log("Hi mom!"); jrei `- | `::_; `\ `' / `--' {% endcomment %} + + + +{% comment "Yo dawg" %} + I heard you like comments +{% endcomment %} + + + +{%comment %} + %} +{% endcomment%} +{%if%} +{%block content %} +Yuck! +{% endblock%} +{% endif %} + + + +{% block %} +1 +{% with %} +2 +{% endblock %} +1 +{% endwith %} diff --git a/tests/suite/django.out b/tests/suite/django.out index 718a452..ac1be15 100644 --- a/tests/suite/django.out +++ b/tests/suite/django.out @@ -55,3 +55,30 @@ jrei `- | `::_; `\ `' / `--' {% endcomment %} + + + +{% comment "Yo dawg" %} + I heard you like comments +{% endcomment %} + + + +{%comment %} + %} +{% endcomment%} +{%if%} + {%block content %} + Yuck! + {% endblock%} +{% endif %} + + + +{% block %} + 1 + {% with %} + 2 + {% endblock %} + 1 +{% endwith %}