Fixed #25848 -- Set template origin on each node.

Prior to 55f12f8709, the template origin was available on each node via
`self.token.source[0]`. This behavior was removed when debug handling was
simplified, but 3rd-party debugging tools still depend on its presence.
This updates the Parser to set origin on individual nodes. This enables the
source template to be determined even when template extending or including is
used.
This commit is contained in:
Preston Timmons 2016-01-14 20:36:05 -06:00 committed by Tim Graham
parent 477274acb4
commit cfda1fa3f8
3 changed files with 22 additions and 2 deletions

View File

@ -225,6 +225,7 @@ class Template(object):
tokens = lexer.tokenize() tokens = lexer.tokenize()
parser = Parser( parser = Parser(
tokens, self.engine.template_libraries, self.engine.template_builtins, tokens, self.engine.template_libraries, self.engine.template_builtins,
self.origin,
) )
try: try:
@ -445,7 +446,7 @@ class DebugLexer(Lexer):
class Parser(object): class Parser(object):
def __init__(self, tokens, libraries=None, builtins=None): def __init__(self, tokens, libraries=None, builtins=None, origin=None):
self.tokens = tokens self.tokens = tokens
self.tags = {} self.tags = {}
self.filters = {} self.filters = {}
@ -459,6 +460,7 @@ class Parser(object):
self.libraries = libraries self.libraries = libraries
for builtin in builtins: for builtin in builtins:
self.add_library(builtin) self.add_library(builtin)
self.origin = origin
def parse(self, parse_until=None): def parse(self, parse_until=None):
""" """
@ -535,8 +537,10 @@ class Parser(object):
) )
if isinstance(nodelist, NodeList) and not isinstance(node, TextNode): if isinstance(nodelist, NodeList) and not isinstance(node, TextNode):
nodelist.contains_nontext = True nodelist.contains_nontext = True
# Set token here since we can't modify the node __init__ method # Set origin and token here since we can't modify the node __init__()
# method.
node.token = token node.token = token
node.origin = self.origin
nodelist.append(node) nodelist.append(node)
def error(self, token, e): def error(self, token, e):

View File

@ -75,3 +75,10 @@ Bugfixes
* Fixed a crash when calling the ``migrate`` command in a test case with the * Fixed a crash when calling the ``migrate`` command in a test case with the
``available_apps`` attribute pointing to an application with migrations ``available_apps`` attribute pointing to an application with migrations
disabled using the ``MIGRATION_MODULES`` setting (:ticket:`26135`). disabled using the ``MIGRATION_MODULES`` setting (:ticket:`26135`).
* Restored the ability for testing and debugging tools to determine the
template from which a node came from, even during template inheritance or
inclusion. Prior to Django 1.9, debugging tools could access the template
origin from the node via ``Node.token.source[0]``. This was an undocumented,
private API. The origin is now available directly on each node using the
``Node.origin`` attribute (:ticket:`25848`).

View File

@ -140,3 +140,12 @@ class TemplateTests(SimpleTestCase):
child = engine.from_string( child = engine.from_string(
'{% extends parent %}{% block content %}child{% endblock %}') '{% extends parent %}{% block content %}child{% endblock %}')
self.assertEqual(child.render(Context({'parent': parent})), 'child') self.assertEqual(child.render(Context({'parent': parent})), 'child')
def test_node_origin(self):
"""
#25848 -- Set origin on Node so debugging tools can determine which
template the node came from even if extending or including templates.
"""
template = Engine().from_string('content')
for node in template.nodelist:
self.assertEqual(node.origin, template.origin)