diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 23e199957d277..0ab179d4f7386 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -19,6 +19,8 @@ */ class Parser { + const FOLDED_SCALAR_PATTERN = '(?P\||>)(?P\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P +#.*)?'; + private $offset = 0; private $lines = array(); private $currentLineNb = -1; @@ -304,10 +306,15 @@ private function getNextEmbedBlock($indentation = null) $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem($this->currentLine); - // We are in string block (ie. after a line ending with "|") - $removeComments = !preg_match('~(.*)\|[\s]*$~', $this->currentLine); + // Comments must not be removed inside a string block (ie. after a line ending with "|") + $removeCommentsPattern = '~'.self::FOLDED_SCALAR_PATTERN.'$~'; + $removeComments = !preg_match($removeCommentsPattern, $this->currentLine); while ($this->moveToNextLine()) { + if ($this->getCurrentLineIndentation() === $newIndent) { + $removeComments = !preg_match($removeCommentsPattern, $this->currentLine); + } + if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem($this->currentLine)) { $this->moveToPreviousLine(); break; @@ -389,7 +396,7 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport) return $this->refs[$value]; } - if (preg_match('/^(?P\||>)(?P\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P +#.*)?$/', $value, $matches)) { + if (preg_match('/^'.self::FOLDED_SCALAR_PATTERN.'$/', $value, $matches)) { $modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : ''; return $this->parseFoldedScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), intval(abs($modifiers))); diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index a74bd3ee8703a..c7d3b071e6dbe 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -551,7 +551,7 @@ public function testStringBlockWithComments() )); } - public function testNestedStringBlockWithComments() + public function testFoldedStringBlockWithComments() { $this->assertEquals(array(array('content' => << footer # comment3 +EOF + )); + } + + public function testNestedFoldedStringBlockWithComments() + { + $this->assertEquals(array(array( + 'title' => 'some title', + 'content' => << +

title

+ + +footer # comment3 +EOT + )), Yaml::parse(<< +

title

+ + + footer # comment3 EOF )); }