[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.
This commit is contained in:
parent
2d14689485
commit
3c72fccc1b
@ -303,6 +303,7 @@ class Parser
|
|||||||
private function getNextEmbedBlock($indentation = null, $inSequence = false)
|
private function getNextEmbedBlock($indentation = null, $inSequence = false)
|
||||||
{
|
{
|
||||||
$oldLineIndentation = $this->getCurrentLineIndentation();
|
$oldLineIndentation = $this->getCurrentLineIndentation();
|
||||||
|
$insideBlockScalar = $this->isBlockScalarHeader();
|
||||||
|
|
||||||
if (!$this->moveToNextLine()) {
|
if (!$this->moveToNextLine()) {
|
||||||
return;
|
return;
|
||||||
@ -339,17 +340,21 @@ class Parser
|
|||||||
|
|
||||||
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
|
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
|
||||||
|
|
||||||
// Comments must not be removed inside a block scalar
|
if (!$insideBlockScalar) {
|
||||||
$removeCommentsPattern = '~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~';
|
$insideBlockScalar = $this->isBlockScalarHeader();
|
||||||
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
|
}
|
||||||
|
|
||||||
|
$previousLineIndentation = $this->getCurrentLineIndentation();
|
||||||
|
|
||||||
while ($this->moveToNextLine()) {
|
while ($this->moveToNextLine()) {
|
||||||
$indent = $this->getCurrentLineIndentation();
|
$indent = $this->getCurrentLineIndentation();
|
||||||
|
|
||||||
if ($indent === $newIndent) {
|
if (!$insideBlockScalar && $indent === $previousLineIndentation) {
|
||||||
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
|
$insideBlockScalar = $this->isBlockScalarHeader();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$previousLineIndentation = $indent;
|
||||||
|
|
||||||
if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
|
if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
|
||||||
$this->moveToPreviousLine();
|
$this->moveToPreviousLine();
|
||||||
break;
|
break;
|
||||||
@ -360,7 +365,8 @@ class Parser
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($removeComments && $this->isCurrentLineComment()) {
|
// we ignore "comment" lines only when we are not inside a scalar block
|
||||||
|
if (!$insideBlockScalar && $this->isCurrentLineComment()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -672,4 +678,14 @@ class Parser
|
|||||||
{
|
{
|
||||||
return 0 === strpos($this->currentLine, '- ');
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -726,6 +726,100 @@ EOF;
|
|||||||
|
|
||||||
$this->assertEquals($expected, $this->parser->parse($yaml));
|
$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
|
class B
|
||||||
|
Reference in New Issue
Block a user