From 756834c8dd3630250974231abd0789ea128b9afc Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sat, 5 Dec 2015 17:57:30 +0100 Subject: [PATCH] [Yaml] fix indented line handling in folded blocks --- src/Symfony/Component/Yaml/Parser.php | 52 +++++++++++++------ .../Component/Yaml/Tests/ParserTest.php | 51 ++++++++++++++++++ 2 files changed, 88 insertions(+), 15 deletions(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index efaaeaf710..52dedfe078 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -468,13 +468,13 @@ class Parser } $isCurrentLineBlank = $this->isCurrentLineBlank(); - $text = ''; + $blockLines = array(); // leading blank lines are consumed before determining indentation while ($notEOF && $isCurrentLineBlank) { // newline only if not EOF if ($notEOF = $this->moveToNextLine()) { - $text .= "\n"; + $blockLines[] = ''; $isCurrentLineBlank = $this->isCurrentLineBlank(); } } @@ -495,37 +495,59 @@ class Parser preg_match($pattern, $this->currentLine, $matches) ) ) { - if ($isCurrentLineBlank) { - $text .= substr($this->currentLine, $indentation); + if ($isCurrentLineBlank && strlen($this->currentLine) > $indentation) { + $blockLines[] = substr($this->currentLine, $indentation); + } elseif ($isCurrentLineBlank) { + $blockLines[] = ''; } else { - $text .= $matches[1]; + $blockLines[] = $matches[1]; } // newline only if not EOF if ($notEOF = $this->moveToNextLine()) { - $text .= "\n"; $isCurrentLineBlank = $this->isCurrentLineBlank(); } } } elseif ($notEOF) { - $text .= "\n"; + $blockLines[] = ''; } if ($notEOF) { + $blockLines[] = ''; $this->moveToPreviousLine(); } // folded style if ('>' === $style) { - // folded lines - // replace all non-leading/non-trailing single newlines with spaces - preg_match('/(\n*)$/', $text, $matches); - $text = preg_replace('/(? +

A heading

+ + +EOT; + + $this->assertSame( + array( + 'test' => <<A heading + +EOT + , + ), + $this->parser->parse($yaml) + ); + } + + public function testAdditionallyIndentedLinesAreParsedAsNewLinesInFoldedBlocks() + { + $yaml = << +

A heading

+ +
    +
  • a list
  • +
  • may be a good example
  • +
+EOT; + + $this->assertSame( + array( + 'test' => <<A heading +
    +
  • a list
  • +
  • may be a good example
  • +
+EOT + , + ), + $this->parser->parse($yaml) + ); + } } class B