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
-------
80af9b8562
treat trailing backslashes in multi-line strings
This commit is contained in:
commit
16107510cb
@ -384,6 +384,7 @@ class Parser
|
|||||||
if (0 === $this->currentLineNb) {
|
if (0 === $this->currentLineNb) {
|
||||||
$parseError = false;
|
$parseError = false;
|
||||||
$previousLineWasNewline = false;
|
$previousLineWasNewline = false;
|
||||||
|
$previousLineWasTerminatedWithBackslash = false;
|
||||||
$value = '';
|
$value = '';
|
||||||
|
|
||||||
foreach ($this->lines as $line) {
|
foreach ($this->lines as $line) {
|
||||||
@ -401,13 +402,25 @@ class Parser
|
|||||||
|
|
||||||
if ('' === trim($parsedLine)) {
|
if ('' === trim($parsedLine)) {
|
||||||
$value .= "\n";
|
$value .= "\n";
|
||||||
$previousLineWasNewline = true;
|
} elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) {
|
||||||
} elseif ($previousLineWasNewline) {
|
$value .= ' ';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ('' !== trim($parsedLine) && '\\' === substr($parsedLine, -1)) {
|
||||||
|
$value .= ltrim(substr($parsedLine, 0, -1));
|
||||||
|
} elseif ('' !== trim($parsedLine)) {
|
||||||
$value .= trim($parsedLine);
|
$value .= trim($parsedLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ('' === trim($parsedLine)) {
|
||||||
|
$previousLineWasNewline = true;
|
||||||
|
$previousLineWasTerminatedWithBackslash = false;
|
||||||
|
} elseif ('\\' === substr($parsedLine, -1)) {
|
||||||
$previousLineWasNewline = false;
|
$previousLineWasNewline = false;
|
||||||
|
$previousLineWasTerminatedWithBackslash = true;
|
||||||
} else {
|
} else {
|
||||||
$value .= ' '.trim($parsedLine);
|
|
||||||
$previousLineWasNewline = false;
|
$previousLineWasNewline = false;
|
||||||
|
$previousLineWasTerminatedWithBackslash = false;
|
||||||
}
|
}
|
||||||
} catch (ParseException $e) {
|
} catch (ParseException $e) {
|
||||||
$parseError = true;
|
$parseError = true;
|
||||||
@ -416,7 +429,7 @@ class Parser
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!$parseError) {
|
if (!$parseError) {
|
||||||
return trim($value);
|
return Inline::parse(trim($value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1543,6 +1543,17 @@ EOT;
|
|||||||
$this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml));
|
$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()
|
public function testParseMultiLineUnquotedString()
|
||||||
{
|
{
|
||||||
$yaml = <<<EOT
|
$yaml = <<<EOT
|
||||||
|
Reference in New Issue
Block a user