test_ext.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. import re
  2. from io import BytesIO
  3. import pytest
  4. from jinja2 import DictLoader
  5. from jinja2 import Environment
  6. from jinja2 import nodes
  7. from jinja2 import pass_context
  8. from jinja2 import TemplateSyntaxError
  9. from jinja2.exceptions import TemplateAssertionError
  10. from jinja2.ext import Extension
  11. from jinja2.lexer import count_newlines
  12. from jinja2.lexer import Token
  13. importable_object = 23
  14. _gettext_re = re.compile(r"_\((.*?)\)", re.DOTALL)
  15. i18n_templates = {
  16. "default.html": '<title>{{ page_title|default(_("missing")) }}</title>'
  17. "{% block body %}{% endblock %}",
  18. "child.html": '{% extends "default.html" %}{% block body %}'
  19. "{% trans %}watch out{% endtrans %}{% endblock %}",
  20. "plural.html": "{% trans user_count %}One user online{% pluralize %}"
  21. "{{ user_count }} users online{% endtrans %}",
  22. "plural2.html": "{% trans user_count=get_user_count() %}{{ user_count }}s"
  23. "{% pluralize %}{{ user_count }}p{% endtrans %}",
  24. "stringformat.html": '{{ _("User: %(num)s")|format(num=user_count) }}',
  25. }
  26. newstyle_i18n_templates = {
  27. "default.html": '<title>{{ page_title|default(_("missing")) }}</title>'
  28. "{% block body %}{% endblock %}",
  29. "child.html": '{% extends "default.html" %}{% block body %}'
  30. "{% trans %}watch out{% endtrans %}{% endblock %}",
  31. "plural.html": "{% trans user_count %}One user online{% pluralize %}"
  32. "{{ user_count }} users online{% endtrans %}",
  33. "stringformat.html": '{{ _("User: %(num)s", num=user_count) }}',
  34. "ngettext.html": '{{ ngettext("%(num)s apple", "%(num)s apples", apples) }}',
  35. "ngettext_long.html": "{% trans num=apples %}{{ num }} apple{% pluralize %}"
  36. "{{ num }} apples{% endtrans %}",
  37. "pgettext.html": '{{ pgettext("fruit", "Apple") }}',
  38. "npgettext.html": '{{ npgettext("fruit", "%(num)s apple", "%(num)s apples",'
  39. " apples) }}",
  40. "pgettext_block": "{% trans 'fruit' num=apples %}Apple{% endtrans %}",
  41. "npgettext_block": "{% trans 'fruit' num=apples %}{{ num }} apple"
  42. "{% pluralize %}{{ num }} apples{% endtrans %}",
  43. "transvars1.html": "{% trans %}User: {{ num }}{% endtrans %}",
  44. "transvars2.html": "{% trans num=count %}User: {{ num }}{% endtrans %}",
  45. "transvars3.html": "{% trans count=num %}User: {{ count }}{% endtrans %}",
  46. "novars.html": "{% trans %}%(hello)s{% endtrans %}",
  47. "vars.html": "{% trans %}{{ foo }}%(foo)s{% endtrans %}",
  48. "explicitvars.html": '{% trans foo="42" %}%(foo)s{% endtrans %}',
  49. }
  50. languages = {
  51. "de": {
  52. "missing": "fehlend",
  53. "watch out": "pass auf",
  54. "One user online": "Ein Benutzer online",
  55. "%(user_count)s users online": "%(user_count)s Benutzer online",
  56. "User: %(num)s": "Benutzer: %(num)s",
  57. "User: %(count)s": "Benutzer: %(count)s",
  58. "Apple": {None: "Apfel", "fruit": "Apple"},
  59. "%(num)s apple": {None: "%(num)s Apfel", "fruit": "%(num)s Apple"},
  60. "%(num)s apples": {None: "%(num)s Äpfel", "fruit": "%(num)s Apples"},
  61. }
  62. }
  63. def _get_with_context(value, ctx=None):
  64. if isinstance(value, dict):
  65. return value.get(ctx, value)
  66. return value
  67. @pass_context
  68. def gettext(context, string):
  69. language = context.get("LANGUAGE", "en")
  70. value = languages.get(language, {}).get(string, string)
  71. return _get_with_context(value)
  72. @pass_context
  73. def ngettext(context, s, p, n):
  74. language = context.get("LANGUAGE", "en")
  75. if n != 1:
  76. value = languages.get(language, {}).get(p, p)
  77. return _get_with_context(value)
  78. value = languages.get(language, {}).get(s, s)
  79. return _get_with_context(value)
  80. @pass_context
  81. def pgettext(context, c, s):
  82. language = context.get("LANGUAGE", "en")
  83. value = languages.get(language, {}).get(s, s)
  84. return _get_with_context(value, c)
  85. @pass_context
  86. def npgettext(context, c, s, p, n):
  87. language = context.get("LANGUAGE", "en")
  88. if n != 1:
  89. value = languages.get(language, {}).get(p, p)
  90. return _get_with_context(value, c)
  91. value = languages.get(language, {}).get(s, s)
  92. return _get_with_context(value, c)
  93. i18n_env = Environment(
  94. loader=DictLoader(i18n_templates), extensions=["jinja2.ext.i18n"]
  95. )
  96. i18n_env.globals.update(
  97. {
  98. "_": gettext,
  99. "gettext": gettext,
  100. "ngettext": ngettext,
  101. "pgettext": pgettext,
  102. "npgettext": npgettext,
  103. }
  104. )
  105. i18n_env_trimmed = Environment(extensions=["jinja2.ext.i18n"])
  106. i18n_env_trimmed.policies["ext.i18n.trimmed"] = True
  107. i18n_env_trimmed.globals.update(
  108. {
  109. "_": gettext,
  110. "gettext": gettext,
  111. "ngettext": ngettext,
  112. "pgettext": pgettext,
  113. "npgettext": npgettext,
  114. }
  115. )
  116. newstyle_i18n_env = Environment(
  117. loader=DictLoader(newstyle_i18n_templates), extensions=["jinja2.ext.i18n"]
  118. )
  119. newstyle_i18n_env.install_gettext_callables( # type: ignore
  120. gettext, ngettext, newstyle=True, pgettext=pgettext, npgettext=npgettext
  121. )
  122. class ExampleExtension(Extension):
  123. tags = {"test"}
  124. ext_attr = 42
  125. context_reference_node_cls = nodes.ContextReference
  126. def parse(self, parser):
  127. return nodes.Output(
  128. [
  129. self.call_method(
  130. "_dump",
  131. [
  132. nodes.EnvironmentAttribute("sandboxed"),
  133. self.attr("ext_attr"),
  134. nodes.ImportedName(__name__ + ".importable_object"),
  135. self.context_reference_node_cls(),
  136. ],
  137. )
  138. ]
  139. ).set_lineno(next(parser.stream).lineno)
  140. def _dump(self, sandboxed, ext_attr, imported_object, context):
  141. return (
  142. f"{sandboxed}|{ext_attr}|{imported_object}|{context.blocks}"
  143. f"|{context.get('test_var')}"
  144. )
  145. class DerivedExampleExtension(ExampleExtension):
  146. context_reference_node_cls = nodes.DerivedContextReference # type: ignore
  147. class PreprocessorExtension(Extension):
  148. def preprocess(self, source, name, filename=None):
  149. return source.replace("[[TEST]]", "({{ foo }})")
  150. class StreamFilterExtension(Extension):
  151. def filter_stream(self, stream):
  152. for token in stream:
  153. if token.type == "data":
  154. yield from self.interpolate(token)
  155. else:
  156. yield token
  157. def interpolate(self, token):
  158. pos = 0
  159. end = len(token.value)
  160. lineno = token.lineno
  161. while True:
  162. match = _gettext_re.search(token.value, pos)
  163. if match is None:
  164. break
  165. value = token.value[pos : match.start()]
  166. if value:
  167. yield Token(lineno, "data", value)
  168. lineno += count_newlines(token.value)
  169. yield Token(lineno, "variable_begin", None)
  170. yield Token(lineno, "name", "gettext")
  171. yield Token(lineno, "lparen", None)
  172. yield Token(lineno, "string", match.group(1))
  173. yield Token(lineno, "rparen", None)
  174. yield Token(lineno, "variable_end", None)
  175. pos = match.end()
  176. if pos < end:
  177. yield Token(lineno, "data", token.value[pos:])
  178. class TestExtensions:
  179. def test_extend_late(self):
  180. env = Environment()
  181. t = env.from_string('{% autoescape true %}{{ "<test>" }}{% endautoescape %}')
  182. assert t.render() == "&lt;test&gt;"
  183. def test_loop_controls(self):
  184. env = Environment(extensions=["jinja2.ext.loopcontrols"])
  185. tmpl = env.from_string(
  186. """
  187. {%- for item in [1, 2, 3, 4] %}
  188. {%- if item % 2 == 0 %}{% continue %}{% endif -%}
  189. {{ item }}
  190. {%- endfor %}"""
  191. )
  192. assert tmpl.render() == "13"
  193. tmpl = env.from_string(
  194. """
  195. {%- for item in [1, 2, 3, 4] %}
  196. {%- if item > 2 %}{% break %}{% endif -%}
  197. {{ item }}
  198. {%- endfor %}"""
  199. )
  200. assert tmpl.render() == "12"
  201. def test_do(self):
  202. env = Environment(extensions=["jinja2.ext.do"])
  203. tmpl = env.from_string(
  204. """
  205. {%- set items = [] %}
  206. {%- for char in "foo" %}
  207. {%- do items.append(loop.index0 ~ char) %}
  208. {%- endfor %}{{ items|join(', ') }}"""
  209. )
  210. assert tmpl.render() == "0f, 1o, 2o"
  211. def test_extension_nodes(self):
  212. env = Environment(extensions=[ExampleExtension])
  213. tmpl = env.from_string("{% test %}")
  214. assert tmpl.render() == "False|42|23|{}|None"
  215. def test_contextreference_node_passes_context(self):
  216. env = Environment(extensions=[ExampleExtension])
  217. tmpl = env.from_string('{% set test_var="test_content" %}{% test %}')
  218. assert tmpl.render() == "False|42|23|{}|test_content"
  219. def test_contextreference_node_can_pass_locals(self):
  220. env = Environment(extensions=[DerivedExampleExtension])
  221. tmpl = env.from_string(
  222. '{% for test_var in ["test_content"] %}{% test %}{% endfor %}'
  223. )
  224. assert tmpl.render() == "False|42|23|{}|test_content"
  225. def test_identifier(self):
  226. assert ExampleExtension.identifier == __name__ + ".ExampleExtension"
  227. def test_rebinding(self):
  228. original = Environment(extensions=[ExampleExtension])
  229. overlay = original.overlay()
  230. for env in original, overlay:
  231. for ext in env.extensions.values():
  232. assert ext.environment is env
  233. def test_preprocessor_extension(self):
  234. env = Environment(extensions=[PreprocessorExtension])
  235. tmpl = env.from_string("{[[TEST]]}")
  236. assert tmpl.render(foo=42) == "{(42)}"
  237. def test_streamfilter_extension(self):
  238. env = Environment(extensions=[StreamFilterExtension])
  239. env.globals["gettext"] = lambda x: x.upper()
  240. tmpl = env.from_string("Foo _(bar) Baz")
  241. out = tmpl.render()
  242. assert out == "Foo BAR Baz"
  243. def test_extension_ordering(self):
  244. class T1(Extension):
  245. priority = 1
  246. class T2(Extension):
  247. priority = 2
  248. env = Environment(extensions=[T1, T2])
  249. ext = list(env.iter_extensions())
  250. assert ext[0].__class__ is T1
  251. assert ext[1].__class__ is T2
  252. def test_debug(self):
  253. env = Environment(extensions=["jinja2.ext.debug"])
  254. t = env.from_string("Hello\n{% debug %}\nGoodbye")
  255. out = t.render()
  256. for value in ("context", "cycler", "filters", "abs", "tests", "!="):
  257. assert f"'{value}'" in out
  258. class TestInternationalization:
  259. def test_trans(self):
  260. tmpl = i18n_env.get_template("child.html")
  261. assert tmpl.render(LANGUAGE="de") == "<title>fehlend</title>pass auf"
  262. def test_trans_plural(self):
  263. tmpl = i18n_env.get_template("plural.html")
  264. assert tmpl.render(LANGUAGE="de", user_count=1) == "Ein Benutzer online"
  265. assert tmpl.render(LANGUAGE="de", user_count=2) == "2 Benutzer online"
  266. def test_trans_plural_with_functions(self):
  267. tmpl = i18n_env.get_template("plural2.html")
  268. def get_user_count():
  269. get_user_count.called += 1
  270. return 1
  271. get_user_count.called = 0
  272. assert tmpl.render(LANGUAGE="de", get_user_count=get_user_count) == "1s"
  273. assert get_user_count.called == 1
  274. def test_complex_plural(self):
  275. tmpl = i18n_env.from_string(
  276. "{% trans foo=42, count=2 %}{{ count }} item{% "
  277. "pluralize count %}{{ count }} items{% endtrans %}"
  278. )
  279. assert tmpl.render() == "2 items"
  280. pytest.raises(
  281. TemplateAssertionError,
  282. i18n_env.from_string,
  283. "{% trans foo %}...{% pluralize bar %}...{% endtrans %}",
  284. )
  285. def test_trans_stringformatting(self):
  286. tmpl = i18n_env.get_template("stringformat.html")
  287. assert tmpl.render(LANGUAGE="de", user_count=5) == "Benutzer: 5"
  288. def test_trimmed(self):
  289. tmpl = i18n_env.from_string(
  290. "{%- trans trimmed %} hello\n world {% endtrans -%}"
  291. )
  292. assert tmpl.render() == "hello world"
  293. def test_trimmed_policy(self):
  294. s = "{%- trans %} hello\n world {% endtrans -%}"
  295. tmpl = i18n_env.from_string(s)
  296. trimmed_tmpl = i18n_env_trimmed.from_string(s)
  297. assert tmpl.render() == " hello\n world "
  298. assert trimmed_tmpl.render() == "hello world"
  299. def test_trimmed_policy_override(self):
  300. tmpl = i18n_env_trimmed.from_string(
  301. "{%- trans notrimmed %} hello\n world {% endtrans -%}"
  302. )
  303. assert tmpl.render() == " hello\n world "
  304. def test_trimmed_vars(self):
  305. tmpl = i18n_env.from_string(
  306. '{%- trans trimmed x="world" %} hello\n {{ x }} {% endtrans -%}'
  307. )
  308. assert tmpl.render() == "hello world"
  309. def test_trimmed_varname_trimmed(self):
  310. # unlikely variable name, but when used as a variable
  311. # it should not enable trimming
  312. tmpl = i18n_env.from_string(
  313. "{%- trans trimmed = 'world' %} hello\n {{ trimmed }} {% endtrans -%}"
  314. )
  315. assert tmpl.render() == " hello\n world "
  316. def test_extract(self):
  317. from jinja2.ext import babel_extract
  318. source = BytesIO(
  319. b"""
  320. {{ gettext('Hello World') }}
  321. {% trans %}Hello World{% endtrans %}
  322. {% trans %}{{ users }} user{% pluralize %}{{ users }} users{% endtrans %}
  323. """
  324. )
  325. assert list(babel_extract(source, ("gettext", "ngettext", "_"), [], {})) == [
  326. (2, "gettext", "Hello World", []),
  327. (3, "gettext", "Hello World", []),
  328. (4, "ngettext", ("%(users)s user", "%(users)s users", None), []),
  329. ]
  330. def test_extract_trimmed(self):
  331. from jinja2.ext import babel_extract
  332. source = BytesIO(
  333. b"""
  334. {{ gettext(' Hello \n World') }}
  335. {% trans trimmed %} Hello \n World{% endtrans %}
  336. {% trans trimmed %}{{ users }} \n user
  337. {%- pluralize %}{{ users }} \n users{% endtrans %}
  338. """
  339. )
  340. assert list(babel_extract(source, ("gettext", "ngettext", "_"), [], {})) == [
  341. (2, "gettext", " Hello \n World", []),
  342. (4, "gettext", "Hello World", []),
  343. (6, "ngettext", ("%(users)s user", "%(users)s users", None), []),
  344. ]
  345. def test_extract_trimmed_option(self):
  346. from jinja2.ext import babel_extract
  347. source = BytesIO(
  348. b"""
  349. {{ gettext(' Hello \n World') }}
  350. {% trans %} Hello \n World{% endtrans %}
  351. {% trans %}{{ users }} \n user
  352. {%- pluralize %}{{ users }} \n users{% endtrans %}
  353. """
  354. )
  355. opts = {"trimmed": "true"}
  356. assert list(babel_extract(source, ("gettext", "ngettext", "_"), [], opts)) == [
  357. (2, "gettext", " Hello \n World", []),
  358. (4, "gettext", "Hello World", []),
  359. (6, "ngettext", ("%(users)s user", "%(users)s users", None), []),
  360. ]
  361. def test_comment_extract(self):
  362. from jinja2.ext import babel_extract
  363. source = BytesIO(
  364. b"""
  365. {# trans first #}
  366. {{ gettext('Hello World') }}
  367. {% trans %}Hello World{% endtrans %}{# trans second #}
  368. {#: third #}
  369. {% trans %}{{ users }} user{% pluralize %}{{ users }} users{% endtrans %}
  370. """
  371. )
  372. assert list(
  373. babel_extract(source, ("gettext", "ngettext", "_"), ["trans", ":"], {})
  374. ) == [
  375. (3, "gettext", "Hello World", ["first"]),
  376. (4, "gettext", "Hello World", ["second"]),
  377. (6, "ngettext", ("%(users)s user", "%(users)s users", None), ["third"]),
  378. ]
  379. def test_extract_context(self):
  380. from jinja2.ext import babel_extract
  381. source = BytesIO(
  382. b"""
  383. {{ pgettext("babel", "Hello World") }}
  384. {{ npgettext("babel", "%(users)s user", "%(users)s users", users) }}
  385. """
  386. )
  387. assert list(babel_extract(source, ("pgettext", "npgettext", "_"), [], {})) == [
  388. (2, "pgettext", ("babel", "Hello World"), []),
  389. (3, "npgettext", ("babel", "%(users)s user", "%(users)s users", None), []),
  390. ]
  391. def test_nested_trans_error(self):
  392. s = "{% trans %}foo{% trans %}{% endtrans %}"
  393. with pytest.raises(TemplateSyntaxError) as excinfo:
  394. i18n_env.from_string(s)
  395. assert "trans blocks can't be nested" in str(excinfo.value)
  396. def test_trans_block_error(self):
  397. s = "{% trans %}foo{% wibble bar %}{% endwibble %}{% endtrans %}"
  398. with pytest.raises(TemplateSyntaxError) as excinfo:
  399. i18n_env.from_string(s)
  400. assert "saw `wibble`" in str(excinfo.value)
  401. class TestScope:
  402. def test_basic_scope_behavior(self):
  403. # This is what the old with statement compiled down to
  404. class ScopeExt(Extension):
  405. tags = {"scope"}
  406. def parse(self, parser):
  407. node = nodes.Scope(lineno=next(parser.stream).lineno)
  408. assignments = []
  409. while parser.stream.current.type != "block_end":
  410. lineno = parser.stream.current.lineno
  411. if assignments:
  412. parser.stream.expect("comma")
  413. target = parser.parse_assign_target()
  414. parser.stream.expect("assign")
  415. expr = parser.parse_expression()
  416. assignments.append(nodes.Assign(target, expr, lineno=lineno))
  417. node.body = assignments + list(
  418. parser.parse_statements(("name:endscope",), drop_needle=True)
  419. )
  420. return node
  421. env = Environment(extensions=[ScopeExt])
  422. tmpl = env.from_string(
  423. """\
  424. {%- scope a=1, b=2, c=b, d=e, e=5 -%}
  425. {{ a }}|{{ b }}|{{ c }}|{{ d }}|{{ e }}
  426. {%- endscope -%}
  427. """
  428. )
  429. assert tmpl.render(b=3, e=4) == "1|2|2|4|5"
  430. class TestNewstyleInternationalization:
  431. def test_trans(self):
  432. tmpl = newstyle_i18n_env.get_template("child.html")
  433. assert tmpl.render(LANGUAGE="de") == "<title>fehlend</title>pass auf"
  434. def test_trans_plural(self):
  435. tmpl = newstyle_i18n_env.get_template("plural.html")
  436. assert tmpl.render(LANGUAGE="de", user_count=1) == "Ein Benutzer online"
  437. assert tmpl.render(LANGUAGE="de", user_count=2) == "2 Benutzer online"
  438. def test_complex_plural(self):
  439. tmpl = newstyle_i18n_env.from_string(
  440. "{% trans foo=42, count=2 %}{{ count }} item{% "
  441. "pluralize count %}{{ count }} items{% endtrans %}"
  442. )
  443. assert tmpl.render() == "2 items"
  444. pytest.raises(
  445. TemplateAssertionError,
  446. i18n_env.from_string,
  447. "{% trans foo %}...{% pluralize bar %}...{% endtrans %}",
  448. )
  449. def test_trans_stringformatting(self):
  450. tmpl = newstyle_i18n_env.get_template("stringformat.html")
  451. assert tmpl.render(LANGUAGE="de", user_count=5) == "Benutzer: 5"
  452. def test_newstyle_plural(self):
  453. tmpl = newstyle_i18n_env.get_template("ngettext.html")
  454. assert tmpl.render(LANGUAGE="de", apples=1) == "1 Apfel"
  455. assert tmpl.render(LANGUAGE="de", apples=5) == "5 Äpfel"
  456. def test_autoescape_support(self):
  457. env = Environment(extensions=["jinja2.ext.i18n"])
  458. env.install_gettext_callables(
  459. lambda x: "<strong>Wert: %(name)s</strong>",
  460. lambda s, p, n: s,
  461. newstyle=True,
  462. )
  463. t = env.from_string(
  464. '{% autoescape ae %}{{ gettext("foo", name='
  465. '"<test>") }}{% endautoescape %}'
  466. )
  467. assert t.render(ae=True) == "<strong>Wert: &lt;test&gt;</strong>"
  468. assert t.render(ae=False) == "<strong>Wert: <test></strong>"
  469. def test_autoescape_macros(self):
  470. env = Environment(autoescape=False)
  471. template = (
  472. "{% macro m() %}<html>{% endmacro %}"
  473. "{% autoescape true %}{{ m() }}{% endautoescape %}"
  474. )
  475. assert env.from_string(template).render() == "<html>"
  476. def test_num_used_twice(self):
  477. tmpl = newstyle_i18n_env.get_template("ngettext_long.html")
  478. assert tmpl.render(apples=5, LANGUAGE="de") == "5 Äpfel"
  479. def test_num_called_num(self):
  480. source = newstyle_i18n_env.compile(
  481. """
  482. {% trans num=3 %}{{ num }} apple{% pluralize
  483. %}{{ num }} apples{% endtrans %}
  484. """,
  485. raw=True,
  486. )
  487. # quite hacky, but the only way to properly test that. The idea is
  488. # that the generated code does not pass num twice (although that
  489. # would work) for better performance. This only works on the
  490. # newstyle gettext of course
  491. assert (
  492. re.search(r"u?'%\(num\)s apple', u?'%\(num\)s apples', 3", source)
  493. is not None
  494. )
  495. def test_trans_vars(self):
  496. t1 = newstyle_i18n_env.get_template("transvars1.html")
  497. t2 = newstyle_i18n_env.get_template("transvars2.html")
  498. t3 = newstyle_i18n_env.get_template("transvars3.html")
  499. assert t1.render(num=1, LANGUAGE="de") == "Benutzer: 1"
  500. assert t2.render(count=23, LANGUAGE="de") == "Benutzer: 23"
  501. assert t3.render(num=42, LANGUAGE="de") == "Benutzer: 42"
  502. def test_novars_vars_escaping(self):
  503. t = newstyle_i18n_env.get_template("novars.html")
  504. assert t.render() == "%(hello)s"
  505. t = newstyle_i18n_env.get_template("vars.html")
  506. assert t.render(foo="42") == "42%(foo)s"
  507. t = newstyle_i18n_env.get_template("explicitvars.html")
  508. assert t.render() == "%(foo)s"
  509. def test_context(self):
  510. tmpl = newstyle_i18n_env.get_template("pgettext.html")
  511. assert tmpl.render(LANGUAGE="de") == "Apple"
  512. def test_context_plural(self):
  513. tmpl = newstyle_i18n_env.get_template("npgettext.html")
  514. assert tmpl.render(LANGUAGE="de", apples=1) == "1 Apple"
  515. assert tmpl.render(LANGUAGE="de", apples=5) == "5 Apples"
  516. def test_context_block(self):
  517. tmpl = newstyle_i18n_env.get_template("pgettext_block")
  518. assert tmpl.render(LANGUAGE="de") == "Apple"
  519. def test_context_plural_block(self):
  520. tmpl = newstyle_i18n_env.get_template("npgettext_block")
  521. assert tmpl.render(LANGUAGE="de", apples=1) == "1 Apple"
  522. assert tmpl.render(LANGUAGE="de", apples=5) == "5 Apples"
  523. class TestAutoEscape:
  524. def test_scoped_setting(self):
  525. env = Environment(autoescape=True)
  526. tmpl = env.from_string(
  527. """
  528. {{ "<HelloWorld>" }}
  529. {% autoescape false %}
  530. {{ "<HelloWorld>" }}
  531. {% endautoescape %}
  532. {{ "<HelloWorld>" }}
  533. """
  534. )
  535. assert tmpl.render().split() == [
  536. "&lt;HelloWorld&gt;",
  537. "<HelloWorld>",
  538. "&lt;HelloWorld&gt;",
  539. ]
  540. env = Environment(autoescape=False)
  541. tmpl = env.from_string(
  542. """
  543. {{ "<HelloWorld>" }}
  544. {% autoescape true %}
  545. {{ "<HelloWorld>" }}
  546. {% endautoescape %}
  547. {{ "<HelloWorld>" }}
  548. """
  549. )
  550. assert tmpl.render().split() == [
  551. "<HelloWorld>",
  552. "&lt;HelloWorld&gt;",
  553. "<HelloWorld>",
  554. ]
  555. def test_nonvolatile(self):
  556. env = Environment(autoescape=True)
  557. tmpl = env.from_string('{{ {"foo": "<test>"}|xmlattr|escape }}')
  558. assert tmpl.render() == ' foo="&lt;test&gt;"'
  559. tmpl = env.from_string(
  560. '{% autoescape false %}{{ {"foo": "<test>"}'
  561. "|xmlattr|escape }}{% endautoescape %}"
  562. )
  563. assert tmpl.render() == " foo=&#34;&amp;lt;test&amp;gt;&#34;"
  564. def test_volatile(self):
  565. env = Environment(autoescape=True)
  566. tmpl = env.from_string(
  567. '{% autoescape foo %}{{ {"foo": "<test>"}'
  568. "|xmlattr|escape }}{% endautoescape %}"
  569. )
  570. assert tmpl.render(foo=False) == " foo=&#34;&amp;lt;test&amp;gt;&#34;"
  571. assert tmpl.render(foo=True) == ' foo="&lt;test&gt;"'
  572. def test_scoping(self):
  573. env = Environment()
  574. tmpl = env.from_string(
  575. '{% autoescape true %}{% set x = "<x>" %}{{ x }}'
  576. '{% endautoescape %}{{ x }}{{ "<y>" }}'
  577. )
  578. assert tmpl.render(x=1) == "&lt;x&gt;1<y>"
  579. def test_volatile_scoping(self):
  580. env = Environment()
  581. tmplsource = """
  582. {% autoescape val %}
  583. {% macro foo(x) %}
  584. [{{ x }}]
  585. {% endmacro %}
  586. {{ foo().__class__.__name__ }}
  587. {% endautoescape %}
  588. {{ '<testing>' }}
  589. """
  590. tmpl = env.from_string(tmplsource)
  591. assert tmpl.render(val=True).split()[0] == "Markup"
  592. assert tmpl.render(val=False).split()[0] == "str"
  593. # looking at the source we should see <testing> there in raw
  594. # (and then escaped as well)
  595. env = Environment()
  596. pysource = env.compile(tmplsource, raw=True)
  597. assert "<testing>\\n" in pysource
  598. env = Environment(autoescape=True)
  599. pysource = env.compile(tmplsource, raw=True)
  600. assert "&lt;testing&gt;\\n" in pysource
  601. def test_overlay_scopes(self):
  602. class MagicScopeExtension(Extension):
  603. tags = {"overlay"}
  604. def parse(self, parser):
  605. node = nodes.OverlayScope(lineno=next(parser.stream).lineno)
  606. node.body = list(
  607. parser.parse_statements(("name:endoverlay",), drop_needle=True)
  608. )
  609. node.context = self.call_method("get_scope")
  610. return node
  611. def get_scope(self):
  612. return {"x": [1, 2, 3]}
  613. env = Environment(extensions=[MagicScopeExtension])
  614. tmpl = env.from_string(
  615. """
  616. {{- x }}|{% set z = 99 %}
  617. {%- overlay %}
  618. {{- y }}|{{ z }}|{% for item in x %}[{{ item }}]{% endfor %}
  619. {%- endoverlay %}|
  620. {{- x -}}
  621. """
  622. )
  623. assert tmpl.render(x=42, y=23) == "42|23|99|[1][2][3]|42"