Skip to content

Commit b67d871

Browse files
committed
Checking for anything between blocks, not only tokens
1 parent 8870ff6 commit b67d871

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

adafruit_templateengine.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ def _find_block(template: str):
205205
return _BLOCK_PATTERN.search(template)
206206

207207

208+
def _find_any_non_whitespace(template: str):
209+
return re.search(r"\S+", template)
210+
211+
208212
def _find_endblock(template: str, name: str = r"\w+?"):
209213
return re.search(r"{% endblock " + name + r" %}", template)
210214

@@ -304,17 +308,17 @@ def _resolve_includes_blocks_and_extends(template: str):
304308
while (block_match := _find_block(template[offset:])) is not None:
305309
block_name = block_match.group(0)[9:-3]
306310

307-
# Check for any tokens between blocks
308-
if token_between_blocks_match := _find_token(
311+
# Check for anything between blocks
312+
if content_between_blocks := _find_any_non_whitespace(
309313
template[offset : offset + block_match.start()]
310314
):
311315
raise TemplateSyntaxError(
312316
Token(
313317
template,
314-
offset + token_between_blocks_match.start(),
315-
offset + token_between_blocks_match.end(),
318+
offset + content_between_blocks.start(),
319+
offset + content_between_blocks.end(),
316320
),
317-
"Token between blocks",
321+
"Content outside block",
318322
)
319323

320324
if not (endblock_match := _find_endblock(template[offset:], block_name)):
@@ -351,6 +355,16 @@ def _resolve_includes_blocks_and_extends(template: str):
351355

352356
offset += endblock_match.end()
353357

358+
if content_after_last_endblock := _find_any_non_whitespace(template[offset:]):
359+
raise TemplateSyntaxError(
360+
Token(
361+
template,
362+
offset + content_after_last_endblock.start(),
363+
offset + content_after_last_endblock.end(),
364+
),
365+
"Content outside block",
366+
)
367+
354368
template = extended_template
355369

356370
# Resolve includes in top-level template

0 commit comments

Comments
 (0)