diff --git a/CHANGELOG.md b/CHANGELOG.md index 8437ccc7..2642a1ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ ## 1.4.1 under development - New #377: Add `Color` field (@samdark) +- Bug #379: Fix newline removal issues in input values (@vjik) +- Enh #379: Improve `Fieldset` field HTML markup (@vjik) ## 1.4.0 March 27, 2025 diff --git a/src/Field/Base/PartsField.php b/src/Field/Base/PartsField.php index 0fd93af6..06f34859 100644 --- a/src/Field/Base/PartsField.php +++ b/src/Field/Base/PartsField.php @@ -485,16 +485,32 @@ private function generateInputContainerEnd(): string return $this->inputContainerTag === null ? '' : ('inputContainerTag . '>'); } + /** + * @psalm-param array $parts + */ private function makeContent(string $template, array $parts): string { if (!empty($this->extraTokens)) { $parts += $this->extraTokens; } + $emptyParts = []; + $nonEmptyParts = []; + foreach ($parts as $key => $value) { + $value = trim((string) $value); + if ($value === '') { + $emptyParts[$key] = ''; + continue; + } + $nonEmptyParts[$key] = $value; + } + /** - * @var string We use correct regular expression, so `preg_replace` will return string. + * @var string $result We use correct regular expression, so `preg_replace` will return string. */ - return preg_replace('/^\h*\v+/m', '', trim(strtr($template, $parts))); + $result = preg_replace('/^\h*\v+/m', '', trim(strtr($template, $emptyParts))); + + return strtr($result, $nonEmptyParts); } private function generateLabel(): string diff --git a/src/Field/Fieldset.php b/src/Field/Fieldset.php index 78123f41..c42e3f8c 100644 --- a/src/Field/Fieldset.php +++ b/src/Field/Fieldset.php @@ -75,9 +75,7 @@ public function name(?string $name): self protected function generateInput(): string { - return $this->generateBeginInput() - . "\n" - . $this->generateEndInput(); + return $this->generateBeginInput() . $this->generateEndInput(); } protected function generateBeginInput(): string @@ -89,7 +87,7 @@ protected function generateEndInput(): string { $content = $this->renderContent(); - return ($content !== '' ? $content . "\n" : '') + return ($content !== '' ? "\n" . $content . "\n" : '') . $this->tag->close(); } } diff --git a/tests/Field/Base/PartsFieldTest.php b/tests/Field/Base/PartsFieldTest.php index d0f4132f..d5dd2d36 100644 --- a/tests/Field/Base/PartsFieldTest.php +++ b/tests/Field/Base/PartsFieldTest.php @@ -831,4 +831,26 @@ public function testFullInput(): void $field->render() ); } + + public function testTokenTrim(): void + { + $result = StubPartsField::widget() + ->tokens([ + '{before}' => '
', + '{after}' => '
', + ]) + ->token('{icon}', " \n\n") + ->template("{before}\n{input}\n{icon}\n{after}") + ->render(); + + $expected = << +
+ +
+ + HTML; + + $this->assertSame($expected, $result); + } } diff --git a/tests/Field/FieldsetTest.php b/tests/Field/FieldsetTest.php index 5c6624af..5219179e 100644 --- a/tests/Field/FieldsetTest.php +++ b/tests/Field/FieldsetTest.php @@ -106,8 +106,7 @@ public function testDisabled(): void $expected = << -
-
+
HTML; @@ -122,8 +121,7 @@ public function testForm(): void $expected = << -
-
+
HTML; @@ -138,8 +136,7 @@ public function testName(): void $expected = << -
-
+
HTML; diff --git a/tests/Field/TextareaTest.php b/tests/Field/TextareaTest.php index ed4ee05b..7b25ee57 100644 --- a/tests/Field/TextareaTest.php +++ b/tests/Field/TextareaTest.php @@ -79,6 +79,23 @@ public static function dataBase(): array HTML, new InputData(name: 'desc', placeholder: 'test'), ], + 'value-with-newlines' => [ + << + + + HTML, + new InputData('desc', "\n\n\none\n\n\ntwo\n\n\n"), + ], ]; } diff --git a/tests/PureField/FieldFactoryTest.php b/tests/PureField/FieldFactoryTest.php index cc0f998c..f500de0d 100644 --- a/tests/PureField/FieldFactoryTest.php +++ b/tests/PureField/FieldFactoryTest.php @@ -342,8 +342,7 @@ public function testFieldset(): void $expected = << -
-
+
HTML; @@ -362,8 +361,7 @@ public function testFieldsetWithTheme(): void $expected = << -
-
+
HTML; diff --git a/tests/PureField/FieldTest.php b/tests/PureField/FieldTest.php index 663015bd..9b47d9a3 100644 --- a/tests/PureField/FieldTest.php +++ b/tests/PureField/FieldTest.php @@ -343,8 +343,7 @@ public function testFieldset(): void $expected = << -
-
+
HTML; @@ -363,8 +362,7 @@ public function testFieldsetWithTheme(): void $expected = << -
-
+
HTML; diff --git a/tests/Theme/ThemeTest.php b/tests/Theme/ThemeTest.php index 1c8ca4ad..42535299 100644 --- a/tests/Theme/ThemeTest.php +++ b/tests/Theme/ThemeTest.php @@ -586,29 +586,20 @@ public function testTextWithValidationRulesEnricher(): void $this->assertSame($expectedHtml, $actualHtml); } - public static function dataFieldSet(): array + public function testEmptyFieldset(): void { - return [ - 'empty' => [ - << -
-
- - HTML, - [], - ], - ]; - } - - #[DataProvider('dataFieldSet')] - public function testFieldSet(string $expected, array $factoryParameters): void - { - $this->initializeThemeContainer($factoryParameters); + $this->initializeThemeContainer(); $result = Fieldset::widget()->render(); - $this->assertSame($expected, $result); + $this->assertSame( + << +
+ + HTML, + $result, + ); } public function testFieldSetWithOverrideTemplateBeginAndTemplateEnd(): void