From 3589a503aa0e1bdbfe2126cecf14faf7dfeb3b6c Mon Sep 17 00:00:00 2001
From: xjm <xjm@65776.no-reply.drupal.org>
Date: Sun, 25 Dec 2022 11:55:04 -0600
Subject: [PATCH] Issue #3311595 by fjgarlin, mfb, pooja saraah, colorfield,
 alexpott, Wim Leers, longwave: Html::transformRootRelativeUrlsToAbsolute()
 replaces "\r\n" with "&#13;\n"

---
 core/lib/Drupal/Component/Utility/Html.php             | 5 +++++
 core/tests/Drupal/Tests/Component/Utility/HtmlTest.php | 5 +++++
 2 files changed, 10 insertions(+)

diff --git a/core/lib/Drupal/Component/Utility/Html.php b/core/lib/Drupal/Component/Utility/Html.php
index 7271519e515d..53bb22e1981b 100644
--- a/core/lib/Drupal/Component/Utility/Html.php
+++ b/core/lib/Drupal/Component/Utility/Html.php
@@ -279,6 +279,11 @@ public static function load($html) {
 <body>!html</body>
 </html>
 EOD;
+
+    // PHP's \DOMDocument::saveXML() encodes carriage returns as &#13; so
+    // normalize all newlines to line feeds.
+    $html = str_replace(["\r\n", "\r"], "\n", $html);
+
     // PHP's \DOMDocument serialization adds extra whitespace when the markup
     // of the wrapping document contains newlines, so ensure we remove all
     // newlines before injecting the actual HTML body to be processed.
diff --git a/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
index a333cdab53e5..e48696136001 100644
--- a/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
+++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
@@ -390,6 +390,11 @@ public function providerTestTransformRootRelativeUrlsToAbsolute() {
       }
     }
 
+    // Double-character carriage return should be normalized.
+    $data['line break with double special character'] = ["Test without links but with\r\nsome special characters", 'http://example.com', "Test without links but with\nsome special characters"];
+    $data['line break with single special character'] = ["Test without links but with&#13;\nsome special characters", 'http://example.com', FALSE];
+    $data['carriage return within html'] = ["<a\rhref='/node'>My link</a>", 'http://example.com', '<a href="http://example.com/node">My link</a>'];
+
     return $data;
   }
 
-- 
GitLab