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 7660fb8

Browse filesBrowse files
committed
bug #16864 [Yaml] fix indented line handling in folded blocks (xabbuh)
This PR was merged into the 2.3 branch. Discussion ---------- [Yaml] fix indented line handling in folded blocks | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #9105 | License | MIT | Doc PR | Commits ------- 756834c [Yaml] fix indented line handling in folded blocks
2 parents af3f4ea + 756834c commit 7660fb8
Copy full SHA for 7660fb8

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
@@ -468,13 +468,13 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
468468
}
469469

470470
$isCurrentLineBlank = $this->isCurrentLineBlank();
471-
$text = '';
471+
$blockLines = array();
472472

473473
// leading blank lines are consumed before determining indentation
474474
while ($notEOF && $isCurrentLineBlank) {
475475
// newline only if not EOF
476476
if ($notEOF = $this->moveToNextLine()) {
477-
$text .= "\n";
477+
$blockLines[] = '';
478478
$isCurrentLineBlank = $this->isCurrentLineBlank();
479479
}
480480
}
@@ -495,37 +495,59 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
495495
preg_match($pattern, $this->currentLine, $matches)
496496
)
497497
) {
498-
if ($isCurrentLineBlank) {
499-
$text .= substr($this->currentLine, $indentation);
498+
if ($isCurrentLineBlank && strlen($this->currentLine) > $indentation) {
499+
$blockLines[] = substr($this->currentLine, $indentation);
500+
} elseif ($isCurrentLineBlank) {
501+
$blockLines[] = '';
500502
} else {
501-
$text .= $matches[1];
503+
$blockLines[] = $matches[1];
502504
}
503505

504506
// newline only if not EOF
505507
if ($notEOF = $this->moveToNextLine()) {
506-
$text .= "\n";
507508
$isCurrentLineBlank = $this->isCurrentLineBlank();
508509
}
509510
}
510511
} elseif ($notEOF) {
511-
$text .= "\n";
512+
$blockLines[] = '';
512513
}
513514

514515
if ($notEOF) {
516+
$blockLines[] = '';
515517
$this->moveToPreviousLine();
516518
}
517519

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

531553
// 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
@@ -820,6 +820,57 @@ public function getCommentLikeStringInScalarBlockData()
820820
array($yaml2, $expected2),
821821
);
822822
}
823+
824+
public function testBlankLinesAreParsedAsNewLinesInFoldedBlocks()
825+
{
826+
$yaml = <<<EOT
827+
test: >
828+
<h2>A heading</h2>
829+
830+
<ul>
831+
<li>a list</li>
832+
<li>may be a good example</li>
833+
</ul>
834+
EOT;
835+
836+
$this->assertSame(
837+
array(
838+
'test' => <<<EOT
839+
<h2>A heading</h2>
840+
<ul> <li>a list</li> <li>may be a good example</li> </ul>
841+
EOT
842+
,
843+
),
844+
$this->parser->parse($yaml)
845+
);
846+
}
847+
848+
public function testAdditionallyIndentedLinesAreParsedAsNewLinesInFoldedBlocks()
849+
{
850+
$yaml = <<<EOT
851+
test: >
852+
<h2>A heading</h2>
853+
854+
<ul>
855+
<li>a list</li>
856+
<li>may be a good example</li>
857+
</ul>
858+
EOT;
859+
860+
$this->assertSame(
861+
array(
862+
'test' => <<<EOT
863+
<h2>A heading</h2>
864+
<ul>
865+
<li>a list</li>
866+
<li>may be a good example</li>
867+
</ul>
868+
EOT
869+
,
870+
),
871+
$this->parser->parse($yaml)
872+
);
873+
}
823874
}
824875

825876
class B

0 commit comments

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