diff --git a/core/lib/Drupal/Component/Utility/Html.php b/core/lib/Drupal/Component/Utility/Html.php
index 99427ea7973035a8b155c0b7c631cec68735562a..749a479636d4f35ba9d8364bbea7395d6e5bd820 100644
--- a/core/lib/Drupal/Component/Utility/Html.php
+++ b/core/lib/Drupal/Component/Utility/Html.php
@@ -480,11 +480,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 3bc7f352548638d3e8c714d39b76170fd16a866a..8f74eae06185ebe4d3418495c4b854002fd69d5d 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) {