8000 [Yaml] fix dumping strings containing CRs · symfony/symfony@e5ae4be · GitHub
[go: up one dir, main page]

Skip to content

Commit e5ae4be

Browse files
committed
[Yaml] fix dumping strings containing CRs
1 parent 4463791 commit e5ae4be

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/Symfony/Component/Yaml/Dumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
9999
$dumpAsMap = Inline::isHash($input);
100100

101101
foreach ($input as $key => $value) {
102-
if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r\n")) {
102+
if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r")) {
103103
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
104104
// http://www.yaml.org/spec/1.2/spec.html#id2793979
105105
$blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : '';

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,11 +580,26 @@ public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace
580580
$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));
581581
}
582582

583-
public function testCarriageReturnIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
583+
public function testCarriageReturnFollowedByNewlineIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
584584
{
585585
$this->assertSame("- \"a\\r\\nb\\nc\"\n", $this->dumper->dump(["a\r\nb\nc"], 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
586586
}
587587

588+
public function testCarriageReturnNotFollowedByNewlineIsPreservedWhenDumpingAsMultiLineLiteralBlock()
589+
{
590+
$expected = <<<'YAML'
591+
parent:
592+
foo: "bar\n\rbaz: qux"
593+
594+
YAML;
595+
596+
$this->assertSame($expected, $this->dumper->dump([
597+
'parent' => [
598+
'foo' => "bar\n\rbaz: qux"
599+
],
600+
], 4, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
601+
}
602+
588603
public function testZeroIndentationThrowsException()
589604
{
590605
$this->expectException('InvalidArgumentException');

0 commit comments

Comments
 (0)
0