Skip to content

Commit bdd66aa

Browse files
committed
bug #36004 [Yaml] fix dumping strings containing CRs (xabbuh)
This PR was merged into the 3.4 branch. Discussion ---------- [Yaml] fix dumping strings containing CRs | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | Commits ------- da78704 [Yaml] fix dumping strings containing CRs
2 parents e44a3f5 + da78704 commit bdd66aa

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
@@ -613,11 +613,26 @@ public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace
613613
$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));
614614
}
615615

616-
public function testCarriageReturnIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
616+
public function testCarriageReturnFollowedByNewlineIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
617617
{
618618
$this->assertSame("- \"a\\r\\nb\\nc\"\n", $this->dumper->dump(["a\r\nb\nc"], 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
619619
}
620620

621+
public function testCarriageReturnNotFollowedByNewlineIsPreservedWhenDumpingAsMultiLineLiteralBlock()
622+
{
623+
$expected = <<<'YAML'
624+
parent:
625+
foo: "bar\n\rbaz: qux"
626+
627+
YAML;
628+
629+
$this->assertSame($expected, $this->dumper->dump([
630+
'parent' => [
631+
'foo' => "bar\n\rbaz: qux",
632+
],
633+
], 4, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
634+
}
635+
621636
public function testZeroIndentationThrowsException()
622637
{
623638
$this->expectException('InvalidArgumentException');

0 commit comments

Comments
 (0)