From ec593b923bc7fff7d21268f636551fa012911053 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Thu, 29 Dec 2016 12:24:11 +0100 Subject: [PATCH] [Yaml] parse multi-line strings --- src/Symfony/Component/Yaml/Parser.php | 38 ++++++++++++++++++- .../Component/Yaml/Tests/ParserTest.php | 26 +++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 55beabea44934..14147a6aeef51 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -295,6 +295,42 @@ public function parse($value, $flags = 0) return $value; } + // try to parse the value as a multi-line string as a last resort + if (0 === $this->currentLineNb) { + $parseError = false; + $previousLineWasNewline = false; + $value = ''; + + foreach ($this->lines as $line) { + try { + $parsedLine = Inline::parse($line, $flags, $this->refs); + + if (!is_string($value)) { + $parseError = true; + break; + } + + if ('' === trim($parsedLine)) { + $value .= "\n"; + $previousLineWasNewline = true; + } elseif ($previousLineWasNewline) { + $value .= trim($parsedLine); + $previousLineWasNewline = false; + } else { + $value .= ' '.trim($parsedLine); + $previousLineWasNewline = false; + } + } catch (ParseException $e) { + $parseError = true; + break; + } + } + + if (!$parseError) { + return trim($value); + } + } + switch (preg_last_error()) { case PREG_INTERNAL_ERROR: $error = 'Internal PCRE error.'; @@ -462,7 +498,7 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false) $previousLineIndentation = $indent; - if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) { + if ($isItUnindentedCollection && !$this->isCurrentLineEmpty() && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) { $this->moveToPreviousLine(); break; } diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index f2498d1b45f95..f16d65c74f8fa 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -1449,6 +1449,32 @@ public function testParseMultiLineUnquotedString() $this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml)); } + + public function testParseMultiLineString() + { + $this->assertEquals("foo bar\nbaz", $this->parser->parse("foo\nbar\n\nbaz")); + } + + public function testParseMultiLineMappingValue() + { + $yaml = <<<'EOF' +foo: +- bar: + one + + two + three +EOF; + $expected = array( + 'foo' => array( + array( + 'bar' => "one\ntwo three", + ), + ), + ); + + $this->assertEquals($expected, $this->parser->parse($yaml)); + } } class B pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy