Skip to content

Commit

Permalink
Merge pull request #28 from grummbeer/add-missig-key
Browse files Browse the repository at this point in the history
Add missing attribute 'rel=“noreferrer noopener”' for external links if target = “_blank”
  • Loading branch information
dmolineus authored Sep 23, 2024
2 parents 74c21f1 + 80340e1 commit a0627e9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Renderer/NavigationRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ private function compileNavigationItem(array $page, bool $forwardResolution = tr
$page['title'] = StringUtil::specialchars($page['_title'], true);
$page['pageTitle'] = StringUtil::specialchars($page['_pageTitle'], true);
$page['target'] = ''; // overwrite DB value
$page['rel'] = '';
$page['nofollow'] = strncmp($page['robots'], 'noindex', 7) === 0;
$page['description'] = str_replace(["\n", "\r"], [' ', ''], $page['_description']);
$page['isInTrail'] = $this->items->isInTrail((int) $page['id']);
Expand All @@ -280,6 +281,10 @@ private function compileNavigationItem(array $page, bool $forwardResolution = tr
} elseif ($redirectPage['type'] === 'redirect') {
$page['href'] = $this->encodeEmailURL($redirectPage['url']);
$page['target'] = $redirectPage['target'] ? 'target="_blank"' : '';

if ('target="_blank"' === $page['target']) {
$page['rel'] = 'rel="noreferrer noopener"';
}
} else {
$page['tid'] = $redirectPage['id'];
$page['href'] = $this->generatePageUrl($redirectPage);
Expand All @@ -294,6 +299,10 @@ private function compileNavigationItem(array $page, bool $forwardResolution = tr
case 'redirect':
$page['href'] = $this->encodeEmailURL($page['url']);
$page['target'] = $page['_target'] ? 'target="_blank"' : '';

if ('target="_blank"' === $page['target']) {
$page['rel'] = 'rel="noreferrer noopener"';
}
break;

case 'root':
Expand Down
1 change: 1 addition & 0 deletions src/Resources/contao/templates/nav_hofff.html5
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<?php if($item['tabindex'] ?? null): ?>tabindex="<?php echo $item['tabindex']; ?>"<?php endif; ?>
<?php if($item['isActive']): ?>rel="nofollow"<?php endif; ?>
<?php echo $item['target']; ?>
<?php echo $item['rel'] ?>
><?php echo $item['link'];
?></a
><?php echo $item['subitems'];
Expand Down

0 comments on commit a0627e9

Please sign in to comment.