diff --git a/core/lib/Drupal/Core/Utility/LinkGenerator.php b/core/lib/Drupal/Core/Utility/LinkGenerator.php
index ab2d232..9bc00d3 100644
--- a/core/lib/Drupal/Core/Utility/LinkGenerator.php
+++ b/core/lib/Drupal/Core/Utility/LinkGenerator.php
@@ -130,21 +130,20 @@ public function generate($text, Url $url, $collect_cacheability_metadata = FALSE
unset($variables['options']['attributes']);
$url->setOptions($variables['options']);
- // The result of the url generator is a plain-text URL. Because we are using
- // it here in an HTML argument context, we need to encode it properly.
+ // The result of the url generator is a plain-text URL.
+ // We encode it below with SafeMarkup::format because we are using it here
+ // in an HTML argument context.
if (!$collect_cacheability_metadata) {
- $url = SafeMarkup::checkPlain($url->toString($collect_cacheability_metadata));
+ $url = $url->toString($collect_cacheability_metadata);
}
else {
$generated_url = $url->toString($collect_cacheability_metadata);
- $url = SafeMarkup::checkPlain($generated_url->getGeneratedUrl());
+ $url = $generated_url->getGeneratedUrl();
$generated_link = GeneratedLink::createFromObject($generated_url);
}
- // Make sure the link text is sanitized.
- $safe_text = SafeMarkup::escape($variables['text']);
+ $result = SafeMarkup::format('@text', ['@url' => $url, '@attributes' => $attributes, '@text' => $variables['text']]);
- $result = SafeMarkup::set('' . $safe_text . '');
return $collect_cacheability_metadata ? $generated_link->setGeneratedLink($result) : $result;
}