8000 bug #24416 [Yaml] treat trailing backslashes in multi-line strings (x… · symfony/symfony@1610751 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1610751

Browse files
committed
bug #24416 [Yaml] treat trailing backslashes in multi-line strings (xabbuh)
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
2 parents 0a963d2 + 80af9b8 commit 1610751

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ private function doParse($value, $flags)
384384
if (0 === $this->currentLineNb) {
385385
$parseError = false;
386386
$previousLineWasNewline = false;
387+
$previousLineWasTerminatedWithBackslash = false;
387388
$value = '';
388389

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

402403
if ('' === trim($parsedLine)) {
403404
$value .= "\n";
404-
$previousLineWasNewline = true;
405-
} elseif ($previousLineWasNewline) {
405+
} elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) {
406+
$value .= ' ';
407+
}
408+
409+
if ('' !== trim($parsedLine) && '\\' === substr($parsedLine, -1)) {
410+
$value .= ltrim(substr($parsedLine, 0, -1));
411+
} elseif ('' !== trim($parsedLine)) {
406412
$value .= trim($parsedLine);
413+
}
414+
415+
if ('' === trim($parsedLine)) {
416+
$previousLineWasNewline = true;
417+
$previousLineWasTerminatedWithBackslash = false;
418+
} elseif ('\\' === substr($parsedLine, -1)) {
407419
$previousLineWasNewline = false;
420+
$previousLineWasTerminatedWithBackslash = true;
408421
} else {
409-
$value .= ' '.trim($parsedLine);
410422
$previousLineWasNewline = false;
423+
$previousLineWasTerminatedWithBackslash = false;
411424
}
412425
} catch (ParseException $e) {
413426
$parseError = true;
@@ -416,7 +429,7 @@ private function doParse($value, $flags)
416429
}
417430

418431
if (!$parseError) {
419-
return trim($value);
432+
return Inline::parse(trim($value));
420433
}
421434
}
422435

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,17 @@ public function testParseMultiLineQuotedString()
15431543
$this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml));
15441544
}
15451545

1546+
public function testMultiLineQuotedStringWithTrailingBackslash()
1547+
{
1548+
$yaml = <<<YAML
1549+
foobar:
1550+
"foo\
1551+
bar"
1552+
YAML;
1553+
1554+
$this->assertSame(array('foobar' => 'foobar'), $this->parser->parse($yaml));
1555+
}
1556+
15461557
public function testParseMultiLineUnquotedString()
15471558
{
15481559
$yaml = <<<EOT

0 commit comments

Comments
 (0)
0