fix: drop blank lines and split on any line break in nl2list
This commit is contained in:
1 parent
9ba6529ee0
commit
eda7856e48
2 files changed
+40
-1
No files matched your search
@@ -214,7 +214,8 @@ class AppRuntime implements RuntimeExtensionInterface
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$items = explode(PHP_EOL, $content);
|
// any line break, as textareas submit \r\n; blank lines (the default benefits end with one) are no items
|
||||||
|
$items = array_filter(array_map('trim', preg_split('/\R/', $content)), fn (string $item) => '' !== $item);
|
||||||
|
|
||||||
$list = '<ul class="'.$class.'">';
|
$list = '<ul class="'.$class.'">';
|
||||||
foreach ($items as $item) {
|
foreach ($items as $item) {
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Tests\Twig;
|
||||||
|
|
||||||
|
use App\Twig\AppRuntime;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default benefits of an assignment end with a line break, and textareas submit \r\n: neither
|
||||||
|
* may turn into an empty bullet or a stray carriage return.
|
||||||
|
*/
|
||||||
|
class Nl2ListTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testTrailingAndBlankLinesAreNoItems(): void
|
||||||
|
{
|
||||||
|
$this->assertSame(
|
||||||
|
'<ul class="list-disc pl-4"><li>Unterkunft</li><li>Skipass</li></ul>',
|
||||||
|
$this->createRuntime()->nl2List("Unterkunft\n\nSkipass\n")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWindowsLineBreaksSplitCleanly(): void
|
||||||
|
{
|
||||||
|
$this->assertSame(
|
||||||
|
'<ul class="list-disc pl-4"><li>Anreise im E&P Reisebus</li><li>Verpflegung</li></ul>',
|
||||||
|
$this->createRuntime()->nl2List("Anreise im E&P Reisebus\r\nVerpflegung\r\n")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createRuntime(): AppRuntime
|
||||||
|
{
|
||||||
|
$reflection = new \ReflectionClass(AppRuntime::class);
|
||||||
|
|
||||||
|
return $reflection->newInstanceWithoutConstructor();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user