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

Commit 9667fc5

Browse filesBrowse files
committed
do not dump extra trailing newlines for multiline blocks
1 parent 0ed047f commit 9667fc5
Copy full SHA for 9667fc5

File tree

4 files changed

+34
-23
lines changed
Filter options

4 files changed

+34
-23
lines changed

‎src/Symfony/Component/Yaml/Dumper.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Dumper.php
+8-4Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,18 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
6464
$dumpAsMap = Inline::isHash($input);
6565

6666
foreach ($input as $key => $value) {
67+
if ('' !== $output && "\n" !== $output[-1]) {
68+
$output .= "\n";
69+
}
70+
6771
if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r")) {
6872
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
6973
// http://www.yaml.org/spec/1.2/spec.html#id2793979
7074
$blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : '';
71-
$output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator);
75+
$output .= sprintf('%s%s%s |%s-', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator);
7276

7377
foreach (explode("\n", $value) as $row) {
74-
$output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
78+
$output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
7579
}
7680

7781
continue;
@@ -84,10 +88,10 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
8488
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
8589
// http://www.yaml.org/spec/1.2/spec.html#id2793979
8690
$blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';
87-
$output .= sprintf(" |%s\n", $blockIndentationIndicator);
91+
$output .= sprintf(' |%s', $blockIndentationIndicator);
8892

8993
foreach (explode("\n", $value->getValue()) as $row) {
90-
$output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
94+
$output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
9195
}
9296

9397
continue;

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/DumperTest.php
+25-14Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -493,19 +493,16 @@ public function testDumpingMultiLineStringAsScalarBlockTaggedValue()
493493
$data = [
494494
'foo' => new TaggedValue('bar', "foo\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"),
495495
];
496-
$expected = <<<YAML
497-
foo: !bar |
498-
foo
499-
line with trailing spaces:
500-
501-
bar
502-
integer like line:
503-
123456789
504-
empty line:
505-
506-
baz
507-
508-
YAML;
496+
$expected = "foo: !bar |\n".
497+
" foo\n".
498+
" line with trailing spaces:\n".
499+
" \n".
500+
" bar\n".
501+
" integer like line:\n".
502+
" 123456789\n".
503+
" empty line:\n".
504+
" \n".
505+
" baz";
509506

510507
$this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
511508
}
@@ -545,7 +542,9 @@ public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace
545542
],
546543
];
547544

548-
$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));
545+
$expected = "data:\n multi_line: |4-\n the first line has leading spaces\n The second line does not.";
546+
547+
$this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
549548
}
550549

551550
public function testCarriageReturnFollowedByNewlineIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
@@ -568,6 +567,18 @@ public function testCarriageReturnNotFollowedByNewlineIsPreservedWhenDumpingAsMu
568567
], 4, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
569568
}
570569

570+
public function testNoTrailingNewlineWhenDumpingAsMultiLineLiteralBlock()
571+
{
572+
$data = [
573+
"a\nb",
574+
"c\nd",
575+
];
576+
$yaml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
577+
578+
$this->assertSame("- |-\n a\n b\n- |-\n c\n d", $yaml);
579+
$this->assertSame($data, Yaml::parse($yaml));
580+
}
581+
571582
public function testZeroIndentationThrowsException()
572583
{
573584
$this->expectException('InvalidArgumentException');

‎src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block.yml

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block.yml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
data:
22
single_line: 'foo bar baz'
3-
multi_line: |
3+
multi_line: |-
44
foo
55
line with trailing spaces:
66

‎src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block_leading_space_in_first_line.yml

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block_leading_space_in_first_line.yml
-4Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.