Skip to content

Commit 7fbb0df

Browse files
committed
[Yaml] fix parsing multi-line mapping values
1 parent 46b44cb commit 7fbb0df

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

Parser.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,29 @@ private function parseValue($value, $flags, $context)
571571
}
572572

573573
try {
574+
$quotation = '' !== $value && ('"' === $value[0] || "'" === $value[0]) ? $value[0] : null;
575+
576+
// do not take following lines into account when the current line is a quoted single line value
577+
if (null !== $quotation && preg_match('/^'.$quotation.'.*'.$quotation.'(\s*#.*)?$/', $value)) {
578+
return Inline::parse($value, $flags, $this->refs);
579+
}
580+
581+
while ($this->moveToNextLine()) {
582+
// unquoted strings end before the first unindented line
583+
if (null === $quotation && $this->getCurrentLineIndentation() === 0) {
584+
$this->moveToPreviousLine();
585+
586+
break;
587+
}
588+
589+
$value .= ' '.trim($this->currentLine);
590+
591+
// quoted string values end with a line that is terminated with the quotation character
592+
if ('' !== $this->currentLine && substr($this->currentLine, -1) === $quotation) {
593+
break;
594+
}
595+
}
596+
574597
Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
575598
$parsedValue = Inline::parse($value, $flags, $this->refs);
576599

Tests/ParserTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,32 @@ public function parserThrowsExceptionWithCorrectLineNumberProvider()
14251425
),
14261426
);
14271427
}
1428+
1429+
public function testParseMultiLineQuotedString()
1430+
{
1431+
$yaml = <<<EOT
1432+
foo: "bar
1433+
baz
1434+
foobar
1435+
foo"
1436+
bar: baz
1437+
EOT;
1438+
1439+
$this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml));
1440+
}
1441+
1442+
public function testParseMultiLineUnquotedString()
1443+
{
1444+
$yaml = <<<EOT
1445+
foo: bar
1446+
baz
1447+
foobar
1448+
foo
1449+
bar: baz
1450+
EOT;
1451+
1452+
$this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml));
1453+
}
14281454
}
14291455

14301456
class B

0 commit comments

Comments
 (0)