do not dump extra trailing newlines for multiline blocks

This commit is contained in:
Christian Flothmann 2020-09-30 09:20:36 +02:00
parent 0ed047f49d
commit 5fa9592d5e
4 changed files with 34 additions and 23 deletions

View File

@ -64,14 +64,18 @@ class Dumper
$dumpAsMap = Inline::isHash($input); $dumpAsMap = Inline::isHash($input);
foreach ($input as $key => $value) { foreach ($input as $key => $value) {
if ('' !== $output && "\n" !== $output[-1]) {
$output .= "\n";
}
if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r")) { if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r")) {
// If the first line starts with a space character, the spec requires a blockIndicationIndicator // If the first line starts with a space character, the spec requires a blockIndicationIndicator
// http://www.yaml.org/spec/1.2/spec.html#id2793979 // http://www.yaml.org/spec/1.2/spec.html#id2793979
$blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : ''; $blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : '';
$output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator); $output .= sprintf('%s%s%s |%s-', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator);
foreach (explode("\n", $value) as $row) { foreach (explode("\n", $value) as $row) {
$output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row); $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
} }
continue; continue;
@ -84,10 +88,10 @@ class Dumper
// If the first line starts with a space character, the spec requires a blockIndicationIndicator // If the first line starts with a space character, the spec requires a blockIndicationIndicator
// http://www.yaml.org/spec/1.2/spec.html#id2793979 // http://www.yaml.org/spec/1.2/spec.html#id2793979
$blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : ''; $blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';
$output .= sprintf(" |%s\n", $blockIndentationIndicator); $output .= sprintf(' |%s', $blockIndentationIndicator);
foreach (explode("\n", $value->getValue()) as $row) { foreach (explode("\n", $value->getValue()) as $row) {
$output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row); $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
} }
continue; continue;

View File

@ -493,19 +493,16 @@ YAML;
$data = [ $data = [
'foo' => new TaggedValue('bar', "foo\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"), 'foo' => new TaggedValue('bar', "foo\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"),
]; ];
$expected = <<<YAML $expected = "foo: !bar |\n".
foo: !bar | " foo\n".
foo " line with trailing spaces:\n".
line with trailing spaces: " \n".
" bar\n".
bar " integer like line:\n".
integer like line: " 123456789\n".
123456789 " empty line:\n".
empty line: " \n".
' baz';
baz
YAML;
$this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)); $this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
} }
@ -545,7 +542,9 @@ YAML;
], ],
]; ];
$this->assertSame(file_get_contents(__DIR__.'/Fixtures/multiple_lines_as_literal_block_leading_space_in_first_line.yml'), $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)); $expected = "data:\n multi_line: |4-\n the first line has leading spaces\n The second line does not.";
$this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
} }
public function testCarriageReturnFollowedByNewlineIsMaintainedWhenDumpingAsMultiLineLiteralBlock() public function testCarriageReturnFollowedByNewlineIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
@ -568,6 +567,18 @@ YAML;
], 4, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)); ], 4, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
} }
public function testNoTrailingNewlineWhenDumpingAsMultiLineLiteralBlock()
{
$data = [
"a\nb",
"c\nd",
];
$yaml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
$this->assertSame("- |-\n a\n b\n- |-\n c\n d", $yaml);
$this->assertSame($data, Yaml::parse($yaml));
}
public function testZeroIndentationThrowsException() public function testZeroIndentationThrowsException()
{ {
$this->expectException('InvalidArgumentException'); $this->expectException('InvalidArgumentException');

View File

@ -1,6 +1,6 @@
data: data:
single_line: 'foo bar baz' single_line: 'foo bar baz'
multi_line: | multi_line: |-
foo foo
line with trailing spaces: line with trailing spaces:

View File

@ -1,4 +0,0 @@
data:
multi_line: |4
the first line has leading spaces
The second line does not.