Skip to content

Commit

Permalink
bug #25309 [Yaml] parse newlines in quoted multiline strings (xabbuh)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.3 branch.

Discussion
----------

[Yaml] parse newlines in quoted multiline strings

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

Commits
-------

b23b957 parse newlines in quoted multiline strings
  • Loading branch information
fabpot committed Dec 4, 2017
2 parents c9f72e2 + b23b957 commit c08602c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Symfony/Component/Yaml/Parser.php
Expand Up @@ -700,6 +700,8 @@ private function parseValue($value, $flags, $context)
return Inline::parse($value, $flags, $this->refs);
}

$lines = array();

while ($this->moveToNextLine()) {
// unquoted strings end before the first unindented line
if (null === $quotation && 0 === $this->getCurrentLineIndentation()) {
Expand All @@ -708,14 +710,27 @@ private function parseValue($value, $flags, $context)
break;
}

$value .= ' '.trim($this->currentLine);
$lines[] = 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;
}
}

for ($i = 0, $linesCount = count($lines), $previousLineBlank = false; $i < $linesCount; ++$i) {
if ('' === $lines[$i]) {
$value .= "\n";
$previousLineBlank = true;
} elseif ($previousLineBlank) {
$value .= $lines[$i];
$previousLineBlank = false;
} else {
$value .= ' '.$lines[$i];
$previousLineBlank = false;
}
}

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

Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Expand Up @@ -1572,6 +1572,20 @@ public function testCommentCharactersInMultiLineQuotedStrings()
$this->assertSame($expected, $this->parser->parse($yaml));
}

public function testBlankLinesInQuotedMultiLineString()
{
$yaml = <<<YAML
foobar: 'foo
bar'
YAML;
$expected = array(
'foobar' => "foo\nbar",
);

$this->assertSame($expected, $this->parser->parse($yaml));
}

public function testParseMultiLineUnquotedString()
{
$yaml = <<<EOT
Expand Down

0 comments on commit c08602c

Please sign in to comment.