Skip to content
Myr

fixed bug with static unicode strings and auto escaping

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 4e6f9a2 · Browse files · 8 files+34 added−22 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+34 added−22 removed

  • tests:+17 added−0 removed
  • other:+17 added−22 removed

8 files+34 added−22 removed

modified:jinja2/compiler.py+14 added-2 removed
13 unmodified lines
14from itertools import chain15from jinja2 import nodes16from jinja2.visitor import NodeVisitor, NodeTransformer17from jinja2.exceptions import TemplateAssertionError18from jinja2.utils import Markup, concat18from jinja2.utils import Markup, concat, escape192021operators = {22 'eq': '==',
1038 unmodified lines
1061 # string at compile time.1062 body = []1063 for child in node.nodes:1064 try:1065 const = unicode(child.as_const())1065 const = child.as_const()1066 except nodes.Impossible:1067 body.append(child)1068 continue1069 try:1070 if self.environment.autoescape:1071 if hasattr(const, '__html__'):1072 const = const.__html__()1073 else:1074 const = escape(const)1075 const = unicode(const)1076 except:1077 # if something goes wrong here we evaluate the node1078 # at runtime for easier debugging1079 body.append(child)1080 continue1081 if body and isinstance(body[-1], list):1082 body[-1].append(const)
311 unmodified lines
modified:jinja2/environment.py+1 added-2 removed
640 unmodified lines
641 """642643 def __init__(self, gen):644 self._gen = gen645 self._next = gen.next646 self.buffered = False645 self.disable_buffering()646647 def disable_buffering(self):648 """Disable the output buffering."""649 self._next = self._gen.next
40 unmodified lines
modified:jinja2/filters.py+1 added-2 removed
576 unmodified lines
577578579class _GroupTuple(tuple):580 __slots__ = ()581 grouper = property(itemgetter(0))582 list = property(itemgetter(1))581 grouper, list = (property(itemgetter(x)) for x in xrange(2))582583 def __new__(cls, (key, value)):584 return tuple.__new__(cls, (key, list(value)))585
78 unmodified lines
modified:jinja2/lexer.py+1 added-1 removed
193 unmodified lines
194 if self.current.test(expr):195 return self.next()196197 def skip_if(self, expr):198 """Like `next_if` but only returns `True` or `False`."""198 """Like :meth:`next_if` but only returns `True` or `False`."""199 return self.next_if(expr) is not None200201 def next(self):202 """Go one token ahead and return the old one"""
353 unmodified lines
modified:jinja2/nodes.py-15 removed
249 unmodified lines
250 This is used both for the `print` statement and the regular template data.251 """252 fields = ('nodes',)253254 def optimized_nodes(self):255 """Try to optimize the nodes."""256 buffer = []257 for node in self.nodes:258 try:259 const = unicode(node.as_const())260 except:261 buffer.append(node)262 else:263 if buffer and isinstance(buffer[-1], unicode):264 buffer[-1] += const265 else:266 buffer.append(const)267 return buffer254269255class Extends(Stmt):256 """Represents an extends statement."""257 fields = ('template',)258
523 unmodified lines
modified:tests/test_ext.py+4 added
64 unmodified lines
65def test_extension_nodes():66 env = Environment(extensions=[TestExtension])67 tmpl = env.from_string('{% test %}')68 assert tmpl.render() == 'False|42|23|{}'697071def test_identifier():72 assert TestExtension.identifier == __name__ + '.TestExtension'
modified:tests/test_filters.py+8 added
306 unmodified lines
307308def test_forceescape(env):309 tmpl = env.from_string('{{ x|forceescape }}')310 assert tmpl.render(x=Markup('<div />')) == u'&lt;div /&gt;'311312313def test_safe():314 env = Environment(autoescape=True)315 tmpl = env.from_string('{{ "<div>foo</div>"|safe }}')316 assert tmpl.render() == '<div>foo</div>'317 tmpl = env.from_string('{{ "<div>foo</div>" }}')318 assert tmpl.render() == '&lt;div&gt;foo&lt;/div&gt;'
modified:tests/test_security.py+5 added
111 unmodified lines
112 return 'awesome'113 assert Markup(Foo()) == '<em>awesome</em>'114 assert Markup('<strong>%s</strong>') % Foo() == \115 '<strong><em>awesome</em></strong>'116117 # escaping and unescaping118 assert escape('"<>&\'') == '&#34;&lt;&gt;&amp;&#39;'119 assert Markup("<em>Foo &amp; Bar</em>").striptags() == "Foo & Bar"120 assert Markup("&lt;test&gt;").unescape() == "<test>"