Skip to content
Myr

added unittest for Markup and let the markup constructor accept `__html__` objects

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 be1dca1 · Browse files · 4 files+56 added−13 removed
This commit

State

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

Diff stats

4 files+56 added−13 removed

  • tests:+50 added−12 removed
  • other:+6 added−1 removed

4 files+56 added−13 removed

modified:jinja2/filters.py+1 added-1 removed
72 unmodified lines
73 not hasattr(s, '__html__'):74 s = escape(s)75 else:76 s = soft_unicode(s)77 return s.replace(old, new, count)77 return s.replace(soft_unicode(old), soft_unicode(new), count)787980def do_upper(s):81 """Convert a value to uppercase."""
583 unmodified lines
modified:jinja2/utils.py+5 added
232 unmodified lines
233 of the environment to `escape`.234 """235 __slots__ = ()236237 def __new__(cls, base=u''):238 if hasattr(base, '__html__'):239 base = base.__html__()240 return unicode.__new__(cls, base)241242 def __html__(self):243 return self244245 def __add__(self, other):
323 unmodified lines
modified:tests/test_filters.py+19 added-12 removed
1 unmodified line
2"""3 unit test for the filters4 ~~~~~~~~~~~~~~~~~~~~~~~~~56 Missing tests:78 - wordcount9 - rst10 - markdown11 - textile1213 :copyright: 2007 by Armin Ronacher.6 :copyright: 2008 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8"""16from jinja2 import Markup9from jinja2 import Markup, Environment101112CAPITALIZE = '''{{ "foo bar"|capitalize }}'''13CENTER = '''{{ "foo"|center(9) }}'''14DEFAULT = '''{{ missing|default("no") }}|{{ false|default('no') }}|\15{{ false|default('no', true) }}|{{ given|default("no") }}'''
141 unmodified lines
157 tmpl = env.from_string(JOIN)158 out = tmpl.render()159 assert out == '1|2|3'160161 env2 = Environment(autoescape=True)162 tmpl = env2.from_string('{{ ["<foo>", "<span>foo</span>"|safe]|join }}')163 assert tmpl.render() == '&lt;foo&gt;<span>foo</span>'164165166def test_last(env):167 tmpl = env.from_string(LAST)168 out = tmpl.render(foo=range(10))169 assert out == '9'
120 unmodified lines
290 tmpl = env.from_string(FILTERTAG)291 assert tmpl.render() == 'fooBAR'292293296def test_replace(env):297 tmpl = env.from_string('{{ "foo"|replace("o", 42)}}')298 assert tmpl.render() == 'f4242'294def test_replace():295 env = Environment()296 tmpl = env.from_string('{{ string|replace("o", 42) }}')297 assert tmpl.render(string='<foo>') == '<f4242>'298299 env = Environment(autoescape=True)300 tmpl = env.from_string('{{ string|replace("o", 42) }}')301 assert tmpl.render(string='<foo>') == '&lt;f4242&gt;'302 tmpl = env.from_string('{{ string|replace("<", 42) }}')303 assert tmpl.render(string='<foo>') == '42foo&gt;'304 tmpl = env.from_string('{{ string|replace("o", ">x<") }}')305 assert tmpl.render(string=Markup('foo')) == 'f&gt;x&lt;&gt;x&lt;'306307308def test_forceescape(env):309 tmpl = env.from_string('{{ x|forceescape }}')310 assert tmpl.render(x=Markup('<div />')) == u'&lt;div /&gt;'
modified:tests/test_security.py+31 added
6 unmodified lines
7 :license: BSD, see LICENSE for more details.8"""9from jinja2.sandbox import SandboxedEnvironment, \10 ImmutableSandboxedEnvironment, unsafe11from jinja2 import Markup, escape121314class PrivateStuff(object):15
66 unmodified lines
82Traceback (most recent call last):83 ...84SecurityError: access to attribute 'clear' of 'dict' object is unsafe.85'''8687def test_markup_operations():88 # adding two strings should escape the unsafe one89 unsafe = '<script type="application/x-some-script">alert("foo");</script>'90 safe = Markup('<em>username</em>')91 assert unsafe + safe == unicode(escape(unsafe)) + unicode(safe)9293 # string interpolations are safe to use too94 assert Markup('<em>%s</em>') % '<bad user>' == \95 '<em>&lt;bad user&gt;</em>'96 assert Markup('<em>%(username)s</em>') % {97 'username': '<bad user>'98 } == '<em>&lt;bad user&gt;</em>'99100 # an escaped object is markup too101 assert type(Markup('foo') + 'bar') is Markup102103 # and it implements __html__ by returning itself104 x = Markup("foo")105 assert x.__html__() is x106107 # it also knows how to treat __html__ objects108 class Foo(object):109 def __html__(self):110 return '<em>awesome</em>'111 def __unicode__(self):112 return 'awesome'113 assert Markup(Foo()) == '<em>awesome</em>'114 assert Markup('<strong>%s</strong>') % Foo() == \115 '<strong><em>awesome</em></strong>'