Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c72fcc

Browse files
committed
[Yaml] do not remove "comments" in scalar blocks
Inside scalar blocks, lines starting with a `#` character must be treated like every other strings and must not be ignored as comments.
1 parent 2d14689 commit 3c72fcc

File tree

2 files changed

+116
-6
lines changed

2 files changed

+116
-6
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ private function getCurrentLineIndentation()
303303
private function getNextEmbedBlock($indentation = null, $inSequence = false)
304304
{
305305
$oldLineIndentation = $this->getCurrentLineIndentation();
306+
$insideBlockScalar = $this->isBlockScalarHeader();
306307

307308
if (!$this->moveToNextLine()) {
308309
return;
@@ -339,17 +340,21 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
339340

340341
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
341342

342-
// Comments must not be removed inside a block scalar
343-
$removeCommentsPattern = '~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~';
344-
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
343+
if (!$insideBlockScalar) {
344+
$insideBlockScalar = $this->isBlockScalarHeader();
345+
}
346+
347+
$previousLineIndentation = $this->getCurrentLineIndentation();
345348

346349
while ($this->moveToNextLine()) {
347350
$indent = $this->getCurrentLineIndentation();
348351

349-
if ($indent === $newIndent) {
350-
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
352+
if (!$insideBlockScalar && $indent === $previousLineIndentation) {
353+
$insideBlockScalar = $this->isBlockScalarHeader();
351354
}
352355

356+
$previousLineIndentation = $indent;
357+
353358
if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
354359
$this->moveToPreviousLine();
355360
break;
@@ -360,7 +365,8 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
360365
continue;
361366
}
362367

363-
if ($removeComments && $this->isCurrentLineComment()) {
368+
// we ignore "comment" lines only when we are not inside a scalar block
369+
if (!$insideBlockScalar && $this->isCurrentLineComment()) {
364370
continue;
365371
}
366372

@@ -672,4 +678,14 @@ private function isStringUnIndentedCollectionItem()
672678
{
673679
return 0 === strpos($this->currentLine, '- ');
674680
}
681+
682+
/**
683+
* Tests whether or not the current line is the header of a block scalar.
684+
*
685+
* @return bool
686+
*/
687+
private function isBlockScalarHeader()
688+
{
689+
return (bool) preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine);
690+
}
675691
}

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,100 @@ public function testFloatKeys()
726726

727727
$this->assertEquals($expected, $this->parser->parse($yaml));
728728
}
729+
730+
/**
731+
* @dataProvider getCommentLikeStringInScalarBlockData
732+
*/
733+
public function testCommentLikeStringsAreNotStrippedInBlockScalars($yaml, $expectedParserResult)
734+
{
735+
$this->assertSame($expectedParserResult, $this->parser->parse($yaml));
736+
}
737+
738+
public function getCommentLikeStringInScalarBlockData()
739+
{
740+
$yaml1 = <<<EOT
741+
pages:
742+
-
743+
title: some title
744+
content: |
745+
# comment 1
746+
header
747+
748+
# comment 2
749+
<body>
750+
<h1>title</h1>
751+
</body>
752+
753+
footer # comment3
754+
EOT;
755+
$expected1 = array(
756+
'pages' => array(
757+
array(
758+
'title' => 'some title',
759+
'content' => <<<EOT
760+
# comment 1
761+
header
762+
763+
# comment 2
764+
<body>
765+
<h1>title</h1>
766+
</body>
767+
768+
footer # comment3
769+
EOT
770+
,
771+
),
772+
),
773+
);
774+
775+
$yaml2 = <<<EOT
776+
test: |
777+
foo
778+
# bar
779+
baz
780+
collection:
781+
- one: |
782+
foo
783+
# bar
784+
baz
785+
- two: |
786+
foo
787+
# bar
788+
baz
789+
EOT;
790+
$expected2 = array(
791+
'test' => <<<EOT
792+
foo
793+
# bar
794+
baz
795+
796+
EOT
797+
,
798+
'collection' => array(
799+
array(
800+
'one' => <<<EOT
801+
foo
802+
# bar
803+
baz
804+
EOT
805+
,
806+
),
807+
array(
808+
'two' => <<<EOT
809+
foo
810+
# bar
811+
baz
812+
EOT
813+
,
814+
),
815+
),
816+
);
817+
818+
return array(
819+
array($yaml1, $expected1),
820+
array($yaml2, $expected2),
821+
);
822+
}
729823
}
730824

731825
class B

0 commit comments

Comments
 (0)