From cfbca4fd8f1c13bd2d25156e885e1771baf27f8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?W=C5=82odzimierz=20Gajda?= Date: Thu, 26 Apr 2012 11:17:07 +0200 Subject: [PATCH] [2.0][Component][Yaml] fix 4022 --- src/Symfony/Component/Yaml/Parser.php | 42 ++++++++++++++++++- .../Tests/Component/Yaml/ParserTest.php | 17 ++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 074306969a202..423093032bca3 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -159,7 +159,7 @@ public function parse($value) // hash } elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) { // if next line is less indented or equal, then it means that the current value is null - if ($this->isNextLineIndented()) { + if ($this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) { $data[$key] = null; } else { $c = $this->getRealCurrentLineNb() + 1; @@ -275,7 +275,12 @@ private function getNextEmbedBlock($indentation = null) if (null === $indentation) { $newIndent = $this->getCurrentLineIndentation(); - if (!$this->isCurrentLineEmpty() && 0 == $newIndent) { + $unindentedEmbedBlock = false; + if (preg_match('/^- .+/', $this->currentLine)) { + $unindentedEmbedBlock = true; + } + + if (!$this->isCurrentLineEmpty() && 0 == $newIndent && !$unindentedEmbedBlock) { throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine); } } else { @@ -553,4 +558,37 @@ private function cleanup($value) return $value; } + + /** + * Returns true if the next line starts unindented collection + * + * @return Boolean Returns true if the next line starts unindented collection, false otherwise + */ + private function isNextLineUnIndentedCollection() + { + $currentIndentation = $this->getCurrentLineIndentation(); + $notEOF = $this->moveToNextLine(); + + while ($notEOF && $this->isCurrentLineEmpty()) { + $notEOF = $this->moveToNextLine(); + } + + if (false === $notEOF) { + return false; + } + + $ret = false; + if ( + $this->getCurrentLineIndentation() == $currentIndentation + && + preg_match('/^- .*/', $this->currentLine) + ) { + $ret = true; + } + + $this->moveToPreviousLine(); + + return $ret; + } + } diff --git a/tests/Symfony/Tests/Component/Yaml/ParserTest.php b/tests/Symfony/Tests/Component/Yaml/ParserTest.php index 6a684ed99f2ef..a497a4a4472aa 100644 --- a/tests/Symfony/Tests/Component/Yaml/ParserTest.php +++ b/tests/Symfony/Tests/Component/Yaml/ParserTest.php @@ -140,6 +140,23 @@ public function testNonUtf8Exception() } } } + + public function testIssue4022() + { + $yaml = << array('item1', 'item2', 'item3')); + + $this->assertEquals($array, $this->parser->parse($yaml)); + } + } class B