-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow custom autoescape functions as result of the autoescape function parameter for the Environment #1377
Comments
I like this idea, it seems like a decent way to expand autoescape without needing to change Jinja itself much. If you want to work on a PR try to get it in soon if you want this in 3.0. |
@davidism I looked a bit in the source code and it seems HTML escaping is quite strong hardcoded in the source code due to the use of markupsafes But the I already was successful in creating a patch that implements the mentioned changes but they seem to be quite unstable as I already found some edges cases in which unexpected behaviour occurs. The only way I see in making this feature really reliable is in removed the hardcoding to the (HTML) Therefore I created #1382 to discuss the idea. Looking through the source code it seems manageable and I would give it a try. Could you specify "soon" a bit? I really would like to see this feature in a release soon and I am willing to put some work into it. |
MarkupSafe implements a string subclass that intercepts all operations and applies escaping again. See https://en.wikipedia.org/wiki/Taint_checking for the concept. It should already be possible to subclass this and override the All Jinja should need is the equivalent of a Soon as in the next few days. I'm working to finish merging existing PRs now before making a new 3.0 release candidate. It can always go in 3.1, and may end up there anyway depending on what I can get to. |
I just added a bit more explanation to the documentation so end users won't run in possible pitfalls. This is a task that could be done also be non technical people :-) |
I am currently trying to build an app that generates PDFs based on LaTeX imports.
For this I use django extensions that renders the LaTeX file and also provides the possibility to escape single variables.
Every variable that is given to the templates system can be modified by the users, so I would need to escape every single occurrence, which is error prone.
Therefore using the autoescape feature of Jinja would be great.
Reading #503, which was closed unsuccessfully there are currently two way to implement this kind of autoescape
filter_stream
and and adds a| my_custom_filter
add the end of every variable. (used by JinjaSQL)Both solution feel hacky and unstable.
That why I would suggest to expand the
autoescape
parameter from the Environment to accept not only functions returning a boolean but also a custom escape function.Like this
I would expect the following to happen in the above case:
do_latex_escape
is wrapped to it does not escape safe text (example| e
) and (| escape
) is replaced by thedo_latex_escape
filterWhat do you think about the suggestion? If you like it, I would try to create a PR.
If I understand it correctly Jinja Vanish already provides some good starting point for it.
The text was updated successfully, but these errors were encountered: