Issue #2540870 by joelpittet, lauriii: Improve the |safe_join Twig to be inline with twig_join_filter()
parent
5dc2bd0613
commit
a53d5aaf8f
|
@ -534,7 +534,7 @@ class TwigExtension extends \Twig_Extension {
|
|||
*
|
||||
* @param \Twig_Environment $env
|
||||
* A Twig_Environment instance.
|
||||
* @param mixed[]|\Traversable $value
|
||||
* @param mixed[]|\Traversable|NULL $value
|
||||
* The pieces to join.
|
||||
* @param string $glue
|
||||
* The delimiter with which to join the string. Defaults to an empty string.
|
||||
|
@ -545,10 +545,14 @@ class TwigExtension extends \Twig_Extension {
|
|||
* The strings joined together.
|
||||
*/
|
||||
public function safeJoin(\Twig_Environment $env, $value, $glue = '') {
|
||||
if ($value instanceof \Traversable) {
|
||||
$value = iterator_to_array($value, false);
|
||||
}
|
||||
|
||||
return implode($glue, array_map(function($item) use ($env) {
|
||||
// If $item is not marked safe then it will be escaped.
|
||||
return $this->escapeFilter($env, $item, 'html', NULL, TRUE);
|
||||
}, $value));
|
||||
}, (array) $value));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -200,6 +200,20 @@ class TwigExtensionTest extends UnitTestCase {
|
|||
];
|
||||
$result = $twig_extension->safeJoin($twig_environment, $items, '<br/>');
|
||||
$this->assertEquals('<em>will be escaped</em><br/><em>will be markup</em><br/><strong>will be rendered</strong>', $result);
|
||||
|
||||
// Ensure safe_join Twig filter supports Traversable variables.
|
||||
$items = new \ArrayObject([
|
||||
'<em>will be escaped</em>',
|
||||
$markup,
|
||||
['#markup' => '<strong>will be rendered</strong>'],
|
||||
]);
|
||||
$result = $twig_extension->safeJoin($twig_environment, $items, ', ');
|
||||
$this->assertEquals('<em>will be escaped</em>, <em>will be markup</em>, <strong>will be rendered</strong>', $result);
|
||||
|
||||
// Ensure safe_join Twig filter supports empty variables.
|
||||
$items = NULL;
|
||||
$result = $twig_extension->safeJoin($twig_environment, $items, '<br>');
|
||||
$this->assertEmpty($result);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue