From 82591820bdae47967eb10284fabd70ac1223d7ae Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Sun, 26 Nov 2017 21:19:15 +0100 Subject: [PATCH] [Form][TwigBridge] Handle MoneyType with non UTF-8 charset --- .../Resources/views/Form/bootstrap_3_layout.html.twig | 4 ++-- .../Twig/Resources/views/Form/form_div_layout.html.twig | 2 +- .../Component/Form/Extension/Core/Type/MoneyType.php | 4 ++-- src/Symfony/Component/Form/Tests/AbstractLayoutTest.php | 8 +++----- .../Form/Tests/Extension/Core/Type/MoneyTypeTest.php | 8 ++++---- 5 files changed, 12 insertions(+), 14 deletions(-) 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 2efc0c4d4a213..00d8d7bc6459f 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 @@ -23,11 +23,11 @@
{% set append = money_pattern starts with '{{' %} {% if not append %} - {{ money_pattern|replace({ '{{ widget }}':''}) }} + {{ money_pattern|convert_encoding(_charset, 'UTF-8')|replace({ '{{ widget }}':''})|raw }} {% endif %} {{- block('form_widget_simple') -}} {% if append %} - {{ money_pattern|replace({ '{{ widget }}':''}) }} + {{ money_pattern|convert_encoding(_charset, 'UTF-8')|replace({ '{{ widget }}':''})|raw }} {% endif %}
{%- endblock money_widget %} 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 a27c81dd495ae..2df87b40eef3f 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|convert_encoding(_charset, 'UTF-8')|replace({ '{{ widget }}': block('form_widget_simple') })|raw }} {%- endblock money_widget -%} {%- block url_widget -%} diff --git a/src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php b/src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php index 267fe9eaf636c..b6c3b393b2571 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php @@ -43,7 +43,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) */ public function buildView(FormView $view, FormInterface $form, array $options) { - $view->vars['money_pattern'] = self::getPattern($options['currency']); + $view->vars['money_pattern'] = htmlentities(self::getPattern($options['currency']), ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8'); } /** @@ -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 diff --git a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php index 52aaacd15ac12..8e7effe0259bc 100644 --- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php @@ -62,7 +62,7 @@ protected function assertMatchesXpath($html, $expression, $count = 1) try { // Wrap in node so we can load HTML with multiple tags at // the top level - $dom->loadXML(''.$html.''); + $dom->loadHTML(''.$html.''); } catch (\Exception $e) { $this->fail(sprintf( "Failed loading HTML:\n\n%s\n\nError: %s", @@ -71,17 +71,15 @@ protected function assertMatchesXpath($html, $expression, $count = 1) )); } $xpath = new \DOMXPath($dom); - $nodeList = $xpath->evaluate('/root'.$expression); + $nodeList = $xpath->evaluate('/html/body'.$expression); if ($nodeList->length != $count) { - $dom->formatOutput = true; $this->fail(sprintf( "Failed asserting that \n\n%s\n\nmatches exactly %s. Matches %s in \n\n%s", $expression, 1 == $count ? 'once' : $count.' times', 1 == $nodeList->length ? 'once' : $nodeList->length.' times', - // strip away and - substr($dom->saveHTML(), 6, -8) + $html )); } else { $this->addToAssertionCount(1); diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/MoneyTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/MoneyTypeTest.php index 4f97d75f972bc..679a30669cae0 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/MoneyTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/MoneyTypeTest.php @@ -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 }} €', $view->vars['money_pattern']); } public function testMoneyPatternWorksForYen() @@ -43,7 +43,7 @@ public function testMoneyPatternWorksForYen() $view = $this->factory->create(static::TESTED_TYPE, null, array('currency' => 'JPY')) ->createView(); - $this->assertTrue((bool) strstr($view->vars['money_pattern'], '¥')); + $this->assertTrue((bool) strstr($view->vars['money_pattern'], '¥')); } // https://github.com/symfony/symfony/issues/5458 @@ -54,8 +54,8 @@ public function testPassDifferentPatternsForDifferentCurrencies() $view1 = $this->factory->create(static::TESTED_TYPE, null, array('currency' => 'GBP'))->createView(); $view2 = $this->factory->create(static::TESTED_TYPE, null, array('currency' => 'EUR'))->createView(); - $this->assertSame('{{ widget }} £', $view1->vars['money_pattern']); - $this->assertSame('{{ widget }} €', $view2->vars['money_pattern']); + $this->assertSame('{{ widget }} £', $view1->vars['money_pattern']); + $this->assertSame('{{ widget }} €', $view2->vars['money_pattern']); } public function testSubmitNull($expected = null, $norm = null, $view = null)