Skip to content

Commit

Permalink
[Yaml] fix dumping strings containing CRs
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Mar 7, 2020
1 parent 4463791 commit 4e55c34
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Yaml/Dumper.php
Expand Up @@ -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 : '';
Expand Down
17 changes: 16 additions & 1 deletion src/Symfony/Component/Yaml/Tests/DumperTest.php
Expand Up @@ -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');
Expand Down

0 comments on commit 4e55c34

Please sign in to comment.