summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPreston Timmons <preston.timmons@rewardstyle.com>2016-01-14 20:36:05 -0600
committerTim Graham <timograham@gmail.com>2016-01-26 06:23:51 -0500
commit218cc71073012cc4e72c150421fc89da626d2923 (patch)
tree342fdbc391eef8f26fd7f82fd620d15ff119402d
parent645fddcd4ee6adaa619b90404b85ccc8d9c316d8 (diff)
[1.9.x] 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. Backport of cfda1fa3f8d95f0f4a369da9021dbd770e5fa44a from master
-rw-r--r--django/template/base.py8
-rw-r--r--docs/releases/1.9.2.txt7
-rw-r--r--tests/template_tests/tests.py9
3 files changed, 22 insertions, 2 deletions
diff --git a/django/template/base.py b/django/template/base.py
index 0df744f43e..30d339a748 100644
--- a/django/template/base.py
+++ b/django/template/base.py
@@ -224,6 +224,7 @@ class Template(object):
tokens = lexer.tokenize()
parser = Parser(
tokens, self.engine.template_libraries, self.engine.template_builtins,
+ self.origin,
)
try:
@@ -444,7 +445,7 @@ class DebugLexer(Lexer):
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.tags = {}
self.filters = {}
@@ -458,6 +459,7 @@ class Parser(object):
self.libraries = libraries
for builtin in builtins:
self.add_library(builtin)
+ self.origin = origin
def parse(self, parse_until=None):
"""
@@ -534,8 +536,10 @@ class Parser(object):
)
if isinstance(nodelist, NodeList) and not isinstance(node, TextNode):
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.origin = self.origin
nodelist.append(node)
def error(self, token, e):
diff --git a/docs/releases/1.9.2.txt b/docs/releases/1.9.2.txt
index 1612a938a2..30527c146a 100644
--- a/docs/releases/1.9.2.txt
+++ b/docs/releases/1.9.2.txt
@@ -75,3 +75,10 @@ Bugfixes
* Fixed a crash when calling the ``migrate`` command in a test case with the
``available_apps`` attribute pointing to an application with migrations
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`).
diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py
index 38d7eaa8bb..e026c1598e 100644
--- a/tests/template_tests/tests.py
+++ b/tests/template_tests/tests.py
@@ -140,3 +140,12 @@ class TemplateTests(SimpleTestCase):
child = engine.from_string(
'{% extends parent %}{% block content %}child{% endblock %}')
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)