diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 443cf73..44134d8 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -15,6 +15,7 @@ use Drupal\Component\Utility\Html; use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\SafeStringInterface; +use Drupal\Component\Utility\Xss; use Drupal\Core\Render\RendererInterface; use Drupal\Core\Routing\UrlGeneratorInterface; use Drupal\Core\Theme\ThemeManagerInterface; @@ -128,12 +129,11 @@ public function getFilters() { // Translation filters. new \Twig_SimpleFilter('t', 't', array('is_safe' => array('html'))), new \Twig_SimpleFilter('trans', 't', array('is_safe' => array('html'))), - // The "raw" filter is not detectable when parsing "trans" tags. To detect - // which prefix must be used for translation (@, !, %), we must clone the - // "raw" filter and give it identifiable names. These filters should only - // be used in "trans" tags. + // To detect which prefix must be used for translation (@, !, %), we + // create filters and give them identifiable names. These filters should + // only be used in "trans" tags. // @see TwigNodeTrans::compileString() - new \Twig_SimpleFilter('passthrough', 'twig_raw_filter', array('is_safe' => array('html'))), + new \Twig_SimpleFilter('passthrough', [$this, 'passthrough'], array('is_safe' => array('html'))), new \Twig_SimpleFilter('placeholder', [$this, 'escapePlaceholder'], array('is_safe' => array('html'), 'needs_environment' => TRUE)), // Replace twig's escape filter with our own. @@ -371,6 +371,22 @@ public function escapePlaceholder($env, $string) { } /** + * Provides a passthrough filter for trans. + * + * @param mixed $string + * The value. + * + * @return string|null + * The string, XSS admin filtered if not safe. + */ + public function passthrough($string) { + if (!SafeMarkup::isSafe($string)) { + $string = Xss::filterAdmin($string); + } + return $string; + } + + /** * Overrides twig_escape_filter(). * * Replacement function for Twig's escape filter. diff --git a/core/modules/system/src/Tests/Theme/TwigExtensionTest.php b/core/modules/system/src/Tests/Theme/TwigExtensionTest.php index 7e25ae5..e67fe61 100644 --- a/core/modules/system/src/Tests/Theme/TwigExtensionTest.php +++ b/core/modules/system/src/Tests/Theme/TwigExtensionTest.php @@ -47,6 +47,9 @@ function testTwigExtensionFilter() { $this->drupalGet('twig-extension-test/filter'); $this->assertText('Every plant is not a mineral.', 'Success: String filtered.'); + $this->assertRaw('<strong>In a placeholder</strong>'); + $this->assertRaw('Passed through'); + $this->assertRaw('translated strings are marked safe'); } /** diff --git a/core/modules/system/src/Tests/Theme/TwigTransTest.php b/core/modules/system/src/Tests/Theme/TwigTransTest.php index b9abb70..0a21083 100644 --- a/core/modules/system/src/Tests/Theme/TwigTransTest.php +++ b/core/modules/system/src/Tests/Theme/TwigTransTest.php @@ -153,7 +153,6 @@ protected function assertTwigTransTags() { '{{ complex.tokens }} were successfully translated with appropriate prefixes.' ); - $this->assertText( 'I have context.', '{% trans %} with a context only msgid was excluded from translation.' diff --git a/core/modules/system/tests/modules/twig_extension_test/src/TwigExtensionTestController.php b/core/modules/system/tests/modules/twig_extension_test/src/TwigExtensionTestController.php index 4ff849a..f45264b 100644 --- a/core/modules/system/tests/modules/twig_extension_test/src/TwigExtensionTestController.php +++ b/core/modules/system/tests/modules/twig_extension_test/src/TwigExtensionTestController.php @@ -6,11 +6,13 @@ */ namespace Drupal\twig_extension_test; +use Drupal\Core\StringTranslation\StringTranslationTrait; /** * Controller routines for Twig extension test routes. */ class TwigExtensionTestController { + use StringTranslationTrait; /** * Menu callback for testing Twig filters in a Twig template. @@ -19,6 +21,9 @@ public function testFilterRender() { return array( '#theme' => 'twig_extension_test_filter', '#message' => 'Every animal is not a mineral.', + '#placeholder_text' => 'In a placeholder', + '#passthrough_text' => 'Passed through', + '#passthrough_safe_text' => $this->t('translated strings are marked safe'), ); } diff --git a/core/modules/system/tests/modules/twig_extension_test/templates/twig_extension_test.filter.html.twig b/core/modules/system/tests/modules/twig_extension_test/templates/twig_extension_test.filter.html.twig index 1e224d0..2e99d2b 100644 --- a/core/modules/system/tests/modules/twig_extension_test/templates/twig_extension_test.filter.html.twig +++ b/core/modules/system/tests/modules/twig_extension_test/templates/twig_extension_test.filter.html.twig @@ -1,3 +1,16 @@
{{ message|testfilter }}
+ +
+ {{ placeholder_text|placeholder }} +
+
+ {{ passthrough_text|passthrough }} +
+
+ {{ passthrough_safe_text|passthrough }} +
\ No newline at end of file diff --git a/core/modules/system/tests/modules/twig_extension_test/twig_extension_test.module b/core/modules/system/tests/modules/twig_extension_test/twig_extension_test.module index 7e89470..899193c 100644 --- a/core/modules/system/tests/modules/twig_extension_test/twig_extension_test.module +++ b/core/modules/system/tests/modules/twig_extension_test/twig_extension_test.module @@ -11,7 +11,7 @@ function twig_extension_test_theme($existing, $type, $theme, $path) { return array( 'twig_extension_test_filter' => array( - 'variables' => array('message' => NULL), + 'variables' => array('message' => NULL, 'placeholder_text' => NULL, 'passthrough_text' => NULL, 'passthrough_safe_text' => NULL), 'template' => 'twig_extension_test.filter', ), 'twig_extension_test_function' => array( diff --git a/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.trans.html.twig b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.trans.html.twig index e2d75d2..a5b9a93 100644 --- a/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.trans.html.twig +++ b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.trans.html.twig @@ -53,7 +53,6 @@ {% endtrans %}
-
{% trans %} Placeholder: {{ string|placeholder }} {% endtrans %}