Skip to content

Commit 75693ea

Browse files
authored
feat: allow to strip html tags in export (#1745)
* feat: allow to strip html tags in export * remove unecessary calls
1 parent 25b5506 commit 75693ea

File tree

6 files changed

+119
-6
lines changed

6 files changed

+119
-6
lines changed

src/Components/Exports/Export.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ public function setData(array $columns, Collection $data): Export
3535
return $this;
3636
}
3737

38-
public function prepare(Collection $data, array $columns): array
38+
public function prepare(Collection $data, array $columns, bool $stripTags): array
3939
{
4040
$header = collect([]);
4141

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

45-
collect($columns)->each(function ($column) use ($row, $header, $item) {
45+
collect($columns)->each(function ($column) use ($row, $header, $item, $stripTags) {
4646
/** @var Model|stdClass $row */
4747
if (method_exists($row, 'withoutRelations')) {
4848
$row = $row->withoutRelations()->toArray();
@@ -73,6 +73,9 @@ public function prepare(Collection $data, array $columns): array
7373
/** @var array $row */
7474
foreach ($row as $key => $value) {
7575
if ($key === $column->field) {
76+
if (true === $stripTags) {
77+
$value = strip_tags($value);
78+
}
7679
$item->put($column->title, html_entity_decode($value, ENT_QUOTES | ENT_HTML5, 'UTF-8'));
7780
}
7881
}

src/Components/Exports/OpenSpout/v4/ExportToCsv.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public function download(Exportable|array $exportOptions): BinaryFileResponse
3232
*/
3333
public function build(Exportable|array $exportOptions): void
3434
{
35-
$data = $this->prepare($this->data, $this->columns);
35+
$stripTags = boolval(data_get($exportOptions, 'stripTags', false));
36+
$data = $this->prepare($this->data, $this->columns, $stripTags);
3637

3738
$csvSeparator = strval(data_get($exportOptions, 'csvSeparator', ','));
3839
$csvDelimiter = strval(data_get($exportOptions, 'csvDelimiter', '"'));

src/Components/Exports/OpenSpout/v4/ExportToXLS.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public function download(Exportable|array $exportOptions): BinaryFileResponse
3939
*/
4040
public function build(Exportable|array $exportOptions): void
4141
{
42-
$data = $this->prepare($this->data, $this->columns);
42+
$stripTags = boolval(data_get($exportOptions, 'stripTags', false));
43+
$data = $this->prepare($this->data, $this->columns, $stripTags);
4344

4445
$options = new Options();
4546
$writer = new Writer($options);

src/Components/SetUp/Exportable.php

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ final class Exportable implements Wireable
2525

2626
public array $batchExport = [];
2727

28+
public bool $stripTags = false;
29+
2830
public function __construct(public string $fileName = 'export')
2931
{
3032
}
@@ -103,4 +105,11 @@ public static function fromLivewire($value)
103105
{
104106
return $value;
105107
}
108+
109+
public function stripTags(bool $value): self
110+
{
111+
$this->stripTags = $value;
112+
113+
return $this;
114+
}
106115
}

tests/Concerns/Components/ExportTable.php

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class ExportTable extends PowerGridComponent
2121

2222
public string $delimiter = '"';
2323

24+
public bool $testStripHtml = false;
25+
2426
public function setUp(): array
2527
{
2628
$this->showCheckBox();
@@ -30,6 +32,7 @@ public function setUp(): array
3032
->striped()
3133
->csvSeparator($this->separator)
3234
->csvDelimiter($this->delimiter)
35+
->stripTags($this->testStripHtml)
3336
->type(Exportable::TYPE_XLS, Exportable::TYPE_CSV),
3437

3538
PowerGrid::header()

tests/Feature/ExportTest.php

+97-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php
22

33
use OpenSpout\Reader\XLSX\Reader;
4+
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
5+
46
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Components\ExportTable;
57

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

8-
use PowerComponents\LivewirePowerGrid\{Button,Column};
10+
use PowerComponents\LivewirePowerGrid\{Button,Column, PowerGridFields};
911

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

133+
$exportWithHtml = new class () extends ExportTable {
134+
public function fields(): PowerGridFields
135+
{
136+
return PowerGrid::fields()
137+
->add('nameWithHtml', function ($dish) {
138+
return sprintf(
139+
'<a> %s </a>',
140+
e($dish->name)
141+
);
142+
});
143+
}
144+
145+
public function columns(): array
146+
{
147+
return [
148+
Column::add()
149+
->title('nameWithHtml')
150+
->field('nameWithHtml'),
151+
];
152+
}
153+
};
154+
155+
it('properly export csv with tags', function (string $component) {
156+
$downloadedFile = livewire($component, ['testStripHtml' => false])
157+
->set('checkboxValues', [
158+
0 => '1',
159+
])
160+
->call('exportToCsv', true)
161+
->assertFileDownloaded('export.csv');
162+
163+
$headings = ['nameWithHtml'];
164+
165+
$rows = [
166+
['a Pastel de Nata a'],
167+
];
168+
169+
expect($downloadedFile)->toBeCsvDownload($headings, $rows);
170+
})->with('export_with_html')->requiresOpenSpout();
171+
172+
it('properly export csv without tags', function (string $component) {
173+
$downloadedFile = livewire($component, ['testStripHtml' => true])
174+
->set('checkboxValues', [
175+
0 => '1',
176+
])
177+
->call('exportToCsv', true)
178+
->assertFileDownloaded('export.csv');
179+
180+
$headings = ['nameWithHtml'];
181+
182+
$rows = [
183+
[' Pastel de Nata '],
184+
];
185+
186+
expect($downloadedFile)->toBeCsvDownload($headings, $rows);
187+
})->with('export_with_html')->requiresOpenSpout();
188+
189+
it('properly export xls with tags', function (string $component) {
190+
$downloadedFile = livewire($component, ['testStripHtml' => false])
191+
->set('checkboxValues', [
192+
0 => '1',
193+
])
194+
->call('exportToXLS', true)
195+
->assertFileDownloaded('export.xlsx');
196+
197+
$headings = ['nameWithHtml'];
198+
199+
$rows = [
200+
['a Pastel de Nata a'],
201+
];
202+
203+
expect($downloadedFile)->toBeXLSDownload($headings, $rows);
204+
})->with('export_with_html')->requiresOpenSpout();
205+
206+
it('properly export xls without tags', function (string $component) {
207+
$downloadedFile = livewire($component, ['testStripHtml' => true])
208+
->set('checkboxValues', [
209+
0 => '1',
210+
])
211+
->call('exportToXLS', true)
212+
->assertFileDownloaded('export.xlsx');
213+
214+
$headings = ['nameWithHtml'];
215+
216+
$rows = [
217+
[' Pastel de Nata '],
218+
];
219+
220+
expect($downloadedFile)->toBeXLSDownload($headings, $rows);
221+
})->with('export_with_html')->requiresOpenSpout();
222+
223+
dataset('export_with_html', [
224+
'html' => [$exportWithHtml::class],
225+
]);
226+
131227
/*
132228
|--------------------------------------------------------------------------
133229
| Expectations for this test

0 commit comments

Comments
 (0)