Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[TwigBridge] Fix rendering of currency by MoneyType#26663
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
| * | ||
| * @author Roland Franssen <franssen.roland@gmail.com> | ||
| */ | ||
| class HtmlEntitiesExtensionextends AbstractExtension |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
instead of a dedicated exception, can't we make it in an existing one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
FormExtension sounds good yes, and to really be conflict-safe id even suggestform_html_entities
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
the conflict-safe id is solved by not providing a legacy extension name at all (we don't have BC issues with this extension)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
but we still need a filter name.html[_]entities has higher chance to be already defined in userland, hencevar|form_html_entities.
nicolas-grekas commentedMar 25, 2018
Tests are red (and lowest deps need to be updated.) |
javiereguiluz commentedMar 27, 2018
I consider this solution "too big" for the tiny use case being solved. Can't this be solved using some PHP code somewhere in Symfony Forms? Roland suggested this: |
| <divclass="input-group"> | ||
| {%ifprepend %} | ||
| <spanclass="input-group-addon">{{money_pattern|replace({'{{ widget }}':''}) }}</span> | ||
| <spanclass="input-group-addon">{{money_pattern|html_entities|replace({'{{ widget }}':''})|raw }}</span> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
replacing{{ widget }} should be done before encoding entities IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
actually no.. 🤔 that would display a HTML literal as{{widget}} contains the<input>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
What do you mean ?{{ widget }} is replaced by an empty string here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
confused the templates :) now updated.
| publicfunctionencode($input) | ||
| { | ||
| returnhtmlentities($input,ENT_QUOTES | (defined('ENT_SUBSTITUTE') ?ENT_SUBSTITUTE :0),'UTF-8'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Twig does not guarantee it always runs in UTF-8. The charset should be handled properly.
| */ | ||
| publicfunctiongetName() | ||
| { | ||
| return'html_entities'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
not needed. The versions we support don't care about the name except for BC reasons (in latest 1.x), and we don't need to provide a BC layer for the name-based access for a new extension.
ro0NL commentedMar 27, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I understand your point, but then again, to me anything related to escaping, raw vars, encoding etc. is serious topic. I prefer the twig filter to properly solve this on the right level, the templating level. Doing it beforehand in PHP / ignoring the issue feels weird. To me the fix is just OK, this is really about 2.7 vs. master. |
| publicfunctionencodeCurrency(Environment$environment,$text) | ||
| { | ||
| if ('UTF-8' ===$charset =$environment->getCharset()) { | ||
| returnhtmlspecialchars($text,ENT_QUOTES | (defined('ENT_SUBSTITUTE') ?ENT_SUBSTITUTE :0),'UTF-8'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
please use\defined, so that OPCache can resolve this at compile-time for recent PHP versions as it will know it is defined.
| <divclass="input-group"> | ||
| {%ifprepend %} | ||
| <spanclass="input-group-addon">{{money_pattern|replace({'{{ widget }}':''}) }}</span> | ||
| <spanclass="input-group-addon">{{money_pattern|form_encode_currency|replace({'{{ widget }}':''})|raw }}</span> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
the replacement should be done first, which will avoid the need for the|raw filter.
| {{-block('form_widget_simple') -}} | ||
| {%ifappend %} | ||
| <spanclass="input-group-addon">{{money_pattern|replace({'{{ widget }}':''}) }}</span> | ||
| <spanclass="input-group-addon">{{money_pattern|form_encode_currency|replace({'{{ widget }}':''})|raw }}</span> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
same here
| ->createView(); | ||
| $this->assertSame('{{ widget }}€',$view->vars['money_pattern']); | ||
| $this->assertSame('{{ widget }}€',$view->vars['money_pattern']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
These test changes look suspicious to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
yeah we need 2 tests for utf+iso. The entity is a direct effect of using htmlentities, and what this topic is about: euro sign not being available in ISO (hence we use entities).
ro0NL commentedMar 28, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
apart from tests; this seems the proper fix. As discussed with@nicolas-grekas to move widget replacement to extension level, making the template concise. functional-wise, ive tested both ISO+UTF on 2.7 without any problems. |
ro0NL commentedMar 28, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
HHVM strikes again :(https://3v4l.org/NCWh2 |
ro0NL left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Now ready. Yay :}
| publicfunctionencodeCurrency(Environment$environment,$text,$widget ='') | ||
| { | ||
| if ('UTF-8' ===$charset =$environment->getCharset()) { | ||
| $text =htmlspecialchars($text,ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ?ENT_SUBSTITUTE :0),'UTF-8'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
ENT_QUOTES | ENT_SUBSTITUTE as of 3.4 instead
| <divclass="input-group"> | ||
| {%ifprepend %} | ||
| <spanclass="input-group-addon">{{money_pattern|replace({'{{ widget }}':''}) }}</span> | ||
| <spanclass="input-group-addon">{{money_pattern|form_encode_currency }}</span> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
bootstrap_base_layout.html.twig + bootstrap_4_layout.html.twig as of 3.4 instead
| "symfony/security-csrf":"~2.6", | ||
| "symfony/stopwatch":"~2.3", | ||
| "symfony/templating":"~2.1", | ||
| "symfony/templating":"~2.7", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
perhaps a conditional test is better, let me know.
| /** | ||
| * @internal | ||
| */ | ||
| publicfunctionencodeCurrency(Environment$environment,$text,$widget ='') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
$currency instead of $text, and mandatory $widget
| }else { | ||
| $text =htmlentities($text,ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ?ENT_SUBSTITUTE :0),'UTF-8'); | ||
| $text =iconv('UTF-8',$charset,$text); | ||
| $widget =iconv('UTF-8',$charset,$widget); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Are we sure $widget is always in UTF-8? This looks strange to me, because that would mean this is also bugged. Is it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
i think we can assume we get raw html in UTF from the form component. Here we include it in a new safe html value concerning the target charset, usinghtmlentities instead ofhtmlspecialchars for escaping.
In general we are consistent with regular escaped output (ie. also in target charset) fromtwig_escape_filter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
At least we treated this as UTF before also (result fromblock() in twig).
fabpot commentedMar 29, 2018
Thank you@ro0NL. |
This PR was squashed before being merged into the 2.7 branch (closes#26663).Discussion----------[TwigBridge] Fix rendering of currency by MoneyType| Q | A| ------------- | ---| Branch? | 2.7| Bug fix? | yes| New feature? | no| BC breaks? | no| Deprecations? | no| Tests pass? | todo| Fixed tickets |#25135| License | MIT| Doc PR | -Split from#25167 as suggested by@nicolas-grekas to see if this can be reasonably fixed on 2.7, the right way using this itsy-bitsy new feature.#25167 still contains some valuable changes regarding tests. Ill continue either one PR depending on the target branch / proposed fix.Commits-------a3a2ff0 [TwigBridge] Fix rendering of currency by MoneyType
Uh oh!
There was an error while loading.Please reload this page.
Split from#25167 as suggested by@nicolas-grekas to see if this can be reasonably fixed on 2.7, the right way using this itsy-bitsy new feature.
#25167 still contains some valuable changes regarding tests. Ill continue either one PR depending on the target branch / proposed fix.