Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions 17 src/Symfony/Bridge/Twig/Extension/FormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public function getFilters()
{
return array(
new TwigFilter('humanize', array($this, 'humanize')),
new TwigFilter('form_encode_currency', array($this, 'encodeCurrency'), array('is_safe' => array('html'), 'needs_environment' => true)),
);
}

Expand Down Expand Up @@ -166,6 +167,22 @@ public function isRootForm(FormView $formView)
return null === $formView->parent;
}

/**
* @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

{
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

} 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).

}

return str_replace('{{ widget }}', $widget, $text);
}

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

{% 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 }}</span>
{% endif %}
</div>
{% else %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
{%- endblock integer_widget -%}

{%- block money_widget -%}
{{ money_pattern|replace({ '{{ widget }}': block('form_widget_simple') })|raw }}
{{ money_pattern|form_encode_currency(block('form_widget_simple')) }}
{%- endblock money_widget -%}

{%- block url_widget -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ protected function tearDown()
$this->extension = null;
}

public function testMoneyWidgetInIso()
{
$environment = new Environment(new StubFilesystemLoader(array(
__DIR__.'/../../Resources/views/Form',
__DIR__.'/Fixtures/templates/form',
)), array('strict_variables' => true));
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addExtension($this->extension);
$environment->setCharset('ISO-8859-1');

$this->extension->initRuntime($environment);

$view = $this->factory
->createNamed('name', 'money')
->createView()
;

$this->assertSame(<<<'HTML'
<div class="input-group">
<span class="input-group-addon">&euro; </span>
<input type="text" id="name" name="name" required="required" class="form-control" /> </div>
HTML
, trim($this->renderWidget($view)));
}

protected function renderForm(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,26 @@ public function testIsRootForm($expected, FormView $formView)
$this->assertSame($expected, $this->extension->isRootForm($formView));
}

public function testMoneyWidgetInIso()
{
$environment = new Environment(new StubFilesystemLoader(array(
__DIR__.'/../../Resources/views/Form',
__DIR__.'/Fixtures/templates/form',
)), array('strict_variables' => true));
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addExtension($this->extension);
$environment->setCharset('ISO-8859-1');

$this->extension->initRuntime($environment);

$view = $this->factory
->createNamed('name', 'money')
->createView()
;

$this->assertSame('&euro; <input type="text" id="name" name="name" required="required" />', $this->renderWidget($view));
}

protected function renderForm(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php echo str_replace('{{ widget }}', $view['form']->block($form, 'form_widget_simple'), $money_pattern) ?>
<?php echo $view['form']->formEncodeCurrency($money_pattern, $view['form']->block($form, 'form_widget_simple')) ?>
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,20 @@ public function humanize($text)
{
return $this->renderer->humanize($text);
}

/**
* @internal
*/
public function formEncodeCurrency($text, $widget = '')
{
if ('UTF-8' === $charset = $this->getCharset()) {
$text = htmlspecialchars($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
} 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);
}

return str_replace('{{ widget }}', $widget, $text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ protected function tearDown()
parent::tearDown();
}

public function testMoneyWidgetInIso()
{
$this->engine->setCharset('ISO-8859-1');

$view = $this->factory
->createNamed('name', 'money')
->createView()
;

$this->assertSame('&euro; <input type="text" id="name" name="name" required="required" />', $this->renderWidget($view));
}

protected function renderForm(FormView $view, array $vars = array())
{
return (string) $this->engine->get('form')->form($view, $vars);
Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.

"symfony/translation": "~2.7",
"doctrine/annotations": "~1.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function getName()
}

/**
* Returns the pattern for this locale.
* Returns the pattern for this locale in UTF-8.
*
* The pattern contains the placeholder "{{ widget }}" where the HTML tag should
* be inserted
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.