From a9269ed899aab66ab11633a1bcc5efa29febd831 Mon Sep 17 00:00:00 2001 From: Adam Weston Date: Tue, 15 Oct 2024 16:16:33 -0400 Subject: [PATCH] Fix: Make converter nullable --- src/Extensions/Extensions/TextAlign.php | 2 +- src/Extensions/Marks/Link.php | 2 +- src/TiptapConverter.php | 24 ++++++++++++++++++++---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Extensions/Extensions/TextAlign.php b/src/Extensions/Extensions/TextAlign.php index ace6420b..5751a3d5 100644 --- a/src/Extensions/Extensions/TextAlign.php +++ b/src/Extensions/Extensions/TextAlign.php @@ -28,7 +28,7 @@ public function addGlobalAttributes(): array 'default' => $this->options['defaultAlignment'], 'parseHTML' => fn ($DOMNode) => InlineStyle::getAttribute($DOMNode, 'text-align') ?? $this->options['defaultAlignment'], 'renderHTML' => function ($attributes) { - if (property_exists($attributes, 'style') && str_contains($attributes->style, 'text-align')) { + if (property_exists($attributes, 'style') && str_contains($attributes->style ?? '', 'text-align')) { return null; } diff --git a/src/Extensions/Marks/Link.php b/src/Extensions/Marks/Link.php index 864413a7..8b450d7b 100644 --- a/src/Extensions/Marks/Link.php +++ b/src/Extensions/Marks/Link.php @@ -75,7 +75,7 @@ public function addAttributes(): array return null; }, 'renderHTML' => function ($attributes) { - if (! property_exists($attributes, 'button_theme') || strlen($attributes->button_theme) === 0) { + if (! property_exists($attributes, 'button_theme') || strlen($attributes->button_theme ?? '') === 0) { return null; } diff --git a/src/TiptapConverter.php b/src/TiptapConverter.php index 5f195099..2530ae38 100644 --- a/src/TiptapConverter.php +++ b/src/TiptapConverter.php @@ -100,8 +100,12 @@ public function mergeTagsMap(array $mergeTagsMap): static return $this; } - public function asHTML(string | array $content, bool $toc = false, int $maxDepth = 3): string + public function asHTML(string | array | null $content, bool $toc = false, int $maxDepth = 3): string { + if (! $content) { + return ''; + } + $editor = $this->getEditor()->setContent($content); if ($toc) { @@ -118,8 +122,12 @@ public function asHTML(string | array $content, bool $toc = false, int $maxDepth return str_replace('', '', $editor->getHTML()); } - public function asJSON(string | array $content, bool $decoded = false, bool $toc = false, int $maxDepth = 3): string | array + public function asJSON(string | array | null $content, bool $decoded = false, bool $toc = false, int $maxDepth = 3): string | array { + if (! $content) { + return ''; + } + $editor = $this->getEditor()->setContent($content); if ($toc) { @@ -133,8 +141,12 @@ public function asJSON(string | array $content, bool $decoded = false, bool $toc return $decoded ? json_decode($editor->getJSON(), true) : $editor->getJSON(); } - public function asText(string | array $content): string + public function asText(string | array | null $content): string { + if (! $content) { + return ''; + } + $editor = $this->getEditor()->setContent($content); if (filled($this->mergeTagsMap)) { @@ -144,8 +156,12 @@ public function asText(string | array $content): string return $editor->getText(); } - public function asTOC(string | array $content, int $maxDepth = 3, bool $array = false): string | array + public function asTOC(string | array | null $content, int $maxDepth = 3, bool $array = false): string | array { + if (! $content) { + return ''; + } + if (is_string($content)) { $content = $this->asJSON($content, decoded: true); }