Skip to content

Commit

Permalink
feature #19304 [Yaml] fix parsing multi-line mapping values (xabbuh)
Browse files Browse the repository at this point in the history
This PR was submitted for the 2.7 branch but it was merged into the 3.2-dev branch instead (closes #19304).

Discussion
----------

[Yaml] fix parsing multi-line mapping values

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #11109
| License       | MIT
| Doc PR        |

Commits
-------

3954530 [Yaml] fix parsing multi-line mapping values
  • Loading branch information
xabbuh committed Sep 20, 2016
2 parents 84229f8 + 3954530 commit 06875e0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,29 @@ private function parseValue($value, $flags, $context)
}

try {
$quotation = '' !== $value && ('"' === $value[0] || "'" === $value[0]) ? $value[0] : null;

// do not take following lines into account when the current line is a quoted single line value
if (null !== $quotation && preg_match('/^'.$quotation.'.*'.$quotation.'(\s*#.*)?$/', $value)) {
return Inline::parse($value, $flags, $this->refs);
}

while ($this->moveToNextLine()) {
// unquoted strings end before the first unindented line
if (null === $quotation && $this->getCurrentLineIndentation() === 0) {
$this->moveToPreviousLine();

break;
}

$value .= ' '.trim($this->currentLine);

// quoted string values end with a line that is terminated with the quotation character
if ('' !== $this->currentLine && substr($this->currentLine, -1) === $quotation) {
break;
}
}

Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
$parsedValue = Inline::parse($value, $flags, $this->refs);

Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,32 @@ public function parserThrowsExceptionWithCorrectLineNumberProvider()
),
);
}

public function testParseMultiLineQuotedString()
{
$yaml = <<<EOT
foo: "bar
baz
foobar
foo"
bar: baz
EOT;

$this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml));
}

public function testParseMultiLineUnquotedString()
{
$yaml = <<<EOT
foo: bar
baz
foobar
foo
bar: baz
EOT;

$this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml));
}
}

class B
Expand Down

0 comments on commit 06875e0

Please sign in to comment.