Skip to content

Commit 3cd2b3d

Browse files
authored
Merge pull request #59 from tonysm/tm/attachment-html-content
HTML Content Attachments
2 parents bc9b0b4 + 8096a0f commit 3cd2b3d

File tree

13 files changed

+75
-87
lines changed

13 files changed

+75
-87
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
<figure class="attachment attachment--content">
12
{!! trim($content->renderTrixContentAttachment($options)) !!}
3+
</figure>

resources/views/contents/_horizontal_rule.blade.php

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/AttachableFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function fromNode(DOMElement $node): Attachables\AttachableContrac
3232
return $attachable;
3333
}
3434

35-
return new Attachables\MissingAttachable();
35+
return new Attachables\MissingAttachable;
3636
}
3737

3838
private static function attachableFromSgid(string $sgid)

src/Attachables/ContentAttachment.php

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,68 @@
33
namespace Tonysm\RichTextLaravel\Attachables;
44

55
use DOMElement;
6-
use Illuminate\Support\Str;
6+
use Tonysm\RichTextLaravel\Content;
77

88
class ContentAttachment implements AttachableContract
99
{
10-
const NAME_PATTERN = '/vnd\.richtextlaravel\.(.+)\.html/';
10+
private Content $contentInstance;
1111

12-
public static $validNames = ['horizontal-rule'];
12+
private string $renderedHtml;
1313

1414
public static function fromNode(DOMElement $node): ?static
1515
{
16-
if (! $node->hasAttribute('content-type')) {
16+
if (! $node->hasAttribute('content-type') || ! $node->hasAttribute('content')) {
1717
return null;
1818
}
1919

20-
if (! preg_match(static::NAME_PATTERN, $node->getAttribute('content-type'), $matches)) {
21-
return null;
22-
}
23-
24-
$name = $matches[1];
20+
$contentType = $node->getAttribute('content-type');
21+
$content = trim($node->getAttribute('content'));
2522

26-
if (! $name || ! static::validName($name)) {
27-
return null;
23+
if (str_contains($contentType, 'html') && ! empty($content)) {
24+
return new static($contentType, $content);
2825
}
29-
30-
return new static($name);
3126
}
3227

33-
private static function validName(string $name): bool
34-
{
35-
return in_array($name, static::$validNames);
36-
}
37-
38-
public function __construct(public $name)
39-
{
40-
}
28+
public function __construct(
29+
public string $contentType,
30+
public string $content,
31+
) {}
4132

4233
public function toRichTextAttributes(array $attributes): array
4334
{
4435
return [
45-
'content' => $this->renderTrixContentAttachment(),
36+
'contentType' => $this->contentType,
37+
'content' => $this->content,
4638
];
4739
}
4840

4941
public function equalsToAttachable(AttachableContract $attachable): bool
5042
{
5143
return $attachable instanceof static
52-
&& $attachable->name === $this->name;
44+
&& $attachable->contentType === $this->contentType
45+
&& $attachable->content === $this->content;
5346
}
5447

5548
public function richTextAsPlainText(): string
5649
{
57-
if ($this->name === 'horizontal-rule') {
58-
return '';
59-
}
60-
61-
return ' ';
50+
return $this->contentInstance()->fragment->source->textContent;
6251
}
6352

6453
public function richTextRender(array $options = []): string
6554
{
66-
return view('rich-text-laravel::contents._content', [
55+
return view('rich-text-laravel::attachables._content', [
6756
'content' => $this,
6857
'options' => $options,
6958
])->render();
7059
}
7160

7261
public function renderTrixContentAttachment(array $options = []): string
7362
{
74-
return view('rich-text-laravel::contents._'.Str::of($this->name)->studly()->snake('_'), [
75-
'content' => $this,
76-
'options' => $options,
77-
])->render();
63+
return $this->renderedHtml ??= $this->contentInstance()->fragment->toHtml();
64+
}
65+
66+
private function contentInstance(): Content
67+
{
68+
return $this->contentInstance ??= new Content($this->content);
7869
}
7970
}

src/Attachment.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Attachment
1818

1919
public static $SELECTOR = '//rich-text-attachment';
2020

21-
const ATTRIBUTES = ['sgid', 'content-type', 'url', 'href', 'filename', 'filesize', 'width', 'height', 'previewable', 'presentation', 'caption'];
21+
const ATTRIBUTES = ['sgid', 'content-type', 'url', 'href', 'filename', 'filesize', 'width', 'height', 'previewable', 'presentation', 'caption', 'content'];
2222

2323
private $cachedAttributes;
2424

@@ -79,9 +79,7 @@ private static function processAttributes(array $attributes): array
7979
->all();
8080
}
8181

82-
public function __construct(public DOMElement $node, public AttachableContract $attachable)
83-
{
84-
}
82+
public function __construct(public DOMElement $node, public AttachableContract $attachable) {}
8583

8684
public function withFullAttributes(): static
8785
{

src/AttachmentGallery.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ public static function selector(): string
3434
);
3535
}
3636

37-
public function __construct(public DOMElement $node)
38-
{
39-
}
37+
public function __construct(public DOMElement $node) {}
4038

4139
public function attachments(): Collection
4240
{

src/Commands/InstallCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,6 @@ protected static function updateNodePackages(callable $callback, $dev = true)
237237

238238
private function phpBinary()
239239
{
240-
return (new PhpExecutableFinder())->find(false) ?: 'php';
240+
return (new PhpExecutableFinder)->find(false) ?: 'php';
241241
}
242242
}

src/Fragment.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ public static function fromHtml(?string $html = null): self
3131
return HtmlConversion::fragmentForHtml($html);
3232
}
3333

34-
public function __construct(public DOMDocument $source)
35-
{
36-
}
34+
public function __construct(public DOMDocument $source) {}
3735

3836
public function findAll(string $selector): Collection
3937
{

src/TrixAttachment.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DOMElement;
66
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Facades\Log;
78
use Illuminate\Support\Str;
89

910
class TrixAttachment
@@ -56,9 +57,7 @@ private static function typeCast(string $key, $value)
5657
};
5758
}
5859

