Autoescaping improvements#577
Merged
Merged
Conversation
kcostagliola2
approved these changes
Aug 7, 2018
Contributor
|
@ollien Can you write a couple of tests for this? Just something that breaks before your changes and passes after your changes. |
Contributor
Author
|
Sure. I'll get that out this afternoon. |
willrowe
pushed a commit
to willrowe/twig.js
that referenced
this pull request
May 11, 2019
* Return value of output instead of string object * Fix double escaping when using html_attr within html * Add unit tests for double escaping and string object mangling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Twig.js's autoescaping currently has some potentially unexpected behavior.
When autoescaping, the returned string object has a
twig_markupvalue of true, which is unexpected and currently breaks certain usages of the twig module. For instance, you cannot insert thisStringas-is to your DOM, and must first call.valueOf(). I've changed the output to always return a properly formatted string, rather than thisStringobject. This should not break any existing code, as you can still call.valueOf()on this newly returned string.The more glaring problem however, is that the current version of twig.js doubly-escapes strings marked as
html_attrwith thehtmlstrategy. PHP Twig does not do this. I've added a fix for this.Here's a quick demo of the problem. Given this template
{{ foo }} {{ bar|escape('html_attr') }}I've written the following test scripts in Node,
and in PHP.
The Node script output is as follows
while the PHP script outputs
Note that the inverse operation (i.e. escaping as 'html' while using 'html_attr') does double escape in both twig.js and PHP twig.