10000 merged branch ezzatron/yaml-folded-leading-newlines (PR #7997) · symfony/symfony@181d0c6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 181d0c6

Browse files
committed
merged branch ezzatron/yaml-folded-leading-newlines (PR #7997)
This PR was merged into the 2.1 branch. Discussion ---------- Fixed parsing of leading blank lines in folded scalars. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #7989 | License | MIT | Doc PR | n/a Fixed the issue raised in #7989. The cause was in the way the parser determined the indentation level of block scalars. When the indentation is not manually specified, the parser now determines the indentation from the first *non-empty* line as per section [8.1.1.1.](http://www.yaml.org/spec/1.2/spec.html#id2793979) of the spec. Commits ------- a5441b2 Fixed parsing of leading blank lines in folded scalars. Closes #7989.
2 parents d2a542c + a5441b2 commit 181d0c6

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 12 additions & 2 deletions
+
// newline only if not EOF
Original file line numberDiff line numberDiff line change
@@ -419,18 +419,28 @@ private function parseFoldedScalar($separator, $indicator = '', $indentation = 0
419419
return '';
420420
}
421421

422+
$isCurrentLineBlank = $this->isCurrentLineBlank();
423+
$text = '';
424+
425+
// leading blank lines are consumed before determining indentation
426+
while ($notEOF && $isCurrentLineBlank) {
427
428+
if ($notEOF = $this->moveToNextLine()) {
429+
$text .= "\n";
430+
$isCurrentLineBlank = $this->isCurrentLineBlank();
431+
}
432+
}
433+
422434
// determine indentation if not specified
423435
if (0 === $indentation) {
424436
if (preg_match('/^ +/', $this->currentLine, $matches)) {
425437
$indentation = strlen($matches[0]);
426438
}
427439
}
428440

429-
$text = '';
430441
if ($indentation > 0) {
431442
$pattern = sprintf('/^ {%d}(.*)$/', $indentation);
432443

433-
$isCurrentLineBlank = $this->isCurrentLineBlank();
434444
while (
435445
$notEOF && (
436446
$isCurrentLineBlank ||

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,27 @@ public function testBlockChomping($expected, $yaml)
396396
$this->assertSame($expected, $this->parser->parse($yaml));
397397
}
398398

399+
/**
400+
* Regression test for issue #7989.
401+
*
402+
* @see https://github.com/symfony/symfony/issues/7989
403+
*/
404+
public function testBlockLiteralWithLeadingNewlines()
405+
{
406+
$yaml = <<<'EOF'
407+
foo: |-
408+
409+
410+
bar
411+
412+
EOF;
413+
$expected = array(
414+
'foo' => "\n\nbar"
415+
);
416+
417+
$this->assertSame($expected, $this->parser->parse($yaml));
418+
}
419+
399420
public function testObjectSupportEnabled()
400421
{
401422
$input = <<<EOF

0 commit comments

Comments
 (0)
0