Django

Code

Ticket #6271 (new)

Opened 1 year ago

Last modified 3 months ago

filter arguments with spaces break several template tags

Reported by: Rob Hudson <treborhudson@gmail.com> Assigned to:
Milestone: Component: Template system
Version: SVN Keywords: tplrf-fixed
Cc: serg@rainboo.com Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 1

Description (Last modified by gwilson)

After [6956], something like this now breaks:

{% regroup object_list by created|date:"F Y" as grouped %}

I believe it now splits the quoted bits and the arg count is more than 6 in this case, so the templatetag errors out.

Attachments

regroup_regression_test.diff (2.2 kB) - added by Rob Hudson <treborhudson@gmail.com> on 12/27/07 15:43:05.
A test case for regroup with filters containing a space
6271.diff (9.5 kB) - added by gwilson on 12/31/07 02:51:45.
smart_split.diff (2.5 kB) - added by SmileyChris on 12/31/07 17:51:35.
A rewrite of smart_split generator to properly handle quoted phrases like we need
smart_split.2.diff (5.6 kB) - added by gwilson on 01/06/08 19:44:20.

Change History

12/27/07 15:43:05 changed by Rob Hudson <treborhudson@gmail.com>

  • attachment regroup_regression_test.diff added.

A test case for regroup with filters containing a space

12/31/07 02:51:20 changed by gwilson

  • description changed.
  • needs_better_patch set to 1.
  • needs_tests changed.
  • summary changed from Changeset 6956 broke regroup tag if filter contains a space to filter arguments with spaces break several template tags.
  • needs_docs changed.
  • has_patch set to 1.
  • stage changed from Unreviewed to Accepted.

The bigger problem here is that many of the template tags do not accept filtered arguments passed an argument with a space because they are simply doing token.contents.split().

Here is a patch that seems to fix things for regroup, ifchanged, and with. firstof, if, and ifequal/ifnotequal need some more (involved) work. I changed smart_split_re to allow text before and after the quote characters. Not sure if we want smart_split() to basically only be for parsing template tags, but it seems to be the only place it's used currently.

12/31/07 02:51:45 changed by gwilson

  • attachment 6271.diff added.

12/31/07 17:51:35 changed by SmileyChris

  • attachment smart_split.diff added.

A rewrite of smart_split generator to properly handle quoted phrases like we need

12/31/07 17:54:22 changed by SmileyChris

I had been thinking about this recently too.

Just allowing text before and after quote characters still isn't good enough. If multiple filters are used which have arguments with spaces then it'll still fall over. Here's my rewrite of the smart_split method to handle it like we need.

01/04/08 18:04:06 changed by adrian

Note that I reverted [6956] in [6996].

01/06/08 16:20:08 changed by gwilson

  • owner changed from nobody to gwilson.
  • status changed from new to assigned.

01/06/08 19:44:00 changed by gwilson

  • owner deleted.
  • status changed from assigned to new.

So there seems to be a problem here in that smart_split is replacing escaped quotes and escaped backslashes with the quote and backslash respectively. The bits from the split are sometimes passed to django.template.compile_filter, which creates a FilterExpression object, which again looks for escaped quotes in filter arguments, and causes a parse error.

I'm thinking that we make smart_split just split and not replace any escaped quotes or backslashes. Then we make sure compile_filter is being used everywhere it needs to be used. Let compile filter do the processing of escaped quotes, translation strings, multiple filters, arguments with spaces, etc.

Anyway, here is a patch for smart_split that takes SmileyChris's regex changes, adds the smart_split tests from my first patch (plus a few more), and removes the unescaping of quotes and backslashes in smart_split...

01/06/08 19:44:20 changed by gwilson

  • attachment smart_split.2.diff added.

03/28/08 08:33:57 changed by anonymous

  • cc set to serg@rainboo.com.

07/18/08 14:02:38 changed by emulbreh

  • keywords set to tplrf-fixed.

This would be fixed by the refactoring proposed in #7806.

09/05/08 16:37:38 changed by dc

#8902 has been marked as a duplicate of this ticket. It also contains small patch.


Add/Change #6271 (filter arguments with spaces break several template tags)




Change Properties
Action