Skip to content

Commit a7e2a49

Browse files
alexpottxabbuh
authored andcommitted
Yaml parser regression with comments and non-strings
1 parent f978588 commit a7e2a49

File tree

2 files changed

+58
-37
lines changed

2 files changed

+58
-37
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -390,54 +390,39 @@ private function doParse($value, $flags)
390390

391391
// try to parse the value as a multi-line string as a last resort
392392
if (0 === $this->currentLineNb) {
393-
$parseError = false;
394393
$previousLineWasNewline = false;
395394
$previousLineWasTerminatedWithBackslash = false;
396395
$value = '';
397396

398397
foreach ($this->lines as $line) {
399-
try {
400-
if (isset($line[0]) && ('"' === $line[0] || "'" === $line[0])) {
401-
$parsedLine = $line;
402-
} else {
403-
$parsedLine = Inline::parse($line, $flags, $this->refs);
404-
}
405-
406-
if (!is_string($parsedLine)) {
407-
$parseError = true;
408-
break;
409-
}
410-
411-
if ('' === trim($parsedLine)) {
412-
$value .= "\n";
413-
} elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) {
414-
$value .= ' ';
415-
}
398+
if ('' === trim($line)) {
399+
$value .= "\n";
400+
} elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) {
401+
$value .= ' ';
402+
}
416403

417-
if ('' !== trim($parsedLine) && '\\' === substr($parsedLine, -1)) {
418-
$value .= ltrim(substr($parsedLine, 0, -1));
419-
} elseif ('' !== trim($parsedLine)) {
420-
$value .= trim($parsedLine);
421-
}
404+
if ('' !== trim($line) && '\\' === substr($line, -1)) {
405+
$value .= ltrim(substr($line, 0, -1));
406+
} elseif ('' !== trim($line)) {
407+
$value .= trim($line);
408+
}
422409

423-
if ('' === trim($parsedLine)) {
424-
$previousLineWasNewline = true;
425-
$previousLineWasTerminatedWithBackslash = false;
426-
} elseif ('\\' === substr($parsedLine, -1)) {
427-
$previousLineWasNewline = false;
428-
$previousLineWasTerminatedWithBackslash = true;
429-
} else {
430-
$previousLineWasNewline = false;
431-
$previousLineWasTerminatedWithBackslash = false;
432-
}
433-
} catch (ParseException $e) {
434-
$parseError = true;
435-
break;
410+
if ('' === trim($line)) {
411+
$previousLineWasNewline = true;
412+
$previousLineWasTerminatedWithBackslash = false;
413+
} elseif ('\\' === substr($line, -1)) {
414+
$previousLineWasNewline = false;
415+
$previousLineWasTerminatedWithBackslash = true;
416+
} else {
417+
$previousLineWasNewline = false;
418+
$previousLineWasTerminatedWithBackslash = false;
436419
}
437420
}
438421

439-
if (!$parseError) {
422+
try {
440423
return Inline::parse(trim($value));
424+
} catch (ParseException $e) {
425+
// fall-through to the ParseException thrown below
441426
}
442427
}
443428

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,42 @@ public function testSequenceFollowedByCommentEmbeddedInMapping()
776776
$this->assertSame($expected, $this->parser->parse($yaml));
777777
}
778778

779+
public function testNonStringFollowedByCommentEmbeddedInMapping()
780+
{
781+
$yaml = <<<'EOT'
782+
a:
783+
b:
784+
{}
785+
# comment
786+
d:
787+
1.1
788+
# another comment
789+
EOT;
790+
$expected = array(
791+
'a' => array(
792+
'b' => array(),
793+
'd' => 1.1,
794+
),
795+
);
796+
797+
$this->assertSame($expected, $this->parser->parse($yaml));
798+
}
799+
800+
public function testMultiLineStringLastResortParsing()
801+
{
802+
$yaml = <<<'EOT'
803+
test:
804+
You can have things that don't look like strings here
805+
true
806+
yes you can
807+
EOT;
808+
$expected = array(
809+
'test' => 'You can have things that don\'t look like strings here true yes you can',
810+
);
811+
812+
$this->assertSame($expected, $this->parser->parse($yaml));
813+
}
814+
779815
/**
780816
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
781817
*/

0 commit comments

Comments
 (0)