Code

Opened 6 years ago

Closed 4 years ago

#5971 closed (fixed)

django.template.TokenParser inconsistent with parsing filters

Reported by: Dmitri Fedortchenko <zeraien@…> Owned by: nobody
Component: Template system Version: master
Severity: Keywords: template filter
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by mtredinnick)

django.template.TokenParser is inconsistent
when parsing filters that follow constant strings or variables.

Meaning that:

{% tag thevar|filter sometag %}

will produce:

self.value() = "thevar|filter"
self.tag() = "sometag"

However:

{% tag "a value"|filter sometag %}

will produce:

self.value() = "a value"
self.tag() = "|filter"
self.tag() = "sometag"

This does not seem like correct behaviour...
I made a very simple patch for this, thus the outcome of the above:

{% tag "a value"|filter sometag %}

will produce:

value = "a value"|filter
tag = sometag

So now we can simply pass the "value" into a FilterExpression to parse the filters...

PS: I had trouble naming an internal method, feel free to complain about the name ;)

Attachments (4)

tokenparser_filter_fix.diff (2.4 KB) - added by Dmitri Fedortchenko <zeraien@…> 6 years ago.
The patch
tests_for_5971.diff (1.1 KB) - added by adamv 6 years ago.
5971-tokenparser-consistency-r10001.diff (3.3 KB) - added by ramiro 5 years ago.
Unified and updated patches (implementation bt Dmitri Fedortchenko and tests by adamv)
5971-parser_consistency_literals_and_filters-r10246.diff (4.2 KB) - added by ramiro 5 years ago.
Patch updated to port r10118

Download all attachments as: .zip

Change History (9)

Changed 6 years ago by Dmitri Fedortchenko <zeraien@…>

The patch

comment:1 Changed 6 years ago by Dmitri Fedortchenko <zeraien@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

django.template.TokenParser is inconsistent when parsing filters that follow constant strings or variables.

Meaning that {% tag thevar|filter sometag %} will produce:

self.value() = "thevar|filter"
self.tag() = "sometag"

However {% tag "a value"|filter sometag %} will produce:

self.value() = "a value"
self.tag() = "|filter"
self.tag() = "sometag"

This does not seem like correct behaviour... I made a very simple patch for this, thus the outcome of the above:

{% tag "a value"|filter sometag %} will produce:
self.value() = "a value"|filter
self.tag() = sometag

So now we can simply pass the "value" into a FilterExpression? to parse the filters...

See this thread:
http://groups.google.com/group/django-developers/browse_thread/thread/c90b6e29d20724ca

PS: I should learn to use the preview button.
PPS: The self is from the perspective of the implementee of the TokenParser class.

comment:2 Changed 6 years ago by mtredinnick

  • Description modified (diff)
  • Triage Stage changed from Unreviewed to Accepted

Fixed description formatting.

comment:3 Changed 6 years ago by gwilson

  • Needs tests set

Changed 6 years ago by adamv

Changed 5 years ago by ramiro

Unified and updated patches (implementation bt Dmitri Fedortchenko and tests by adamv)

Changed 5 years ago by ramiro

Patch updated to port r10118

comment:4 Changed 5 years ago by ramiro

  • Needs tests unset

I've update the patch to expand regressiontests/template/parser.py (added in r10118) rather than adding a regressiontests/tokenparser/ subdir.

While at it, also made sure the variable_parsing doctests get executed, they had been added in that revision but weren't being run.

comment:5 Changed 4 years ago by jezdez

  • Resolution set to fixed
  • Status changed from new to closed

(In [12471]) Fixed #5971 - Fixed inconsistent behaviour of the TokenParser when parsing filters that follow constant strings or variables. Thanks Dmitri Fedortchenko, Adam Vandenberg and Ramiro Morales.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.