diff --git a/core/lib/Drupal/Component/Utility/Html.php b/core/lib/Drupal/Component/Utility/Html.php index 8c7d3888cd..ea0fa6660d 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) { !html EOD; + + // PHP's \DOMDocument::saveXML() encodes carriage returns as 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 3f2ab9de2f..2da1560795 100644 --- a/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php @@ -387,6 +387,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 \nsome special characters", 'http://example.com', FALSE]; + $data['carriage return within html'] = ["My link", 'http://example.com', 'My link']; + return $data; }