From da7870433f0567cde7371d66192103d417e99661 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sat, 7 Mar 2020 12:56:51 +0100 Subject: [PATCH] [Yaml] fix dumping strings containing CRs --- src/Symfony/Component/Yaml/Dumper.php | 2 +- src/Symfony/Component/Yaml/Tests/DumperTest.php | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Yaml/Dumper.php b/src/Symfony/Component/Yaml/Dumper.php index a496dcc88ec2..5daf22e3119f 100644 --- a/src/Symfony/Component/Yaml/Dumper.php +++ b/src/Symfony/Component/Yaml/Dumper.php @@ -99,7 +99,7 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0) $dumpAsMap = Inline::isHash($input); foreach ($input as $key => $value) { - if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r\n")) { + if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r")) { // If the first line starts with a space character, the spec requires a blockIndicationIndicator // http://www.yaml.org/spec/1.2/spec.html#id2793979 $blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : ''; diff --git a/src/Symfony/Component/Yaml/Tests/DumperTest.php b/src/Symfony/Component/Yaml/Tests/DumperTest.php index 1a1ef25a5a4b..074c94942acf 100644 --- a/src/Symfony/Component/Yaml/Tests/DumperTest.php +++ b/src/Symfony/Component/Yaml/Tests/DumperTest.php @@ -580,11 +580,26 @@ public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace $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)); } - public function testCarriageReturnIsMaintainedWhenDumpingAsMultiLineLiteralBlock() + public function testCarriageReturnFollowedByNewlineIsMaintainedWhenDumpingAsMultiLineLiteralBlock() { $this->assertSame("- \"a\\r\\nb\\nc\"\n", $this->dumper->dump(["a\r\nb\nc"], 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)); } + public function testCarriageReturnNotFollowedByNewlineIsPreservedWhenDumpingAsMultiLineLiteralBlock() + { + $expected = <<<'YAML' +parent: + foo: "bar\n\rbaz: qux" + +YAML; + + $this->assertSame($expected, $this->dumper->dump([ + 'parent' => [ + 'foo' => "bar\n\rbaz: qux", + ], + ], 4, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)); + } + public function testZeroIndentationThrowsException() { $this->expectException('InvalidArgumentException');