@@ -205,6 +205,10 @@ def _find_block(template: str):
205
205
return _BLOCK_PATTERN .search (template )
206
206
207
207
208
+ def _find_any_non_whitespace (template : str ):
209
+ return re .search (r"\S+" , template )
210
+
211
+
208
212
def _find_endblock (template : str , name : str = r"\w+?" ):
209
213
return re .search (r"{% endblock " + name + r" %}" , template )
210
214
@@ -255,7 +259,9 @@ def _resolve_includes(template: str):
255
259
return template
256
260
257
261
258
- def _resolve_includes_blocks_and_extends (template : str ):
262
+ def _resolve_includes_blocks_and_extends ( # pylint: disable=,too-many-locals
263
+ template : str ,
264
+ ):
259
265
extended_templates : "set[str]" = set ()
260
266
block_replacements : "dict[str, str]" = {}
261
267
@@ -304,17 +310,17 @@ def _resolve_includes_blocks_and_extends(template: str):
304
310
while (block_match := _find_block (template [offset :])) is not None :
305
311
block_name = block_match .group (0 )[9 :- 3 ]
306
312
307
- # Check for any tokens between blocks
308
- if token_between_blocks_match := _find_token (
313
+ # Check for anything between blocks
314
+ if content_between_blocks := _find_any_non_whitespace (
309
315
template [offset : offset + block_match .start ()]
310
316
):
311
317
raise TemplateSyntaxError (
312
318
Token (
313
319
template ,
314
- offset + token_between_blocks_match .start (),
315
- offset + token_between_blocks_match .end (),
320
+ offset + content_between_blocks .start (),
321
+ offset + content_between_blocks .end (),
316
322
),
317
- "Token between blocks " ,
323
+ "Content outside block " ,
318
324
)
319
325
320
326
if not (endblock_match := _find_endblock (template [offset :], block_name )):
@@ -351,6 +357,16 @@ def _resolve_includes_blocks_and_extends(template: str):
351
357
352
358
offset += endblock_match .end ()
353
359
360
+ if content_after_last_endblock := _find_any_non_whitespace (template [offset :]):
361
+ raise TemplateSyntaxError (
362
+ Token (
363
+ template ,
364
+ offset + content_after_last_endblock .start (),
365
+ offset + content_after_last_endblock .end (),
366
+ ),
367
+ "Content outside block" ,
368
+ )
369
+
354
370
template = extended_template
355
371
356
372
# Resolve includes in top-level template
0 commit comments