diff --git a/core/lib/Drupal/Component/Utility/Html.php b/core/lib/Drupal/Component/Utility/Html.php
index 9980888da1ecf06ed78c96b769b90312a521a595..447142d66aa678eeb9569e55d014ba9c63c30b1c 100644
--- a/core/lib/Drupal/Component/Utility/Html.php
+++ b/core/lib/Drupal/Component/Utility/Html.php
@@ -484,11 +484,10 @@ public static function transformRootRelativeUrlsToAbsolute($html, $scheme_and_ho
       // @see https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-srcset
       // @see https://html.spec.whatwg.org/multipage/embedded-content.html#image-candidate-string
       $image_candidate_strings = explode(',', $node->getAttribute('srcset'));
-      $image_candidate_strings = array_map('trim', $image_candidate_strings);
-      for ($i = 0; $i < count($image_candidate_strings); $i++) {
-        $image_candidate_string = $image_candidate_strings[$i];
+      $image_candidate_strings = array_filter(array_map('trim', $image_candidate_strings));
+      foreach ($image_candidate_strings as $key => $image_candidate_string) {
         if ($image_candidate_string[0] === '/' && $image_candidate_string[1] !== '/') {
-          $image_candidate_strings[$i] = $scheme_and_host . $image_candidate_string;
+          $image_candidate_strings[$key] = $scheme_and_host . $image_candidate_string;
         }
       }
       $node->setAttribute('srcset', implode(', ', $image_candidate_strings));
diff --git a/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
index b2867ec012dd93efebd27e78b6d11c02a18de86c..4f4860011645252c541580f398622e464e344aad 100644
--- a/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
+++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
@@ -379,6 +379,7 @@ public function providerTestTransformRootRelativeUrlsToAbsolute() {
         "$tag_name, srcset, $base_path: root-relative" => ["<$tag_name srcset=\"http://example.com{$base_path}already-absolute 200w, {$base_path}root-relative 300w\">root-relative test</$tag_name>", 'http://example.com', "<$tag_name srcset=\"http://example.com{$base_path}already-absolute 200w, http://example.com{$base_path}root-relative 300w\">root-relative test</$tag_name>"],
         "$tag_name, srcset, $base_path: protocol-relative" => ["<$tag_name srcset=\"http://example.com{$base_path}already-absolute 200w, //example.com{$base_path}protocol-relative 300w\">protocol-relative test</$tag_name>", 'http://example.com', FALSE],
         "$tag_name, srcset, $base_path: absolute" => ["<$tag_name srcset=\"http://example.com{$base_path}already-absolute 200w, http://example.com{$base_path}absolute 300w\">absolute test</$tag_name>", 'http://example.com', FALSE],
+        "$tag_name, empty srcset" => ["<$tag_name srcset>empty test</$tag_name>", 'http://example.com', FALSE],
       ];
 
       foreach (['href', 'poster', 'src', 'cite', 'data', 'action', 'formaction', 'about'] as $attribute) {