Django

Code

Ticket #7295 (closed: fixed)

Opened 1 year ago

Last modified 3 months ago

quotes, escaping and translation of string literals handled inconsistently in templates

Reported by: akaihola Assigned to: nobody
Milestone: Component: Template system
Version: SVN Keywords: tplrf-fixed
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 1 Patch needs improvement: 0

Description

The Django template syntax sometimes accepts literal strings only in double quotes, whereas in other situations also single quotes work. Including a quote in a string by escaping it with a backslash doesn't work everywhere. Implementing SmileyChris's suggestion to #5756 (filter expressions as arguments for the cycle, ifchanged, ifequal, firstof and include tags) is difficult because of incosistencies in the underlying mechanisms.

String literals are dealt with in the FilterExpression and Variable classes in django/template/__init__.py. The classes contain two different and overlapping approaches to unquoting string literals. FilterExpression embeds unquoting in the regex which parses filter expressions, and Variable takes a more "manual" approach.

With some modifications the following could be achieved:

  • both single and double quoted strings work everywhere
  • filters as template tag arguments easier to implement (see #5756)
  • escaping quotes works consistently
  • code re-use (DRY)

The challenge is to not lose performance.

Attachments

7295.1.diff (10.6 kB) - added by akaihola on 05/23/08 06:30:43.
Patch for fixing issues with string literals in templates
7295.2.diff (11.2 kB) - added by akaihola on 05/23/08 10:30:18.
Patch now handles un-escaping correctly and optimizes numeric literals.

Change History

05/23/08 06:30:43 changed by akaihola

  • attachment 7295.1.diff added.

Patch for fixing issues with string literals in templates

05/23/08 07:17:58 changed by akaihola

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

The template tag argument splitter seems to strip backslashes. This has to be addressed before this patch is valid.

05/23/08 10:30:18 changed by akaihola

  • attachment 7295.2.diff added.

Patch now handles un-escaping correctly and optimizes numeric literals.

05/23/08 10:37:37 changed by akaihola

The patch above delegates filter expression constant resolution to the Variable class to make sure that the same mechanism is used for both.

Resolving numeric constants is optimized compared to current trunk, since they are now resolved at template compile time. Trunk resolves them on every render.

django.utils.text.smart_split() is used only in template/__init__.py and it's hard to come up a use case outside template tag processing with its current behavior (see django-developers message). To make things simpler I moved the un-escaping behavior out from it to the unescape_string_literal() function.

As a result, variable resolution now supports quote escaping.

This patch passes all current tests, and makes it possible to implement #5756 as well.

07/13/08 05:32:57 changed by serialx

  • needs_tests set to 1.
  • stage changed from Unreviewed to Accepted.

07/18/08 14:05:05 changed by emulbreh

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

But the smart_split() change would be useful anyway.

07/22/08 12:22:09 changed by mir

  • milestone set to post-1.0.

10/24/08 09:19:26 changed by anonymous

  • keywords set to tplrf-fixed.

02/25/09 13:51:44 changed by

  • milestone deleted.

Milestone post-1.0 deleted

03/23/09 04:40:25 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [10118]) Added consistent support for double- and single-quote delimiters in templates.

Some template filters and tags understood single-quoted arguments, others didn't. This makes everything consistent. Based on a patch from akaihola.

Fixed #7295.


Add/Change #7295 (quotes, escaping and translation of string literals handled inconsistently in templates)




Change Properties
Action