Skip to content
Myr

Merge branch 'alanhamlett-master'

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

Fix #1515

authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents 3747487, 99c99c4 · Browse files · 7 files+49 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

7 files+49 added−13 removed

  • tests:+28 added−1 removed
  • other:+21 added−12 removed

7 files+49 added−13 removed

modified:docs/templating.rst+4 added-1 removed
16 unmodified lines
1718Unless customized, Jinja2 is configured by Flask as follows:1920- autoescaping is enabled for all templates ending in ``.html``,21 ``.htm``, ``.xml`` as well as ``.xhtml``21 ``.htm``, ``.xml`` as well as ``.xhtml`` when using22 :func:`~flask.templating.render_template`.23- autoescaping is enabled for all strings when using24 :func:`~flask.templating.render_template_string`.25- a template has the ability to opt in/out autoescaping with the26 ``{% autoescape %}`` tag.27- Flask inserts a couple of global functions and helpers into the28 Jinja2 context, additionally to the values that are present by
197 unmodified lines
modified:docs/upgrading.rst+4 added
35 unmodified lines
36Now the inheritance hierarchy takes precedence and handlers for more37specific exception classes are executed instead of more general ones.38See :ref:`error-handlers` for specifics.3940The :func:`~flask.templating.render_template_string` function has changed to41autoescape template variables by default. This better matches the behavior42of :func:`~flask.templating.render_template`.4344.. note::4546 There used to be a logic error allowing you to register handlers47 only for exception *instances*. This was unintended and plain wrong,
356 unmodified lines
modified:flask/app.py+10 added-10 removed
722 unmodified lines
723 """724725 def select_jinja_autoescape(self, filename):726 """Returns ``True`` if autoescaping should be active for the given727 template name.727 template name. If no template name is given, returns `True`.728729 .. versionadded:: 0.5730 """731 if filename is None:732 return False732 return True733 return filename.endswith(('.html', '.htm', '.xml', '.xhtml'))734735 def update_template_context(self, context):736 """Update the template context with some commonly used variables.
352 unmodified lines
1089 if isinstance(exc_class_or_code, integer_types):1090 exc_class = default_exceptions[exc_class_or_code]1091 else:1092 exc_class = exc_class_or_code1093 10931094 assert issubclass(exc_class, Exception)1095 10951096 if issubclass(exc_class, HTTPException):1097 return exc_class, exc_class.code1098 else:1099 return exc_class, None1100 11001101 @setupmethod1102 def errorhandler(self, code_or_exception):1103 """A decorator that is used to register a function give a given1104 error code. Example::
60 unmodified lines
1165 raise ValueError(1166 'Tried to register a handler for an exception instance {0!r}. '1167 'Handlers can only be registered for exception classes or HTTP error codes.'1168 .format(code_or_exception))1169 11691170 exc_class, code = self._get_exc_class_and_code(code_or_exception)1171 11711172 handlers = self.error_handler_spec.setdefault(key, {}).setdefault(code, {})1173 handlers[exc_class] = f11741175 @setupmethod
283 unmodified lines
1459 # Proxy exceptions don't have error codes. We want to always return1460 # those unchanged as errors1461 if e.code is None:1462 return e1463 14631464 handler = self._find_error_handler(e)1465 if handler is None:1466 return e1467 return handler(e)
34 unmodified lines
1502 # ensure not to trash sys.exc_info() at that point in case someone1503 # wants the traceback preserved in handle_http_exception. Of course1504 # we cannot prevent users from trashing it themselves in a custom1505 # trap_http_exception method so that's their fault then.1506 15061507 if isinstance(e, HTTPException) and not self.trap_http_exception(e):1508 return self.handle_http_exception(e)15091510 handler = self._find_error_handler(e)1511 15111512 if handler is None:1513 reraise(exc_type, exc_value, tb)1514 return handler(e)1515
459 unmodified lines
modified:flask/templating.py+1 added-1 removed
125 unmodified lines
126127128def render_template_string(source, **context):129 """Renders a template from the given template source string130 with the given context.130 with the given context. Template variables will be autoescaped.131132 :param source: the source code of the template to be133 rendered134 :param context: the variables that should be available in the
6 unmodified lines
added:tests/templates/non_escaping_template.txt+8 added
1{{ text }}2{{ html }}3{% autoescape false %}{{ text }}4{{ html }}{% endautoescape %}5{% autoescape true %}{{ text }}6{{ html }}{% endautoescape %}7{{ text }}8{{ html }}
modified:tests/test_templating.py+20 added-1 removed
79 unmodified lines
80 b'<p>Hello World!'81 ]8283def test_no_escaping():84 text = '<p>Hello World!'85 app = flask.Flask(__name__)86 @app.route('/')87 def index():88 return flask.render_template('non_escaping_template.txt', text=text,89 html=flask.Markup(text))90 lines = app.test_client().get('/').data.splitlines()91 assert lines == [92 b'<p>Hello World!',93 b'<p>Hello World!',94 b'<p>Hello World!',95 b'<p>Hello World!',96 b'&lt;p&gt;Hello World!',97 b'<p>Hello World!',98 b'<p>Hello World!',99 b'<p>Hello World!'100 ]101102def test_escaping_without_template_filename():103 app = flask.Flask(__name__)104 with app.test_request_context():105 assert flask.render_template_string(87 '{{ foo }}', foo='<test>') == '<test>'106 '{{ foo }}', foo='<test>') == '&lt;test&gt;'107 assert flask.render_template('mail.txt', foo='<test>') == \108 '<test> Mail'109110def test_macros():
282 unmodified lines
modified:CHANGES+2 added
66 unmodified lines
67- Don't leak exception info of already catched exceptions to context teardown68 handlers (pull request ``#1393``).69- Allow custom Jinja environment subclasses (pull request ``#1422``).70- ``flask.g`` now has ``pop()`` and ``setdefault`` methods.71- Turn on autoescape for ``flask.templating.render_template_string`` by default72 (pull request ``#1515``).7374Version 0.10.275--------------76
478 unmodified lines