8000 bug #39683 [Yaml] keep trailing newlines when dumping multi-line stri… · symfony/symfony@a902dd8 · GitHub
[go: up one dir, main page]

Skip to content

Commit a902dd8

Browse files
committed
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 #39668 (comment) | License | MIT | Doc PR | Commits ------- 4c513c2 keep trailing newlines when dumping multi-line strings
2 parents da07550 + 4c513c2 commit a902dd8

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

src/Symfony/Component/Yaml/Dumper.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,23 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
7272
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
7373
// http://www.yaml.org/spec/1.2/spec.html#id2793979
7474
$blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : '';
75-
$output .= sprintf('%s%s%s |%s-', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator);
75+
76+
if (isset($value[-2]) && "\n" === $value[-2] && "\n" === $value[-1]) {
77+
$blockChompingIndicator = '+';
78+
} elseif ("\n" === $value[-1]) {
79+
$blockChompingIndicator = '';
80+
} else {
81+
$blockChompingIndicator = '-';
82+
}
83+
84+
$output .= sprintf('%s%s%s |%s%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator, $blockChompingIndicator);
7685

7786
foreach (explode("\n", $value) as $row) {
78-
$output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
87+
if ('' === $row) {
88+
$output .= "\n";
89+
} else {
90+
$output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
91+
}
7992
}
8093

8194
continue;

src/Symfony/Component/Yaml/Tests/DumperTest.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ public function testCarriageReturnNotFollowedByNewlineIsPreservedWhenDumpingAsMu
567567
], 4, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
568568
}
569569

570-
public function testNoTrailingNewlineWhenDumpingAsMultiLineLiteralBlock()
570+
public function testNoExtraTrailingNewlineWhenDumpingAsMultiLineLiteralBlock()
571571
{
572572
$data = [
573573
"a\nb",
@@ -579,6 +579,44 @@ public function testNoTrailingNewlineWhenDumpingAsMultiLineLiteralBlock()
579579
$this->assertSame($data, Yaml::parse($yaml));
580580
}
581581

582+
public function testDumpTrailingNewlineInMultiLineLiteralBlocks()
583+
{
584+
$data = [
585+
'clip 1' => "one\ntwo\n",
586+
'clip 2' => "one\ntwo\n",
587+
'keep 1' => "one\ntwo\n",
588+
'keep 2' => "one\ntwo\n\n",
589+
'strip 1' => "one\ntwo",
590+
'strip 2' => "one\ntwo",
591+
];
592+
$yaml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
593+
594+
$expected = <<<YAML
595+
'clip 1': |
596+
one
597+
two
598+
'clip 2': |
599+
one
600+
two
601+
'keep 1': |
602+
one
603+
two
604+
'keep 2': |+
605+
one
606+
two
607+
608+
'strip 1': |-
609+
one
610+
two
611+
'strip 2': |-
612+
one
613+
two
614+
YAML;
615+
616+
$this->assertSame($expected, $yaml);
617+
$this->assertSame($data, Yaml::parse($yaml));
618+
}
619+
582620
public function testZeroIndentationThrowsException()
583621
{
584622
$this->expectException('InvalidArgumentException');

src/Symfony/Component/Yaml/Tests/ 67E6 Fixtures/multiple_lines_as_literal_block.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ data:
88
integer like line:
99
123456789
1010
empty line:
11-
11+
1212
baz
1313
multi_line_with_carriage_return: "foo\nbar\r\nbaz"
1414
nested_inlined_multi_line_string: { inlined_multi_line: "foo\nbar\r\nempty line:\n\nbaz" }

0 commit comments

Comments
 (0)
0