Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 286367e

Browse filesBrowse files
committed
[Yaml] fix indented line handling in folded blocks
1 parent 2d14689 commit 286367e
Copy full SHA for 286367e

File tree

2 files changed

+89
-16
lines changed
Filter options

2 files changed

+89
-16
lines changed

‎src/Symfony/Component/Yaml/Parser.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Parser.php
+38-16Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -462,13 +462,13 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
462462
}
463463

464464
$isCurrentLineBlank = $this->isCurrentLineBlank();
465-
$text = '';
465+
$blockLines = array();
466466

467467
// leading blank lines are consumed before determining indentation
468468
while ($notEOF && $isCurrentLineBlank) {
469469
// newline only if not EOF
470470
if ($notEOF = $this->moveToNextLine()) {
471-
$text .= "\n";
471+
$blockLines[] = '';
472472
$isCurrentLineBlank = $this->isCurrentLineBlank();
473473
}
474474
}
@@ -489,37 +489,59 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
489489
preg_match($pattern, $this->currentLine, $matches)
490490
)
491491
) {
492-
if ($isCurrentLineBlank) {
493-
$text .= substr($this->currentLine, $indentation);
492+
if ($isCurrentLineBlank && strlen($this->currentLine) > $indentation) {
493+
$blockLines[] = substr($this->currentLine, $indentation);
494+
} elseif ($isCurrentLineBlank) {
495+
$blockLines[] = '';
494496
} else {
495-
$text .= $matches[1];
497+
$blockLines[] = $matches[1];
496498
}
497499

498500
// newline only if not EOF
499501
if ($notEOF = $this->moveToNextLine()) {
500-
$text .= "\n";
501502
$isCurrentLineBlank = $this->isCurrentLineBlank();
502503
}
503504
}
504505
} elseif ($notEOF) {
505-
$text .= "\n";
506+
$blockLines[] = '';
506507
}
507508

508509
if ($notEOF) {
510+
$blockLines[] = '';
509511
$this->moveToPreviousLine();
510512
}
511513

512514
// folded style
513515
if ('>' === $style) {
514-
// folded lines
515-
// replace all non-leading/non-trailing single newlines with spaces
516-
preg_match('/(\n*)$/', $text, $matches);
517-
$text = preg_replace('/(?<!\n|^)\n(?!\n)/', ' ', rtrim($text, "\n"));
518-
$text .= $matches[1];
519-
520-
// empty separation lines
521-
// remove one newline from each group of non-leading/non-trailing newlines
522-
$text = preg_replace('/[^\n]\n+\K\n(?=[^\n])/', '', $text);
516+
$text = '';
517+
$previousLineIndented = false;
518+
$previousLineBlank = false;
519+
520+
for ($i = 0; $i < count($blockLines); $i++) {
521+
if ('' === $blockLines[$i]) {
522+
$text .= "\n";
523+
$previousLineIndented = false;
524+
$previousLineBlank = true;
525+
} elseif (' ' === $blockLines[$i][0]) {
526+
$text .= "\n".$blockLines[$i];
527+
$previousLineIndented = true;
528+
$previousLineBlank = false;
529+
} elseif ($previousLineIndented) {
530+
$text .= "\n".$blockLines[$i];
531+
$previousLineIndented = false;
532+
$previousLineBlank = false;
533+
} elseif ($previousLineBlank || 0 === $i) {
534+
$text .= $blockLines[$i];
535+
$previousLineIndented = false;
536+
$previousLineBlank = false;
537+
} else {
538+
$text .= ' '.$blockLines[$i];
539+
$previousLineIndented = false;
540+
$previousLineBlank = false;
541+
}
542+
}
543+
} else {
544+
$text = implode("\n", $blockLines);
523545
}
524546

525547
// deal with trailing newlines

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/ParserTest.php
+51Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,57 @@ public function testFloatKeys()
726726

727727
$this->assertEquals($expected, $this->parser->parse($yaml));
728728
}
729+
730+
public function testBlankLinesAreParsedAsNewLinesInFoldedBlocks()
731+
{
732+
$yaml = <<<EOT
733+
test: >
734+
<h2>A heading</h2>
735+
736+
<ul>
737+
<li>a list</li>
738+
<li>may be a good example</li>
739+
</ul>
740+
EOT;
741+
742+
$this->assertSame(
743+
array(
744+
'test' => <<<EOT
745+
<h2>A heading</h2>
746+
<ul> <li>a list</li> <li>may be a good example</li> </ul>
747+
EOT
748+
,
749+
),
750+
$this->parser->parse($yaml)
751+
);
752+
}
753+
754+
public function testAdditionallyIndentedLinesAreParsedAsNewLinesInFoldedBlocks()
755+
{
756+
$yaml = <<<EOT
757+
test: >
758+
<h2>A heading</h2>
759+
760+
<ul>
761+
<li>a list</li>
762+
<li>may be a good example</li>
763+
</ul>
764+
EOT;
765+
766+
$this->assertSame(
767+
array(
768+
'test' => <<<EOT
769+
<h2>A heading</h2>
770+
<ul>
771+
<li>a list</li>
772+
<li>may be a good example</li>
773+
</ul>
774+
EOT
775+
,
776+
),
777+
$this->parser->parse($yaml)
778+
);
779+
}
729780
}
730781

731782
class B

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.