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
Provenance
- Commit
db09c67- Tree
6bf0c18- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent 1
3747487- Parent 2
99c99c4
7 files changed+49 added−13 removed
modified:docs/templating.rst+4 added-1 removed
16 unmodified lines17 18 Unless customized, Jinja2 is configured by Flask as follows:19 20 - 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 by197 unmodified linesmodified:docs/upgrading.rst+4 added
35 unmodified lines36 Now the inheritance hierarchy takes precedence and handlers for more37 specific exception classes are executed instead of more general ones.38 See :ref:`error-handlers` for specifics.39 40+The :func:`~flask.templating.render_template_string` function has changed to41+autoescape template variables by default. This better matches the behavior42+of :func:`~flask.templating.render_template`.43+44 .. note::45 46 There used to be a logic error allowing you to register handlers47 only for exception *instances*. This was unintended and plain wrong,356 unmodified linesmodified:flask/app.py+10 added-10 removed
722 unmodified lines723 """724 725 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`.728 729 .. versionadded:: 0.5730 """731 if filename is None:732- return False732+ return True733 return filename.endswith(('.html', '.htm', '.xml', '.xhtml'))734 735 def update_template_context(self, context):736 """Update the template context with some commonly used variables.352 unmodified lines1089 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- 1093+1094 assert issubclass(exc_class, Exception)1095- 1095+1096 if issubclass(exc_class, HTTPException):1097 return exc_class, exc_class.code1098 else:1099 return exc_class, None1100- 1100+1101 @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 lines1165 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- 1169+1170 exc_class, code = self._get_exc_class_and_code(code_or_exception)1171- 1171+1172 handlers = self.error_handler_spec.setdefault(key, {}).setdefault(code, {})1173 handlers[exc_class] = f1174 1175 @setupmethod283 unmodified lines1459 # Proxy exceptions don't have error codes. We want to always return1460 # those unchanged as errors1461 if e.code is None:1462 return e1463- 1463+1464 handler = self._find_error_handler(e)1465 if handler is None:1466 return e1467 return handler(e)34 unmodified lines1502 # 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- 1506+1507 if isinstance(e, HTTPException) and not self.trap_http_exception(e):1508 return self.handle_http_exception(e)1509 1510 handler = self._find_error_handler(e)1511- 1511+1512 if handler is None:1513 reraise(exc_type, exc_value, tb)1514 return handler(e)1515 459 unmodified linesmodified:flask/templating.py+1 added-1 removed
125 unmodified lines126 127 128 def 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.131 132 :param source: the source code of the template to be133 rendered134 :param context: the variables that should be available in the6 unmodified linesadded: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 lines80 b'<p>Hello World!'81 ]82 83 def 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'<p>Hello World!',97+ b'<p>Hello World!',98+ b'<p>Hello World!',99+ b'<p>Hello World!'100+ ]101+102+def 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>') == '<test>'107 assert flask.render_template('mail.txt', foo='<test>') == \108 '<test> Mail'109 110 def test_macros():282 unmodified linesmodified:CHANGES+2 added
66 unmodified lines67 - 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``).73 74 Version 0.10.275 --------------76 478 unmodified lines