Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix 19916. Smarter tokenizing of contrib.comment's templatetags argum…

…ents.
  • Loading branch information...
commit 81804ae474d6fe5d3ab9348cfe86ee3779473a2a 1 parent 4cccb85
@bmispelon bmispelon authored
View
6 django/contrib/comments/templatetags/comments.py
@@ -17,7 +17,7 @@ class BaseCommentNode(template.Node):
@classmethod
def handle_token(cls, parser, token):
"""Class method to parse get_comment_list/count/form and return a Node."""
- tokens = token.contents.split()
+ tokens = token.split_contents()
if tokens[1] != 'for':
raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])
@@ -146,7 +146,7 @@ class RenderCommentFormNode(CommentFormNode):
@classmethod
def handle_token(cls, parser, token):
"""Class method to parse render_comment_form and return a Node."""
- tokens = token.contents.split()
+ tokens = token.split_contents()
if tokens[1] != 'for':
raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])
@@ -182,7 +182,7 @@ class RenderCommentListNode(CommentListNode):
@classmethod
def handle_token(cls, parser, token):
"""Class method to parse render_comment_list and return a Node."""
- tokens = token.contents.split()
+ tokens = token.split_contents()
if tokens[1] != 'for':
raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])
View
36 tests/comment_tests/tests/templatetag_tests.py
@@ -3,11 +3,19 @@
from django.contrib.comments.forms import CommentForm
from django.contrib.comments.models import Comment
from django.contrib.contenttypes.models import ContentType
-from django.template import Template, Context
+from django.template import Template, Context, Library, libraries
from ..models import Article, Author
from . import CommentTestCase
+register = Library()
+
+@register.filter
+def noop(variable, param=None):
+ return variable
+
+libraries['comment_testtags'] = register
+
class CommentTemplateTagTests(CommentTestCase):
@@ -32,6 +40,9 @@ def testGetCommentFormFromLiteral(self):
def testGetCommentFormFromObject(self):
self.testGetCommentForm("{% get_comment_form for a as form %}")
+ def testWhitespaceInGetCommentFormTag(self):
+ self.testGetCommentForm("{% load comment_testtags %}{% get_comment_form for a|noop:'x y' as form %}")
+
def testRenderCommentForm(self, tag=None):
t = "{% load comments %}" + (tag or "{% render_comment_form for comment_tests.article a.id %}")
ctx, out = self.render(t, a=Article.objects.get(pk=1))
@@ -44,6 +55,9 @@ def testRenderCommentFormFromLiteral(self):
def testRenderCommentFormFromObject(self):
self.testRenderCommentForm("{% render_comment_form for a %}")
+ def testWhitespaceInRenderCommentFormTag(self):
+ self.testRenderCommentForm("{% load comment_testtags %}{% render_comment_form for a|noop:'x y' %}")
+
def testRenderCommentFormFromObjectWithQueryCount(self):
with self.assertNumQueries(1):
self.testRenderCommentFormFromObject()
@@ -65,6 +79,10 @@ def testGetCommentCountFromObject(self):
self.createSomeComments()
self.verifyGetCommentCount("{% get_comment_count for a as cc %}")
+ def testWhitespaceInGetCommentCountTag(self):
+ self.createSomeComments()
+ self.verifyGetCommentCount("{% load comment_testtags %}{% get_comment_count for a|noop:'x y' as cc %}")
+
def verifyGetCommentList(self, tag=None):
c1, c2, c3, c4 = Comment.objects.all()[:4]
t = "{% load comments %}" + (tag or "{% get_comment_list for comment_tests.author a.id as cl %}")
@@ -84,6 +102,10 @@ def testGetCommentListFromObject(self):
self.createSomeComments()
self.verifyGetCommentList("{% get_comment_list for a as cl %}")
+ def testWhitespaceInGetCommentListTag(self):
+ self.createSomeComments()
+ self.verifyGetCommentList("{% load comment_testtags %}{% get_comment_list for a|noop:'x y' as cl %}")
+
def testGetCommentPermalink(self):
c1, c2, c3, c4 = self.createSomeComments()
t = "{% load comments %}{% get_comment_list for comment_tests.author author.id as cl %}"
@@ -102,6 +124,15 @@ def testGetCommentPermalinkFormatted(self):
ctx, out = self.render(t, author=author)
self.assertEqual(out, "/cr/%s/%s/#c%s-by-Joe Somebody" % (ct.id, author.id, c2.id))
+ def testWhitespaceInGetCommentPermalinkTag(self):
+ c1, c2, c3, c4 = self.createSomeComments()
+ t = "{% load comments comment_testtags %}{% get_comment_list for comment_tests.author author.id as cl %}"
+ t += "{% get_comment_permalink cl.0|noop:'x y' %}"
+ ct = ContentType.objects.get_for_model(Author)
+ author = Author.objects.get(pk=1)
+ ctx, out = self.render(t, author=author)
+ self.assertEqual(out, "/cr/%s/%s/#c%s" % (ct.id, author.id, c2.id))
+
def testRenderCommentList(self, tag=None):
t = "{% load comments %}" + (tag or "{% render_comment_list for comment_tests.article a.id %}")
ctx, out = self.render(t, a=Article.objects.get(pk=1))
@@ -114,6 +145,9 @@ def testRenderCommentListFromLiteral(self):
def testRenderCommentListFromObject(self):
self.testRenderCommentList("{% render_comment_list for a %}")
+ def testWhitespaceInRenderCommentListTag(self):
+ self.testRenderCommentList("{% load comment_testtags %}{% render_comment_list for a|noop:'x y' %}")
+
def testNumberQueries(self):
"""
Ensure that the template tags use cached content types to reduce the
Please sign in to comment.
Something went wrong with that request. Please try again.