8000 [Yaml] treat trailing backslashes in multi-line strings by xabbuh · Pull Request #24416 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Yaml] treat trailing backslashes in multi-line strings #24416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
L 8000 oading
Diff view
Diff view
treat trailing backslashes in multi-line strings
  • Loading branch information
xabbuh committed Oct 3, 2017
commit 80af9b8562ca87a1a0a05ccb3179c2888e90e4a2
21 changes: 17 additions & 4 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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