fix lexing mapping values with trailing whitespaces

This commit is contained in:
Christian Flothmann 2020-12-01 17:22:27 +01:00
parent 27127a4bab
commit 5e455f321a
2 changed files with 11 additions and 3 deletions

View File

@ -753,10 +753,10 @@ class Parser
switch ($value[0] ?? '') { switch ($value[0] ?? '') {
case '"': case '"':
case "'": case "'":
$cursor = \strlen($this->currentLine) - \strlen($value); $cursor = \strlen(rtrim($this->currentLine)) - \strlen(rtrim($value));
$parsedValue = Inline::parse($this->lexInlineQuotedString($cursor), $flags, $this->refs); $parsedValue = Inline::parse($this->lexInlineQuotedString($cursor), $flags, $this->refs);
if (isset($this->currentLine[$cursor]) && preg_replace('/\s*#.*$/A', '', substr($this->currentLine, $cursor))) { if (isset($this->currentLine[$cursor]) && preg_replace('/\s*(#.*)?$/A', '', substr($this->currentLine, $cursor))) {
throw new ParseException(sprintf('Unexpected characters near "%s".', substr($this->currentLine, $cursor))); throw new ParseException(sprintf('Unexpected characters near "%s".', substr($this->currentLine, $cursor)));
} }

View File

@ -2671,7 +2671,7 @@ YAML;
); );
} }
public function testMultipleWhitespaceAtEndOfLine() public function testWhitespaceAtEndOfLine()
{ {
$yaml = "\nfoo:\n arguments: [ '@bar' ] \n"; $yaml = "\nfoo:\n arguments: [ '@bar' ] \n";
$this->assertSame( $this->assertSame(
@ -2692,6 +2692,14 @@ YAML;
], ],
$this->parser->parse($yaml) $this->parser->parse($yaml)
); );
$this->assertSame(
[
'foo' => 'bar',
'foobar' => 'baz',
],
$this->parser->parse("foo: 'bar' \nfoobar: baz")
);
} }
/** /**