Ticket #6510: 6510.2.diff

File 6510.2.diff, 5.3 KB (added by SmileyChris, 5 years ago)
  • django/template/__init__.py

    diff --git a/django/template/__init__.py b/django/template/__init__.py
    index 7fb01f0..33ea0aa 100644
    a b class Node(object): 
    770770    # Set this to True for nodes that must be first in the template (although
    771771    # they can be preceded by text nodes.
    772772    must_be_first = False
     773    child_nodelists = ('nodelist',)
    773774
    774775    def render(self, context):
    775776        "Return the node rendered as a string"
    class Node(object): 
    783784        nodes = []
    784785        if isinstance(self, nodetype):
    785786            nodes.append(self)
    786         if hasattr(self, 'nodelist'):
    787             nodes.extend(self.nodelist.get_nodes_by_type(nodetype))
     787        for attr in self.child_nodelists:
     788            nodelist = getattr(self, attr, None)
     789            if nodelist:
     790                nodes.extend(nodelist.get_nodes_by_type(nodetype))
    788791        return nodes
    789792
    790793class NodeList(list):
  • django/template/defaulttags.py

    diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
    index 69afd84..4b0a4d1 100644
    a b class FirstOfNode(Node): 
    9797        return u''
    9898
    9999class ForNode(Node):
     100    child_nodelists = ('nodelist_loop', 'nodelist_empty')
     101
    100102    def __init__(self, loopvars, sequence, is_reversed, nodelist_loop, nodelist_empty=None):
    101103        self.loopvars, self.sequence = loopvars, sequence
    102104        self.is_reversed = is_reversed
    class ForNode(Node): 
    118120        for node in self.nodelist_empty:
    119121            yield node
    120122
    121     def get_nodes_by_type(self, nodetype):
    122         nodes = []
    123         if isinstance(self, nodetype):
    124             nodes.append(self)
    125         nodes.extend(self.nodelist_loop.get_nodes_by_type(nodetype))
    126         nodes.extend(self.nodelist_empty.get_nodes_by_type(nodetype))
    127         return nodes
    128 
    129123    def render(self, context):
    130124        if 'forloop' in context:
    131125            parentloop = context['forloop']
    class ForNode(Node): 
    181175        return nodelist.render(context)
    182176
    183177class IfChangedNode(Node):
     178    child_nodelists = ('nodelist_true', 'nodelist_false')
     179
    184180    def __init__(self, nodelist_true, nodelist_false, *varlist):
    185181        self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false
    186182        self._last_seen = None
    class IfChangedNode(Node): 
    211207        return ''
    212208
    213209class IfEqualNode(Node):
     210    child_nodelists = ('nodelist_true', 'nodelist_false')
     211
    214212    def __init__(self, var1, var2, nodelist_true, nodelist_false, negate):
    215213        self.var1, self.var2 = var1, var2
    216214        self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false
    class IfEqualNode(Node): 
    227225        return self.nodelist_false.render(context)
    228226
    229227class IfNode(Node):
     228    child_nodelists = ('nodelist_true', 'nodelist_false')
     229
    230230    def __init__(self, var, nodelist_true, nodelist_false=None):
    231231        self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false
    232232        self.var = var
    class IfNode(Node): 
    240240        for node in self.nodelist_false:
    241241            yield node
    242242
    243     def get_nodes_by_type(self, nodetype):
    244         nodes = []
    245         if isinstance(self, nodetype):
    246             nodes.append(self)
    247         nodes.extend(self.nodelist_true.get_nodes_by_type(nodetype))
    248         nodes.extend(self.nodelist_false.get_nodes_by_type(nodetype))
    249         return nodes
    250 
    251243    def render(self, context):
    252244        if self.var.eval(context):
    253245            return self.nodelist_true.render(context)
  • new file tests/regressiontests/templates/nodelist.py

    diff --git a/tests/regressiontests/templates/nodelist.py b/tests/regressiontests/templates/nodelist.py
    new file mode 100644
    index 0000000..89fac97
    - +  
     1from unittest import TestCase
     2from django.template.loader import get_template_from_string
     3from django.template import VariableNode
     4
     5
     6class NodelistTest(TestCase):
     7
     8    def test_for(self):
     9        source = '{% for i in 1 %}{{ a }}{% endfor %}'
     10        template = get_template_from_string(source)
     11        vars = template.nodelist.get_nodes_by_type(VariableNode)
     12        self.assertEqual(len(vars), 1)
     13
     14    def test_if(self):
     15        source = '{% if x %}{{ a }}{% endif %}'
     16        template = get_template_from_string(source)
     17        vars = template.nodelist.get_nodes_by_type(VariableNode)
     18        self.assertEqual(len(vars), 1)
     19
     20    def test_ifequal(self):
     21        source = '{% ifequal x y %}{{ a }}{% endifequal %}'
     22        template = get_template_from_string(source)
     23        vars = template.nodelist.get_nodes_by_type(VariableNode)
     24        self.assertEqual(len(vars), 1)
     25
     26    def test_ifchanged(self):
     27        source = '{% ifchanged x %}{{ a }}{% endifchanged %}'
     28        template = get_template_from_string(source)
     29        vars = template.nodelist.get_nodes_by_type(VariableNode)
     30        self.assertEqual(len(vars), 1)
  • tests/regressiontests/templates/tests.py

    diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
    index 31c9e24..3e7b7fb 100644
    a b from context import context_tests 
    2424from custom import custom_filters
    2525from parser import token_parsing, filter_parsing, variable_parsing
    2626from unicode import unicode_tests
     27from nodelist import NodelistTest
    2728from smartif import *
    2829
    2930try:
Back to Top