core/includes/common.inc | 10 +++---- .../Drupal/Core/Utility/LinkGeneratorInterface.php | 11 +++---- .../Drupal/system/Controller/SystemController.php | 31 +++++++++++++++----- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/core/includes/common.inc b/core/includes/common.inc index 0a77f0d..24d52a0 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -1214,11 +1214,11 @@ function drupal_http_header_attributes(array $attributes = array()) { * internal to the site, $options['language'] is used to determine whether * the link is "active", or pointing to the current page (the language as * well as the path must match). This element is also used by url(). - * - 'set_active_class' (default FALSE): Whether l() should compare the $path, - * language and query options to the current URL to determine whether the - * link is "active". If so, an "active" class will be applied to the link. - * It is important to use this sparingly since it is usually unnecessary and - * requires extra processing. + * - 'set_active_class': Whether l() should compare the $path, language and + * query options to the current URL to determine whether the link is + * "active". Defaults to FALSE. If TRUE, an "active" class will be applied + * to the link. It is important to use this sparingly since it is usually + * unnecessary and requires extra processing. * For anonymous users, the "active" class will be calculated on the server, * because most sites serve each anonymous user the same cached page anyway. * For authenticated users, the "active" class will be calculated on the diff --git a/core/lib/Drupal/Core/Utility/LinkGeneratorInterface.php b/core/lib/Drupal/Core/Utility/LinkGeneratorInterface.php index b832873..02e31af 100644 --- a/core/lib/Drupal/Core/Utility/LinkGeneratorInterface.php +++ b/core/lib/Drupal/Core/Utility/LinkGeneratorInterface.php @@ -55,11 +55,12 @@ * internal to the site, $options['language'] is used to determine whether * the link is "active", or pointing to the current page (the language as * well as the path must match). - * - 'set_active_class' (default FALSE): Whether this method should compare - * the $route_name, $parameters, language and query options to the current - * URL to determine whether the link is "active". If so, an "active" class - * will be applied to the link. It is important to use this sparingly - * since it is usually unnecessary and requires extra processing. + * - 'set_active_class': Whether this method should compare the $route_name, + * $parameters, language and query options to the current URL to determine + * whether the link is "active". Defaults to FALSE. If TRUE, an "active" + * class will be applied to the link. It is important to use this + * sparingly since it is usually unnecessary and requires extra + * processing. * * @return string * An HTML string containing a link to the given route and parameters. diff --git a/core/modules/system/lib/Drupal/system/Controller/SystemController.php b/core/modules/system/lib/Drupal/system/Controller/SystemController.php index d6a1de9..e16c838 100644 --- a/core/modules/system/lib/Drupal/system/Controller/SystemController.php +++ b/core/modules/system/lib/Drupal/system/Controller/SystemController.php @@ -373,7 +373,7 @@ public static function setLinkActiveClass(array $element, array $context) { $pos_match = $pos_current_path; $type_match = 'path'; } - else if ($context['front'] && $pos_front !== FALSE) { + elseif ($context['front'] && $pos_front !== FALSE) { $pos_match = $pos_front; $type_match = 'front'; } @@ -399,7 +399,7 @@ public static function setLinkActiveClass(array $element, array $context) { // Parse it into a DOMDocument so we can reliably read and modify // attributes. $dom = new \DOMDocument(); - $dom->loadHTML('' . $tag . ''); + @$dom->loadHTML('' . $tag . ''); $node = $dom->getElementsByTagName('body')->item(0)->firstChild; // The language of an active link is equal to the current language. @@ -411,9 +411,16 @@ public static function setLinkActiveClass(array $element, array $context) { } // The query parameters of an active link are equal to the current // parameters. - if ($is_active && $context['query']) { - if ($node->hasAttribute('data-drupal-link-query') && $node->getAttribute('data-drupal-link-query') !== Json::encode($context['query'])) { - $is_active = FALSE; + if ($is_active) { + if ($context['query']) { + if (!$node->hasAttribute('data-drupal-link-query') || $node->getAttribute('data-drupal-link-query') !== Json::encode($context['query'])) { + $is_active = FALSE; + } + } + else { + if ($node->hasAttribute('data-drupal-link-query')) { + $is_active = FALSE; + } } } @@ -430,11 +437,19 @@ public static function setLinkActiveClass(array $element, array $context) { $body_dom_node = $dom->getElementsByTagName('body')->item(0); $updated_tag = substr($dom->saveHTML($body_dom_node), 6, -7); + // If saveHTML() added a closing tag (it does so for ), remove it. + if (substr_count($updated_tag, '<') > 1) { + $updated_tag = substr($updated_tag, 0, strrpos($updated_tag, '<')); + } $element['#markup'] = str_replace($tag, $updated_tag, $element['#markup']); - } - // Ensure we only search the remaining HTML. - $offset = $pos_tag_end - strlen($tag) + strlen($updated_tag); + // Ensure we only search the remaining HTML. + $offset = $pos_tag_end - strlen($tag) + strlen($updated_tag); + } + else { + // Ensure we only search the remaining HTML. + $offset = $pos_tag_end + 1; + } } return $element;