From 510fdea5990b06aa9b4aa0b7035966ee5d42686d Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 2 Oct 2015 12:12:23 +0200 Subject: [PATCH 1/2] Revert "bug #15860 [Yaml] Fix improper comments removal (ogizanagi)" This reverts commit 150f52f3aa8eab56c15d2e830660e85f387ce533, reversing changes made to 74af02a7711409dff3b0d2aafda4102f86ca8700. --- src/Symfony/Component/Yaml/Parser.php | 12 ++++++ .../Component/Yaml/Tests/ParserTest.php | 37 ------------------- 2 files changed, 12 insertions(+), 37 deletions(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index ab023ff862..9dffa27811 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -339,9 +339,17 @@ class Parser $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem($this->currentLine); + // Comments must not be removed inside a block scalar + $removeCommentsPattern = '~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~'; + $removeComments = !preg_match($removeCommentsPattern, $this->currentLine); + while ($this->moveToNextLine()) { $indent = $this->getCurrentLineIndentation(); + if ($indent === $newIndent) { + $removeComments = !preg_match($removeCommentsPattern, $this->currentLine); + } + if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem($this->currentLine) && $newIndent === $indent) { $this->moveToPreviousLine(); break; @@ -352,6 +360,10 @@ class Parser continue; } + if ($removeComments && $this->isCurrentLineComment()) { + continue; + } + if ($indent >= $newIndent) { $data[] = substr($this->currentLine, $newIndent); } elseif (0 == $indent) { diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index 21fdd1bf70..3054c807fd 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -617,43 +617,6 @@ EOF )); } - public function testSecondLevelFoldedStringBlockWithComments() - { - $this->assertEquals(array( - 'pages' => array( - array( - 'title' => 'some title', - 'content' => << -

title

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

title

- - - footer # comment3 -EOF - )); - } - public function testNestedFoldedStringBlockWithComments() { $this->assertEquals(array(array( From 7b9d519b0f7144e5052ef3f5a3656ecb59732655 Mon Sep 17 00:00:00 2001 From: "maxime.steinhausser" Date: Fri, 2 Oct 2015 13:13:24 +0200 Subject: [PATCH 2/2] [Yaml] Add regression test for comments indents --- .../Component/Yaml/Tests/ParserTest.php | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index 3054c807fd..c88267a649 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -560,6 +560,32 @@ EOF; $this->assertEquals(array('hash' => null), Yaml::parse($input)); } + public function testCommentAtTheRootIndent() + { + $this->assertEquals(array( + 'services' => array( + 'app.foo_service' => array( + 'class' => 'Foo', + ), + 'app/bar_service' => array( + 'class' => 'Bar', + ), + ), + ), Yaml::parse(<<assertEquals(array('content' => <<