 core/modules/system/src/Controller/SystemController.php       | 11 +++++------
 .../system/tests/src/Unit/Controller/SystemControllerTest.php |  7 +++++++
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/core/modules/system/src/Controller/SystemController.php b/core/modules/system/src/Controller/SystemController.php
index 22bade6..cce4706 100644
--- a/core/modules/system/src/Controller/SystemController.php
+++ b/core/modules/system/src/Controller/SystemController.php
@@ -359,21 +359,18 @@ public static function setLinkActiveClass(array $element, array $context) {
     // An active link's path is equal to the current path, so search the HTML
     // for an attribute with that value.
     $offset = 0;
-    while ((strpos($element['#markup'], 'data-drupal-link-system-path="' . $context['path'] . '"', $offset) !== FALSE || ($context['front'] && strpos($element['#markup'], 'data-drupal-link-system-path="&lt;front&gt;"', $offset) !== FALSE))) {
+    while (strpos($element['#markup'], $search_key_current_path, $offset) !== FALSE || ($context['front'] && strpos($element['#markup'], $search_key_front, $offset) !== FALSE)) {
       $pos_current_path = strpos($element['#markup'], $search_key_current_path, $offset);
       $pos_front = strpos($element['#markup'], $search_key_front, $offset);
 
       // Determine which of the two values matched: the exact path, or the
       // <front> special case.
       $pos_match = NULL;
-      $type_match = NULL;
       if ($pos_current_path !== FALSE) {
         $pos_match = $pos_current_path;
-        $type_match = 'path';
       }
       elseif ($context['front'] && $pos_front !== FALSE) {
         $pos_match = $pos_front;
-        $type_match = 'front';
       }
 
       // Find beginning and ending of opening tag.
@@ -400,9 +397,11 @@ public static function setLinkActiveClass(array $element, array $context) {
       @$dom->loadHTML('<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>' . $tag . '</body></html>');
       $node = $dom->getElementsByTagName('body')->item(0)->firstChild;
 
+      // Ensure we don't set the "active" class twice on the same element.
+      $is_active = !in_array('active', explode(' ', $node->getAttribute('class')));
+
       // The language of an active link is equal to the current language.
-      $is_active = TRUE;
-      if ($context['language']) {
+      if ($is_active && $context['language']) {
         if ($node->hasAttribute('hreflang') && $node->getAttribute('hreflang') !== $context['language']) {
           $is_active = FALSE;
         }
diff --git a/core/modules/system/tests/src/Unit/Controller/SystemControllerTest.php b/core/modules/system/tests/src/Unit/Controller/SystemControllerTest.php
index e5ec1da..ab227a0 100644
--- a/core/modules/system/tests/src/Unit/Controller/SystemControllerTest.php
+++ b/core/modules/system/tests/src/Unit/Controller/SystemControllerTest.php
@@ -298,6 +298,13 @@ public function providerTestSetLinkActiveClass() {
       }
     }
 
+    // Test case to verify that the 'active' class is not added multiple times.
+    $data[] = [
+      0 => ['#markup' => '<a data-drupal-link-system-path="&lt;front&gt;">Once</a> <a data-drupal-link-system-path="&lt;front&gt;">Twice</a>'],
+      1 => ['path' => '', 'front' => TRUE, 'language' => 'en', 'query' => []],
+      2 => ['#markup' => '<a data-drupal-link-system-path="&lt;front&gt;" class="active">Once</a> <a data-drupal-link-system-path="&lt;front&gt;" class="active">Twice</a>'],
+    ];
+
     return $data;
   }
 
