Skip to content

Commit

Permalink
[stimulus-controller] fix bool attributes from being rendered incorre…
Browse files Browse the repository at this point in the history
…ctly
  • Loading branch information
jrushlow authored and weaverryan committed Apr 26, 2021
1 parent 0cd625e commit 2694dca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Twig/StimulusTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public function renderStimulusController(Environment $env, $dataOrControllerName
$value = json_encode($value);
}

if (is_bool($value)) {
$value = $value ? 'true' : 'false';
}

$key = twig_escape_filter($env, $this->normalizeKeyName($key), 'html_attr');
$value = twig_escape_filter($env, $value, 'html_attr');

Expand Down
16 changes: 14 additions & 2 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public function provideRenderStimulusController()
],
],
'controllerValues' => [],
'expected' => 'data-controller="my-controller" data-my-controller-boolean-value="1" data-my-controller-number-value="4" data-my-controller-string-value="str"',
'expected' => 'data-controller="my-controller" data-my-controller-boolean-value="true" data-my-controller-number-value="4" data-my-controller-string-value="str"',
];

yield 'single-controller-nested-data' => [
Expand Down Expand Up @@ -247,7 +247,7 @@ public function provideRenderStimulusController()
],
],
'controllerValues' => [],
'expected' => 'data-controller="symfony--ux-dropzone--dropzone" data-symfony--ux-dropzone--dropzone-my-key-value="1"',
'expected' => 'data-controller="symfony--ux-dropzone--dropzone" data-symfony--ux-dropzone--dropzone-my-key-value="true"',
];

yield 'short-single-controller-no-data' => [
Expand All @@ -261,6 +261,18 @@ public function provideRenderStimulusController()
'controllerValues' => ['myValue' => 'scalar-value'],
'expected' => 'data-controller="my-controller" data-my-controller-my-value-value="scalar-value"',
];

yield 'false-attribute-value-renders-false' => [
'dataOrControllerName' => 'false-controller',
'controllerValues' => ['isEnabled' => false],
'expected' => 'data-controller="false-controller" data-false-controller-is-enabled-value="false"',
];

yield 'true-attribute-value-renders-true' => [
'dataOrControllerName' => 'true-controller',
'controllerValues' => ['isEnabled' => true],
'expected' => 'data-controller="true-controller" data-true-controller-is-enabled-value="true"',
];
}

/**
Expand Down

0 comments on commit 2694dca

Please sign in to comment.