59-
public function __construct(public DOMElement $node)
60-
{
61-
}
60+
public function __construct(public DOMElement $node) {}
6261

6362
public function attributes(): array
6463
{
@@ -93,7 +92,7 @@ private function readJsonAttribute(string $key): array
9392
$data = json_decode($value ?: '[]', true);
9493

9594
if (json_last_error() !== JSON_ERROR_NONE) {
96-
logger(sprintf(
95+
Log::notice(sprintf(
9796
'[%s] Couldnt parse JSON %s from NODE %s',
9897
static::class,
9998
$value,

tests/ContentTest.php

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Tonysm\RichTextLaravel\Tests;
44

5+
use Illuminate\Support\Facades\Log;
6+
use Tonysm\RichTextLaravel\Attachables\ContentAttachment;
57
use Tonysm\RichTextLaravel\Attachables\MissingAttachable;
68
use Tonysm\RichTextLaravel\Attachables\RemoteImage;
79
use Tonysm\RichTextLaravel\Attachment;
@@ -208,6 +210,8 @@ public function converts_trix_formatetd_attachments_with_custom_tag_name()
208210
/** @test */
209211
public function ignores_trix_formatteed_attachments_with_bad_json()
210212
{
213+
Log::shouldReceive('notice')->once();
214+
211215
$html = <<<'HTML'
212216
<div data-trix-attachment='{"sgid": "pure garbate...}'></div>
213217
HTML;
@@ -505,44 +509,28 @@ public function renders_file_attachments()
505509
}
506510

507511
/** @test */
508-
public function renders_horizontal_rules_as_content_attachment()
512+
public function renders_html_content_attachment()
509513
{
510-
$content = $this->fromHtml(<<<'HTML'
511-
<div>
512-
<figure
513-
data-trix-attachment='{"contentType": "vnd.richtextlaravel.horizontal-rule.html", "content": "<hr>"}'
514-
>
515-
<hr>
516-
</figure>
517-
</div>
518-
HTML);
514+
$attachment = $this->attachmentFromHtml('<rich-text-attachment content-type="text/html" content="abc"></rich-text-attachment>');
515+
$attachable = $attachment->attachable;
519516

520-
$this->assertEquals(<<<'HTML'
521-
<div>
522-
<hr>
517+
$this->assertInstanceOf(ContentAttachment::class, $attachable);
518+
$this->assertEquals('text/html', $attachable->contentType);
519+
$this->assertEquals('abc', $attachable->content);
523520

524-
</div>
525-
HTML, $content->renderWithAttachments());
521+
$trixAttachment = $attachment->toTrixAttachment();
522+
$this->assertEquals('text/html', $trixAttachment->attributes()['contentType']);
523+
$this->assertEquals('abc', $trixAttachment->attributes()['content']);
526524
}
527525

528526
/** @test */
529-
public function renders_horizontal_rules_for_trix()
527+
public function renders_content_attachment()
530528
{
531-
$content = $this->fromHtml(<<<'HTML'
532-
<div>
533-
<figure
534-
data-trix-attachment='{"contentType": "vnd.richtextlaravel.horizontal-rule.html", "content": "<hr>"}'
535-
>
536-
<hr>
537-
</figure>
538-
</div>
539-
HTML);
529+
$attachment = $this->attachmentFromHtml('<rich-text-attachment content-type="text/html" content="&lt;p&gt;abc&lt;/p&gt;"></rich-text-attachment>');
530+
/** @var ContentAttachment $attachable */
531+
$attachable = $attachment->attachable;
540532

541-
$this->assertEquals(<<<'HTML'
542-
<div>
543-
<figure data-trix-attachment='{"contentType":"vnd.richtextlaravel.horizontal-rule.html","content":"&lt;hr&gt;\n"}'></figure>
544-
</div>
545-
HTML, $content->toTrixHtml());
533+
$this->assertEquals('<p>abc</p>', $attachable->renderTrixContentAttachment());
546534
}
547535

548536
/** @test */
@@ -584,6 +572,11 @@ private function fromHtml(string $html): Content
584572
{
585573
return tap(new Content($html), fn (Content $content) => $this->assertNotEmpty($content->toHtml()));
586574
}
575+
576+
private function attachmentFromHtml(string $html): Attachment
577+
{
578+
return $this->fromHtml($html)->attachments()->first();
579+
}
587580
}
588581

589582
class UserWithCustomRenderContent extends User

0 commit comments

Comments
 (0)