Skip to content

Commit

Permalink
Fix ExternalLinkProcessor not fully disabling the rel attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
colinodell committed Aug 30, 2023
1 parent 564525f commit 85f76ad
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi

## [Unreleased][unreleased]

### Fixed

- Fixed `ExternalLinkProcessor` not fully disabling the `rel` attribute when configured to do so (#992)

## [2.4.0] - 2023-03-24

### Added
Expand Down
5 changes: 5 additions & 0 deletions src/Extension/ExternalLink/ExternalLinkProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ private function applyRelAttribute(Link $link, bool $isExternal): void
$link->data->append('attributes/rel', $type);
}
}

// No rel attributes? Mark the attribute as 'false' so LinkRenderer doesn't add defaults
if (! $link->data->has('attributes/rel')) {
$link->data->set('attributes/rel', false);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace League\CommonMark\Tests\Functional\Extension\ExternalLink;

use League\CommonMark\CommonMarkConverter;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Environment\EnvironmentInterface;
use League\CommonMark\Extension\Autolink\AutolinkExtension;
Expand Down Expand Up @@ -65,4 +66,25 @@ public function provideEnvironmentForTestingExtensionWithAutolinks(): iterable

yield 'Register Autolink extension first' => [$environment2];
}

public function testExtensionWithRelAttrsDisabled(): void
{
$config = [
'external_link' => [
'internal_hosts' => ['my-internal-domain.com'],
'open_in_new_window' => true,
'nofollow' => '',
'noopener' => '',
'noreferrer' => '',
],
];

$converter = new CommonMarkConverter($config);
$converter->getEnvironment()->addExtension(new ExternalLinkExtension());

$input = 'This is an external link [Google](https://google.com/).';
$expectedHtml = '<p>This is an external link <a target="_blank" href="https://google.com/">Google</a>.</p>';

$this->assertSame($expectedHtml, \rtrim($converter->convert($input)->getContent()));
}
}

0 comments on commit 85f76ad

Please sign in to comment.