From a3a2ff0c74c7c262fac6456d3ce6a9eaaff5a85f Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Sat, 24 Mar 2018 17:52:09 +0100 Subject: [PATCH] [TwigBridge] Fix rendering of currency by MoneyType --- .../Bridge/Twig/Extension/FormExtension.php | 17 +++++++++++++ .../views/Form/bootstrap_3_layout.html.twig | 4 +-- .../views/Form/form_div_layout.html.twig | 2 +- .../FormExtensionBootstrap3LayoutTest.php | 25 +++++++++++++++++++ .../Extension/FormExtensionDivLayoutTest.php | 20 +++++++++++++++ .../views/Form/money_widget.html.php | 2 +- .../Templating/Helper/FormHelper.php | 16 ++++++++++++ .../Helper/FormHelperDivLayoutTest.php | 12 +++++++++ .../Bundle/FrameworkBundle/composer.json | 2 +- .../Form/Extension/Core/Type/MoneyType.php | 2 +- 10 files changed, 96 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Extension/FormExtension.php b/src/Symfony/Bridge/Twig/Extension/FormExtension.php index e101704efa..ab7c6ab626 100644 --- a/src/Symfony/Bridge/Twig/Extension/FormExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/FormExtension.php @@ -88,6 +88,7 @@ class FormExtension extends AbstractExtension implements InitRuntimeInterface { return array( new TwigFilter('humanize', array($this, 'humanize')), + new TwigFilter('form_encode_currency', array($this, 'encodeCurrency'), array('is_safe' => array('html'), 'needs_environment' => true)), ); } @@ -166,6 +167,22 @@ class FormExtension extends AbstractExtension implements InitRuntimeInterface return null === $formView->parent; } + /** + * @internal + */ + 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'); + } 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); + } + /** * {@inheritdoc} */ diff --git a/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig b/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig index c61f28a2d3..215f0ce754 100644 --- a/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig +++ b/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig @@ -25,11 +25,11 @@ {% if prepend or append %}
{% if prepend %} - {{ money_pattern|replace({ '{{ widget }}':''}) }} + {{ money_pattern|form_encode_currency }} {% endif %} {{- block('form_widget_simple') -}} {% if append %} - {{ money_pattern|replace({ '{{ widget }}':''}) }} + {{ money_pattern|form_encode_currency }} {% endif %}
{% else %} diff --git a/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig b/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig index 922b3ec7a7..9f1d95f278 100644 --- a/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig +++ b/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig @@ -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 -%} diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php index 85ef2ceed5..2a22f7d88e 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php @@ -63,6 +63,31 @@ class FormExtensionBootstrap3LayoutTest extends AbstractBootstrap3LayoutTest $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' +
+ +
+HTML + , trim($this->renderWidget($view))); + } + protected function renderForm(FormView $view, array $vars = array()) { return (string) $this->extension->renderer->renderBlock($view, 'form', $vars); diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php index e11daf1831..cb81f7bbc7 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php @@ -162,6 +162,26 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest $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('€ ', $this->renderWidget($view)); + } + protected function renderForm(FormView $view, array $vars = array()) { return (string) $this->extension->renderer->renderBlock($view, 'form', $vars); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/money_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/money_widget.html.php index 644d284915..25fe13f7e0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/money_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/money_widget.html.php @@ -1 +1 @@ -block($form, 'form_widget_simple'), $money_pattern) ?> +formEncodeCurrency($money_pattern, $view['form']->block($form, 'form_widget_simple')) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php index 909fd14bb7..ba63cad397 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php @@ -260,4 +260,20 @@ class FormHelper extends Helper { 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); + } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php index c745818b1e..b1e8efaa6d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php @@ -61,6 +61,18 @@ class FormHelperDivLayoutTest extends AbstractDivLayoutTest parent::tearDown(); } + public function testMoneyWidgetInIso() + { + $this->engine->setCharset('ISO-8859-1'); + + $view = $this->factory + ->createNamed('name', 'money') + ->createView() + ; + + $this->assertSame('€ ', $this->renderWidget($view)); + } + protected function renderForm(FormView $view, array $vars = array()) { return (string) $this->engine->get('form')->form($view, $vars); diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index ed611114fe..b910cd9ed3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -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", "symfony/translation": "~2.7", "doctrine/annotations": "~1.0" }, diff --git a/src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php b/src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php index bfdf92d946..7f55dc123a 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php @@ -83,7 +83,7 @@ class MoneyType extends AbstractType } /** - * 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