From cc91856fa65ac107824c90c4f1ad6a6ef2b8f75f Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sat, 5 Dec 2015 10:54:40 +0100 Subject: [PATCH] [Yaml] fix parsing multi-line mapping values --- src/Symfony/Component/Yaml/Parser.php | 23 ++++++++++++++++ .../Component/Yaml/Tests/ParserTest.php | 26 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index dcc9a0150eb76..08df30a54ab0a 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -538,6 +538,29 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $ob } try { + $quotation = '' !== $value && ('"' === $value[0] || "'" === $value[0]) ? $value[0] : null; + + // do not take following lines into account when the current line is a quoted single line value + if (null !== $quotation && preg_match('/^'.$quotation.'.*'.$quotation.'(\s*#.*)?$/', $value)) { + return Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs); + } + + while ($this->moveToNextLine()) { + // unquoted strings end before the first unindented line + if (null === $quotation && $this->getCurrentLineIndentation() === 0) { + $this->moveToPreviousLine(); + + break; + } + + $value .= ' '.trim($this->currentLine); + + // quoted string values end with a line that is terminated with the quotation character + if ('' !== $this->currentLine && substr($this->currentLine, -1) === $quotation) { + break; + } + } + return Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs); } catch (ParseException $e) { $e->setParsedLine($this->getRealCurrentLineNb() + 1); diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index 1b6193a6926ba..70d413ae849a9 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -1127,6 +1127,32 @@ public function parserThrowsExceptionWithCorrectLineNumberProvider() ), ); } + + public function testParseMultiLineQuotedString() + { + $yaml = <<assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml)); + } + + public function testParseMultiLineUnquotedString() + { + $yaml = <<assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml)); + } } class B