Example #1
0
 def test_include(self):
     loader = DictLoader({
             "index.html": '{% include "header.html" %}\nbody text',
             "header.html": "header text",
             })
     self.assertEqual(loader.load("index.html").generate(),
                      b("header text\nbody text"))
Example #2
0
 def test_relative_load(self):
     loader = DictLoader({
         "a/1.html": "{% include '2.html' %}",
         "a/2.html": "{% include '../b/3.html' %}",
         "b/3.html": "ok",
     })
     self.assertEqual(loader.load("a/1.html").generate(), b("ok"))
Example #3
0
 def test_include(self):
     loader = DictLoader({
         "index.html": '{% include "header.html" %}\nbody text',
         "header.html": "header text",
     })
     self.assertEqual(
         loader.load("index.html").generate(), b("header text\nbody text"))
Example #4
0
 def test_unextended_block(self):
     loader = DictLoader(self.templates)
     name = "<script>"
     self.assertEqual(loader.load("escaped_block.html").generate(name=name),
                      b("base: &lt;script&gt;"))
     self.assertEqual(loader.load("unescaped_block.html").generate(name=name),
                      b("base: <script>"))
Example #5
0
 def test_relative_load(self):
     loader = DictLoader({
             "a/1.html": "{% include '2.html' %}",
             "a/2.html": "{% include '../b/3.html' %}",
             "b/3.html": "ok",
             })
     self.assertEqual(loader.load("a/1.html").generate(),
                      b("ok"))
Example #6
0
 def test_unextended_block(self):
     loader = DictLoader(self.templates)
     name = "<script>"
     self.assertEqual(
         loader.load("escaped_block.html").generate(name=name),
         b("base: &lt;script&gt;"))
     self.assertEqual(
         loader.load("unescaped_block.html").generate(name=name),
         b("base: <script>"))
Example #7
0
    def test_extends(self):
        loader = DictLoader({
                "base.html": """\
<title>{% block title %}default title{% end %}</title>
<body>{% block body %}default body{% end %}</body>
""",
                "page.html": """\
{% extends "base.html" %}
{% block title %}page title{% end %}
{% block body %}page body{% end %}
""",
                })
        self.assertEqual(loader.load("page.html").generate(),
                         b("<title>page title</title>\n<body>page body</body>\n"))
Example #8
0
 def test_default_on(self):
     loader = DictLoader(self.templates, autoescape="xhtml_escape")
     name = "Bobby <table>s"
     self.assertEqual(loader.load("escaped.html").generate(name=name),
                      b("Bobby &lt;table&gt;s"))
     self.assertEqual(loader.load("unescaped.html").generate(name=name),
                      b("Bobby <table>s"))
     self.assertEqual(loader.load("default.html").generate(name=name),
                      b("Bobby &lt;table&gt;s"))
     
     self.assertEqual(loader.load("include.html").generate(name=name),
                      b("escaped: Bobby &lt;table&gt;s\n"
                        "unescaped: Bobby <table>s\n"
                        "default: Bobby &lt;table&gt;s\n"))
Example #9
0
    def get_app(self):
        loader = DictLoader({
            "linkify.html":
            "{% module linkify(message) %}",
            "page.html":
            """\
<html><head></head><body>
{% for e in entries %}
{% module Template("entry.html", entry=e) %}
{% end %}
</body></html>""",
            "entry.html":
            """\
{{ set_resources(embedded_css=".entry { margin-bottom: 1em; }", embedded_javascript="js_embed()", css_files=["/base.css", "/foo.css"], javascript_files="/common.js", html_head="<meta>", html_body='<script src="/analytics.js"/>') }}
<div class="entry">...</div>""",
        })
        urls = [
            url("/typecheck/(.*)", TypeCheckHandler, name='typecheck'),
            url("/decode_arg/(.*)", DecodeArgHandler),
            url("/decode_arg_kw/(?P<arg>.*)", DecodeArgHandler),
            url("/linkify", LinkifyHandler),
            url("/uimodule_resources", UIModuleResourceHandler),
            url("/optional_path/(.+)?", OptionalPathHandler),
            url("/flow_control", FlowControlHandler),
            url("/multi_header", MultiHeaderHandler),
        ]
        return Application(urls,
                           template_loader=loader,
                           autoescape="xhtml_escape")
Example #10
0
    def test_extends(self):
        loader = DictLoader({
            "base.html":
            """\
<title>{% block title %}default title{% end %}</title>
<body>{% block body %}default body{% end %}</body>
""",
            "page.html":
            """\
{% extends "base.html" %}
{% block title %}page title{% end %}
{% block body %}page body{% end %}
""",
        })
        self.assertEqual(
            loader.load("page.html").generate(),
            b("<title>page title</title>\n<body>page body</body>\n"))
Example #11
0
    def test_raw_expression(self):
        loader = DictLoader(self.templates)

        def render(name):
            return loader.load(name).generate(name='<>&"')

        self.assertEqual(render("raw_expression.html"),
                         b("expr: &lt;&gt;&amp;&quot;\n"
                           "raw: <>&\""))
Example #12
0
    def test_extended_block(self):
        loader = DictLoader(self.templates)

        def render(name):
            return loader.load(name).generate(name="<script>")

        self.assertEqual(render("escaped_extends_unescaped.html"),
                         b("base: <script>"))
        self.assertEqual(render("escaped_overrides_unescaped.html"),
                         b("extended: &lt;script&gt;"))

        self.assertEqual(render("unescaped_extends_escaped.html"),
                         b("base: &lt;script&gt;"))
        self.assertEqual(render("unescaped_overrides_escaped.html"),
                         b("extended: <script>"))
Example #13
0
    def test_custom_escape(self):
        loader = DictLoader(
            {"foo.py": "{% autoescape py_escape %}s = {{ name }}\n"})

        def py_escape(s):
            self.assertEqual(type(s), bytes_type)
            return repr(native_str(s))

        def render(template, name):
            return loader.load(template).generate(py_escape=py_escape,
                                                  name=name)

        self.assertEqual(render("foo.py", "<html>"), b("s = '<html>'\n"))
        self.assertEqual(render("foo.py", "';sys.exit()"),
                         b("""s = "';sys.exit()"\n"""))
        self.assertEqual(render("foo.py", ["not a string"]),
                         b("""s = "['not a string']"\n"""))
Example #14
0
    def test_default_on(self):
        loader = DictLoader(self.templates, autoescape="xhtml_escape")
        name = "Bobby <table>s"
        self.assertEqual(
            loader.load("escaped.html").generate(name=name),
            b("Bobby &lt;table&gt;s"))
        self.assertEqual(
            loader.load("unescaped.html").generate(name=name),
            b("Bobby <table>s"))
        self.assertEqual(
            loader.load("default.html").generate(name=name),
            b("Bobby &lt;table&gt;s"))

        self.assertEqual(
            loader.load("include.html").generate(name=name),
            b("escaped: Bobby &lt;table&gt;s\n"
              "unescaped: Bobby <table>s\n"
              "default: Bobby &lt;table&gt;s\n"))
Example #15
0
 def test_custom_namespace(self):
     loader = DictLoader({"test.html": "{{ inc(5) }}"},
                         namespace={"inc": lambda x: x + 1})
     self.assertEqual(loader.load("test.html").generate(), b("6"))
Example #16
0
 def test_custom_namespace(self):
     loader = DictLoader({"test.html": "{{ inc(5) }}"}, namespace={"inc": lambda x: x+1})
     self.assertEqual(loader.load("test.html").generate(), b("6"))