Skip to content

Commit 55f2c00

Browse files
committed
Merge branch '3.4' into 4.4
* 3.4: drop logger mock in favor of using the BufferingLogger [Yaml Parser] Fix edge cases when parsing multiple documents fix parsing comments not prefixed by a space
2 parents e53b8b6 + a2b5a78 commit 55f2c00

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

Inline.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static function parse(string $value = null, int $flags = 0, array $refere
9090
}
9191

9292
// some comments are allowed at the end
93-
if (preg_replace('/\s+#.*$/A', '', substr($value, $i))) {
93+
if (preg_replace('/\s*#.*$/A', '', substr($value, $i))) {
9494
throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value, $i)), self::$parsedLineNumber + 1, $value, self::$parsedFilename);
9595
}
9696

Parser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public function parse(string $value, int $flags = 0)
102102
$this->refs = [];
103103
$this->skippedLineNumbers = [];
104104
$this->locallySkippedLineNumbers = [];
105+
$this->totalNumberOfLines = null;
105106
}
106107

107108
return $data;

Tests/InlineTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,16 @@ public function testParseNegativeOctalNumberContainingInvalidDigits()
799799
self::assertSame('-0123456789', Inline::parse('-0123456789'));
800800
}
801801

802+
public function testParseCommentNotPrefixedBySpaces()
803+
{
804+
self::assertSame('foo', Inline::parse('"foo"#comment'));
805+
}
806+
807+
public function testParseUnquotedStringContainingHashTagNotPrefixedBySpace()
808+
{
809+
self::assertSame('foo#nocomment', Inline::parse('foo#nocomment'));
810+
}
811+
802812
/**
803813
* @dataProvider unquotedExclamationMarkThrowsProvider
804814
*/

Tests/ParserTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,6 +2438,39 @@ public function testParseValueWithNegativeModifiers()
24382438
$this->parser->parse($yaml)
24392439
);
24402440
}
2441+
2442+
/**
2443+
* This is a regression test for a bug where a YAML block with a nested multiline string using | was parsed without
2444+
* a trailing \n when a shorter YAML document was parsed before.
2445+
*
2446+
* When a shorter document was parsed before, the nested string did not have a \n at the end of the string, because
2447+
* the Parser thought it was the end of the file, even though it is not.
2448+
*/
2449+
public function testParsingMultipleDocuments()
2450+
{
2451+
$shortDocument = 'foo: bar';
2452+
$longDocument = <<<YAML
2453+
a:
2454+
b: |
2455+
row
2456+
row2
2457+
c: d
2458+
YAML;
2459+
2460+
$expected = ['a' => ['b' => "row\nrow2\n"], 'c' => 'd'];
2461+
2462+
// The parser was not used before, so there is a new line after row2
2463+
$this->assertSame($expected, $this->parser->parse($longDocument));
2464+
2465+
$parser = new Parser();
2466+
// The first parsing set and fixed the totalNumberOfLines in the Parser before, so parsing the short document here
2467+
// to reproduce the issue. If the issue would not have been fixed, the next assertion will fail
2468+
$parser->parse($shortDocument);
2469+
2470+
// After the total number of lines has been rset the result will be the same as if a new parser was used
2471+
// (before, there was no \n after row2)
2472+
$this->assertSame($expected, $parser->parse($longDocument));
2473+
}
24412474
}
24422475

24432476
class B

0 commit comments

Comments
 (0)