Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ private function getCurrentLineIndentation()
private function getNextEmbedBlock($indentation = null, $inSequence = false)
{
$oldLineIndentation = $this->getCurrentLineIndentation();
$insideBlockScalar = $this->isBlockScalarHeader();

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

$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();

// Comments must not be removed inside a block scalar
$removeCommentsPattern = '~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~';
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
if (!$insideBlockScalar) {
$insideBlockScalar = $this->isBlockScalarHeader();
}

$previousLineIndentation = $this->getCurrentLineIndentation();

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

if ($indent === $newIndent) {
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
if (!$insideBlockScalar && $indent === $previousLineIndentation) {
$insideBlockScalar = $this->isBlockScalarHeader();
}

$previousLineIndentation = $indent;

if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
$this->moveToPreviousLine();
break;
Expand All @@ -360,7 +365,8 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
continue;
}

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

Expand Down Expand Up @@ -672,4 +678,14 @@ private function isStringUnIndentedCollectionItem()
{
return 0 === strpos($this->currentLine, '- ');
}

/**
* Tests whether or not the current line is the header of a block scalar.
*
* @return bool
*/
private function isBlockScalarHeader()
{
return (bool) preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine);
}
}
94 changes: 94 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,100 @@ public function testFloatKeys()

$this->assertEquals($expected, $this->parser->parse($yaml));
}

/**
* @dataProvider getCommentLikeStringInScalarBlockData
*/
public function testCommentLikeStringsAreNotStrippedInBlockScalars($yaml, $expectedParserResult)
{
$this->assertSame($expectedParserResult, $this->parser->parse($yaml));
}

public function getCommentLikeStringInScalarBlockData()
{
$yaml1 = <<<EOT
pages:
-
title: some title
content: |
# comment 1
header

# comment 2
<body>
<h1>title</h1>
</body>

footer # comment3
EOT;
$expected1 = array(
'pages' => array(
array(
'title' => 'some title',
'content' => <<<EOT
# comment 1
header

# comment 2
<body>
<h1>title</h1>
</body>

footer # comment3
EOT
,
),
),
);

$yaml2 = <<<EOT
test: |
foo
# bar
baz
collection:
- one: |
foo
# bar
baz
- two: |
foo
# bar
baz
EOT;
$expected2 = array(
'test' => <<<EOT
foo
# bar
baz

EOT
,
'collection' => array(
array(
'one' => <<<EOT
foo
# bar
baz
EOT
,
),
array(
'two' => <<<EOT
foo
# bar
baz
EOT
,
),
),
);

return array(
array($yaml1, $expected1),
array($yaml2, $expected2),
);
}
}

class B
Expand Down