diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 55beabea44934..14147a6aeef51 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -295,6 +295,42 @@ public function parse($value, $flags = 0) return $value; } + // try to parse the value as a multi-line string as a last resort + if (0 === $this->currentLineNb) { + $parseError = false; + $previousLineWasNewline = false; + $value = ''; + + foreach ($this->lines as $line) { + try { + $parsedLine = Inline::parse($line, $flags, $this->refs); + + if (!is_string($value)) { + $parseError = true; + break; + } + + if ('' === trim($parsedLine)) { + $value .= "\n"; + $previousLineWasNewline = true; + } elseif ($previousLineWasNewline) { + $value .= trim($parsedLine); + $previousLineWasNewline = false; + } else { + $value .= ' '.trim($parsedLine); + $previousLineWasNewline = false; + } + } catch (ParseException $e) { + $parseError = true; + break; + } + } + + if (!$parseError) { + return trim($value); + } + } + switch (preg_last_error()) { case PREG_INTERNAL_ERROR: $error = 'Internal PCRE error.'; @@ -462,7 +498,7 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false) $previousLineIndentation = $indent; - if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) { + if ($isItUnindentedCollection && !$this->isCurrentLineEmpty() && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) { $this->moveToPreviousLine(); break; } diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index f2498d1b45f95..f16d65c74f8fa 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -1449,6 +1449,32 @@ public function testParseMultiLineUnquotedString() $this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml)); } + + public function testParseMultiLineString() + { + $this->assertEquals("foo bar\nbaz", $this->parser->parse("foo\nbar\n\nbaz")); + } + + public function testParseMultiLineMappingValue() + { + $yaml = <<<'EOF' +foo: +- bar: + one + + two + three +EOF; + $expected = array( + 'foo' => array( + array( + 'bar' => "one\ntwo three", + ), + ), + ); + + $this->assertEquals($expected, $this->parser->parse($yaml)); + } } class B