From 662d984671dbdd7f19c245b087398f2949d9d3d9 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 27 Dec 2024 12:50:18 +0100 Subject: [PATCH] do not swallow subsequent spaces in unquoted mapping values --- src/Symfony/Component/Yaml/Parser.php | 10 +++++----- src/Symfony/Component/Yaml/Tests/ParserTest.php | 5 +++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 2a15bcae3d157..cb2553e2f41ea 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -1213,8 +1213,8 @@ private function lexInlineStructure(int &$cursor, string $closingTag): string $value .= $this->lexUnquotedString($cursor); } - if ($this->consumeWhitespaces($cursor)) { - $value .= ' '; + if (0 < $numberOfConsumedWhitespaces = $this->consumeWhitespaces($cursor)) { + $value .= isset($this->currentLine[$cursor]) ? str_repeat(' ', $numberOfConsumedWhitespaces) : ' '; } } @@ -1226,7 +1226,7 @@ private function lexInlineStructure(int &$cursor, string $closingTag): string throw new ParseException('Malformed inline YAML string.'); } - private function consumeWhitespaces(int &$cursor): bool + private function consumeWhitespaces(int &$cursor): int { $whitespacesConsumed = 0; @@ -1236,7 +1236,7 @@ private function consumeWhitespaces(int &$cursor): bool $cursor += $whitespaceOnlyTokenLength; if (isset($this->currentLine[$cursor])) { - return 0 < $whitespacesConsumed; + return $whitespacesConsumed; } if ($this->hasMoreLines()) { @@ -1244,6 +1244,6 @@ private function consumeWhitespaces(int &$cursor): bool } } while ($this->moveToNextLine()); - return 0 < $whitespacesConsumed; + return $whitespacesConsumed; } } diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index 3c4c071135855..b26256887e82a 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -2212,6 +2212,11 @@ public static function inlineNotationSpanningMultipleLinesProvider(): array ]; } + public function testParseSubsequentSpacesInsideUnquotedInlineMappingValueNestedInSequence() + { + $this->assertSame([['name' => 'A B']], $this->parser->parse('- {name: A B}')); + } + public function testRootLevelInlineMappingFollowedByMoreContentIsInvalid() { $this->expectException(ParseException::class);