diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 01c6687f4af8..2771f51fd29e 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -96,7 +96,6 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport = $data[] = $parser->parse($this->getNextEmbedBlock(null, true), $exceptionOnInvalidType, $objectSupport); } else { if (isset($values['leadspaces']) - && ' ' == $values['leadspaces'] && preg_match('#^(?P'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P.+?))?\s*$#u', $values['value'], $matches) ) { // this is a compact notation element, add to next block and parse @@ -106,7 +105,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport = $block = $values['value']; if ($this->isNextLineIndented()) { - $block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + 2); + $block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + strlen($values['leadspaces']) + 1); } $data[] = $parser->parse($block, $exceptionOnInvalidType, $objectSupport); @@ -313,7 +312,14 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false) $newIndent = $indentation; } - $data = array(substr($this->currentLine, $newIndent)); + $data = array(); + if ($this->getCurrentLineIndentation() >= $newIndent) { + $data[] = substr($this->currentLine, $newIndent); + } else { + $this->moveToPreviousLine(); + + return; + } if ($inSequence && $oldLineIndentation === $newIndent && '-' === $data[0][0]) { // the previous line contained a dash but no item content, this line is a sequence item with the same indentation @@ -336,7 +342,7 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false) $removeComments = !preg_match($removeCommentsPattern, $this->currentLine); } - if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem($this->currentLine)) { + if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem($this->currentLine) && $newIndent === $indent) { $this->moveToPreviousLine(); break; } diff --git a/src/Symfony/Component/Yaml/Tests/Fixtures/unindentedCollections.yml b/src/Symfony/Component/Yaml/Tests/Fixtures/unindentedCollections.yml index fd8ad7ed448d..0c96108e9911 100644 --- a/src/Symfony/Component/Yaml/Tests/Fixtures/unindentedCollections.yml +++ b/src/Symfony/Component/Yaml/Tests/Fixtures/unindentedCollections.yml @@ -60,3 +60,23 @@ yaml: | foo: bar php: | array('collection' => array('key' => array('a', 'b', 'c'), 'foo' => 'bar')) +--- +test: Shortcut Key after unindented collection +brief: > + Key/value after unindented collection +yaml: | + collection: + - key: foo + foo: bar +php: | + array('collection' => array(array('key' => 'foo', 'foo' => 'bar'))) +--- +test: Shortcut Key after unindented collection with custom spaces +brief: > + Key/value after unindented collection +yaml: | + collection: + - key: foo + foo: bar +php: | + array('collection' => array(array('key' => 'foo', 'foo' => 'bar'))) diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index 5bda9c3be5ef..aca190ac8b16 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -475,6 +475,22 @@ public function testUnindentedCollectionException() -item2 -item3 +EOF; + + $this->parser->parse($yaml); + } + + /** + * @expectedException \Symfony\Component\Yaml\Exception\ParseException + */ + public function testShortcutKeyUnindentedCollectionException() + { + $yaml = <<parser->parse($yaml);