Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Yaml] Allow parsing of unindented closing quotation mark #33119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 1 == \strlen($trimmedLine) && '' !== $trimmedLine && ("'" === $trimmedLine[0] || '"' === $trimmedLine[0]);
}

private function isCurrentLineLastLineInDocument(): bool
{
return ($this->offset + $this->currentLineNb) >= ($this->totalNumberOfLines - 1);
Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2150,6 +2150,38 @@ public function testParseValueWithNegativeModifiers()
$this->parser->parse($yaml)
);
}

public function testParseValueWithUnindentedClosingQuotation()
{
$expected = ['foo' => ['bar' => 'baz ']];

// unindented closing single quote
$yaml = <<<YAML
---
foo:
bar: 'baz
'
YAML;
$this->assertSame($expected, $this->parser->parse($yaml));

// unindented closing double quote
$yaml = <<<YAML
---
foo:
bar: "baz
"
YAML;
$this->assertSame($expected, $this->parser->parse($yaml));

// correctly-indented closing double quote
$yaml = <<<YAML
---
foo:
bar: "baz
"
YAML;
$this->assertSame($expected, $this->parser->parse($yaml));
}
}

class B
Expand Down