8000 bug #26663 [TwigBridge] Fix rendering of currency by MoneyType (ro0NL) · symfony/symfony@4243db5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4243db5

Browse files
committed
bug #26663 [TwigBridge] Fix rendering of currency by MoneyType (ro0NL)
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
2 parents ae80466 + a3a2ff0 commit 4243db5

File tree

10 files changed

+96
-6
lines changed

10 files changed

+96
-6
lines changed

src/Symfony/Bridge/Twig/Extension/FormExtension.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public function getFilters()
8888
{
8989
return array(
9090
new TwigFilter('humanize', array($this, 'humanize')),
91+
new TwigFilter('form_encode_currency', array($this, 'encodeCurrency'), array('is_safe' => array('html'), 'needs_environment' => true)),
9192
);
9293
}
9394

@@ -166,6 +167,22 @@ public function isRootForm(FormView $formView)
166167
return null === $formView->parent;
167168
}
168169

170+
/**
171+
* @internal
172+
*/
173+
public function encodeCurrency(Environment $environment, $text, $widget = '')
174+
{
175+
if ('UTF-8' === $charset = $environment->getCharset()) {
176+
$text = htmlspecialchars($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
177+
} else {
178+
$text = htmlentities($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
179+
$text = iconv('UTF-8', $charset, $text);
180+
$widget = iconv('UTF-8', $charset, $widget);
181+
}
182+
183+
return str_replace('{{ widget }}', $widget, $text);
184+
}
185+
169186
/**
170187
* {@inheritdoc}
171188
*/

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
{% if prepend or append %}
2626
<div class="input-group">
2727
{% if prepend %}
28-
<span class="input-group-addon">{{ money_pattern|replace({ '{{ widget }}':''}) }}</span>
28+
<span class="input-group-addon">{{ money_pattern|form_encode_currency }}</span>
2929
{% endif %}
3030
{{- block('form_widget_simple') -}}
3131
{% if append %}
32-
<span class="input-group-addon">{{ money_pattern|replace({ '{{ widget }}':''}) }}</span>
32+
<span class="input-group-addon">{{ money_pattern|form_encode_currency }}</span>
3333
{% endif %}
3434
</div>
3535
{% else %}

src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
{%- endblock integer_widget -%}
143143

144144
{%- block money_widget -%}
145-
{{ money_pattern|replace({ '{{ widget }}': block('form_widget_simple') })|raw }}
145+
{{ money_pattern|form_encode_currency(block('form_widget_simple')) }}
146146
{%- endblock money_widget -%}
147147

148148
{%- block url_widget -%}

src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,31 @@ protected function tearDown()
6363
$this->extension = null;
6464
}
6565

66+
public function testMoneyWidgetInIso()
67+
{
68+
$environment = new Environment(new StubFilesystemLoader(array(
69+
__DIR__.'/../../Resources/views/Form',
70+
__DIR__.'/Fixtures/templates/form',
71+
)), array('strict_variables' => true));
72+
$environment->addExtension(new TranslationExtension(new StubTranslator()));
73+
$environment->addExtension($this->extension);
74+
$environment->setCharset('ISO-8859-1');
75+
76+
$this->extension->initRuntime($environment);
77+
78+
$view = $this->factory
79+
->createNamed('name', 'money')
80+
->createView()
81+
;
82+
83+
$this->assertSame(<<<'HTML'
84+
<div class="input-group">
85+
<span class="input-group-addon">&euro; </span>
86+
<input type="text" id="name" name="name" required="required" class="form-control" /> </div>
87+
HTML
88+
, trim($this->renderWidget($view)));
89+
}
90+
6691
protected function renderForm(FormView $view, array $vars = array())
6792
{
6893
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);

src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,26 @@ public function testIsRootForm($expected, FormView $formView)
162162
$this->assertSame($expected, $this->extension->isRootForm($formView));
163163
}
164164

165+
public function testMoneyWidgetInIso()
166+
{
167+
$environment = new Environment(new StubFilesystemLoader(array(
168+
__DIR__.'/../../Resources/views/Form',
169+
__DIR__.'/Fixtures/templates/form',
170+
)), array('strict_variables' => true));
171+
$environment->addExtension(new TranslationExtension(new StubTranslator()));
172+
$environment->addExtension($this->extension);
173+
$environment->setCharset('ISO-8859-1');
174+
175+
$this->extension->initRuntime($environment);
176+
177+
$view = $this->factory
178+
->createNamed('name', 'money')
179+
->createView()
180+
;
181+
182+
$this->assertSame('&euro; <input type="text" id="name" name="name" required="required" />', $this->renderWidget($view));
183+
}
184+
165185
protected function renderForm(FormView $view, array $vars = array())
166186
{
167187
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php echo str_replace('{{ widget }}', $view['form']->block($form, 'form_widget_simple'), $money_pattern) ?>
1+
<?php echo $view['form']->formEncodeCurrency($money_pattern, $view['form']->block($form, 'form_widget_simple')) ?>

src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,20 @@ public function humanize($text)
260260
{
261261
return $this->renderer->humanize($text);
262262
}
263+
264+
/**
265+
* @internal
266+
*/
267+
public function formEncodeCurrency($text, $widget = '')
268+
{
269+
if ('UTF-8' === $charset = $this->getCharset()) {
270+
$text = htmlspecialchars($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
271+
} else {
272+
$text = htmlentities($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
273+
$text = iconv('UTF-8', $charset, $text);
274+
$widget = iconv('UTF-8', $charset, $widget);
275+
}
276+
277+
return str_replace('{{ widget }}', $widget, $text);
278+
}
263279
}

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ protected function tearDown()
6161
parent::tearDown();
6262
}
6363

64+
public function testMoneyWidgetInIso()
65+
{
66+
$this->engine->setCharset('ISO-8859-1');
67+
68+
$view = $this->factory
69+
->createNamed('name', 'money')
70+
->createView()
71+
;
72+
73+
$this->assertSame('&euro; <input type="text" id="name" name="name" required="required" />', $this->renderWidget($view));
74+
}
75+
6476
protected function renderForm(FormView $view, array $vars = array())
6577
{
6678
return (string) $this->engine->get('form')->form($view, $vars);

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"symfony/security-core": "~2.6.13|~2.7.9|~2.8",
3131
"symfony/security-csrf": "~2.6",
3232
"symfony/stopwatch": "~2.3",
33-
"symfony/templating": "~2.1",
33+
"symfony/templating": "~2.7",
3434
"symfony/translation": "~2.7",
3535
"doctrine/annotations": "~1.0"
3636
},

src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function getName()
8383
}
8484

8585
/**
86-
* Returns the pattern for this locale.
86+
* Returns the pattern for this locale in UTF-8.
8787
*
8888
* The pattern contains the placeholder "{{ widget }}" where the HTML tag should
8989
* be inserted

0 commit comments

Comments
 (0)
0