Skip to content
Snippets Groups Projects
Commit 4a97009b authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

do not decode passed links in SkipLinks::addLink(), fixes #951

Closes #951

Merge request studip/studip!545
parent bd0aea77
No related branches found
No related tags found
No related merge requests found
...@@ -25,12 +25,6 @@ class SkipLinks ...@@ -25,12 +25,6 @@ class SkipLinks
*/ */
private static $links = []; private static $links = [];
/**
* array of positions of skip links
* @var array
*/
private static $position = [];
/** /**
* Returns whether skiplinks are enabled. * Returns whether skiplinks are enabled.
* *
...@@ -59,34 +53,28 @@ class SkipLinks ...@@ -59,34 +53,28 @@ class SkipLinks
* @param string $name the displayed name of the links * @param string $name the displayed name of the links
* @param string $url the url of the links * @param string $url the url of the links
* @param integer $position the position of the link in the list * @param integer $position the position of the link in the list
* @param boolean $overwriteable false if position is not overwritable by another link
*/ */
public static function addLink($name, $url, $position = null, $overwriteable = false) public static function addLink(string $name, string $url, $position = null)
{ {
$position = (!$position || $position < 1) ? count(self::$links) + 100 : (int) $position; $position = (!$position || $position < 1) ? count(self::$links) + 100 : (int) $position;
$new_link = [ self::$links[$url] = [
'name' => $name, 'name' => $name,
'url' => decodeHTML($url), 'url' => $url,
'position' => $position, 'position' => $position,
'overwriteable' => $overwriteable,
]; ];
if (self::checkOverwrite($new_link)) {
self::$links[$new_link['url']] = $new_link;
}
} }
/** /**
* Adds a link to an anker on the same page to the list of skip links. * Adds a link to an anker on the same page to the list of skip links.
* *
* @param string $name the displayed name of the links * @param string $name the displayed name of the links
* @param string $id the id of the anker * @param string $id the id of the anker
* @param integer $position the position of the link in the list * @param integer $position the position of the link in the list
* @param boolean $overwriteable false if position is not overwritable by another link
*/ */
public static function addIndex($name, $id, $position = null, $overwriteable = false) public static function addIndex($name, $id, $position = null)
{ {
$url = '#' . $id; $url = '#' . $id;
self::addLink($name, $url, $position, $overwriteable); self::addLink($name, $url, $position);
} }
/** /**
...@@ -101,11 +89,11 @@ class SkipLinks ...@@ -101,11 +89,11 @@ class SkipLinks
} }
usort(self::$links, function ($a, $b) { usort(self::$links, function ($a, $b) {
return $a['position'] > $b['position']; return $a['position'] - $b['position'];
}); });
$navigation = new Navigation(''); $navigation = new Navigation('');
foreach (self::$links as $index => $link) { foreach (array_values(self::$links) as $index => $link) {
$navigation->addSubNavigation( $navigation->addSubNavigation(
"/skiplinks/link-{$index}", "/skiplinks/link-{$index}",
new Navigation($link['name'], $link['url']) new Navigation($link['name'], $link['url'])
...@@ -113,21 +101,4 @@ class SkipLinks ...@@ -113,21 +101,4 @@ class SkipLinks
} }
return $GLOBALS['template_factory']->render('skiplinks', compact('navigation')); return $GLOBALS['template_factory']->render('skiplinks', compact('navigation'));
} }
/**
* Checks if there is another link at the same position and if it is overwritable.
*
* @return boolean true if the link at the same position is overwritable
*/
private static function checkOverwrite($link)
{
if (isset(self::$position[$link['position']])) {
return false;
}
if (!$link['overwrite']) {
self::$position[$link['position']] = true;
}
return true;
}
} }
...@@ -97,7 +97,7 @@ $getInstalledLanguages = function () { ...@@ -97,7 +97,7 @@ $getInstalledLanguages = function () {
<body id="<?= $body_id ?: PageLayout::getBodyElementId() ?>" <? if (SkipLinks::isEnabled()) echo 'class="enable-skiplinks"'; ?>> <body id="<?= $body_id ?: PageLayout::getBodyElementId() ?>" <? if (SkipLinks::isEnabled()) echo 'class="enable-skiplinks"'; ?>>
<div id="layout_wrapper"> <div id="layout_wrapper">
<? SkipLinks::insertContainer() ?> <? SkipLinks::insertContainer() ?>
<? SkipLinks::addIndex(_('Hauptinhalt'), 'layout_content', 100, true) ?> <? SkipLinks::addIndex(_('Hauptinhalt'), 'layout_content', 100) ?>
<?= PageLayout::getBodyElements() ?> <?= PageLayout::getBodyElements() ?>
<? include 'lib/include/header.php' ?> <? include 'lib/include/header.php' ?>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment