From 0be9f155c6b9d5bccc499587d22985a90b282d53 Mon Sep 17 00:00:00 2001 From: johnillo Date: Mon, 12 Aug 2019 11:36:35 +0800 Subject: [PATCH 1/2] fix(yml): allow parsing of unindented closing quotation mark. --- src/Symfony/Component/Yaml/Parser.php | 20 +++++++++--- .../Component/Yaml/Tests/ParserTest.php | 32 +++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index e51391f35deaa..dc901570ee08e 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -529,8 +529,8 @@ private function getNextEmbedBlock(int $indentation = null, bool $inSequence = f do { $EOF = false; - // empty and comment-like lines do not influence the indentation depth - if ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()) { + // empty, quotation mark only, and comment-like lines do not influence the indentation depth + if ($this->isCurrentLineEmpty() || $this->isCurrentLineComment() || $this->isCurrentLineQuotationOnly()) { $EOF = !$this->moveToNextLine(); if (!$EOF) { @@ -557,7 +557,7 @@ private function getNextEmbedBlock(int $indentation = null, bool $inSequence = f $data = []; if ($this->getCurrentLineIndentation() >= $newIndent) { $data[] = substr($this->currentLine, $newIndent); - } elseif ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()) { + } elseif ($this->isCurrentLineEmpty() || $this->isCurrentLineComment() || $this->isCurrentLineQuotationOnly()) { $data[] = $this->currentLine; } else { $this->moveToPreviousLine(); @@ -590,7 +590,7 @@ private function getNextEmbedBlock(int $indentation = null, bool $inSequence = f if ($indent >= $newIndent) { $data[] = substr($this->currentLine, $newIndent); - } elseif ($this->isCurrentLineComment()) { + } elseif ($this->isCurrentLineComment() || $this->isCurrentLineQuotationOnly()) { $data[] = $this->currentLine; } elseif (0 == $indent) { $this->moveToPreviousLine(); @@ -916,6 +916,18 @@ private function isCurrentLineComment(): bool return '' !== $ltrimmedLine && '#' === $ltrimmedLine[0]; } + /** + * Returns true if the current line contains quotation mark only. + * + * @return bool Returns true if the current line contains quotation mark only, false otherwise + */ + private function isCurrentLineQuotationOnly(): bool + { + $trimmedLine = trim($this->currentLine, ' '); + + return strlen($trimmedLine) == 1 && '' !== $trimmedLine && ("'" === $trimmedLine[0] || '"' === $trimmedLine[0]); + } + private function isCurrentLineLastLineInDocument(): bool { return ($this->offset + $this->currentLineNb) >= ($this->totalNumberOfLines - 1); diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index 366902a8647e4..3c0596b16531c 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -2150,6 +2150,38 @@ public function testParseValueWithNegativeModifiers() $this->parser->parse($yaml) ); } + + public function testParseValueWithUnindentedClosingQuotation() + { + $expected = ['foo' => ['bar' => 'baz ']]; + + // unindented closing single quote + $yaml = <<assertSame($expected, $this->parser->parse($yaml)); + + // unindented closing double quote + $yaml = <<assertSame($expected, $this->parser->parse($yaml)); + + // correctly-indented closing double quote + $yaml = <<assertSame($expected, $this->parser->parse($yaml)); + } } class B From 90cb4c52d79f6036cc555e8fc0cc550abedc5d09 Mon Sep 17 00:00:00 2001 From: johnillo Date: Mon, 12 Aug 2019 11:55:48 +0800 Subject: [PATCH 2/2] fix code to meet coding symfony coding standards. --- src/Symfony/Component/Yaml/Parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index dc901570ee08e..edef30b3745e9 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -925,7 +925,7 @@ private function isCurrentLineQuotationOnly(): bool { $trimmedLine = trim($this->currentLine, ' '); - return strlen($trimmedLine) == 1 && '' !== $trimmedLine && ("'" === $trimmedLine[0] || '"' === $trimmedLine[0]); + return 1 == \strlen($trimmedLine) && '' !== $trimmedLine && ("'" === $trimmedLine[0] || '"' === $trimmedLine[0]); } private function isCurrentLineLastLineInDocument(): bool