From 566d79c2f838eedc0a261fa73d83ad77d4829052 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Simon?= Date: Sat, 24 Aug 2013 08:35:53 +0200 Subject: [PATCH] [Yaml] fixed embedded folded string parsing --- src/Symfony/Component/Yaml/Parser.php | 13 +++++-- .../Component/Yaml/Tests/ParserTest.php | 34 ++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 23e199957d..0ab179d4f7 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -19,6 +19,8 @@ use Symfony\Component\Yaml\Exception\ParseException; */ 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 @@ class Parser $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 @@ class Parser 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 a74bd3ee87..c7d3b071e6 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -551,7 +551,7 @@ EOF )); } - 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 )); }