Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/MacroReplacer/LinkMacroReplacer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Artemeon\Confluence\MacroReplacer;

/**
* Replace <ac:link> with its link text, dropping the link itself.
*
* Must run before {@see OtherMacroRemover}, which would otherwise remove the link together with its text.
*/
class LinkMacroReplacer implements MacroReplacerInterface
{
public function replace(string $haystack): string
{
return preg_replace_callback(
'/<ac:link\b[^>]*>(.*?)<\/ac:link>/is',
function ($match) {
$linkContent = $match[1];

if (preg_match('/<ac:link-body[^>]*>(.*?)<\/ac:link-body>/is', $linkContent, $body)) {
return $body[1];
}

if (preg_match('/<ac:plain-text-link-body[^>]*>\s*<!\[CDATA\[(.*?)]]>\s*<\/ac:plain-text-link-body>/is', $linkContent, $body)) {
return htmlspecialchars($body[1], ENT_QUOTES | ENT_HTML5);
}

// Without a link body, Confluence displays the title of the linked page or attachment.
if (preg_match('/ri:(?:content-title|filename)="([^"]*)"/i', $linkContent, $title)) {
return $title[1];
}

return '';
},
$haystack
) ?? $haystack;
}
}
65 changes: 65 additions & 0 deletions tests/MacroReplacer/LinkMacroReplacerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Artemeon\Confluence\Tests\MacroReplacer;

use Artemeon\Confluence\MacroReplacer\LinkMacroReplacer;
use Artemeon\Confluence\MacroReplacer\OtherMacroRemover;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

/**
* Regression test for #35420: links to other Confluence pages must keep their text,
* instead of being removed entirely by {@see OtherMacroRemover}.
*/
final class LinkMacroReplacerTest extends TestCase
{
/**
* @return array<string, array{string, string}>
*/
public static function links(): array
{
return [
'plain text link body' => [
'<p>See <ac:link><ri:page ri:content-title="Contracts" /><ac:plain-text-link-body><![CDATA[contracts & more]]></ac:plain-text-link-body></ac:link> here</p>',
'<p>See contracts &amp; more here</p>',
],
'rich text link body' => [
'<p>See <ac:link ac:anchor="top"><ri:page ri:content-title="Contracts" /><ac:link-body><strong>Contracts</strong></ac:link-body></ac:link> here</p>',
'<p>See <strong>Contracts</strong> here</p>',
],
'page link without body' => [
'<p>See <ac:link><ri:page ri:content-title="Contracts" /></ac:link> here</p>',
'<p>See Contracts here</p>',
],
'attachment link without body' => [
'<p>See <ac:link><ri:attachment ri:filename="manual.pdf" /></ac:link> here</p>',
'<p>See manual.pdf here</p>',
],
'user link without body' => [
'<p>Ask <ac:link><ri:user ri:account-id="123" /></ac:link> here</p>',
'<p>Ask here</p>',
],
'multiple links' => [
'<ac:link><ri:page ri:content-title="A" /></ac:link>, <ac:link><ri:page ri:content-title="B" /></ac:link>',
'A, B',
],
];
}

#[DataProvider('links')]
public function testReplacesLinkWithItsText(string $input, string $expected): void
{
$this->assertSame($expected, (new LinkMacroReplacer())->replace($input));
}

public function testLinkTextSurvivesOtherMacroRemover(): void
{
$input = '<p>See <ac:link><ri:page ri:content-title="Contracts" /><ac:plain-text-link-body><![CDATA[Contracts]]></ac:plain-text-link-body></ac:link> here</p>';

$output = (new OtherMacroRemover())->replace((new LinkMacroReplacer())->replace($input));

$this->assertSame('<p>See Contracts here</p>', $output);
}
}
Loading