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
Provenance
- Commit
9cf9591- Tree
7a22163- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
4e6f9a2
8 files changed+34 added−22 removed
modified:jinja2/compiler.py+14 added-2 removed
13 unmodified lines14 from itertools import chain15 from jinja2 import nodes16 from jinja2.visitor import NodeVisitor, NodeTransformer17 from jinja2.exceptions import TemplateAssertionError18-from jinja2.utils import Markup, concat18+from jinja2.utils import Markup, concat, escape19 20 21 operators = {22 'eq': '==',1038 unmodified lines1061 # 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 linesmodified:jinja2/environment.py+1 added-2 removed
640 unmodified lines641 """642 643 def __init__(self, gen):644 self._gen = gen645- self._next = gen.next646- self.buffered = False645+ self.disable_buffering()646 647 def disable_buffering(self):648 """Disable the output buffering."""649 self._next = self._gen.next40 unmodified linesmodified:jinja2/filters.py+1 added-2 removed
576 unmodified lines577 578 579 class _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))582 583 def __new__(cls, (key, value)):584 return tuple.__new__(cls, (key, list(value)))585 78 unmodified linesmodified:jinja2/lexer.py+1 added-1 removed
193 unmodified lines194 if self.current.test(expr):195 return self.next()196 197 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 None200 201 def next(self):202 """Go one token ahead and return the old one"""353 unmodified linesmodified:jinja2/nodes.py-15 removed
249 unmodified lines250 This is used both for the `print` statement and the regular template data.251 """252 fields = ('nodes',)253 254- 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 buffer254 269-255 class Extends(Stmt):256 """Represents an extends statement."""257 fields = ('template',)258 523 unmodified linesmodified:tests/test_ext.py+4 added
64 unmodified lines65 def test_extension_nodes():66 env = Environment(extensions=[TestExtension])67 tmpl = env.from_string('{% test %}')68 assert tmpl.render() == 'False|42|23|{}'69+70+71+def test_identifier():72+ assert TestExtension.identifier == __name__ + '.TestExtension'modified:tests/test_filters.py+8 added
306 unmodified lines307 308 def test_forceescape(env):309 tmpl = env.from_string('{{ x|forceescape }}')310 assert tmpl.render(x=Markup('<div />')) == u'<div />'311+312+313+def 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() == '<div>foo</div>'modified:tests/test_security.py+5 added
111 unmodified lines112 return 'awesome'113 assert Markup(Foo()) == '<em>awesome</em>'114 assert Markup('<strong>%s</strong>') % Foo() == \115 '<strong><em>awesome</em></strong>'116+117+ # escaping and unescaping118+ assert escape('"<>&\'') == '"<>&''119+ assert Markup("<em>Foo & Bar</em>").striptags() == "Foo & Bar"120+ assert Markup("<test>").unescape() == "<test>"