-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<script setup> | ||
import ColumnTypeOptions from "./options/column.md"; | ||
</script> | ||
|
||
# HtmlColumnType | ||
|
||
The [`HtmlColumnType`](https://github.com/Kreyu/data-table-bundle/blob/main/src/Column/Type/HtmlColumnType.php) represents a column with value displayed as HTML. | ||
|
||
## Options | ||
|
||
### `raw` | ||
|
||
- **type**: `bool` | ||
- **default**: `true` | ||
|
||
Defines whether the value should be rendered as raw HTML. | ||
|
||
For example, if your column contains a string `<strong>Foo</strong>`: | ||
- setting it to `true` will render the value as a bold text: **Foo** | ||
- setting it to `false` will render the value as a plain text: `<strong>Foo</strong>` | ||
|
||
### `strip_tags` | ||
|
||
- **type**: `bool` | ||
- **default**: `false` | ||
|
||
Defines whether the tags should be stripped. Internally uses the [`strip_tags`](https://twig.symfony.com/doc/3.x/filters/striptags.html) function. | ||
|
||
For example, if your column contains a string `<strong>Foo</strong>`: | ||
- setting it to `true` will render the value as a simple text: Foo | ||
- setting it to `false` will render the value as is: `<strong>Foo</strong>` | ||
|
||
### `allowed_tags` | ||
|
||
- **type**: `null`, `string` or `string[]` | ||
- **default**: `null` | ||
|
||
Defines tags which should not be stripped if `strip_tags` is set to `true`, e.g. `<br><p>`. | ||
|
||
For example, if your column contains a string `<strong>Foo</strong><br/>`: | ||
- setting it to `"<strong>"` will render the value as: `Foo<br/>` | ||
- setting it to `"<strong><br>"` will render the value as: `<strong>Foo</strong><br/>` | ||
- setting it to `["<strong>", "<br>"]` will render the value as: `<strong>Foo</strong><br/>` | ||
- setting it to `null` (by default) will render the value as: `<strong>Foo</strong><br/>` | ||
|
||
> [!WARNING] | ||
> In Twig, this option is ignored if `strip_tags` option is set to `false`. | ||
## Inherited options | ||
|
||
<ColumnTypeOptions/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kreyu\Bundle\DataTableBundle\Column\Type; | ||
|
||
use Kreyu\Bundle\DataTableBundle\Column\ColumnInterface; | ||
use Kreyu\Bundle\DataTableBundle\Column\ColumnValueView; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class HtmlColumnType extends AbstractColumnType | ||
{ | ||
public function buildValueView(ColumnValueView $view, ColumnInterface $column, array $options): void | ||
{ | ||
$view->vars = array_replace($view->vars, [ | ||
'raw' => $options['raw'], | ||
'strip_tags' => $options['strip_tags'], | ||
'allowed_tags' => $options['allowed_tags'], | ||
]); | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver->define('raw') | ||
->default(true) | ||
->allowedTypes('bool') | ||
->info('Defines whether the value should be rendered as raw HTML.') | ||
; | ||
|
||
/* @see https://www.php.net/strip_tags */ | ||
$resolver->define('strip_tags') | ||
->default(false) | ||
->allowedTypes('bool') | ||
->info('Defines whether the tags should be stripped. Internally uses the "strip_tags" function.') | ||
; | ||
|
||
/* @see https://www.php.net/strip_tags */ | ||
$resolver->define('allowed_tags') | ||
->default(null) | ||
->allowedTypes('null', 'string', 'string[]') | ||
->info('Defines tags which should not be stripped if "strip_tags" is set to true, e.g. "<br><p>"') | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kreyu\Bundle\DataTableBundle\Tests\Unit\Column\Type; | ||
|
||
use Kreyu\Bundle\DataTableBundle\Column\Type\ColumnTypeInterface; | ||
use Kreyu\Bundle\DataTableBundle\Column\Type\HtmlColumnType; | ||
use Kreyu\Bundle\DataTableBundle\Test\Column\Type\ColumnTypeTestCase; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
|
||
class HtmlColumnTypeTest extends ColumnTypeTestCase | ||
{ | ||
protected function getTestedColumnType(): ColumnTypeInterface | ||
{ | ||
return new HtmlColumnType(); | ||
} | ||
|
||
public function testDefaultRawOption(): void | ||
{ | ||
$column = $this->createColumn(); | ||
$columnValueView = $this->createColumnValueView($column); | ||
|
||
$this->assertTrue($columnValueView->vars['raw']); | ||
} | ||
|
||
#[DataProvider('provideRawOption')] | ||
public function testPassingRawOption(bool $raw): void | ||
{ | ||
$column = $this->createColumn(['raw' => $raw]); | ||
$columnValueView = $this->createColumnValueView($column); | ||
|
||
$this->assertEquals($raw, $columnValueView->vars['raw']); | ||
} | ||
|
||
public static function provideRawOption(): iterable | ||
{ | ||
yield 'true' => [true]; | ||
yield 'false' => [false]; | ||
} | ||
|
||
public function testDefaultStripTagsOption(): void | ||
{ | ||
$column = $this->createColumn(); | ||
$columnValueView = $this->createColumnValueView($column); | ||
|
||
$this->assertFalse($columnValueView->vars['strip_tags']); | ||
} | ||
|
||
#[DataProvider('provideStripTagsOption')] | ||
public function testPassingStripTagsOption(bool $stripTags): void | ||
{ | ||
$column = $this->createColumn(['strip_tags' => $stripTags]); | ||
$columnValueView = $this->createColumnValueView($column); | ||
|
||
$this->assertEquals($stripTags, $columnValueView->vars['strip_tags']); | ||
} | ||
|
||
public static function provideStripTagsOption(): iterable | ||
{ | ||
yield 'true' => [true]; | ||
yield 'false' => [false]; | ||
} | ||
|
||
public function testDefaultAllowedTagsOption(): void | ||
{ | ||
$column = $this->createColumn(); | ||
$columnValueView = $this->createColumnValueView($column); | ||
|
||
$this->assertNull($columnValueView->vars['allowed_tags']); | ||
} | ||
|
||
#[DataProvider('provideAllowedTagsOption')] | ||
public function testPassingAllowedTagsOption(null|string|array $allowedTags): void | ||
{ | ||
$column = $this->createColumn(['allowed_tags' => $allowedTags]); | ||
$columnValueView = $this->createColumnValueView($column); | ||
|
||
$this->assertEquals($allowedTags, $columnValueView->vars['allowed_tags']); | ||
} | ||
|
||
public static function provideAllowedTagsOption(): iterable | ||
{ | ||
yield 'null' => [null]; | ||
yield 'string' => ['<strong><br>']; | ||
yield 'array of strings' => [['<strong>', '<br>']]; | ||
} | ||
} |