8000 [Yaml] fixed embedded folded string parsing · symfony/symfony@566d79c · GitHub
[go: up one dir, main page]

Skip to content

Commit 566d79c

Browse files
committed
[Yaml] fixed embedded folded string parsing
1 parent 2165d5d commit 566d79c

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020
class Parser
2121
{
22+
const FOLDED_SCALAR_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';
23+
2224
private $offset = 0;
2325
private $lines = array();
2426
private $currentLineNb = -1;
@@ -304,10 +306,15 @@ private function getNextEmbedBlock($indentation = null)
304306

305307
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem($this->currentLine);
306308

307-
// We are in string block (ie. after a line ending with "|")
308-
$removeComments = !preg_match('~(.*)\|[\s]*$~', $this->currentLine);
309+
// Comments must not be removed inside a string block (ie. after a line ending with "|")
310+
$removeCommentsPattern = '~'.self::FOLDED_SCALAR_PATTERN.'$~';
311+
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
309312

310313
while ($this->moveToNextLine()) {
314+
if ($this->getCurrentLineIndentation() === $newIndent) {
315+
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
316+
}
317+
311318
if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem($this->currentLine)) {
312319
$this->moveToPreviousLine();
313320
break;
@@ -389,7 +396,7 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
389396
return $this->refs[$value];
390397
}
391398

392-
if (preg_match('/^(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?$/', $value, $matches)) {
399+
if (preg_match('/^'.self::FOLDED_SCALAR_PATTERN.'$/', $value, $matches)) {
393400
$modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
394401

395402
return $this->parseFoldedScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), intval(abs($modifiers)));

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ public function testStringBlockWithComments()
551551
));
552552
}
553553

554-
public function testNestedStringBlockWithComments()
554+
public function testFoldedStringBlockWithComments()
555555
{
556556
$this->assertEquals(array(array('content' => <<<EOT
557557
# comment 1
@@ -576,6 +576,38 @@ public function testNestedStringBlockWithComments()
576576
</body>
577577
578578
footer # comment3
579+
EOF
580+
));
581+
}
582+
583+
public function testNestedFoldedStringBlockWithComments()
584+
{
585+
$this->assertEquals(array(array(
586+
'title' => 'some title',
587+
'content' => <<<EOT
588+
# comment 1
589+
header
590+
591+
# comment 2
592+
<body>
593+
<h1>title</h1>
594+
</body>
595+
596+
footer # comment3
597+
EOT
598+
)), Yaml::parse(<<<EOF
599+
-
600+
title: some title
601+
content: |
602+
# comment 1
603+
header
604+
605+
# comment 2
606+
<body>
607+
<h1>title</h1>
608+
</body>
609+
610+
footer # comment3
579611
EOF
580612
));
581613
}

0 commit comments

Comments
 (0)
0