escapeFilter example


  public function escapePlaceholder(Environment $env$string) {
    $return = $this->escapeFilter($env$string);

    return $return ? '<em class="placeholder">' . $return . '</em>' : NULL;
  }

  /** * Overrides twig_escape_filter(). * * Replacement function for Twig's escape filter. * * @param \Twig\Environment $env * A Twig Environment instance. * @param mixed $arg * The value to be escaped. * @param string $strategy * The escaping strategy. Defaults to 'html'. * @param string $charset * The charset. * @param bool $autoescape * Whether the function is called by the auto-escaping feature (TRUE) or by * the developer (FALSE). * * @return string|null * The escaped, rendered output, or NULL if there is no valid output. * * @throws \Exception * When $arg is passed as an object which does not implement __toString(), * RenderableInterface or toString(). */
/** * Tests output of integer and double 0 values of TwigExtension::escapeFilter(). * * @see https://www.drupal.org/node/2417733 */
  public function testsRenderEscapedZeroValue() {
    /** @var \Drupal\Core\Template\TwigExtension $extension */
    $extension = \Drupal::service('twig.extension');
    /** @var \Drupal\Core\Template\TwigEnvironment $twig */
    $twig = \Drupal::service('twig');
    $this->assertSame(0, $extension->escapeFilter($twig, 0), 'TwigExtension::escapeFilter() returns zero correctly when provided as an integer.');
    $this->assertSame(0, $extension->escapeFilter($twig, 0.0), 'TwigExtension::escapeFilter() returns zero correctly when provided as a double.');
  }

  /** * Tests output of integer and double 0 values of TwigExtension->renderVar(). * * @see https://www.drupal.org/node/2417733 */
  public function testsRenderZeroValue() {
    /** @var \Drupal\Core\Template\TwigExtension $extension */
    $extension = \Drupal::service('twig.extension');
    
$loader = new FilesystemLoader();
    $twig = new Environment($loader[
      'debug' => TRUE,
      'cache' => FALSE,
      'autoescape' => 'html',
      'optimizations' => 0,
    ]);

    // By default, TwigExtension will attempt to cast objects to strings.     // Ensure objects that implement MarkupInterface are unchanged.     $safe_string = $this->createMock('\Drupal\Component\Render\MarkupInterface');
    $this->assertSame($safe_string$this->systemUnderTest->escapeFilter($twig$safe_string, 'html', 'UTF-8', TRUE));

    // Ensure objects that do not implement MarkupInterface are escaped.     $string_object = new TwigExtensionTestString("<script>alert('here');</script>");
    $this->assertSame('&lt;script&gt;alert(&#039;here&#039;);&lt;/script&gt;', $this->systemUnderTest->escapeFilter($twig$string_object, 'html', 'UTF-8', TRUE));
  }

  /** * @covers ::safeJoin */
  public function testSafeJoin() {
    $this->renderer->expects($this->any())
      
Home | Imprint | This part of the site doesn't use cookies.