Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to strip html tags in export #1745

Merged
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
9 changes: 6 additions & 3 deletions src/Components/Exports/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public function setData(array $columns, Collection $data): Export
return $this;
}

public function prepare(Collection $data, array $columns): array
public function prepare(Collection $data, array $columns, bool $stripTags): array
{
$header = collect([]);

$data = $data->transform(function ($row) use ($columns, $header) {
$data = $data->transform(function ($row) use ($columns, $header, $stripTags) {
$item = collect([]);

collect($columns)->each(function ($column) use ($row, $header, $item) {
collect($columns)->each(function ($column) use ($row, $header, $item, $stripTags) {
/** @var Model|stdClass $row */
if (method_exists($row, 'withoutRelations')) {
$row = $row->withoutRelations()->toArray();
Expand Down Expand Up @@ -73,6 +73,9 @@ public function prepare(Collection $data, array $columns): array
/** @var array $row */
foreach ($row as $key => $value) {
if ($key === $column->field) {
if (true === $stripTags) {
$value = strip_tags($value);
}
$item->put($column->title, html_entity_decode($value, ENT_QUOTES | ENT_HTML5, 'UTF-8'));
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Components/Exports/OpenSpout/v4/ExportToCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function download(Exportable|array $exportOptions): BinaryFileResponse
*/
public function build(Exportable|array $exportOptions): void
{
$data = $this->prepare($this->data, $this->columns);
$stripTags = boolval(data_get($exportOptions, 'stripTags', false));
$data = $this->prepare($this->data, $this->columns, $stripTags);

$csvSeparator = strval(data_get($exportOptions, 'csvSeparator', ','));
$csvDelimiter = strval(data_get($exportOptions, 'csvDelimiter', '"'));
Expand Down
3 changes: 2 additions & 1 deletion src/Components/Exports/OpenSpout/v4/ExportToXLS.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public function download(Exportable|array $exportOptions): BinaryFileResponse
*/
public function build(Exportable|array $exportOptions): void
{
$data = $this->prepare($this->data, $this->columns);
$stripTags = boolval(data_get($exportOptions, 'stripTags', false));
$data = $this->prepare($this->data, $this->columns, $stripTags);

$options = new Options();
$writer = new Writer($options);
Expand Down
9 changes: 9 additions & 0 deletions src/Components/SetUp/Exportable.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ final class Exportable implements Wireable

public array $batchExport = [];

public bool $stripTags = false;

public function __construct(public string $fileName = 'export')
{
}
Expand Down Expand Up @@ -103,4 +105,11 @@ public static function fromLivewire($value)
{
return $value;
}

public function stripTags(bool $value): self
{
$this->stripTags = $value;

return $this;
}
}
3 changes: 3 additions & 0 deletions tests/Concerns/Components/ExportTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class ExportTable extends PowerGridComponent

public string $delimiter = '"';

public bool $testStripHtml = false;

public function setUp(): array
{
$this->showCheckBox();
Expand All @@ -30,6 +32,7 @@ public function setUp(): array
->striped()
->csvSeparator($this->separator)
->csvDelimiter($this->delimiter)
->stripTags($this->testStripHtml)
->type(Exportable::TYPE_XLS, Exportable::TYPE_CSV),

PowerGrid::header()
Expand Down
98 changes: 97 additions & 1 deletion tests/Feature/ExportTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

use OpenSpout\Reader\XLSX\Reader;
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;

use PowerComponents\LivewirePowerGrid\Tests\Concerns\Components\ExportTable;

use function PowerComponents\LivewirePowerGrid\Tests\Plugins\livewire;

use PowerComponents\LivewirePowerGrid\{Button,Column};
use PowerComponents\LivewirePowerGrid\{Button,Column, PowerGridFields};

it('properly export xls - all data', function () {
livewire(ExportTable::class)
Expand Down Expand Up @@ -128,6 +130,100 @@ public function actions($row): array
'data' => [$exportWithAction::class],
]);

$exportWithHtml = new class () extends ExportTable {
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('nameWithHtml', function ($dish) {
return sprintf(
'<a> %s </a>',
e($dish->name)
);
});
}

public function columns(): array
{
return [
Column::add()
->title('nameWithHtml')
->field('nameWithHtml'),
];
}
};

it('properly export csv with tags', function (string $component) {
$downloadedFile = livewire($component, ['testStripHtml' => false])
->set('checkboxValues', [
0 => '1',
])
->call('exportToCsv', true)
->assertFileDownloaded('export.csv');

$headings = ['nameWithHtml'];

$rows = [
['a Pastel de Nata a'],
];

expect($downloadedFile)->toBeCsvDownload($headings, $rows);
})->with('export_with_html')->requiresOpenSpout();

it('properly export csv without tags', function (string $component) {
$downloadedFile = livewire($component, ['testStripHtml' => true])
->set('checkboxValues', [
0 => '1',
])
->call('exportToCsv', true)
->assertFileDownloaded('export.csv');

$headings = ['nameWithHtml'];

$rows = [
[' Pastel de Nata '],
];

expect($downloadedFile)->toBeCsvDownload($headings, $rows);
})->with('export_with_html')->requiresOpenSpout();

it('properly export xls with tags', function (string $component) {
$downloadedFile = livewire($component, ['testStripHtml' => false])
->set('checkboxValues', [
0 => '1',
])
->call('exportToXLS', true)
->assertFileDownloaded('export.xlsx');

$headings = ['nameWithHtml'];

$rows = [
['a Pastel de Nata a'],
];

expect($downloadedFile)->toBeXLSDownload($headings, $rows);
})->with('export_with_html')->requiresOpenSpout();

it('properly export xls without tags', function (string $component) {
$downloadedFile = livewire($component, ['testStripHtml' => true])
->set('checkboxValues', [
0 => '1',
])
->call('exportToXLS', true)
->assertFileDownloaded('export.xlsx');

$headings = ['nameWithHtml'];

$rows = [
[' Pastel de Nata '],
];

expect($downloadedFile)->toBeXLSDownload($headings, $rows);
})->with('export_with_html')->requiresOpenSpout();

dataset('export_with_html', [
'html' => [$exportWithHtml::class],
]);

/*
|--------------------------------------------------------------------------
| Expectations for this test
Expand Down