writer.current_template.autoescape)
writer.write_line("_buffer.append(_tmp)")
+class _Module(_Expression):
+ def __init__(self, expression):
+ super(_Module, self).__init__("modules." + expression,
+ raw=True)
class _Text(_Node):
def __init__(self, value):
return body
elif operator in ("extends", "include", "set", "import", "from",
- "comment", "autoescape", "raw"):
+ "comment", "autoescape", "raw", "module"):
if operator == "comment":
continue
if operator == "extends":
continue
elif operator == "raw":
block = _Expression(suffix, raw=True)
+ elif operator == "module":
+ block = _Module(suffix)
body.chunks.append(block)
continue
from tornado.escape import json_decode, utf8, to_unicode, recursive_unicode, native_str
from tornado.iostream import IOStream
+from tornado.template import DictLoader
from tornado.testing import LogTrapTestCase, AsyncHTTPTestCase
from tornado.util import b, bytes_type
from tornado.web import RequestHandler, _O, authenticated, Application, asynchronous, url
'query': describe(self.get_argument("foo")),
})
+class LinkifyHandler(RequestHandler):
+ def get(self):
+ self.render("linkify.html", message="http://example.com")
+
class WebTest(AsyncHTTPTestCase, LogTrapTestCase):
def get_app(self):
- return Application([
- url("/typecheck/(.*)", TypeCheckHandler, name='typecheck'),
- url("/decode_arg/(.*)", DecodeArgHandler),
- url("/decode_arg_kw/(?P<arg>.*)", DecodeArgHandler),
- ])
+ loader = DictLoader({
+ "linkify.html": "{% module linkify(message) %}"
+ })
+ urls = [
+ url("/typecheck/(.*)", TypeCheckHandler, name='typecheck'),
+ url("/decode_arg/(.*)", DecodeArgHandler),
+ url("/decode_arg_kw/(?P<arg>.*)", DecodeArgHandler),
+ url("/linkify", LinkifyHandler),
+ ]
+ return Application(urls,
+ template_loader=loader,
+ autoescape="xhtml_escape")
def test_types(self):
response = self.fetch("/typecheck/asdf?foo=bar",
self.assertEqual(data, {u'path': [u'bytes', u'c3a9'],
u'query': [u'bytes', u'c3a9'],
})
+
+ def test_uimodule_unescaped(self):
+ response = self.fetch("/linkify")
+ self.assertEqual(response.body,
+ b("<a href=\"http://example.com\">http://example.com</a>"))
self.named_handlers = {}
self.default_host = default_host
self.settings = settings
- self.ui_modules = {}
+ self.ui_modules = {'linkify': _linkify,
+ 'xsrf_form_html': _xsrf_form_html}
self.ui_methods = {}
self._wsgi = wsgi
self._load_ui_modules(settings.get("ui_modules", {}))
def render_string(self, path, **kwargs):
return self.handler.render_string(path, **kwargs)
+class _linkify(UIModule):
+ def render(self, text, **kwargs):
+ return escape.linkify(text, **kwargs)
+
+class _xsrf_form_html(UIModule):
+ def render(self):
+ return self.handler.xsrf_form_html()
+
class URLSpec(object):
"""Specifies mappings between URLs and handlers."""
def __init__(self, pattern, handler_class, kwargs={}, name=None):