From 5e455f321a4e1121c83317a2c48bf097635b6fba Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Tue, 1 Dec 2020 17:22:27 +0100 Subject: [PATCH] fix lexing mapping values with trailing whitespaces --- src/Symfony/Component/Yaml/Parser.php | 4 ++-- src/Symfony/Component/Yaml/Tests/ParserTest.php | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 56322c387d..68b237d37c 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -753,10 +753,10 @@ class Parser switch ($value[0] ?? '') { 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); - 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))); } diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index cea3d3f330..a39138c6ea 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -2671,7 +2671,7 @@ YAML; ); } - public function testMultipleWhitespaceAtEndOfLine() + public function testWhitespaceAtEndOfLine() { $yaml = "\nfoo:\n arguments: [ '@bar' ] \n"; $this->assertSame( @@ -2692,6 +2692,14 @@ YAML; ], $this->parser->parse($yaml) ); + + $this->assertSame( + [ + 'foo' => 'bar', + 'foobar' => 'baz', + ], + $this->parser->parse("foo: 'bar' \nfoobar: baz") + ); } /**