From 0a9438369a868e963ddcaa2c13f856bc432acb4f Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Sat, 24 Mar 2018 17:52:09 +0100 Subject: [PATCH 01/12] [TwigBridge] Add html_entities filter --- src/Symfony/Bridge/Twig/Extension/FormExtension.php | 8 ++++++++ .../Resources/views/Form/bootstrap_3_layout.html.twig | 4 ++-- .../Twig/Resources/views/Form/form_div_layout.html.twig | 2 +- .../Resources/views/Form/money_widget.html.php | 2 +- .../Component/Form/Extension/Core/Type/MoneyType.php | 2 +- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Extension/FormExtension.php b/src/Symfony/Bridge/Twig/Extension/FormExtension.php index e101704efaa9d..c0efebfa34760 100644 --- a/src/Symfony/Bridge/Twig/Extension/FormExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/FormExtension.php @@ -88,6 +88,7 @@ public function getFilters() { return array( new TwigFilter('humanize', array($this, 'humanize')), + new TwigFilter('form_html_entities', array($this, 'htmlEntities'), array('is_safe' => array('html'), 'needs_environment' => true)), ); } @@ -126,6 +127,13 @@ public function humanize($text) return $this->renderer->humanize($text); } + public function htmlEntities(Environment $environment, $text) + { + $text = htmlentities($text, ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8'); + + return iconv('UTF-8', $environment->getCharset(), $text); + } + /** * Returns whether a choice is selected for a given form value. * 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 c61f28a2d302f..76cf95da380b0 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_html_entities|replace({ '{{ widget }}':''})|raw }} {% endif %} {{- block('form_widget_simple') -}} {% if append %} - {{ money_pattern|replace({ '{{ widget }}':''}) }} + {{ money_pattern|form_html_entities|replace({ '{{ widget }}':''})|raw }} {% 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 922b3ec7a7cee..f896061e4e3f5 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|html_entities|replace({ '{{ widget }}': block('form_widget_simple') })|raw }} {%- endblock money_widget -%} {%- block url_widget -%} 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 644d284915371..3063f1c3df48f 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) ?> +block($form, 'form_widget_simple'), htmlentities($money_pattern, ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8')) ?> diff --git a/src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php b/src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php index bfdf92d9466b6..7f55dc123aab1 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 @@ 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 From 95218e1b7ebeb5b34f876ab99e299ef2048d1cbb Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Tue, 27 Mar 2018 18:32:45 +0200 Subject: [PATCH 02/12] optim --- src/Symfony/Bridge/Twig/Extension/FormExtension.php | 6 +++++- .../Twig/Resources/views/Form/bootstrap_3_layout.html.twig | 4 ++-- .../Twig/Resources/views/Form/form_div_layout.html.twig | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Extension/FormExtension.php b/src/Symfony/Bridge/Twig/Extension/FormExtension.php index c0efebfa34760..327eef4ce2b9c 100644 --- a/src/Symfony/Bridge/Twig/Extension/FormExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/FormExtension.php @@ -131,7 +131,11 @@ public function htmlEntities(Environment $environment, $text) { $text = htmlentities($text, ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8'); - return iconv('UTF-8', $environment->getCharset(), $text); + if ('UTF-8' === $charset = $environment->getCharset()) { + return $text; + } + + return iconv('UTF-8', $charset, $text); } /** 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 76cf95da380b0..e94896e593df6 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|form_html_entities|replace({ '{{ widget }}':''})|raw }} + {{ money_pattern|replace({ '{{ widget }}':''})|form_html_entities|raw }} {% endif %} {{- block('form_widget_simple') -}} {% if append %} - {{ money_pattern|form_html_entities|replace({ '{{ widget }}':''})|raw }} + {{ money_pattern|replace({ '{{ widget }}':''})|form_html_entities|raw }} {% 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 f896061e4e3f5..f90efdb2cdccc 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|html_entities|replace({ '{{ widget }}': block('form_widget_simple') })|raw }} + {{ money_pattern|form_html_entities|replace({ '{{ widget }}': block('form_widget_simple') })|raw }} {%- endblock money_widget -%} {%- block url_widget -%} From 2c692c3912a444be23cab57e24ece34255c5e086 Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Tue, 27 Mar 2018 18:39:45 +0200 Subject: [PATCH 03/12] php templates --- .../Resources/views/Form/money_widget.html.php | 2 +- .../FrameworkBundle/Templating/Helper/FormHelper.php | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) 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 3063f1c3df48f..17ac364d19f2f 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'), htmlentities($money_pattern, ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8')) ?> +block($form, 'form_widget_simple'), $view['form']->htmlEntities($money_pattern)) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php index 909fd14bb7ad6..1ea049bd363c8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php @@ -260,4 +260,15 @@ public function humanize($text) { return $this->renderer->humanize($text); } + + public function htmlEntities($text) + { + $text = htmlentities($text, ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8'); + + if ('UTF-8' === $charset = $this->getCharset()) { + return $text; + } + + return iconv('UTF-8', $charset, $text); + } } From f128bcfde8d5dfe2bd549966685ba7928b8482df Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Tue, 27 Mar 2018 18:43:19 +0200 Subject: [PATCH 04/12] tests --- .../Component/Form/Tests/AbstractLayoutTest.php | 10 +++------- .../Form/Tests/Extension/Core/Type/MoneyTypeTest.php | 8 ++++---- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php index 2d0bbf97be379..b6860509bf96f 100644 --- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php @@ -60,9 +60,7 @@ protected function assertMatchesXpath($html, $expression, $count = 1) { $dom = new \DOMDocument('UTF-8'); 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 +69,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 8d788144ea9b8..3eb5cfa47f520 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->assertSame('¥ {{ widget }}', $view->vars['money_pattern']); + $this->assertSame('¥ {{ widget }}', $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) From 3887e4976a590c4ec0db064616983e56bf502587 Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Tue, 27 Mar 2018 18:52:52 +0200 Subject: [PATCH 05/12] favor consistency --- .../Twig/Resources/views/Form/bootstrap_3_layout.html.twig | 4 ++-- 1 file changed, 2 insertions(+), 2 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 e94896e593df6..76cf95da380b0 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 }}':''})|form_html_entities|raw }} + {{ money_pattern|form_html_entities|replace({ '{{ widget }}':''})|raw }} {% endif %} {{- block('form_widget_simple') -}} {% if append %} - {{ money_pattern|replace({ '{{ widget }}':''})|form_html_entities|raw }} + {{ money_pattern|form_html_entities|replace({ '{{ widget }}':''})|raw }} {% endif %}
{% else %} From 768d1e2940d720eacb73dd2ac093e0c4a408b1b4 Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Tue, 27 Mar 2018 19:22:52 +0200 Subject: [PATCH 06/12] revise --- .../Bridge/Twig/Extension/FormExtension.php | 27 ++++++++++--------- .../views/Form/bootstrap_3_layout.html.twig | 4 +-- .../views/Form/form_div_layout.html.twig | 2 +- .../views/Form/money_widget.html.php | 2 +- .../Templating/Helper/FormHelper.php | 11 +++++--- 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Extension/FormExtension.php b/src/Symfony/Bridge/Twig/Extension/FormExtension.php index 327eef4ce2b9c..c67db0179b430 100644 --- a/src/Symfony/Bridge/Twig/Extension/FormExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/FormExtension.php @@ -88,7 +88,7 @@ public function getFilters() { return array( new TwigFilter('humanize', array($this, 'humanize')), - new TwigFilter('form_html_entities', array($this, 'htmlEntities'), array('is_safe' => array('html'), 'needs_environment' => true)), + new TwigFilter('form_encode_currency', array($this, 'encodeCurrency'), array('is_safe' => array('html'), 'needs_environment' => true)), ); } @@ -127,17 +127,6 @@ public function humanize($text) return $this->renderer->humanize($text); } - public function htmlEntities(Environment $environment, $text) - { - $text = htmlentities($text, ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8'); - - if ('UTF-8' === $charset = $environment->getCharset()) { - return $text; - } - - return iconv('UTF-8', $charset, $text); - } - /** * Returns whether a choice is selected for a given form value. * @@ -178,6 +167,20 @@ public function isRootForm(FormView $formView) return null === $formView->parent; } + /** + * @internal + */ + 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'); + } + + $text = htmlentities($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8'); + + return iconv('UTF-8', $charset, $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 76cf95da380b0..7ab98753ea68d 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|form_html_entities|replace({ '{{ widget }}':''})|raw }} + {{ money_pattern|form_encode_currency|replace({ '{{ widget }}':''})|raw }} {% endif %} {{- block('form_widget_simple') -}} {% if append %} - {{ money_pattern|form_html_entities|replace({ '{{ widget }}':''})|raw }} + {{ money_pattern|form_encode_currency|replace({ '{{ widget }}':''})|raw }} {% 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 f90efdb2cdccc..59507dd3f3a9a 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|form_html_entities|replace({ '{{ widget }}': block('form_widget_simple') })|raw }} + {{ money_pattern|form_encode_currency|replace({ '{{ widget }}': block('form_widget_simple') })|raw }} {%- endblock money_widget -%} {%- block url_widget -%} 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 17ac364d19f2f..a3bf90c19a94c 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'), $view['form']->htmlEntities($money_pattern)) ?> +block($form, 'form_widget_simple'), $view['form']->formEncodeCurrency($money_pattern)) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php index 1ea049bd363c8..f692d958bcccc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php @@ -261,14 +261,17 @@ public function humanize($text) return $this->renderer->humanize($text); } - public function htmlEntities($text) + /** + * @internal + */ + public function formEncodeCurrency($text) { - $text = htmlentities($text, ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8'); - if ('UTF-8' === $charset = $this->getCharset()) { - return $text; + return htmlspecialchars($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8'); } + $text = htmlentities($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8'); + return iconv('UTF-8', $charset, $text); } } From 94958260505468e7f8ed8c00f97eff0466a3b2f2 Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Wed, 28 Mar 2018 12:33:49 +0200 Subject: [PATCH 07/12] revision round 2 --- .../Bridge/Twig/Extension/FormExtension.php | 12 +++++++----- .../views/Form/bootstrap_3_layout.html.twig | 4 ++-- .../Resources/views/Form/form_div_layout.html.twig | 2 +- .../Resources/views/Form/money_widget.html.php | 2 +- .../Templating/Helper/FormHelper.php | 14 ++++++++------ 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Extension/FormExtension.php b/src/Symfony/Bridge/Twig/Extension/FormExtension.php index c67db0179b430..ab7c6ab626d8c 100644 --- a/src/Symfony/Bridge/Twig/Extension/FormExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/FormExtension.php @@ -170,15 +170,17 @@ public function isRootForm(FormView $formView) /** * @internal */ - public function encodeCurrency(Environment $environment, $text) + public function encodeCurrency(Environment $environment, $text, $widget = '') { if ('UTF-8' === $charset = $environment->getCharset()) { - return htmlspecialchars($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8'); + $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); } - $text = htmlentities($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8'); - - return iconv('UTF-8', $charset, $text); + return str_replace('{{ widget }}', $widget, $text); } /** 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 7ab98753ea68d..215f0ce754531 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|form_encode_currency|replace({ '{{ widget }}':''})|raw }} + {{ money_pattern|form_encode_currency }} {% endif %} {{- block('form_widget_simple') -}} {% if append %} - {{ money_pattern|form_encode_currency|replace({ '{{ widget }}':''})|raw }} + {{ 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 59507dd3f3a9a..9f1d95f2782f1 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|form_encode_currency|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/Bundle/FrameworkBundle/Resources/views/Form/money_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/money_widget.html.php index a3bf90c19a94c..25fe13f7e057c 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'), $view['form']->formEncodeCurrency($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 f692d958bcccc..4bb59c2356092 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php @@ -264,14 +264,16 @@ public function humanize($text) /** * @internal */ - public function formEncodeCurrency($text) + public function formEncodeCurrency($text, $widget = '') { - if ('UTF-8' === $charset = $this->getCharset()) { - return htmlspecialchars($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8'); + 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); } - $text = htmlentities($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8'); - - return iconv('UTF-8', $charset, $text); + return str_replace('{{ widget }}', $widget, $text); } } From a9e9081d618f1162813bdd0bf90812b51f166c4d Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Wed, 28 Mar 2018 12:59:47 +0200 Subject: [PATCH 08/12] revert form type tests --- .../Form/Tests/Extension/Core/Type/MoneyTypeTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 3eb5cfa47f520..8d788144ea9b8 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->assertSame('¥ {{ widget }}', $view->vars['money_pattern']); + $this->assertSame('¥ {{ widget }}', $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) From b078f7ecc570188007ac674e5d86f39e96f79d7f Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Wed, 28 Mar 2018 13:15:52 +0200 Subject: [PATCH 09/12] first tests --- .../Extension/FormExtensionDivLayoutTest.php | 20 +++++++++++++++++++ .../Templating/Helper/FormHelper.php | 2 +- .../Helper/FormHelperDivLayoutTest.php | 12 +++++++++++ .../Form/Tests/AbstractLayoutTest.php | 10 +++++++--- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php index e11daf1831349..915b3fc20f256 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php @@ -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-15'); + + $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/Templating/Helper/FormHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php index 4bb59c2356092..ba63cad397c3a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php @@ -266,7 +266,7 @@ public function humanize($text) */ public function formEncodeCurrency($text, $widget = '') { - if ('UTF-8' === $charset = $environment->getCharset()) { + 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'); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php index c745818b1e72b..576ea0ad7a7d1 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 @@ protected function tearDown() parent::tearDown(); } + public function testMoneyWidgetInIso() + { + $this->engine->setCharset('ISO-8859-15'); + + $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/Component/Form/Tests/AbstractLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php index b6860509bf96f..2d0bbf97be379 100644 --- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php @@ -60,7 +60,9 @@ protected function assertMatchesXpath($html, $expression, $count = 1) { $dom = new \DOMDocument('UTF-8'); try { - $dom->loadHTML(''.$html.''); + // Wrap in node so we can load HTML with multiple tags at + // the top level + $dom->loadXML(''.$html.''); } catch (\Exception $e) { $this->fail(sprintf( "Failed loading HTML:\n\n%s\n\nError: %s", @@ -69,15 +71,17 @@ protected function assertMatchesXpath($html, $expression, $count = 1) )); } $xpath = new \DOMXPath($dom); - $nodeList = $xpath->evaluate('/html/body'.$expression); + $nodeList = $xpath->evaluate('/root'.$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', - $html + // strip away and + substr($dom->saveHTML(), 6, -8) )); } else { $this->addToAssertionCount(1); From 73d56021acb0da3c639de880df1ab88f858b73b9 Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Wed, 28 Mar 2018 13:44:05 +0200 Subject: [PATCH 10/12] fix tests --- .../FormExtensionBootstrap3LayoutTest.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php index 85ef2ceed53af..fd68ce37478e9 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php @@ -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-15'); + + $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); From ee5031875696031a0d87bb5937fe9519d1326c1c Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Wed, 28 Mar 2018 13:58:28 +0200 Subject: [PATCH 11/12] for hhvm --- .../Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php | 2 +- .../Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php | 2 +- .../Tests/Templating/Helper/FormHelperDivLayoutTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php index fd68ce37478e9..2a22f7d88e2b3 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php @@ -71,7 +71,7 @@ public function testMoneyWidgetInIso() )), array('strict_variables' => true)); $environment->addExtension(new TranslationExtension(new StubTranslator())); $environment->addExtension($this->extension); - $environment->setCharset('ISO-8859-15'); + $environment->setCharset('ISO-8859-1'); $this->extension->initRuntime($environment); diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php index 915b3fc20f256..cb81f7bbc70ee 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php @@ -170,7 +170,7 @@ public function testMoneyWidgetInIso() )), array('strict_variables' => true)); $environment->addExtension(new TranslationExtension(new StubTranslator())); $environment->addExtension($this->extension); - $environment->setCharset('ISO-8859-15'); + $environment->setCharset('ISO-8859-1'); $this->extension->initRuntime($environment); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php index 576ea0ad7a7d1..b1e8efaa6d90d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php @@ -63,7 +63,7 @@ protected function tearDown() public function testMoneyWidgetInIso() { - $this->engine->setCharset('ISO-8859-15'); + $this->engine->setCharset('ISO-8859-1'); $view = $this->factory ->createNamed('name', 'money') From 1182c697cc6abf95407a49661a24003f262303d0 Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Wed, 28 Mar 2018 14:13:25 +0200 Subject: [PATCH 12/12] raise deps=low --- src/Symfony/Bundle/FrameworkBundle/composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index ed611114fe2aa..b910cd9ed33e9 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" },