diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 01e37a4..69e0bb3 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -52,7 +52,8 @@ public function getFilters() { new \Twig_SimpleFilter('drupal_escape', 'twig_drupal_escape_filter', array('needs_environment' => true, 'is_safe_callback' => 'twig_escape_filter_is_safe')), // Implements safe joining. - // @todo Make that the default for |join? + // @todo Make that the default for |join? Upstream issue: + // https://github.com/fabpot/Twig/issues/1420 new \Twig_SimpleFilter('safe_join', 'twig_drupal_join_filter', array('is_safe' => array('html'))), // Array filters. diff --git a/core/themes/engines/twig/twig.engine b/core/themes/engines/twig/twig.engine index a951a8b..67bfdcf 100644 --- a/core/themes/engines/twig/twig.engine +++ b/core/themes/engines/twig/twig.engine @@ -130,7 +130,7 @@ function twig_render_var($arg) { return NULL; } - // Optimize for strings as its likely they come from escape filter. + // Optimize for strings as it is likely they come from the escape filter. if (is_string($arg)) { return $arg; } @@ -189,14 +189,29 @@ function twig_without($element) { * Overrides twig_escape_filter(). * * Replacement function for twig's escape filter. + * + * @param Twig_Environment $env + * A Twig_Environment instance. + * @param string $string + * The value to be escaped. + * @param string $strategy + * The escaping strategy. Defaults to 'html'. + * @param string $charset + * The charset. + * @param Boolean $autoescape + * Whether the function is called by the auto-escaping feature (TRUE) or by + * the developer (FALSE). + * + * @return string|null + * The escaped, renderd output, or NULL if there is no valid output. */ -function twig_drupal_escape_filter(\Twig_Environment $env, $arg, $strategy = 'html', $charset = null, $autoescape = false) { - // Check for numeric zero. +function twig_drupal_escape_filter(\Twig_Environment $env, $arg, $strategy = 'html', $charset = NULL, $autoescape = FALSE) { + // Check for a numeric zero. if ($arg === 0) { return 0; } - // Return early for NULL and also true for empty arrays. + // Return early for NULL or an empty array. if ($arg == NULL) { return NULL; } @@ -210,21 +225,23 @@ function twig_drupal_escape_filter(\Twig_Environment $env, $arg, $strategy = 'ht if (is_scalar($arg)) { $return = (string) $arg; - } else if (is_object($arg)) { + } + elseif (is_object($arg)) { if (method_exists($arg, '__toString')) { $return = (string) $arg; } else { - throw new Exception(t('Object of type "@class" cannot be printed.', array('@class' => get_class($arg)))); + throw new \Exception(t('Object of type "@class" cannot be printed.', array('@class' => get_class($arg)))); } } // We have a string or an object converted to a string: Autoescape it! if (isset($return)) { if ($strategy != 'html') { - // Drupal only supports HTML strategy, fallback for other strategies. - // @todo Add optional strategy parameter to SafeMarkup function calls, - // to avoid this when its already safe for this strategy. + // Drupal only supports the HTML escaping strategy, so provide a + // fallback for other strategies. + // @todo Add an optional strategy parameter to SafeMarkup function calls, + // to avoid this when it is already safe for this strategy. return twig_escape_filter($env, $return, $strategy, $charset, $autoescape); } if (!SafeMarkup::isSafe($return)) { @@ -242,11 +259,17 @@ function twig_drupal_escape_filter(\Twig_Environment $env, $arg, $strategy = 'ht * * Safely joins several strings together. * - * Note: Glue is considered safe here and _not_ escaped. + * @param array|Traversable $value + * The pieces to join. + * @param string $glue + * The delimiter with which to join the string. Defaults to an empty string. + * + * @return \Drupal\Component\Utility\SafeMarkup|string + * The imploded string, which is now also marked as safe. */ function twig_drupal_join_filter($value, $glue = '') { - if (is_object($value) && $value instanceof Traversable) { - $value = iterator_to_array($value, false); + if (is_object($value) && $value instanceof \Traversable) { + $value = iterator_to_array($value, false); } return SafeMarkup::implode($glue, (array) $value);