Opened 5 weeks ago

Closed 2 weeks ago

Last modified 2 weeks ago

#36559 closed Bug (fixed)

partialdef tag embedded in verbatim tag is treated as the source property for that named partial

Reported by: Jacob Walls Owned by: Farhan Ali
Component: Template system Version: dev
Severity: Release blocker Keywords: partials
Cc: Carlton Gibson, Farhan Ali Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Jacob Walls)

Although partial and partialdef are correctly ignored when rendered if embedded within {% verbatim %}, the PartialTemplate.source property is fooled, potentially impacting error reporting:

    @setup(
        {
            "partial_embedded_in_verbatim": (
                "{% verbatim %}\n"
                "{% partialdef testing-name %}\n"
                "{% endverbatim %}\n"
                "{% partialdef testing-name %}\n"
                "<p>Content</p>\n"
                "{% endpartialdef %}\n"
            ),
        },
        test_once=True,
    )
    def test_partial_template_embedded_in_verbatim(self):
        template = self.engine.get_template("partial_embedded_in_verbatim")
        partial_template = template.extra_data["partials"]["testing-name"]
        self.assertIn("Content", partial_template.source)
======================================================================
FAIL: test_partial_template_embedded_in_verbatim (template_tests.syntax_tests.test_partials.PartialTagTests.test_partial_template_embedded_in_verbatim)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/jwalls/django/django/test/utils.py", line 458, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jwalls/django/tests/template_tests/utils.py", line 58, in inner
    func(self)
  File "/Users/jwalls/django/tests/template_tests/syntax_tests/test_partials.py", line 676, in test_partial_template_embedded_in_verbatim
    self.assertIn("Content", partial_template.source)
AssertionError: 'Content' not found in ''

The use case presented here isn't very serious, so I'm loathe to really mark this as a release blocker, but as it's a new feature and that's our process, and I'm not sure if there are other more important uses of the source property, I'll start there.

Change History (13)

comment:1 by Jacob Walls, 5 weeks ago

Description: modified (diff)
Summary: partialdef tag embedded in verbatim tag is treated as the source attribute for that named partialpartialdef tag embedded in verbatim tag is treated as the source property for that named partial

comment:2 by Natalia Bidart, 5 weeks ago

Cc: Carlton Gibson Farhan Ali added
Keywords: partials added
Triage Stage: UnreviewedAccepted

Thank you Jacob, I agree that we should treat as release blocker.

comment:3 by Farhan Ali, 5 weeks ago

Owner: set to Farhan Ali
Status: newassigned

comment:4 by Jacob Walls, 5 weeks ago

Description: modified (diff)

Replaced the sketched regression test, feel free to make further improvements.

comment:5 by Carlton Gibson, 5 weeks ago

The same issue occurs with {% comment %} too. It's dependent on the partial name being reused in the non-active block, and that must appear first in the source.

I'm not sure it's worth the price of entry — sometimes don't do that is the right answer — but I'd be inclined to strip comment and verbatim blocks before looking the match (if this usage is to be supported)

Last edited 5 weeks ago by Carlton Gibson (previous) (diff)

comment:6 by Farhan Ali, 4 weeks ago

Has patch: set

comment:7 by Farhan Ali, 4 weeks ago

We can add a parser approach for getting the source when in non-debug mode, but I don’t think that is being used. Because the source is not accessed when not in debug mode. If we decide to remove the parser approach, we will just need to update the tests because the @setup decorator runs in both debug and non-debug mode.

comment:8 by Jacob Walls, 2 weeks ago

Patch needs improvement: set

comment:9 by Farhan Ali, 2 weeks ago

Patch needs improvement: unset

comment:10 by Jacob Walls, 2 weeks ago

Triage Stage: AcceptedReady for checkin

comment:11 by Jacob Walls <jacobtylerwalls@…>, 2 weeks ago

In 3485599:

Refs #36559 -- Ran template partial source tests in debug mode only.

Added a warning for accessing PartialTemplate.source when debugging is disabled.
Thanks Sarah Boyce for the idea.

comment:12 by Jacob Walls <jacobtylerwalls@…>, 2 weeks ago

Resolution: fixed
Status: assignedclosed

In d82f25d3:

Fixed #36559 -- Respected verbatim and comment blocks in PartialTemplate.source.

comment:13 by Jacob Walls <jacobtylerwalls@…>, 2 weeks ago

In 34bd3ed:

Refs #36559, #35667 -- Used skip_file_prefixes in PartialTemplate.source warning.

Note: See TracTickets for help on using tickets.
Back to Top