diff --git a/core/lib/Drupal/Component/Utility/Html.php b/core/lib/Drupal/Component/Utility/Html.php
index 1a53a8de0e8..6c82881e96b 100644
--- a/core/lib/Drupal/Component/Utility/Html.php
+++ b/core/lib/Drupal/Component/Utility/Html.php
@@ -419,7 +419,7 @@ public static function decodeEntities(string $text): string {
    * output of Html::escape() to '#markup' is not recommended. Use the
    * '#plain_text' key instead and the renderer will autoescape the text.
    *
-   * @param string $text
+   * @param string | null $text
    *   The input text.
    *
    * @return string
@@ -430,8 +430,8 @@ public static function decodeEntities(string $text): string {
    *
    * @ingroup sanitization
    */
-  public static function escape(string $text): string {
-    return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
+  public static function escape(string | null $text): string {
+    return !empty($text) ? htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') : '';
   }
 
   /**
