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

Skip to content

[Yaml] Revert "feature #48022 Fix Yaml Parser with quote end in a newline (maxbeckers)" #53747

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

Merged
merged 1 commit into from
Feb 3, 2024
Merged
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
57 changes: 0 additions & 57 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,12 +597,8 @@ private function getNextEmbedBlock(?int $indentation = null, bool $inSequence =
}

$data = [];
$isInMultiLineQuote = false;

if ($this->getCurrentLineIndentation() >= $newIndent) {
if ($this->isCurrentLineMultiLineQuoteStart()) {
$isInMultiLineQuote = true;
}
$data[] = substr($this->currentLine, $newIndent ?? 0);
} elseif ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()) {
$data[] = $this->currentLine;
Expand Down Expand Up @@ -639,16 +635,6 @@ private function getNextEmbedBlock(?int $indentation = null, bool $inSequence =
if ($this->isCurrentLineBlank()) {
$data[] = substr($this->currentLine, $newIndent);
continue;
} elseif (!$isInMultiLineQuote && $this->isCurrentLineMultiLineQuoteStart()) {
$isInMultiLineQuote = true;
$data[] = substr($this->currentLine, $newIndent);
continue;
} elseif ($isInMultiLineQuote) {
$data[] = $this->currentLine;
if ("'" === (rtrim($this->currentLine)[-1] ?? '')) {
$isInMultiLineQuote = false;
}
continue;
}

if ($indent >= $newIndent) {
Expand Down Expand Up @@ -979,49 +965,6 @@ private function isCurrentLineLastLineInDocument(): bool
return ($this->offset + $this->currentLineNb) >= ($this->totalNumberOfLines - 1);
}

/**
* Returns true if the current line is the beginning of a multiline quoted block.
*/
private function isCurrentLineMultiLineQuoteStart(): bool
{
$trimmedLine = trim($this->currentLine);
$trimmedLineLength = \strlen($trimmedLine);
$quoteCount = 0;
$value = '';
// check if the key is quoted
for ($i = 0; $i < $trimmedLineLength; ++$i) {
$char = $trimmedLine[$i];
if ("'" === $char) {
++$quoteCount;
} elseif (':' === $char && 0 === $quoteCount % 2 && ($i === $trimmedLineLength - 1 || ' ' === $trimmedLine[$i + 1])) {
// key and value are separated by the first colon after the (quoted) key followed by a space or linebreak
$value = trim(substr($trimmedLine, ++$i), ' ');
break;
}
}

if (0 !== strpos($value, "'")) {
return false;
}

$lineEndQuoteCount = 0;
for ($i = \strlen($value) - 1; $i > 0; --$i) {
$char = $value[$i];
if ("'" === $char) {
++$lineEndQuoteCount;
} else {
break;
}
}

return 0 === $lineEndQuoteCount % 2;
}

/**
* Cleanups a YAML string to be parsed.
*
* @param string $value The input YAML string
*/
private function cleanup(string $value): string
{
$value = str_replace(["\r\n", "\r"], "\n", $value);
Expand Down
51 changes: 0 additions & 51 deletions src/Symfony/Component/Yaml/Tests/YamlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Yaml\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;

class YamlTest extends TestCase
Expand All @@ -25,56 +24,6 @@ public function testParseAndDump()
$this->assertEquals($data, $parsed);
}

public function testParseWithMultilineQuotes()
{
$yaml = <<<YAML
foo:
bar: 'baz
biz

'
baz: 'Lorem

ipsum'
error: Une erreur s'est produite.
trialMode: 'période d''essai'
double_line: 'Les utilisateurs sélectionnés
n''ont pas d''email.

'
a: 'b''
c'
empty: ''
foo:bar: 'foobar'
YAML;

$this->assertSame(['foo' => [
'bar' => "baz biz\n",
'baz' => "Lorem\nipsum",
'error' => "Une erreur s'est produite.",
'trialMode' => "période d'essai",
'double_line' => "Les utilisateurs sélectionnés n'ont pas d'email.\n",
'a' => "b' c",
'empty' => '',
'foo:bar' => 'foobar',
]], Yaml::parse($yaml));
}

public function testParseWithMultilineQuotesExpectException()
{
$yaml = <<<YAML
foo:
bar: 'baz

'
'
YAML;

$this->expectException(ParseException::class);
$this->expectExceptionMessage('Unable to parse at line 5 (near "\'").');
Yaml::parse($yaml);
}

public function testZeroIndentationThrowsException()
{
$this->expectException(\InvalidArgumentException::class);
Expand Down