Skip to content

Commit

Permalink
feat: Add target blank to existing external links - MEED-6879 - Meeds…
Browse files Browse the repository at this point in the history
…-io/MIPs#124 (#3816)

Prior to this change, existing external links opened in the same tab.  
This PR ensures that existing external links now open in a new tab.
  • Loading branch information
SaraBoutej authored May 21, 2024
1 parent 7e7d000 commit c8084c3
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions webapp/portlet/src/main/webapp/js/ExtendedDomPurify.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@
ADD_ATTR: ['target', 'allow', 'allowfullscreen', 'frameborder', 'scrolling', 'v-identity-popover'],
});
DOMPurify.addHook('afterSanitizeAttributes', function(node) {

if ('target' in node) {
// add noopener attribute to external links to eliminate vulnerabilities
node.setAttribute('rel', 'noopener');
// add text ellipsis when link length is up to 75 characters
const nodeText = node.textContent;
const nodeLink = node.getAttribute('href');
// add noopener attribute to external links to eliminate vulnerabilities
if (nodeLink.indexOf(window.location.origin) === -1) {
node.setAttribute('target', '_blank');
node.setAttribute('rel', 'nofollow noopener noreferrer');
}
// add text ellipsis when link length is up to 75 characters
if (nodeText && nodeText.length > 75) {
node.setAttribute('title', nodeLink);
node.setAttribute('Aria-label', nodeLink);
Expand Down

0 comments on commit c8084c3

Please sign in to comment.