Skip to content

Commit 5ec5da6

Browse files
authored
Merge pull request #734 from code16/embeds-remove-false-attributes
Remove embed attributes when the value is `false` or `null`
2 parents 7c601ff + 1a6680f commit 5ec5da6

4 files changed

Lines changed: 43 additions & 9 deletions

File tree

src/Form/Fields/Formatters/EditorEmbedsFormatter.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Code16\Sharp\Form\Fields\SharpFormUploadField;
88
use Code16\Sharp\Utils\Fields\Formatters\FormatsEditorEmbeds;
99
use Code16\Sharp\Utils\Fields\Formatters\FormatsHtmlContent;
10+
use DOMElement;
1011
use Illuminate\Support\Str;
1112

1213
class EditorEmbedsFormatter extends SharpFieldFormatter implements FormatsAfterUpdate
@@ -66,10 +67,7 @@ function (string $content) use ($field, $value) {
6667
if ($fieldKey === 'slot') {
6768
$this->setInnerHtml($element, $fieldValue);
6869
} else {
69-
$element->setAttribute(
70-
Str::kebab($fieldKey),
71-
is_array($fieldValue) ? json_encode($fieldValue) : $fieldValue
72-
);
70+
$this->setAttribute($element, $fieldKey, $fieldValue);
7371
}
7472
}
7573
}
@@ -113,10 +111,7 @@ function (string $content) use ($field) {
113111
$this->tryJsonDecode($element->getAttribute(Str::kebab($fieldKey)))
114112
);
115113

116-
$element->setAttribute(
117-
Str::kebab($fieldKey),
118-
is_array($formatted) ? json_encode($formatted) : $formatted
119-
);
114+
$this->setAttribute($element, $fieldKey, $formatted);
120115
}
121116
}
122117
}
@@ -126,4 +121,15 @@ function (string $content) use ($field) {
126121
}
127122
);
128123
}
124+
125+
protected function setAttribute(DOMElement $element, string $fieldKey, mixed $value): void
126+
{
127+
$attribute = Str::kebab($fieldKey);
128+
129+
if ($value === false || $value === null) {
130+
$element->removeAttribute($attribute);
131+
} else {
132+
$element->setAttribute($attribute, is_array($value) ? json_encode($value) : $value);
133+
}
134+
}
129135
}

tests/Unit/Form/Fields/Formatters/EditorFormatterTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@
350350
'width' => 600,
351351
'height' => 600,
352352
],
353+
'check' => null,
354+
'nullable' => null,
353355
'_html' => sprintf('<img src="%s"> My <em>contentful</em> content',
354356
$thumbnail,
355357
),
@@ -373,6 +375,7 @@
373375
(new EditorFormatterTestEmbed())->key() => [
374376
'0' => [
375377
'slot' => 'My <em>contentful</em> content',
378+
'check' => true,
376379
'visual' => [
377380
'name' => 'image.jpg',
378381
'path' => 'data/Posts/1/image.jpg',
@@ -386,7 +389,7 @@
386389
],
387390
],
388391
],
389-
]))->toEqual(sprintf('<x-embed visual="%s">My <em>contentful</em> content</x-embed>',
392+
]))->toEqual(sprintf('<x-embed check="1" visual="%s">My <em>contentful</em> content</x-embed>',
390393
e(json_encode([
391394
'file_name' => 'data/Posts/1/image.jpg',
392395
'size' => 120,
@@ -396,6 +399,26 @@
396399
));
397400
});
398401

402+
it('allows to format embeds with false/null values from front', function () {
403+
$formatter = (new EditorFormatter())->setInstanceId(1);
404+
$field = SharpFormEditorField::make('md')
405+
->allowEmbeds([EditorFormatterTestEmbed::class]);
406+
407+
expect($formatter->fromFront($field, 'attribute', [
408+
'text' => <<<'HTML'
409+
<x-embed data-key="0"></x-embed>
410+
HTML,
411+
'embeds' => [
412+
(new EditorFormatterTestEmbed())->key() => [
413+
'0' => [
414+
'check' => false,
415+
'nullable' => null,
416+
],
417+
],
418+
],
419+
]))->toEqual('<x-embed></x-embed>');
420+
});
421+
399422
it('allows to format a unicode text value from front', function () {
400423
// This test was created to demonstrate preg_replace failure
401424
// without the unicode modifier

tests/Unit/Form/Fields/Formatters/Fixtures/EditorFormatterTestEmbed.php

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

55
use Code16\Sharp\Form\Eloquent\Uploads\Transformers\SharpUploadModelFormAttributeTransformer;
66
use Code16\Sharp\Form\Fields\Embeds\SharpFormEditorEmbed;
7+
use Code16\Sharp\Form\Fields\SharpFormCheckField;
78
use Code16\Sharp\Form\Fields\SharpFormTextField;
89
use Code16\Sharp\Form\Fields\SharpFormUploadField;
910
use Code16\Sharp\Utils\Fields\FieldsContainer;
@@ -23,6 +24,8 @@ public function buildFormFields(FieldsContainer $formFields): void
2324
{
2425
$formFields
2526
->addField(SharpFormTextField::make('slot'))
27+
->addField(SharpFormCheckField::make('check', 'Check'))
28+
->addField(SharpFormTextField::make('nullable'))
2629
->addField(SharpFormUploadField::make('visual')->setImageOnly());
2730
}
2831

tests/Unit/Show/Fields/Formatters/TextFormatterTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@
140140
'0' => [
141141
'slot' => 'My <em>contentful</em> content',
142142
'visual' => null,
143+
'check' => null,
144+
'nullable' => null,
143145
'_html' => '<img src=""> My <em>contentful</em> content',
144146
],
145147
],

0 commit comments

Comments
 (0)