From 6f456ae28870b5cb1b2ef6c1b90e7c79fe10e421 Mon Sep 17 00:00:00 2001
From: xjm <xjm@65776.no-reply.drupal.org>
Date: Mon, 13 Nov 2023 14:34:41 -0600
Subject: [PATCH] Issue #3195583 by smokris, smustgrave, mohit.bansal623, xjm,
 quietone: Fix warning when RSS feed includes markup with an empty srcset
 attribute

---
 core/lib/Drupal/Component/Utility/Html.php             | 7 +++----
 core/tests/Drupal/Tests/Component/Utility/HtmlTest.php | 1 +
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/core/lib/Drupal/Component/Utility/Html.php b/core/lib/Drupal/Component/Utility/Html.php
index 99427ea79730..749a479636d4 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 3bc7f3525486..8f74eae06185 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) {
-- 
GitLab