bug #39683 [Yaml] keep trailing newlines when dumping multi-line strings (xabbuh)
This PR was merged into the 4.4 branch.
Discussion
----------
[Yaml] keep trailing newlines when dumping multi-line strings
| Q | A
| ------------- | ---
| Branch? | 4.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | Fix https://github.com/symfony/symfony/pull/39668#issuecomment-753484545
| License | MIT
| Doc PR |
Commits
-------
4c513c24c7
keep trailing newlines when dumping multi-line strings
This commit is contained in:
commit
a902dd8a1b
@ -72,10 +72,23 @@ 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, 0, 1)) ? (string) $this->indentation : '';
|
$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) {
|
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;
|
continue;
|
||||||
|
@ -567,7 +567,7 @@ YAML;
|
|||||||
], 4, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
|
], 4, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNoTrailingNewlineWhenDumpingAsMultiLineLiteralBlock()
|
public function testNoExtraTrailingNewlineWhenDumpingAsMultiLineLiteralBlock()
|
||||||
{
|
{
|
||||||
$data = [
|
$data = [
|
||||||
"a\nb",
|
"a\nb",
|
||||||
@ -579,6 +579,44 @@ YAML;
|
|||||||
$this->assertSame($data, Yaml::parse($yaml));
|
$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()
|
public function testZeroIndentationThrowsException()
|
||||||
{
|
{
|
||||||
$this->expectException('InvalidArgumentException');
|
$this->expectException('InvalidArgumentException');
|
||||||
|
@ -8,7 +8,7 @@ data:
|
|||||||
integer like line:
|
integer like line:
|
||||||
123456789
|
123456789
|
||||||
empty line:
|
empty line:
|
||||||
|
|
||||||
baz
|
baz
|
||||||
multi_line_with_carriage_return: "foo\nbar\r\nbaz"
|
multi_line_with_carriage_return: "foo\nbar\r\nbaz"
|
||||||
nested_inlined_multi_line_string: { inlined_multi_line: "foo\nbar\r\nempty line:\n\nbaz" }
|
nested_inlined_multi_line_string: { inlined_multi_line: "foo\nbar\r\nempty line:\n\nbaz" }
|
||||||
|
Reference in New Issue
Block a user