diff --git a/core/modules/system/src/Controller/SystemController.php b/core/modules/system/src/Controller/SystemController.php
index 22bade6..9ed4227 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,32 +397,34 @@ 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.
+      $class = $node->getAttribute('class');
+      $add_active = !in_array('active', explode(' ', $class));
+
       // The language of an active link is equal to the current language.
-      $is_active = TRUE;
-      if ($context['language']) {
+      if ($add_active && $context['language']) {
         if ($node->hasAttribute('hreflang') && $node->getAttribute('hreflang') !== $context['language']) {
-          $is_active = FALSE;
+          $add_active = FALSE;
         }
       }
       // The query parameters of an active link are equal to the current
       // parameters.
-      if ($is_active) {
+      if ($add_active) {
         if ($context['query']) {
           if (!$node->hasAttribute('data-drupal-link-query') || $node->getAttribute('data-drupal-link-query') !== Json::encode($context['query'])) {
-            $is_active = FALSE;
+            $add_active = FALSE;
           }
         }
         else {
           if ($node->hasAttribute('data-drupal-link-query')) {
-            $is_active = FALSE;
+            $add_active = FALSE;
           }
         }
       }
 
       // Only if the the path, the language and the query match, we set the
       // "active" class.
-      if ($is_active) {
-        $class = $node->getAttribute('class');
+      if ($add_active) {
         if (strlen($class) > 0) {
           $class .= ' ';
         }
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;
   }
 
