Skip to content

Commit 12e006a

Browse files
committed
Fix tests + add docs
1 parent 9d78132 commit 12e006a

6 files changed

Lines changed: 40 additions & 0 deletions

File tree

docs/guide/building-entity-list.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ Setting the label, allowing the column to be sortable and to display html is opt
5353
The optional `->setWidth()` method accepts either an integer (eg: `20` for 20%), a float (eg: `.2` for 20%) or a string (eg: `'20'` or `'20%'`); if missing, it will be deduced (you can use `->setWidthFill()` to force this last behavior).
5454
To hide the column on small screens, use `->hideOnSmallScreens()`.
5555

56+
::: warning
57+
HTML sanitization is enabled by default for list fields (to prevent XSS attacks when displaying the list). You can disable it by using `->shouldSanitizeHtml(false)` field method.
58+
:::
59+
5660
Sorting columns must be handled in the `getListData()` method, see below.
5761

5862
#### Add a badge field

docs/guide/show-fields/text.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@ Reset the collapse configuration.
2020

2121
By default, the text is escaped. If you want to display HTML, set this to true.
2222

23+
### `shouldSanitizeHtml(bool $sanitize = true)`
24+
25+
HTML sanitization is enabled by default for text fields (to prevent XSS attacks when displaying the show). To disable it, call `->shouldSanitizeHtml(false)`.
26+
27+
2328
### `allowEmbeds(array $embeds)`
2429

2530
This method expects an array of embeds that could be inserted in the content, declared as full class names. An embed class must extend `Code16\Sharp\Form\Fields\Embeds\SharpFormEditorEmbed`.
2631

2732
The [documentation on how to write an Embed class is available here](../form-editor-embeds.md).
2833

34+
2935
## Transformer
3036

3137
For markdown-formatted texts, be sure to use the built-in `MarkdownAttributeTransformer`:

tests/Unit/EntityList/SharpEntityListStateTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public function buildList(EntityListFieldsContainer $fields): void
145145
'label' => '',
146146
'sortable' => false,
147147
'html' => true,
148+
'sanitize' => true,
148149
'width' => null,
149150
'hideOnXS' => false,
150151
],
@@ -162,6 +163,7 @@ public function buildList(EntityListFieldsContainer $fields): void
162163
'label' => '',
163164
'sortable' => false,
164165
'html' => true,
166+
'sanitize' => true,
165167
'width' => null,
166168
'hideOnXS' => false,
167169
],

tests/Unit/EntityList/SharpEntityListTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function buildList(EntityListFieldsContainer $fields): void
3232
'label' => 'Name',
3333
'sortable' => false,
3434
'html' => true,
35+
'sanitize' => true,
3536
'width' => '50%',
3637
'hideOnXS' => false,
3738
],
@@ -365,3 +366,20 @@ public function buildListConfig(): void
365366

366367
expect($list->listConfig()['createButtonLabel'])->toEqual('New post...');
367368
});
369+
370+
it('allows to disable HTML sanitization', function () {
371+
$list = new class() extends FakeSharpEntityList
372+
{
373+
public function buildList(EntityListFieldsContainer $fields): void
374+
{
375+
$fields->addField(
376+
EntityListField::make('name')
377+
->setLabel('Name')
378+
->shouldSanitizeHtml(false)
379+
->setWidth(.5)
380+
);
381+
}
382+
};
383+
384+
expect($list->fields()[0]['sanitize'])->toBeFalse();
385+
});

tests/Unit/Show/Fields/SharpShowListFieldTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
'key' => 'textField',
1919
'emptyVisible' => false,
2020
'html' => true,
21+
'sanitize' => true,
2122
'type' => 'text',
2223
],
2324
],

tests/Unit/Show/Fields/SharpShowTextFieldTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
'type' => 'text',
1313
'emptyVisible' => false,
1414
'html' => true,
15+
'sanitize' => true,
1516
'label' => 'Label',
1617
]);
1718
});
@@ -56,3 +57,11 @@
5657

5758
expect($field->toArray())->toHaveKey('localized', true);
5859
});
60+
61+
it('allows to disable HTML sanitization', function () {
62+
$field = SharpShowTextField::make('textfield')
63+
->shouldSanitizeHtml(false)
64+
->setLabel('Label');
65+
66+
expect($field->toArray()['sanitize'])->toBe(false);
67+
});

0 commit comments

Comments
 (0)