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
Provenance
- Commit
4e6f9a2- Tree
d1ab358- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
be1dca1
4 files changed+56 added−13 removed
modified:jinja2/filters.py+1 added-1 removed
72 unmodified lines73 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)78 79 80 def do_upper(s):81 """Convert a value to uppercase."""583 unmodified linesmodified:jinja2/utils.py+5 added
232 unmodified lines233 of the environment to `escape`.234 """235 __slots__ = ()236 237+ def __new__(cls, base=u''):238+ if hasattr(base, '__html__'):239+ base = base.__html__()240+ return unicode.__new__(cls, base)241+242 def __html__(self):243 return self244 245 def __add__(self, other):323 unmodified linesmodified:tests/test_filters.py+19 added-12 removed
1 unmodified line2 """3 unit test for the filters4 ~~~~~~~~~~~~~~~~~~~~~~~~~5 6- Missing tests:7-8- - wordcount9- - rst10- - markdown11- - textile12-13- :copyright: 2007 by Armin Ronacher.6+ :copyright: 2008 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8 """16-from jinja2 import Markup9+from jinja2 import Markup, Environment10 11+12 CAPITALIZE = '''{{ "foo bar"|capitalize }}'''13 CENTER = '''{{ "foo"|center(9) }}'''14 DEFAULT = '''{{ missing|default("no") }}|{{ false|default('no') }}|\15 {{ false|default('no', true) }}|{{ given|default("no") }}'''141 unmodified lines157 tmpl = env.from_string(JOIN)158 out = tmpl.render()159 assert out == '1|2|3'160 161+ env2 = Environment(autoescape=True)162+ tmpl = env2.from_string('{{ ["<foo>", "<span>foo</span>"|safe]|join }}')163+ assert tmpl.render() == '<foo><span>foo</span>'164 165+166 def test_last(env):167 tmpl = env.from_string(LAST)168 out = tmpl.render(foo=range(10))169 assert out == '9'120 unmodified lines290 tmpl = env.from_string(FILTERTAG)291 assert tmpl.render() == 'fooBAR'292 293 296-def test_replace(env):297- tmpl = env.from_string('{{ "foo"|replace("o", 42)}}')298- assert tmpl.render() == 'f4242'294+def test_replace():295+ env = Environment()296+ tmpl = env.from_string('{{ string|replace("o", 42) }}')297+ assert tmpl.render(string='<foo>') == '<f4242>'298 299+ env = Environment(autoescape=True)300+ tmpl = env.from_string('{{ string|replace("o", 42) }}')301+ assert tmpl.render(string='<foo>') == '<f4242>'302+ tmpl = env.from_string('{{ string|replace("<", 42) }}')303+ assert tmpl.render(string='<foo>') == '42foo>'304+ tmpl = env.from_string('{{ string|replace("o", ">x<") }}')305+ assert tmpl.render(string=Markup('foo')) == 'f>x<>x<'306 307+308 def test_forceescape(env):309 tmpl = env.from_string('{{ x|forceescape }}')310 assert tmpl.render(x=Markup('<div />')) == u'<div />'modified:tests/test_security.py+31 added
6 unmodified lines7 :license: BSD, see LICENSE for more details.8 """9 from jinja2.sandbox import SandboxedEnvironment, \10 ImmutableSandboxedEnvironment, unsafe11+from jinja2 import Markup, escape12 13 14 class PrivateStuff(object):15 66 unmodified lines82 Traceback (most recent call last):83 ...84 SecurityError: access to attribute 'clear' of 'dict' object is unsafe.85 '''86+87+def 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)92+93+ # string interpolations are safe to use too94+ assert Markup('<em>%s</em>') % '<bad user>' == \95+ '<em><bad user></em>'96+ assert Markup('<em>%(username)s</em>') % {97+ 'username': '<bad user>'98+ } == '<em><bad user></em>'99+100+ # an escaped object is markup too101+ assert type(Markup('foo') + 'bar') is Markup102+103+ # and it implements __html__ by returning itself104+ x = Markup("foo")105+ assert x.__html__() is x106+107+ # 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>'