diff --git a/src/Symfony/Component/Yaml/Dumper.php b/src/Symfony/Component/Yaml/Dumper.php index 5dec10945c4e8..2de07d023142f 100644 --- a/src/Symfony/Component/Yaml/Dumper.php +++ b/src/Symfony/Component/Yaml/Dumper.php @@ -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; diff --git a/src/Symfony/Component/Yaml/Tests/DumperTest.php b/src/Symfony/Component/Yaml/Tests/DumperTest.php index ef449a4dfd166..5bf0530220298 100644 --- a/src/Symfony/Component/Yaml/Tests/DumperTest.php +++ b/src/Symfony/Component/Yaml/Tests/DumperTest.php @@ -567,7 +567,7 @@ public function testCarriageReturnNotFollowedByNewlineIsPreservedWhenDumpingAsMu ], 4, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)); } - public function testNoTrailingNewlineWhenDumpingAsMultiLineLiteralBlock() + public function testNoExtraTrailingNewlineWhenDumpingAsMultiLineLiteralBlock() { $data = [ "a\nb", @@ -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 = <<assertSame($expected, $yaml); + $this->assertSame($data, Yaml::parse($yaml)); + } + public function testZeroIndentationThrowsException() { $this->expectException('InvalidArgumentException'); diff --git a/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block.yml b/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block.yml index 8c43bd475435c..1f61eb1216a52 100644 --- a/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block.yml +++ b/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block.yml @@ -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" } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy