fix check for empty required block (#1858)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by GitHub · parent 37f5b05 · Browse files · 3 files·+33 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+33 added−20 removed
- tests:+22 added−14 removed
- other:+11 added−6 removed
Provenance
- Commit
051df10- Tree
b84ee05- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
37f5b05
3 files changed+33 added−20 removed
modified:src/jinja2/parser.py+8 added-6 removed
309 unmodified lines310 311 # enforce that required blocks only contain whitespace or comments312 # by asserting that the body, if not empty, is just TemplateData nodes313 # with whitespace data314- if node.required and not all(315- isinstance(child, nodes.TemplateData) and child.data.isspace()316- for body in node.body317- for child in body.nodes # type: ignore318- ):319- self.fail("Required blocks can only contain comments or whitespace")314+ if node.required:315+ for body_node in node.body:316+ if not isinstance(body_node, nodes.Output) or any(317+ not isinstance(output_node, nodes.TemplateData)318+ or not output_node.data.isspace()319+ for output_node in body_node.nodes320+ ):321+ self.fail("Required blocks can only contain comments or whitespace")322 323 self.stream.skip_if("name:" + node.name)324 return node325 709 unmodified linesmodified:tests/test_inheritance.py+22 added-14 removed
285 unmodified lines286 def test_invalid_required(self, env):287 env = Environment(288 loader=DictLoader(289 {290- "default": "{% block x required %}data {# #}{% endblock %}",291- "default1": "{% block x required %}{% block y %}"292- "{% endblock %} {% endblock %}",293- "default2": "{% block x required %}{% if true %}"294- "{% endif %} {% endblock %}",295- "level1": "{% if default %}{% extends default %}"296- "{% else %}{% extends 'default' %}{% endif %}"297- "{%- block x %}CHILD{% endblock %}",290+ "empty": "{% block x required %}{% endblock %}",291+ "blank": "{% block x required %} {# c #}{% endblock %}",292+ "text": "{% block x required %}data {# c #}{% endblock %}",293+ "block": "{% block x required %}{% block y %}"294+ "{% endblock %}{% endblock %}",295+ "if": "{% block x required %}{% if true %}"296+ "{% endif %}{% endblock %}",297+ "top": "{% extends t %}{% block x %}CHILD{% endblock %}",298 }299 )300 )301- t = env.get_template("level1")301+ t = env.get_template("top")302+ assert t.render(t="empty") == "CHILD"303+ assert t.render(t="blank") == "CHILD"304 303- with pytest.raises(305+ required_block_check = pytest.raises(306 TemplateSyntaxError,307 match="Required blocks can only contain comments or whitespace",306- ):307- assert t.render(default="default")308- assert t.render(default="default2")309- assert t.render(default="default3")308+ )309 310+ with required_block_check:311+ t.render(t="text")312+313+ with required_block_check:314+ t.render(t="block")315+316+ with required_block_check:317+ t.render(t="if")318+319 def test_required_with_scope(self, env):320 env = Environment(321 loader=DictLoader(322 {91 unmodified linesmodified:CHANGES.rst+3 added
3 unmodified lines4 -------------5 6 Unreleased7 8+- Fix compiler error when checking if required blocks in parent templates are9+ empty. :pr:`1858`10 11+12 Version 3.1.213 -------------14 15 Released 2022-04-28969 unmodified lines