Skip to content

Commit

Permalink
bug #24416 [Yaml] treat trailing backslashes in multi-line strings (x…
Browse files Browse the repository at this point in the history
…abbuh)

This PR was merged into the 3.3 branch.

Discussion
----------

[Yaml] treat trailing backslashes in multi-line strings

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

Commits
-------

80af9b8 treat trailing backslashes in multi-line strings
  • Loading branch information
fabpot committed Oct 5, 2017
2 parents 0a963d2 + 80af9b8 commit 1610751
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/Symfony/Component/Yaml/Parser.php
Expand Up @@ -384,6 +384,7 @@ private function doParse($value, $flags)
if (0 === $this->currentLineNb) {
$parseError = false;
$previousLineWasNewline = false;
$previousLineWasTerminatedWithBackslash = false;
$value = '';

foreach ($this->lines as $line) {
Expand All @@ -401,13 +402,25 @@ private function doParse($value, $flags)

if ('' === trim($parsedLine)) {
$value .= "\n";
$previousLineWasNewline = true;
} elseif ($previousLineWasNewline) {
} elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) {
$value .= ' ';
}

if ('' !== trim($parsedLine) && '\\' === substr($parsedLine, -1)) {
$value .= ltrim(substr($parsedLine, 0, -1));
} elseif ('' !== trim($parsedLine)) {
$value .= trim($parsedLine);
}

if ('' === trim($parsedLine)) {
$previousLineWasNewline = true;
$previousLineWasTerminatedWithBackslash = false;
} elseif ('\\' === substr($parsedLine, -1)) {
$previousLineWasNewline = false;
$previousLineWasTerminatedWithBackslash = true;
} else {
$value .= ' '.trim($parsedLine);
$previousLineWasNewline = false;
$previousLineWasTerminatedWithBackslash = false;
}
} catch (ParseException $e) {
$parseError = true;
Expand All @@ -416,7 +429,7 @@ private function doParse($value, $flags)
}

if (!$parseError) {
return trim($value);
return Inline::parse(trim($value));
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Expand Up @@ -1543,6 +1543,17 @@ public function testParseMultiLineQuotedString()
$this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml));
}

public function testMultiLineQuotedStringWithTrailingBackslash()
{
$yaml = <<<YAML
foobar:
"foo\
bar"
YAML;

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

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

0 comments on commit 1610751

Please sign in to comment.