Skip to content

Commit e56733b

Browse files
committed
drop comments while lexing unquoted strings
1 parent 0266d3c commit e56733b

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Symfony/Component/Yaml/Parser.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,18 @@ private function lexInlineQuotedString(int &$cursor = 0): string
11581158
private function lexUnquotedString(int &$cursor): string
11591159
{
11601160
$offset = $cursor;
1161-
$cursor += strcspn($this->currentLine, '[]{},:', $cursor);
1161+
1162+
while ($cursor < strlen($this->currentLine)) {
1163+
if (in_array($this->currentLine[$cursor], ['[', ']', '{', '}', ',', ':'], true)) {
1164+
break;
1165+
}
1166+
1167+
if (' ' === $this->currentLine[$cursor] && isset($this->currentLine[$cursor + 1]) && '#' === $this->currentLine[$cursor + 1]) {
1168+
break;
1169+
}
1170+
1171+
++$cursor;
1172+
}
11621173

11631174
if ($cursor === $offset) {
11641175
throw new ParseException('Malformed unquoted YAML string.');

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

+13
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,19 @@ public function testParseMultiLineUnquotedString()
17511751
$this->assertSame(['foo' => 'bar baz foobar foo', 'bar' => 'baz'], $this->parser->parse($yaml));
17521752
}
17531753

1754+
public function testParseMultiLineUnquotedStringWithTrailingComment()
1755+
{
1756+
$yaml = <<<YAML
1757+
{
1758+
foo: 3, # comment
1759+
bar: example.com/#about,
1760+
baz: 30 # comment
1761+
}
1762+
YAML;
1763+
1764+
$this->assertSame(['foo' => 3, 'bar' => 'example.com/#about', 'baz' => 30], $this->parser->parse($yaml));
1765+
}
1766+
17541767
/**
17551768
* @dataProvider escapedQuotationCharactersInQuotedStrings
17561769
*/

0 commit comments

Comments
 (0)