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
22 changes: 14 additions & 8 deletions src/Form/Fields/Formatters/EditorEmbedsFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Code16\Sharp\Form\Fields\SharpFormUploadField;
use Code16\Sharp\Utils\Fields\Formatters\FormatsEditorEmbeds;
use Code16\Sharp\Utils\Fields\Formatters\FormatsHtmlContent;
use DOMElement;
use Illuminate\Support\Str;

class EditorEmbedsFormatter extends SharpFieldFormatter implements FormatsAfterUpdate
Expand Down Expand Up @@ -66,10 +67,7 @@ function (string $content) use ($field, $value) {
if ($fieldKey === 'slot') {
$this->setInnerHtml($element, $fieldValue);
} else {
$element->setAttribute(
Str::kebab($fieldKey),
is_array($fieldValue) ? json_encode($fieldValue) : $fieldValue
);
$this->setAttribute($element, $fieldKey, $fieldValue);
}
}
}
Expand Down Expand Up @@ -113,10 +111,7 @@ function (string $content) use ($field) {
$this->tryJsonDecode($element->getAttribute(Str::kebab($fieldKey)))
);

$element->setAttribute(
Str::kebab($fieldKey),
is_array($formatted) ? json_encode($formatted) : $formatted
);
$this->setAttribute($element, $fieldKey, $formatted);
}
}
}
Expand All @@ -126,4 +121,15 @@ function (string $content) use ($field) {
}
);
}

protected function setAttribute(DOMElement $element, string $fieldKey, mixed $value): void
{
$attribute = Str::kebab($fieldKey);

if ($value === false || $value === null) {
$element->removeAttribute($attribute);
} else {
$element->setAttribute($attribute, is_array($value) ? json_encode($value) : $value);
}
}
}
25 changes: 24 additions & 1 deletion tests/Unit/Form/Fields/Formatters/EditorFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@
'width' => 600,
'height' => 600,
],
'check' => null,
'nullable' => null,
'_html' => sprintf('<img src="%s"> My <em>contentful</em> content',
$thumbnail,
),
Expand All @@ -373,6 +375,7 @@
(new EditorFormatterTestEmbed())->key() => [
'0' => [
'slot' => 'My <em>contentful</em> content',
'check' => true,
'visual' => [
'name' => 'image.jpg',
'path' => 'data/Posts/1/image.jpg',
Expand All @@ -386,7 +389,7 @@
],
],
],
]))->toEqual(sprintf('<x-embed visual="%s">My <em>contentful</em> content</x-embed>',
]))->toEqual(sprintf('<x-embed check="1" visual="%s">My <em>contentful</em> content</x-embed>',
e(json_encode([
'file_name' => 'data/Posts/1/image.jpg',
'size' => 120,
Expand All @@ -396,6 +399,26 @@
));
});

it('allows to format embeds with false/null values from front', function () {
$formatter = (new EditorFormatter())->setInstanceId(1);
$field = SharpFormEditorField::make('md')
->allowEmbeds([EditorFormatterTestEmbed::class]);

expect($formatter->fromFront($field, 'attribute', [
'text' => <<<'HTML'
<x-embed data-key="0"></x-embed>
HTML,
'embeds' => [
(new EditorFormatterTestEmbed())->key() => [
'0' => [
'check' => false,
'nullable' => null,
],
],
],
]))->toEqual('<x-embed></x-embed>');
});

it('allows to format a unicode text value from front', function () {
// This test was created to demonstrate preg_replace failure
// without the unicode modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Code16\Sharp\Form\Eloquent\Uploads\Transformers\SharpUploadModelFormAttributeTransformer;
use Code16\Sharp\Form\Fields\Embeds\SharpFormEditorEmbed;
use Code16\Sharp\Form\Fields\SharpFormCheckField;
use Code16\Sharp\Form\Fields\SharpFormTextField;
use Code16\Sharp\Form\Fields\SharpFormUploadField;
use Code16\Sharp\Utils\Fields\FieldsContainer;
Expand All @@ -23,6 +24,8 @@ public function buildFormFields(FieldsContainer $formFields): void
{
$formFields
->addField(SharpFormTextField::make('slot'))
->addField(SharpFormCheckField::make('check', 'Check'))
->addField(SharpFormTextField::make('nullable'))
->addField(SharpFormUploadField::make('visual')->setImageOnly());
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Show/Fields/Formatters/TextFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@
'0' => [
'slot' => 'My <em>contentful</em> content',
'visual' => null,
'check' => null,
'nullable' => null,
'_html' => '<img src=""> My <em>contentful</em> content',
],
],
Expand Down
Loading