diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 52dedfe07851a..14d3d3b1ea141 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -303,7 +303,11 @@ private function getCurrentLineIndentation() private function getNextEmbedBlock($indentation = null, $inSequence = false) { $oldLineIndentation = $this->getCurrentLineIndentation(); - $insideBlockScalar = $this->isBlockScalarHeader(); + $blockScalarIndentations = array(); + + if ($this->isBlockScalarHeader()) { + $blockScalarIndentations[] = $this->getCurrentLineIndentation(); + } if (!$this->moveToNextLine()) { return; @@ -340,8 +344,8 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false) $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem(); - if (!$insideBlockScalar) { - $insideBlockScalar = $this->isBlockScalarHeader(); + if (empty($blockScalarIndentations) && $this->isBlockScalarHeader()) { + $blockScalarIndentations[] = $this->getCurrentLineIndentation(); } $previousLineIndentation = $this->getCurrentLineIndentation(); @@ -349,8 +353,17 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false) while ($this->moveToNextLine()) { $indent = $this->getCurrentLineIndentation(); - if (!$insideBlockScalar && $indent === $previousLineIndentation) { - $insideBlockScalar = $this->isBlockScalarHeader(); + // terminate all block scalars that are more indented than the current line + if (!empty($blockScalarIndentations) && $indent < $previousLineIndentation && trim($this->currentLine) !== '') { + foreach ($blockScalarIndentations as $key => $blockScalarIndentation) { + if ($blockScalarIndentation >= $this->getCurrentLineIndentation()) { + unset($blockScalarIndentations[$key]); + } + } + } + + if (empty($blockScalarIndentations) && $this->isBlockScalarHeader()) { + $blockScalarIndentations[] = $this->getCurrentLineIndentation(); } $previousLineIndentation = $indent; @@ -366,7 +379,7 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false) } // we ignore "comment" lines only when we are not inside a scalar block - if (!$insideBlockScalar && $this->isCurrentLineComment()) { + if (empty($blockScalarIndentations) && $this->isCurrentLineComment()) { continue; } @@ -523,7 +536,7 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0) $previousLineIndented = false; $previousLineBlank = false; - for ($i = 0; $i < count($blockLines); $i++) { + for ($i = 0; $i < count($blockLines); ++$i) { if ('' === $blockLines[$i]) { $text .= "\n"; $previousLineIndented = false; diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index 7a1485eb2dd83..bc64cdd55a2c4 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -737,7 +737,9 @@ public function testCommentLikeStringsAreNotStrippedInBlockScalars($yaml, $expec public function getCommentLikeStringInScalarBlockData() { - $yaml1 = <<<'EOT' + $tests = array(); + + $yaml = <<<'EOT' pages: - title: some title @@ -752,7 +754,7 @@ public function getCommentLikeStringInScalarBlockData() footer # comment3 EOT; - $expected1 = array( + $expected = array( 'pages' => array( array( 'title' => 'some title', @@ -771,8 +773,9 @@ public function getCommentLikeStringInScalarBlockData() ), ), ); + $tests[] = array($yaml, $expected); - $yaml2 = <<<'EOT' + $yaml = <<<'EOT' test: | foo # bar @@ -787,7 +790,7 @@ public function getCommentLikeStringInScalarBlockData() # bar baz EOT; - $expected2 = array( + $expected = array( 'test' => <<<'EOT' foo # bar @@ -814,11 +817,31 @@ public function getCommentLikeStringInScalarBlockData() ), ), ); + $tests[] = array($yaml, $expected); - return array( - array($yaml1, $expected1), - array($yaml2, $expected2), + $yaml = << + line1 + line2> + baz: +# comment + foobar: ~ +EOT; + $expected = array( + 'foo' => array( + 'bar' => array( + 'scalar-block' => 'line1 line2>', + ), + 'baz' => array( + 'foobar' => null, + ), + ), ); + $tests[] = array($yaml, $expected); + + return $tests; } public function testBlankLinesAreParsedAsNewLinesInFoldedBlocks()