8000 [Yaml] fixed parse shortcut Key after unindented collection. · symfony/symfony@58a7426 · GitHub
[go: up one dir, main page]

Skip to content

Commit 58a7426

Browse files
committed
[Yaml] fixed parse shortcut Key after unindented collection.
1 parent 76de700 commit 58a7426

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
9696
$data[] = $parser->parse($this->getNextEmbedBlock(null, true), $exceptionOnInvalidType, $objectSupport);
9797
} else {
9898
if (isset($values['leadspaces'])
99-
&& ' ' == $values['leadspaces']
10099
&& preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $values['value'], $matches)
101100
) {
102101
// this is a compact notation element, add to next block and parse
@@ -106,7 +105,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
106105

107106
$block = $values['value'];
108107
if ($this->isNextLineIndented()) {
109-
$block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + 2);
108+
$block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + strlen($values['leadspaces']) + 1);
110109
}
111110

112111
$data[] = $parser->parse($block, $exceptionOnInvalidType, $objectSupport);
@@ -313,7 +312,14 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
313312
$newIndent = $indentation;
314313
}
315314

316-
$data = array(substr($this->currentLine, $newIndent));
315+
$data = array();
316+
if ($this->getCurrentLineIndentation() >= $newIndent) {
317+
$data[] = substr($this->currentLine, $newIndent);
318+
} else {
319+
$this->moveToPreviousLine();
320+
321+
return;
322+
}
317323

318324
if ($inSequence && $oldLineIndentation === $newIndent && '-' === $data[0][0]) {
319325
// the previous line contained a dash but no item content, this line is a sequence item with the same indentation
@@ -336,7 +342,7 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
336342
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
337343
}
338344

339-
if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem($this->currentLine)) {
345+
if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem($this->currentLine) && $newIndent === $indent) {
340346
$this->moveToPreviousLine();
341347
break;
342348
}

src/Symfony/Component/Yaml/Tests/Fixtures/unindentedCollections.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,23 @@ yaml: |
6060
foo: bar
6161
php: |
6262
array('collection' => array('key' => array('a', 'b', 'c'), 'foo' => 'bar'))
63+
---
64+
test: Shortcut Key after unindented collection
65+
brief: >
66+
Key/value after unindented collection
67+
yaml: |
68+
collection:
69+
- key: foo
70+
foo: bar
71+
php: |
72+
array('collection' => array(array('key' => 'foo', 'foo' => 'bar')))
73+
---
74+
test: Shortcut Key after unindented collection with custom spaces
75+
brief: >
76+
Key/value after unindented collection
77+
yaml: |
78+
collection:
79+
- key: foo
80+
foo: bar
81+
php: |
82+
array('collection' => array(array('key' => 'foo', 'foo' => 'bar')))

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,22 @@ public function testUnindentedCollectionException()
475475
-item2
476476
-item3
477477
478+
EOF;
479+
480+
$this->parser->parse($yaml);
481+
}
482+
483+
/**
484+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
485+
*/
486+
public function testShortcutKeyUnindentedCollectionException()
487+
{
488+
$yaml = <<<EOF
489+
490+
collection:
491+
- key: foo
492+
foo: bar
493+
478494
EOF;
479495

480496
$this->parser->parse($yaml);

0 commit comments

Comments
 (0)
0