Skip to content

Commit 9c5f8c6

Browse files
committed
[Yaml] removed wrong comment removal inside a string block
1 parent 183ff09 commit 9c5f8c6

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,16 @@ private function getNextEmbedBlock($indentation = null)
304304

305305
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem($this->currentLine);
306306

307-
while ($this->moveToNextLine()) {
307+
// We are in string block (ie. after a line ending with "|")
308+
$removeComments = !preg_match('~(.*)\|[\s]*$~', $this->currentLine);
308309

310+
while ($this->moveToNextLine()) {
309311
if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem($this->currentLine)) {
310312
$this->moveToPreviousLine();
311313
break;
312314
}
313315

314-
if ($this->isCurrentLineEmpty()) {
316+
if ($removeComments && $this->isCurrentLineEmpty() || $this->isCurrentLineBlank()) {
315317
if ($this->isCurrentLineBlank()) {
316318
$data[] = substr($this->currentLine, $newIndent);
317319
}

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,63 @@ public function testEmptyValue()
522522

523523
$this->assertEquals(array('hash' => null), Yaml::parse($input));
524524
}
525+
526+
public function testStringBlockWithComments()
527+
{
528+
$this->assertEquals(array('content' => <<<EOT
529+
# comment 1
530+
header
531+
532+
# comment 2
533+
<body>
534+
<h1>title</h1>
535+
</body>
536+
537+
footer # comment3
538+
EOT
539+
), Yaml::parse(<<<EOF
540+
content: |
541+
# comment 1
542+
header
543+
544+
# comment 2
545+
<body>
546+
<h1>title</h1>
547+
</body>
548+
549+
footer # comment3
550+
EOF
551+
));
552+
}
553+
554+
public function testNestedStringBlockWithComments()
555+
{
556+
$this->assertEquals(array(array('content' => <<<EOT
557+
# comment 1
558+
header
559+
560+
# comment 2
561+
<body>
562+
<h1>title</h1>
563+
</body>
564+
565+
footer # comment3
566+
EOT
567+
)), Yaml::parse(<<<EOF
568+
-
569+
content: |
570+
# comment 1
571+
header
572+
573+
# comment 2
574+
<body>
575+
<h1>title</h1>
576+
</body>
577+
578+
footer # comment3
579+
EOF
580+
));
581+
}
525582
}
526583

527584
class B

0 commit comments

Comments
 (0)