10000 feature #19304 [Yaml] fix parsing multi-line mapping values (xabbuh) · symfony/symfony@06875e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 06875e0

Browse files
committed
feature #19304 [Yaml] fix parsing multi-line mapping values (xabbuh)
This PR was submitted for the 2.7 branch but it was merged into the 3.2-dev branch instead (closes #19304). Discussion ---------- [Yaml] fix parsing multi-line mapping values | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #11109 | License | MIT | Doc PR | Commits ------- 3954530 [Yaml] fix parsing multi-line mapping values
2 parents 84229f8 + 3954530 commit 06875e0

File tree

10000

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/Symfony/Component/Yaml/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

src/Symfony/Component/Yaml/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)
0