Add rel="noreferrer noopener" when target attribute would open window
[lhc/web/wiklou.git] / includes / Linker.php
index 6a869dd..b090817 100644 (file)
@@ -1084,7 +1084,16 @@ class Linker {
                if ( !$title ) {
                        $title = $wgTitle;
                }
-               $attribs['rel'] = Parser::getExternalLinkRel( $url, $title );
+               $newRel = Parser::getExternalLinkRel( $url, $title );
+               if ( !isset( $attribs['rel'] ) || $attribs['rel'] === '' ) {
+                       $attribs['rel'] = $newRel;
+               } elseif( $newRel !== '' ) {
+                       // Merge the rel attributes.
+                       $newRels = explode( ' ', $newRel );
+                       $oldRels = explode( ' ', $attribs['rel'] );
+                       $combined = array_unique( array_merge( $newRels, $oldRels ) );
+                       $attribs['rel'] = implode( ' ', $combined );
+               }
                $link = '';
                $success = Hooks::run( 'LinkerMakeExternalLink',
                        [ &$url, &$text, &$link, &$attribs, $linktype ] );