Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Yaml] keep trailing newlines when dumping multi-line strings #39683

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

Merged
merged 1 commit into from
Jan 3, 2021
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
17 changes: 15 additions & 2 deletions 17 src/Symfony/Component/Yaml/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,23 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
// 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, 0, 1)) ? (string) $this->indentation : '';
$output .= sprintf('%s%s%s |%s-', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator);

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

$output .= sprintf('%s%s%s |%s%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator, $blockChompingIndicator);

foreach (explode("\n", $value) 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
40 changes: 39 additions & 1 deletion 40 src/Symfony/Component/Yaml/Tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ public function testCarriageReturnNotFollowedByNewlineIsPreservedWhenDumpingAsMu
], 4, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
}

public function testNoTrailingNewlineWhenDumpingAsMultiLineLiteralBlock()
public function testNoExtraTrailingNewlineWhenDumpingAsMultiLineLiteralBlock()
{
$data = [
"a\nb",
Expand All @@ -579,6 +579,44 @@ public function testNoTrailingNewlineWhenDumpingAsMultiLineLiteralBlock()
$this->assertSame($data, Yaml::parse($yaml));
}

public function testDumpTrailingNewlineInMultiLineLiteralBlocks()
{
$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",
];
$yaml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);

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

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

$this->assertSame($expected, $yaml);
$this->assertSame($data, Yaml::parse($yaml));
}

public function testZeroIndentationThrowsException()
{
$this->expectException('InvalidArgumentException');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ data:
integer like line:
123456789
empty line:

baz
multi_line_with_carriage_return: "foo\nbar\r\nbaz"
nested_inlined_multi_line_string: { inlined_multi_line: "foo\nbar\r\nempty line:\n\nbaz" }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.