Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 18 additions & 2 deletions src/Field/Base/PartsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,16 +485,32 @@ private function generateInputContainerEnd(): string
return $this->inputContainerTag === null ? '' : ('</' . $this->inputContainerTag . '>');
}

/**
* @psalm-param array<non-empty-string, string> $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
Expand Down
6 changes: 2 additions & 4 deletions src/Field/Fieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -89,7 +87,7 @@ protected function generateEndInput(): string
{
$content = $this->renderContent();

return ($content !== '' ? $content . "\n" : '')
return ($content !== '' ? "\n" . $content . "\n" : '')
. $this->tag->close();
}
}
22 changes: 22 additions & 0 deletions tests/Field/Base/PartsFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -831,4 +831,26 @@ public function testFullInput(): void
$field->render()
);
}

public function testTokenTrim(): void
{
$result = StubPartsField::widget()
->tokens([
'{before}' => '<section>',
'{after}' => '</section>',
])
->token('{icon}', " <span class=\"icon\"></span>\n\n")
->template("{before}\n{input}\n{icon}\n{after}")
->render();

$expected = <<<HTML
<div>
<section>
<span class="icon"></span>
</section>
</div>
HTML;

$this->assertSame($expected, $result);
}
}
9 changes: 3 additions & 6 deletions tests/Field/FieldsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ public function testDisabled(): void

$expected = <<<HTML
<div>
<fieldset disabled>
</fieldset>
<fieldset disabled></fieldset>
</div>
HTML;

Expand All @@ -122,8 +121,7 @@ public function testForm(): void

$expected = <<<HTML
<div>
<fieldset form="CreatePost">
</fieldset>
<fieldset form="CreatePost"></fieldset>
</div>
HTML;

Expand All @@ -138,8 +136,7 @@ public function testName(): void

$expected = <<<HTML
<div>
<fieldset name="test">
</fieldset>
<fieldset name="test"></fieldset>
</div>
HTML;

Expand Down
17 changes: 17 additions & 0 deletions tests/Field/TextareaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ public static function dataBase(): array
HTML,
new InputData(name: 'desc', placeholder: 'test'),
],
'value-with-newlines' => [
<<<HTML
<div>
<textarea name="desc">


one


two


</textarea>
</div>
HTML,
new InputData('desc', "\n\n\none\n\n\ntwo\n\n\n"),
],
];
}

Expand Down
6 changes: 2 additions & 4 deletions tests/PureField/FieldFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,7 @@ public function testFieldset(): void

$expected = <<<HTML
<div>
<fieldset>
</fieldset>
<fieldset></fieldset>
</div>
HTML;

Expand All @@ -362,8 +361,7 @@ public function testFieldsetWithTheme(): void

$expected = <<<HTML
<span>
<fieldset>
</fieldset>
<fieldset></fieldset>
</span>
HTML;

Expand Down
6 changes: 2 additions & 4 deletions tests/PureField/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,7 @@ public function testFieldset(): void

$expected = <<<HTML
<div>
<fieldset>
</fieldset>
<fieldset></fieldset>
</div>
HTML;

Expand All @@ -363,8 +362,7 @@ public function testFieldsetWithTheme(): void

$expected = <<<HTML
<span>
<fieldset>
</fieldset>
<fieldset></fieldset>
</span>
HTML;

Expand Down
29 changes: 10 additions & 19 deletions tests/Theme/ThemeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,29 +586,20 @@ public function testTextWithValidationRulesEnricher(): void
$this->assertSame($expectedHtml, $actualHtml);
}

public static function dataFieldSet(): array
public function testEmptyFieldset(): void
{
return [
'empty' => [
<<<HTML
<div>
<fieldset>
</fieldset>
</div>
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
<div>
<fieldset></fieldset>
</div>
HTML,
$result,
);
}

public function testFieldSetWithOverrideTemplateBeginAndTemplateEnd(): void
Expand Down