Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…ents.
  • Loading branch information...
commit 81804ae474d6fe5d3ab9348cfe86ee3779473a2a 1 parent 4cccb85
Baptiste Mispelon authored February 25, 2013
6  django/contrib/comments/templatetags/comments.py
@@ -17,7 +17,7 @@ class BaseCommentNode(template.Node):
17 17
     @classmethod
18 18
     def handle_token(cls, parser, token):
19 19
         """Class method to parse get_comment_list/count/form and return a Node."""
20  
-        tokens = token.contents.split()
  20
+        tokens = token.split_contents()
21 21
         if tokens[1] != 'for':
22 22
             raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])
23 23
 
@@ -146,7 +146,7 @@ class RenderCommentFormNode(CommentFormNode):
146 146
     @classmethod
147 147
     def handle_token(cls, parser, token):
148 148
         """Class method to parse render_comment_form and return a Node."""
149  
-        tokens = token.contents.split()
  149
+        tokens = token.split_contents()
150 150
         if tokens[1] != 'for':
151 151
             raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])
152 152
 
@@ -182,7 +182,7 @@ class RenderCommentListNode(CommentListNode):
182 182
     @classmethod
183 183
     def handle_token(cls, parser, token):
184 184
         """Class method to parse render_comment_list and return a Node."""
185  
-        tokens = token.contents.split()
  185
+        tokens = token.split_contents()
186 186
         if tokens[1] != 'for':
187 187
             raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])
188 188
 
36  tests/comment_tests/tests/templatetag_tests.py
@@ -3,11 +3,19 @@
3 3
 from django.contrib.comments.forms import CommentForm
4 4
 from django.contrib.comments.models import Comment
5 5
 from django.contrib.contenttypes.models import ContentType
6  
-from django.template import Template, Context
  6
+from django.template import Template, Context, Library, libraries
7 7
 
8 8
 from ..models import Article, Author
9 9
 from . import CommentTestCase
10 10
 
  11
+register = Library()
  12
+
  13
+@register.filter
  14
+def noop(variable, param=None):
  15
+    return variable
  16
+
  17
+libraries['comment_testtags'] = register
  18
+
11 19
 
12 20
 class CommentTemplateTagTests(CommentTestCase):
13 21
 
@@ -32,6 +40,9 @@ def testGetCommentFormFromLiteral(self):
32 40
     def testGetCommentFormFromObject(self):
33 41
         self.testGetCommentForm("{% get_comment_form for a as form %}")
34 42
 
  43
+    def testWhitespaceInGetCommentFormTag(self):
  44
+        self.testGetCommentForm("{% load comment_testtags %}{% get_comment_form for a|noop:'x y' as form %}")
  45
+
35 46
     def testRenderCommentForm(self, tag=None):
36 47
         t = "{% load comments %}" + (tag or "{% render_comment_form for comment_tests.article a.id %}")
37 48
         ctx, out = self.render(t, a=Article.objects.get(pk=1))
@@ -44,6 +55,9 @@ def testRenderCommentFormFromLiteral(self):
44 55
     def testRenderCommentFormFromObject(self):
45 56
         self.testRenderCommentForm("{% render_comment_form for a %}")
46 57
 
  58
+    def testWhitespaceInRenderCommentFormTag(self):
  59
+        self.testRenderCommentForm("{% load comment_testtags %}{% render_comment_form for a|noop:'x y' %}")
  60
+
47 61
     def testRenderCommentFormFromObjectWithQueryCount(self):
48 62
         with self.assertNumQueries(1):
49 63
             self.testRenderCommentFormFromObject()
@@ -65,6 +79,10 @@ def testGetCommentCountFromObject(self):
65 79
         self.createSomeComments()
66 80
         self.verifyGetCommentCount("{% get_comment_count for a as cc %}")
67 81
 
  82
+    def testWhitespaceInGetCommentCountTag(self):
  83
+        self.createSomeComments()
  84
+        self.verifyGetCommentCount("{% load comment_testtags %}{% get_comment_count for a|noop:'x y' as cc %}")
  85
+
68 86
     def verifyGetCommentList(self, tag=None):
69 87
         c1, c2, c3, c4 = Comment.objects.all()[:4]
70 88
         t = "{% load comments %}" +  (tag or "{% get_comment_list for comment_tests.author a.id as cl %}")
@@ -84,6 +102,10 @@ def testGetCommentListFromObject(self):
84 102
         self.createSomeComments()
85 103
         self.verifyGetCommentList("{% get_comment_list for a as cl %}")
86 104
 
  105
+    def testWhitespaceInGetCommentListTag(self):
  106
+        self.createSomeComments()
  107
+        self.verifyGetCommentList("{% load comment_testtags %}{% get_comment_list for a|noop:'x y' as cl %}")
  108
+
87 109
     def testGetCommentPermalink(self):
88 110
         c1, c2, c3, c4 = self.createSomeComments()
89 111
         t = "{% load comments %}{% get_comment_list for comment_tests.author author.id as cl %}"
@@ -102,6 +124,15 @@ def testGetCommentPermalinkFormatted(self):
102 124
         ctx, out = self.render(t, author=author)
103 125
         self.assertEqual(out, "/cr/%s/%s/#c%s-by-Joe Somebody" % (ct.id, author.id, c2.id))
104 126
 
  127
+    def testWhitespaceInGetCommentPermalinkTag(self):
  128
+        c1, c2, c3, c4 = self.createSomeComments()
  129
+        t = "{% load comments comment_testtags %}{% get_comment_list for comment_tests.author author.id as cl %}"
  130
+        t += "{% get_comment_permalink cl.0|noop:'x y' %}"
  131
+        ct = ContentType.objects.get_for_model(Author)
  132
+        author = Author.objects.get(pk=1)
  133
+        ctx, out = self.render(t, author=author)
  134
+        self.assertEqual(out, "/cr/%s/%s/#c%s" % (ct.id, author.id, c2.id))
  135
+
105 136
     def testRenderCommentList(self, tag=None):
106 137
         t = "{% load comments %}" + (tag or "{% render_comment_list for comment_tests.article a.id %}")
107 138
         ctx, out = self.render(t, a=Article.objects.get(pk=1))
@@ -114,6 +145,9 @@ def testRenderCommentListFromLiteral(self):
114 145
     def testRenderCommentListFromObject(self):
115 146
         self.testRenderCommentList("{% render_comment_list for a %}")
116 147
 
  148
+    def testWhitespaceInRenderCommentListTag(self):
  149
+        self.testRenderCommentList("{% load comment_testtags %}{% render_comment_list for a|noop:'x y' %}")
  150
+
117 151
     def testNumberQueries(self):
118 152
         """
119 153
         Ensure that the template tags use cached content types to reduce the

0 notes on commit 81804ae

Please sign in to comment.
Something went wrong with that request. Please try again.