Skip to content

Commit 85f76ad

Browse files
committed
Fix ExternalLinkProcessor not fully disabling the rel attribute
1 parent 564525f commit 85f76ad

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi
66

77
## [Unreleased][unreleased]
88

9+
### Fixed
10+
11+
- Fixed `ExternalLinkProcessor` not fully disabling the `rel` attribute when configured to do so (#992)
12+
913
## [2.4.0] - 2023-03-24
1014

1115
### Added

src/Extension/ExternalLink/ExternalLinkProcessor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ private function applyRelAttribute(Link $link, bool $isExternal): void
9090
$link->data->append('attributes/rel', $type);
9191
}
9292
}
93+
94+
// No rel attributes? Mark the attribute as 'false' so LinkRenderer doesn't add defaults
95+
if (! $link->data->has('attributes/rel')) {
96+
$link->data->set('attributes/rel', false);
97+
}
9398
}
9499

95100
/**

tests/functional/Extension/ExternalLink/ExternalLinkExtensionTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

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

16+
use League\CommonMark\CommonMarkConverter;
1617
use League\CommonMark\Environment\Environment;
1718
use League\CommonMark\Environment\EnvironmentInterface;
1819
use League\CommonMark\Extension\Autolink\AutolinkExtension;
@@ -65,4 +66,25 @@ public function provideEnvironmentForTestingExtensionWithAutolinks(): iterable
6566

6667
yield 'Register Autolink extension first' => [$environment2];
6768
}
69+
70+
public function testExtensionWithRelAttrsDisabled(): void
71+
{
72+
$config = [
73+
'external_link' => [
74+
'internal_hosts' => ['my-internal-domain.com'],
75+
'open_in_new_window' => true,
76+
'nofollow' => '',
77+
'noopener' => '',
78+
'noreferrer' => '',
79+
],
80+
];
81+
82+
$converter = new CommonMarkConverter($config);
83+
$converter->getEnvironment()->addExtension(new ExternalLinkExtension());
84+
85+
$input = 'This is an external link [Google](https://google.com/).';
86+
$expectedHtml = '<p>This is an external link <a target="_blank" href="https://google.com/">Google</a>.</p>';
87+
88+
$this->assertSame($expectedHtml, \rtrim($converter->convert($input)->getContent()));
89+
}
6890
}

0 commit comments

Comments
 (0)