Issue #2412241 by idebr, Wim Leers: Active menu links with identical paths get a duplicate "active" class for every active menu link

8.0.x
Alex Pott 2015-01-30 10:49:47 +00:00
parent 9be3165caf
commit a5bbb06e6d
2 changed files with 18 additions and 12 deletions

View File

@ -359,21 +359,18 @@ class SystemController extends ControllerBase {
// 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="<front>"', $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 @@ class SystemController extends ControllerBase {
@$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 .= ' ';
}

View File

@ -298,6 +298,13 @@ class SystemControllerTest extends UnitTestCase {
}
}
// 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;
}