8000 [Yaml] fixed embedded folded string parsing by jfsimon · Pull Request #8841 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Yaml] fixed embedded folded string parsing #8841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2013
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.
8000
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
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;
Expand Down Expand Up @@ -304,10 +306,15 @@ private function getNextEmbedBlock($indentation = null)

$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;
Expand Down Expand Up @@ -389,7 +396,7 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
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)));
Expand Down
34 changes: 33 additions & 1 deletion src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ public function testStringBlockWithComments()
));
}

public function testNestedStringBlockWithComments()
public function testFoldedStringBlockWithComments()
{
$this->assertEquals(array(array('content' => <<<EOT
# comment 1
Expand All @@ -576,6 +576,38 @@ public function testNestedStringBlockWithComments()
</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
));
}
Expand Down
0