Skip to content

Commit 352d9c4

Browse files
committed
Merge branch '4.x' into 5.x
2 parents fc10069 + 41f0963 commit 352d9c4

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

packages/forms/src/Components/RichEditor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class RichEditor extends Field implements Contracts\CanBeLengthConstrained
5252
/**
5353
* @var array<string> | Closure
5454
*/
55-
protected array | Closure $linkProtocols = ['http', 'https', 'mailto'];
55+
protected array | Closure $linkProtocols = ['http', 'https', 'ftp', 'ftps', 'mailto', 'tel', 'callto', 'sms', 'cid', 'xmpp'];
5656

5757
protected bool | Closure | null $isJson = null;
5858

@@ -574,6 +574,7 @@ public function getTipTapEditor(): Editor
574574
{
575575
return RichContentRenderer::make()
576576
->plugins($this->getPlugins())
577+
->linkProtocols($this->getLinkProtocols())
577578
->getEditor();
578579
}
579580

packages/forms/src/Components/RichEditor/RichContentRenderer.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ class RichContentRenderer implements Htmlable
104104
*/
105105
protected ?array $textColors = null;
106106

107+
/**
108+
* @var ?array<string>
109+
*/
110+
protected ?array $linkProtocols = null;
111+
107112
/**
108113
* @param string | array<string, mixed> | null $content
109114
*/
@@ -392,7 +397,11 @@ public function getTipTapPhpExtensions(): array
392397
app(Italic::class),
393398
app(ImageExtension::class),
394399
app(LeadExtension::class),
395-
app(Link::class),
400+
app(Link::class, [
401+
'options' => [
402+
'allowedProtocols' => $this->getLinkProtocols(),
403+
],
404+
]),
396405
app(ListItem::class),
397406
app(MentionExtension::class),
398407
app(MergeTagExtension::class),
@@ -597,4 +606,22 @@ public function getTextColors(): array
597606
fn (string | TextColor $color, string $name): array => [$name => ($color instanceof TextColor) ? $color : TextColor::make($color, $name)],
598607
);
599608
}
609+
610+
/**
611+
* @param ?array<string> $protocols
612+
*/
613+
public function linkProtocols(?array $protocols): static
614+
{
615+
$this->linkProtocols = $protocols;
616+
617+
return $this;
618+
}
619+
620+
/**
621+
* @return array<string>
622+
*/
623+
public function getLinkProtocols(): array
624+
{
625+
return $this->linkProtocols ?? app(Link::class)->options['allowedProtocols'];
626+
}
600627
}

tests/src/Forms/Components/RichEditor/RichContentRendererTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,3 +770,50 @@ public function toHtml(): string
770770

771771
expect($html)->toContain('<span data-type="mention" data-id="1" data-char="@"></span>');
772772
});
773+
774+
it('preserves links with default protocols', function (): void {
775+
$renderer = RichContentRenderer::make(
776+
'<p><a href="https://example.com">Link</a></p>',
777+
);
778+
779+
$html = $renderer->toUnsafeHtml();
780+
781+
expect($html)->toContain('href="https://example.com"');
782+
});
783+
784+
it('strips links with unknown protocols by default', function (): void {
785+
$renderer = RichContentRenderer::make(
786+
'<p><a href="myapp:///path"><strong>Link</strong></a></p>',
787+
);
788+
789+
$html = $renderer->toUnsafeHtml();
790+
791+
expect($html)->not->toContain('myapp:///path');
792+
});
793+
794+
it('preserves links with custom protocols when `linkProtocols()` includes them', function (): void {
795+
$renderer = RichContentRenderer::make(
796+
'<p><a href="myapp:///cards?id=123"><strong>Open App</strong></a></p>',
797+
);
798+
799+
$renderer->linkProtocols([
800+
...RichContentRenderer::make()->getLinkProtocols(),
801+
'myapp',
802+
]);
803+
804+
$html = $renderer->toUnsafeHtml();
805+
806+
expect($html)->toContain('href="myapp:///cards?id=123"');
807+
});
808+
809+
it('uses default protocols from `Link` when `linkProtocols()` is not set', function (): void {
810+
$renderer = RichContentRenderer::make();
811+
812+
$protocols = $renderer->getLinkProtocols();
813+
814+
expect($protocols)
815+
->toContain('http')
816+
->toContain('https')
817+
->toContain('mailto')
818+
->toContain('tel');
819+
});

0 commit comments

Comments
 (0)