Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Tweaked [7082] and [7084] a little bit to also allow comment nodes pr…

…ior to the extends tag.

This would be little less fiddly if we knew nodelist were always of type
NodeList, but they could be normal lists. Or if we merged successive TextNodes,
instead of appending them. Something to think about going forwards.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7089 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 00f96de5d65ef5a2dfbc62ab5d2b369b5a7392cf 1 parent 0cdd65a
Malcolm Tredinnick authored
15  django/template/__init__.py
@@ -289,9 +289,14 @@ def create_nodelist(self):
289 289
         return NodeList()
290 290
 
291 291
     def extend_nodelist(self, nodelist, node, token):
292  
-        if (node.must_be_first and nodelist and
293  
-                (not isinstance(nodelist[0], TextNode) or len(nodelist) > 2)):
294  
-            raise TemplateSyntaxError("%r must be the first tag in the template." % node)
  292
+        if node.must_be_first and nodelist:
  293
+            try:
  294
+                if nodelist.contains_nontext:
  295
+                    raise AttributeError
  296
+            except AttributeError:
  297
+                raise TemplateSyntaxError("%r must be the first tag in the template." % node)
  298
+        if isinstance(nodelist, NodeList) and not isinstance(node, TextNode):
  299
+            nodelist.contains_nontext = True
295 300
         nodelist.append(node)
296 301
 
297 302
     def enter_command(self, command, token):
@@ -732,6 +737,10 @@ def get_nodes_by_type(self, nodetype):
732 737
         return nodes
733 738
 
734 739
 class NodeList(list):
  740
+    # Set to True the first time a non-TextNode is inserted by
  741
+    # extend_nodelist().
  742
+    contains_nontext = False
  743
+
735 744
     def render(self, context):
736 745
         bits = []
737 746
         for node in self:
14  django/template/loader_tags.py
@@ -69,14 +69,10 @@ def get_parent(self, context):
69 69
 
70 70
     def render(self, context):
71 71
         compiled_parent = self.get_parent(context)
72  
-        if len(compiled_parent.nodelist) > 1:
73  
-            n0, n1 = compiled_parent.nodelist[:2]
74  
-        else:
75  
-            n0, n1 = compiled_parent.nodelist[0], None
76  
-        parent_is_child = (isinstance(n0, ExtendsNode) or
77  
-                (isinstance(n0, TextNode) and isinstance(n1, ExtendsNode)))
78  
-        if parent_is_child:
79  
-            extend_node = int(not isinstance(n0, ExtendsNode))
  72
+        pos = 0
  73
+        while isinstance(compiled_parent.nodelist[pos], TextNode):
  74
+            pos += 1
  75
+        parent_is_child = isinstance(compiled_parent.nodelist[pos], ExtendsNode)
80 76
         parent_blocks = dict([(n.name, n) for n in compiled_parent.nodelist.get_nodes_by_type(BlockNode)])
81 77
         for block_node in self.nodelist.get_nodes_by_type(BlockNode):
82 78
             # Check for a BlockNode with this node's name, and replace it if found.
@@ -88,7 +84,7 @@ def render(self, context):
88 84
                 # add this BlockNode to the parent's ExtendsNode nodelist, so
89 85
                 # it'll be checked when the parent node's render() is called.
90 86
                 if parent_is_child:
91  
-                    compiled_parent.nodelist[extend_node].nodelist.append(block_node)
  87
+                    compiled_parent.nodelist[pos].nodelist.append(block_node)
92 88
             else:
93 89
                 # Keep any existing parents and add a new one. Used by BlockNode.
94 90
                 parent_block.parent = block_node.parent

0 notes on commit 00f96de

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