diff --git a/core/lib/Drupal/Component/Utility/Html.php b/core/lib/Drupal/Component/Utility/Html.php index 8c7d3888cd..58774e51e9 100644 --- a/core/lib/Drupal/Component/Utility/Html.php +++ b/core/lib/Drupal/Component/Utility/Html.php @@ -456,6 +456,11 @@ public static function transformRootRelativeUrlsToAbsolute($html, $scheme_and_ho assert(isset(parse_url($scheme_and_host)["scheme"]), '$scheme_and_host is absolute and hence has a scheme.'); assert(isset(parse_url($scheme_and_host)["host"]), '$base_url is absolute and hence has a host.'); + // \DOMDocument::loadHtml replaces \r with and there is no flag to + // prevent it, so use a placeholder and then reverse it after loading. + $placeholder = uniqid(); + $html = str_replace("\r", $placeholder, $html); + $html_dom = Html::load($html); $xpath = new \DOMXpath($html_dom); @@ -478,7 +483,9 @@ public static function transformRootRelativeUrlsToAbsolute($html, $scheme_and_ho $node->setAttribute('srcset', implode(', ', $image_candidate_strings)); } } - return Html::serialize($html_dom); + $html = Html::serialize($html_dom); + + return str_replace($placeholder, "\r", $html); } } diff --git a/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php index 3f2ab9de2f..c35328e8cc 100644 --- a/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php @@ -387,6 +387,9 @@ public function providerTestTransformRootRelativeUrlsToAbsolute() { } } + $data['html without links'] = ["Test without links but with\r\nsome special characters", 'http://example.com', FALSE]; + $data['html with html entity'] = ["Test without links but with \nsome special characters", 'http://example.com', FALSE]; + return $data; }