Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Component/Render/HtmlEscapedText.php \Drupal\Component\Render\HtmlEscapedText
  2. 9 core/lib/Drupal/Component/Render/HtmlEscapedText.php \Drupal\Component\Render\HtmlEscapedText

Escapes HTML syntax characters to HTML entities for display in markup.

This class can be used to provide theme engine-like late escaping functionality.

Hierarchy

Expanded class hierarchy of HtmlEscapedText

2 files declare their use of HtmlEscapedText
HtmlEscapedTextTest.php in core/tests/Drupal/Tests/Component/Render/HtmlEscapedTextTest.php
Token.php in core/lib/Drupal/Core/Utility/Token.php

File

core/lib/Drupal/Component/Render/HtmlEscapedText.php, line 15

Namespace

Drupal\Component\Render
View source
class HtmlEscapedText implements MarkupInterface, \Countable {

  /**
   * The string to escape.
   *
   * @var string
   */
  protected $string;

  /**
   * Constructs an HtmlEscapedText object.
   *
   * @param $string
   *   The string to escape. This value will be cast to a string.
   */
  public function __construct($string) {
    $this->string = (string) $string;
  }

  /**
   * {@inheritdoc}
   */
  public function __toString() {
    return Html::escape($this->string);
  }

  /**
   * {@inheritdoc}
   */

  #[\ReturnTypeWillChange]
  public function count() {
    return mb_strlen($this->string);
  }

  /**
   * {@inheritdoc}
   */

  #[\ReturnTypeWillChange]
  public function jsonSerialize() {
    return $this
      ->__toString();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HtmlEscapedText::$string protected property The string to escape.
HtmlEscapedText::count public function
HtmlEscapedText::jsonSerialize public function
HtmlEscapedText::__construct public function Constructs an HtmlEscapedText object.
HtmlEscapedText::__toString public function Returns markup. Overrides MarkupInterface::__toString