diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 377efe6dde..1b7bc14659 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -171,6 +171,9 @@ public function getFilters() { // Replace twig's escape filter with our own. new \Twig_SimpleFilter('drupal_escape', [$this, 'escapeFilter'], ['needs_environment' => TRUE, 'is_safe_callback' => 'twig_escape_filter_is_safe']), + // Remove html comments from a string. + new \Twig_SimpleFilter('remove_html_comments', [$this, 'removeHtmlComments']), + // Implements safe joining. // @todo Make that the default for |join? Upstream issue: // https://github.com/fabpot/Twig/issues/1420 @@ -395,6 +398,20 @@ public function escapePlaceholder($env, $string) { return '<em class="placeholder">' . $this->escapeFilter($env, $string) . '</em>'; } + /** + * Removes html comments from string. + * Example: A string which has value <!--Start DEBUG--> ABCD <!--End DEBUG--> + * will be returned the output ABCD after using the the following function. + * @param string $string + * A string, which have html comments. + * @return string + * A string, which have no html comments. + */ + public function removeHtmlComments($string) { + $output = preg_replace('/<!--(.|\s)*?-->\s*/', '', $string); + return $output; + } + /** * Overrides twig_escape_filter(). *