Skip to content

Commit

Permalink
bug #13352 [Yaml] fixed parse shortcut Key after unindented collectio…
Browse files Browse the repository at this point in the history
…n. (aitboudad)

This PR was merged into the 2.3 branch.

Discussion
----------

[Yaml] fixed parse shortcut Key after unindented collection.

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Fixed tickets  | #13345, #8093, #11019 and #10885
| Tests pass?   | yes
| License       | MIT

Commits
-------

58a7426 [Yaml] fixed parse shortcut Key after unindented collection.
  • Loading branch information
fabpot committed Jan 25, 2015
2 parents 17184f3 + 58a7426 commit 4b36893
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Symfony/Component/Yaml/Parser.php
Expand Up @@ -96,7 +96,6 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
$data[] = $parser->parse($this->getNextEmbedBlock(null, true), $exceptionOnInvalidType, $objectSupport);
} else {
if (isset($values['leadspaces'])
&& ' ' == $values['leadspaces']
&& preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $values['value'], $matches)
) {
// this is a compact notation element, add to next block and parse
Expand All @@ -106,7 +105,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =

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

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

$data = array(substr($this->currentLine, $newIndent));
$data = array();
if ($this->getCurrentLineIndentation() >= $newIndent) {
$data[] = substr($this->currentLine, $newIndent);
} else {
$this->moveToPreviousLine();

return;
}

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

if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem($this->currentLine)) {
if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem($this->currentLine) && $newIndent === $indent) {
$this->moveToPreviousLine();
break;
}
Expand Down
Expand Up @@ -60,3 +60,23 @@ yaml: |
foo: bar
php: |
array('collection' => array('key' => array('a', 'b', 'c'), 'foo' => 'bar'))
---
test: Shortcut Key after unindented collection
brief: >
Key/value after unindented collection
yaml: |
collection:
- key: foo
foo: bar
php: |
array('collection' => array(array('key' => 'foo', 'foo' => 'bar')))
---
test: Shortcut Key after unindented collection with custom spaces
brief: >
Key/value after unindented collection
yaml: |
collection:
- key: foo
foo: bar
php: |
array('collection' => array(array('key' => 'foo', 'foo' => 'bar')))
16 changes: 16 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Expand Up @@ -475,6 +475,22 @@ public function testUnindentedCollectionException()
-item2
-item3
EOF;

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

/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
public function testShortcutKeyUnindentedCollectionException()
{
$yaml = <<<EOF
collection:
- key: foo
foo: bar
EOF;

$this->parser->parse($yaml);
Expand Down

0 comments on commit 4b36893

Please sign in to comment.