Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 50250dc

Browse files
maxbeckersnicolas-grekas
authored andcommitted
[Yaml] Fix Yaml Parser with quote end in a new line
1 parent 5a68b8c commit 50250dc

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,9 @@ private function getNextEmbedBlock(int $indentation = null, bool $inSequence = f
656656
if ($this->isCurrentLineBlank()) {
657657
$data[] = substr($this->currentLine, $newIndent);
658658
continue;
659+
} elseif ($this->isCurrentLineQuoteEnd()) {
660+
$data[] = $this->currentLine;
661+
continue;
659662
}
660663

661664
if ($indent >= $newIndent) {
@@ -977,6 +980,14 @@ private function isCurrentLineBlank(): bool
977980
return '' === $this->currentLine || '' === trim($this->currentLine, ' ');
978981
}
979982

983+
/**
984+
* Returns true if the current line is end of a quote.
985+
*/
986+
private function isCurrentLineQuoteEnd(): bool
987+
{
988+
return \in_array(trim($this->currentLine), ["'", '"']);
989+
}
990+
980991
/**
981992
* Returns true if the current line is a comment line.
982993
*/

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Yaml\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Yaml\Exception\ParseException;
1516
use Symfony\Component\Yaml\Yaml;
1617

1718
class YamlTest extends TestCase
@@ -24,6 +25,37 @@ public function testParseAndDump()
2425
$this->assertEquals($data, $parsed);
2526
}
2627

28+
public function testParseWithMultilineQuotes()
29+
{
30+
$yaml = <<<YAML
31+
foo:
32+
bar: 'baz
33+
34+
'
35+
baz: 'Lorem
36+
37+
ipsum'
38+
foobar: 'foobar'
39+
YAML;
40+
41+
$this->assertSame(['foo' => ['bar' => "baz\n", 'baz' => "Lorem\nipsum", 'foobar' => 'foobar']], Yaml::parse($yaml));
42+
}
43+
44+
public function testParseWithMultilineQuotesExpectException()
45+
{
46+
$yaml = <<<YAML
47+
foo:
48+
bar: 'baz
49+
50+
'
51+
'
52+
YAML;
53+
54+
$this->expectException(ParseException::class);
55+
$this->expectExceptionMessage('Unable to parse at line 5 (near "\'").');
56+
Yaml::parse($yaml);
57+
}
58+
2759
public function testZeroIndentationThrowsException()
2860
{
2961
$this->expectException(\InvalidArgumentException::class);

0 commit comments

Comments
 (0)