Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed

[TwigBridge] Fix rendering of currency by MoneyType #26663

wants to merge 12 commits into from

Conversation

ro0NL
Copy link
Contributor

@ro0NL ro0NL commented Mar 24, 2018

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.

*
* @author Roland Franssen <[email protected]>
*/
class HtmlEntitiesExtension extends AbstractExtension
Copy link
Member

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?

Copy link
Contributor Author

@ro0NL ro0NL Mar 25, 2018

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 suggest form_html_entities

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Member

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)

Copy link
Contributor Author

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, hence var|form_html_entities.

@nicolas-grekas nicolas-grekas added this to the 2.7 milestone Mar 25, 2018
@nicolas-grekas nicolas-grekas changed the title [TwigBridge] Add html_entities filter [TwigBridge] Add html_entities filter to fix rendering of currency by MoneyType Mar 25, 2018
@nicolas-grekas
Copy link
Member

Tests are red (and lowest deps need to be updated.)

@javiereguiluz
Copy link
Member

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: htmlentities(self::getPattern($options['currency']), ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8') but Nico said that the place was wrong (#25167 (comment)). Could some Form expert help us here? Thanks!

@@ -25,11 +25,11 @@
{% if prepend or append %}
<div class="input-group">
{% if prepend %}
<span class="input-group-addon">{{ money_pattern|replace({ '{{ widget }}':''}) }}</span>
<span class="input-group-addon">{{ money_pattern|html_entities|replace({ '{{ widget }}':''})|raw }}</span>
Copy link
Member

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.

Copy link
Contributor Author

@ro0NL ro0NL Mar 27, 2018

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>

Copy link
Member

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

Copy link
Contributor Author

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.


public function encode($input)
{
return htmlentities($input, ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
Copy link
Member

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.

*/
public function getName()
{
return 'html_entities';
Copy link
Member

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
Copy link
Contributor Author

ro0NL commented Mar 27, 2018

@javiereguiluz

I consider this solution "too big" for the tiny use case being solved.

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.

@nicolas-grekas nicolas-grekas changed the title [TwigBridge] Add html_entities filter to fix rendering of currency by MoneyType [TwigBridge] Fix rendering of currency by MoneyType Mar 27, 2018
public function encodeCurrency(Environment $environment, $text)
{
if ('UTF-8' === $charset = $environment->getCharset()) {
return htmlspecialchars($text, ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
Copy link
Member

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.

@@ -25,11 +25,11 @@
{% if prepend or append %}
<div class="input-group">
{% if prepend %}
<span class="input-group-addon">{{ money_pattern|replace({ '{{ widget }}':''}) }}</span>
<span class="input-group-addon">{{ money_pattern|form_encode_currency|replace({ '{{ widget }}':''})|raw }}</span>
Copy link
Member

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.

{% endif %}
{{- block('form_widget_simple') -}}
{% if append %}
<span class="input-group-addon">{{ money_pattern|replace({ '{{ widget }}':''}) }}</span>
<span class="input-group-addon">{{ money_pattern|form_encode_currency|replace({ '{{ widget }}':''})|raw }}</span>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@@ -33,7 +33,7 @@ public function testPassMoneyPatternToView()
$view = $this->factory->create(static::TESTED_TYPE)
->createView();

$this->assertSame('{{ widget }} ', $view->vars['money_pattern']);
$this->assertSame('{{ widget }} &euro;', $view->vars['money_pattern']);
Copy link
Member

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

Copy link
Contributor Author

@ro0NL ro0NL Mar 28, 2018

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
Copy link
Contributor Author

ro0NL commented Mar 28, 2018

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
Copy link
Contributor Author

ro0NL commented Mar 28, 2018

HHVM strikes again :( https://3v4l.org/NCWh2

Copy link
Contributor Author

@ro0NL ro0NL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now ready. Yay :}

public function encodeCurrency(Environment $environment, $text, $widget = '')
{
if ('UTF-8' === $charset = $environment->getCharset()) {
$text = htmlspecialchars($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
Copy link
Contributor Author

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

@@ -25,11 +25,11 @@
{% if prepend or append %}
<div class="input-group">
{% if prepend %}
<span class="input-group-addon">{{ money_pattern|replace({ '{{ widget }}':''}) }}</span>
<span class="input-group-addon">{{ money_pattern|form_encode_currency }}</span>
Copy link
Contributor Author

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

@@ -30,7 +30,7 @@
"symfony/security-core": "~2.6.13|~2.7.9|~2.8",
"symfony/security-csrf": "~2.6",
"symfony/stopwatch": "~2.3",
"symfony/templating": "~2.1",
"symfony/templating": "~2.7",
Copy link
Contributor Author

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
*/
public function encodeCurrency(Environment $environment, $text, $widget = '')
Copy link
Member

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);
Copy link
Member

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?

Copy link
Contributor Author

@ro0NL ro0NL Mar 28, 2018

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, using htmlentities instead of htmlspecialchars for escaping.

In general we are consistent with regular escaped output (ie. also in target charset) from twig_escape_filter.

Copy link
Contributor Author

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 from block() in twig).

@fabpot
Copy link
Member

fabpot commented Mar 29, 2018

Thank you @ro0NL.

fabpot added a commit that referenced this pull request Mar 29, 2018
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
@fabpot fabpot closed this Mar 29, 2018
@ro0NL ro0NL deleted the htmlentities branch March 29, 2018 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants