Skip to content

Commit 1480a23

Browse files
committed
[Yaml] Fix regression handling blank lines in unquoted scalars
1 parent baf4a16 commit 1480a23

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -762,11 +762,6 @@ private function parseValue(string $value, int $flags, string $context): mixed
762762
$lines = [];
763763

764764
while ($this->moveToNextLine()) {
765-
if ($this->isCurrentLineBlank()) {
766-
$lines[] = '';
767-
continue;
768-
}
769-
770765
// unquoted strings end before the first unindented line
771766
if (0 === $this->getCurrentLineIndentation()) {
772767
$this->moveToPreviousLine();
@@ -778,6 +773,10 @@ private function parseValue(string $value, int $flags, string $context): mixed
778773
break;
779774
}
780775

776+
if ('mapping' === $context && str_contains($this->currentLine, ': ') && !$this->isCurrentLineComment()) {
777+
throw new ParseException('A colon cannot be used in an unquoted mapping value.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
778+
}
779+
781780
$lines[] = trim($this->currentLine);
782781
}
783782

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,6 +1829,77 @@ public function testUnquotedMultilineScalarThrowsOnOrphanedLineAfterComment()
18291829
$this->parser->parse($yaml);
18301830
}
18311831

1832+
public function testUnquotedMultilineScalarWithBlankLines()
1833+
{
1834+
$yaml = <<<YAML
1835+
foo:
1836+
line 1
1837+
1838+
line 2
1839+
YAML;
1840+
$this->assertSame(['foo' => "line 1\nline 2"], $this->parser->parse($yaml));
1841+
}
1842+
1843+
/**
1844+
* @dataProvider provideInvalidYamlFiles
1845+
*/
1846+
public function testLineNumberInException(int $expectedLine, string $yaml, string $message)
1847+
{
1848+
$this->expectException(ParseException::class);
1849+
$this->expectExceptionMessage($message);
1850+
$this->expectExceptionMessage(\sprintf('at line %d', $expectedLine));
1851+
1852+
$this->parser->parse($yaml);
1853+
}
1854+
1855+
public static function provideInvalidYamlFiles(): iterable
1856+
{
1857+
yield 'invalid_nested_under_scalar' => [
1858+
4,
1859+
<<<YAML
1860+
en:
1861+
NONAMESPACE: Include Entity without Namespace
1862+
Invalid: Foo
1863+
About: 'About us'
1864+
- Invalid
1865+
YAML,
1866+
'Unable to parse',
1867+
];
1868+
1869+
yield 'invalid_nested_under_scalar_with_trailing_newlines' => [
1870+
4,
1871+
<<<YAML
1872+
en:
1873+
NONAMESPACE: Include Entity without Namespace
1874+
Invalid: Foo
1875+
About: 'About us'
1876+
- Invalid
1877+
1878+
1879+
YAML,
1880+
'Unable to parse',
1881+
];
1882+
1883+
yield 'colon_in_unquoted_value' => [
1884+
2,
1885+
<<<YAML
1886+
foo: bar
1887+
baz: qux
1888+
YAML,
1889+
'A colon cannot be used in an unquoted mapping value',
1890+
];
1891+
1892+
yield 'colon_in_unquoted_value_multiline' => [
1893+
2,
1894+
<<<YAML
1895+
foo:
1896+
bar
1897+
baz: qux
1898+
YAML,
1899+
'Mapping values are not allowed in multi-line blocks',
1900+
];
1901+
}
1902+
18321903
/**
18331904
* @dataProvider unquotedStringWithTrailingComment
18341905
*/

0 commit comments

Comments
 (0)