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:
Fabien Potencier 2013-08-24 08:58:46 +02:00
commit b2cbc67289
2 changed files with 43 additions and 4 deletions

View File

@ -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)));

View File

@ -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
));
}