8000 [YAML] Issue #26065: leading spaces in YAML multi-line string literals · symfony/symfony@aa95663 · GitHub
[go: up one dir, main page]

Skip to content

Commit aa95663

Browse files
tamcnicolas-grekas
authored andcommitted
[YAML] Issue #26065: leading spaces in YAML multi-line string literals
1 parent 717e1c3 commit aa95663

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/Symfony/Component/Yaml/Dumper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
9898

9999
foreach ($input as $key => $value) {
100100
if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r\n")) {
101-
$output .= sprintf("%s%s%s |\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '');
101+
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
102+
// http://www.yaml.org/spec/1.2/spec.html#id2793979
103+
$blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : '';
104+
$output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator);
102105

103106
foreach (preg_split('/\n|\r\n/', $value) as $row) {
104107
$output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,17 @@ public function testDumpMultiLineStringAsScalarBlock()
454454
$this->assertSame(file_get_contents(__DIR__.'/Fixtures/multiple_lines_as_literal_block.yml'), $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
455455
}
456456

457+
public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace()
458+
{
459+
$data = array(
460+
'data' => array(
461+
'multi_line' => " the first line has leading spaces\nThe second line does not.",
462+
),
463+
);
464+
465+
$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));
466+
}
467+
457468
public function testCarriageReturnIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
458469
{
459470
$this->assertSame("- \"a\\r\\nb\\nc\"\n", $this->dumper->dump(array("a\r\nb\nc"), 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
data:
2+
multi_line: |4
3+
the first line has leading spaces
4+
The second line does not.

0 commit comments

Comments
 (0)
0