Skip to content

[Yaml] Implement $blockChompingIndicator for TaggedValue multi-line literal blocks #40431

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

Open
wants to merge 5 commits into
base: 7.4
Choose a base branch
from
Open
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
19 changes: 16 additions & 3 deletions src/Symfony/Component/Yaml/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,27 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
if ($value instanceof TaggedValue) {
$output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag());

if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && false !== strpos($value->getValue(), "\n") && false === strpos($value->getValue(), "\r\n")) {
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && false !== strpos($value->getValue(), "\n") && false === strpos($value->getValue(), "\r")) {
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
// http://www.yaml.org/spec/1.2/spec.html#id2793979
$blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';
$output .= sprintf(' |%s', $blockIndentationIndicator);

if (isset($value->getValue()[-2]) && "\n" === $value->getValue()[-2] && "\n" === $value->getValue()[-1]) {
$blockChompingIndicator = '+';
} elseif ("\n" === $value->getValue()[-1]) {
$blockChompingIndicator = '';
} else {
$blockChompingIndicator = '-';
}

$output .= sprintf(' |%s%s', $blockIndentationIndicator, $blockChompingIndicator);

foreach (explode("\n", $value->getValue()) as $row) {
$output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
if ('' === $row) {
$output .= "\n";
} else {
$output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
}
}

continue;
Expand Down
60 changes: 44 additions & 16 deletions src/Symfony/Component/Yaml/Tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,15 +494,15 @@ public function testDumpingMultiLineStringAsScalarBlockTaggedValue()
$data = [
'foo' => new TaggedValue('bar', "foo\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"),
];
$expected = "foo: !bar |\n".
$expected = "foo: !bar |-\n".
" foo\n".
" line with trailing spaces:\n".
" \n".
" bar\n".
" integer like line:\n".
" 123456789\n".
" empty line:\n".
" \n".
"\n".
' baz';

$this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
Expand Down Expand Up @@ -553,6 +553,21 @@ public function testCarriageReturnFollowedByNewlineIsMaintainedWhenDumpingAsMult
$this->assertSame("- \"a\\r\\nb\\nc\"\n", $this->dumper->dump(["a\r\nb\nc"], 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
}

public function testCarriageReturnNotFollowedByNewlineIsPreservedWhenDumpingTaggedValueAsMultiLineLiteralBlock()
{
$expected = <<<'YAML'
parent:
foo: !my-tag "bar\n\rbaz: qux"

YAML;

$this->assertSame($expected, $this->dumper->dump([
'parent' => [
'foo' => new TaggedValue('my-tag', "bar\n\rbaz: qux"),
],
], 4, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
}

public function testCarriageReturnNotFollowedByNewlineIsPreservedWhenDumpingAsMultiLineLiteralBlock()
{
$expected = <<<'YAML'
Expand Down Expand Up @@ -580,38 +595,51 @@ public function testNoExtraTrailingNewlineWhenDumpingAsMultiLineLiteralBlock()
$this->assertSame($data, Yaml::parse($yaml));
}

public function testDumpTrailingNewlineInMultiLineLiteralBlocks()
public function testDumpTrailingNewlineInMultiLineLiteralBlocksForTaggedValues()
{
$data = [
'clip 1' => "one\ntwo\n",
'clip 2' => "one\ntwo\n",
'keep 1' => "one\ntwo\n",
'keep 2' => "one\ntwo\n\n",
'strip 1' => "one\ntwo",
'strip 2' => "one\ntwo",
'clip 1' => new TaggedValue('my-tag', "one\ntwo\n"),
'keep 1' => new TaggedValue('my-tag', "one\ntwo\n\n"),
'strip 1' => new TaggedValue('my-tag', "one\ntwo"),
];
$yaml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);

$expected = <<<YAML
'clip 1': |
'clip 1': !my-tag |
one
two
'keep 1': !my-tag |+
one
two
'clip 2': |

'strip 1': !my-tag |-
one
two
'keep 1': |
YAML;

$this->assertSame($expected, $yaml);
}

public function testDumpTrailingNewlineInMultiLineLiteralBlocks()
{
$data = [
'clip 1' => "one\ntwo\n",
'keep 1' => "one\ntwo\n\n",
'strip 1' => "one\ntwo",
];
$yaml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);

$expected = <<<YAML
'clip 1': |
one
two
'keep 2': |+
'keep 1': |+
one
two

'strip 1': |-
one
two
'strip 2': |-
one
two
YAML;

$this->assertSame($expected, $yaml);
Expand Down