merged branch jfsimon/issue-8472 (PR #8841)
This PR was merged into the 2.2 branch.
Discussion
----------
[Yaml] fixed embedded folded string parsing
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #8779
Commits
-------
566d79c
[Yaml] fixed embedded folded string parsing
This commit is contained in:
commit
b2cbc67289
|
@ -19,6 +19,8 @@ use Symfony\Component\Yaml\Exception\ParseException;
|
|||
*/
|
||||
class Parser
|
||||
{
|
||||
const FOLDED_SCALAR_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';
|
||||
|
||||
private $offset = 0;
|
||||
private $lines = array();
|
||||
private $currentLineNb = -1;
|
||||
|
@ -304,10 +306,15 @@ class Parser
|
|||
|
||||
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem($this->currentLine);
|
||||
|
||||
// We are in string block (ie. after a line ending with "|")
|
||||
$removeComments = !preg_match('~(.*)\|[\s]*$~', $this->currentLine);
|
||||
// Comments must not be removed inside a string block (ie. after a line ending with "|")
|
||||
$removeCommentsPattern = '~'.self::FOLDED_SCALAR_PATTERN.'$~';
|
||||
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
|
||||
|
||||
while ($this->moveToNextLine()) {
|
||||
if ($this->getCurrentLineIndentation() === $newIndent) {
|
||||
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
|
||||
}
|
||||
|
||||
if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem($this->currentLine)) {
|
||||
$this->moveToPreviousLine();
|
||||
break;
|
||||
|
@ -389,7 +396,7 @@ class Parser
|
|||
return $this->refs[$value];
|
||||
}
|
||||
|
||||
if (preg_match('/^(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?$/', $value, $matches)) {
|
||||
if (preg_match('/^'.self::FOLDED_SCALAR_PATTERN.'$/', $value, $matches)) {
|
||||
$modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
|
||||
|
||||
return $this->parseFoldedScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), intval(abs($modifiers)));
|
||||
|
|
|
@ -551,7 +551,7 @@ EOF
|
|||
));
|
||||
}
|
||||
|
||||
public function testNestedStringBlockWithComments()
|
||||
public function testFoldedStringBlockWithComments()
|
||||
{
|
||||
$this->assertEquals(array(array('content' => <<<EOT
|
||||
# comment 1
|
||||
|
@ -576,6 +576,38 @@ EOT
|
|||
</body>
|
||||
|
||||
footer # comment3
|
||||
EOF
|
||||
));
|
||||
}
|
||||
|
||||
public function testNestedFoldedStringBlockWithComments()
|
||||
{
|
||||
$this->assertEquals(array(array(
|
||||
'title' => 'some title',
|
||||
'content' => <<<EOT
|
||||
# comment 1
|
||||
header
|
||||
|
||||
# comment 2
|
||||
<body>
|
||||
<h1>title</h1>
|
||||
</body>
|
||||
|
||||
footer # comment3
|
||||
EOT
|
||||
)), Yaml::parse(<<<EOF
|
||||
-
|
||||
title: some title
|
||||
content: |
|
||||
# comment 1
|
||||
header
|
||||
|
||||
# comment 2
|
||||
<body>
|
||||
<h1>title</h1>
|
||||
</body>
|
||||
|
||||
footer # comment3
|
||||
EOF
|
||||
));
|
||||
}
|
||||
|
|
Reference in New Issue