Skip to content

Commit 1d96518

Browse files
committed
Merge branch '2.3' into 2.7
* 2.3: [2.7] Fixed flatten exception recursion with errors Embedded identifier support Change the ExtensionInterface load method definition to bo identical to the documentation. add and correct armenian translations [Config] Fix array sort on normalization in edge case [Yaml] fix indented line handling in folded blocks improve BrowserKit test coverage p1
2 parents 79d7494 + 8f5afcf commit 1d96518

File tree

2 files changed

+89
-16
lines changed

2 files changed

+89
-16
lines changed

Parser.php

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -509,13 +509,13 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
509509
}
510510

511511
$isCurrentLineBlank = $this->isCurrentLineBlank();
512-
$text = '';
512+
$blockLines = array();
513513

514514
// leading blank lines are consumed before determining indentation
515515
while ($notEOF && $isCurrentLineBlank) {
516516
// newline only if not EOF
517517
if ($notEOF = $this->moveToNextLine()) {
518-
$text .= "\n";
518+
$blockLines[] = '';
519519
$isCurrentLineBlank = $this->isCurrentLineBlank();
520520
}
521521
}
@@ -536,37 +536,59 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
536536
preg_match($pattern, $this->currentLine, $matches)
537537
)
538538
) {
539-
if ($isCurrentLineBlank) {
540-
$text .= substr($this->currentLine, $indentation);
539+
if ($isCurrentLineBlank && strlen($this->currentLine) > $indentation) {
540+
$blockLines[] = substr($this->currentLine, $indentation);
541+
} elseif ($isCurrentLineBlank) {
542+
$blockLines[] = '';
541543
} else {
542-
$text .= $matches[1];
544+
$blockLines[] = $matches[1];
543545
}
544546

545547
// newline only if not EOF
546548
if ($notEOF = $this->moveToNextLine()) {
547-
$text .= "\n";
548549
$isCurrentLineBlank = $this->isCurrentLineBlank();
549550
}
550551
}
551552
} elseif ($notEOF) {
552-
$text .= "\n";
553+
$blockLines[] = '';
553554
}
554555

555556
if ($notEOF) {
557+
$blockLines[] = '';
556558
$this->moveToPreviousLine();
557559
}
558560

559561
// folded style
560562
if ('>' === $style) {
561-
// folded lines
562-
// replace all non-leading/non-trailing single newlines with spaces
563-
preg_match('/(\n*)$/', $text, $matches);
564-
$text = preg_replace('/(?<!\n|^)\n(?!\n)/', ' ', rtrim($text, "\n"));
565-
$text .= $matches[1];
566-
567-
// empty separation lines
568-
// remove one newline from each group of non-leading/non-trailing newlines
569-
$text = preg_replace('/[^\n]\n+\K\n(?=[^\n])/', '', $text);
563+
$text = '';
564+
$previousLineIndented = false;
565+
$previousLineBlank = false;
566+
567+
for ($i = 0; $i < count($blockLines); $i++) {
568+
if ('' === $blockLines[$i]) {
569+
$text .= "\n";
570+
$previousLineIndented = false;
571+
$previousLineBlank = true;
572+
} elseif (' ' === $blockLines[$i][0]) {
573+
$text .= "\n".$blockLines[$i];
574+
$previousLineIndented = true;
575+
$previousLineBlank = false;
576+
} elseif ($previousLineIndented) {
577+
$text .= "\n".$blockLines[$i];
578+
$previousLineIndented = false;
579+
$previousLineBlank = false;
580+
} elseif ($previousLineBlank || 0 === $i) {
581+
$text .= $blockLines[$i];
582+
$previousLineIndented = false;
583+
$previousLineBlank = false;
584+
} else {
585+
$text .= ' '.$blockLines[$i];
586+
$previousLineIndented = false;
587+
$previousLineBlank = false;
588+
}
589+
}
590+
} else {
591+
$text = implode("\n", $blockLines);
570592
}
571593

572594
// deal with trailing newlines

Tests/ParserTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,57 @@ public function getCommentLikeStringInScalarBlockData()
899899
array($yaml2, $expected2),
900900
);
901901
}
902+
903+
public function testBlankLinesAreParsedAsNewLinesInFoldedBlocks()
904+
{
905+
$yaml = <<<EOT
906+
test: >
907+
<h2>A heading</h2>
908+
909+
<ul>
910+
<li>a list</li>
911+
<li>may be a good example</li>
912+
</ul>
913+
EOT;
914+
915+
$this->assertSame(
916+
array(
917+
'test' => <<<EOT
918+
<h2>A heading</h2>
919+
<ul> <li>a list</li> <li>may be a good example</li> </ul>
920+
EOT
921+
,
922+
),
923+
$this->parser->parse($yaml)
924+
);
925+
}
926+
927+
public function testAdditionallyIndentedLinesAreParsedAsNewLinesInFoldedBlocks()
928+
{
929+
$yaml = <<<EOT
930+
test: >
931+
<h2>A heading</h2>
932+
933+
<ul>
934+
<li>a list</li>
935+
<li>may be a good example</li>
936+
</ul>
937+
EOT;
938+
939+
$this->assertSame(
940+
array(
941+
'test' => <<<EOT
942+
<h2>A heading</h2>
943+
<ul>
944+
<li>a list</li>
945+
<li>may be a good example</li>
946+
</ul>
947+
EOT
948+
,
949+
),
950+
$this->parser->parse($yaml)
951+
);
952+
}
902953
}
903954

904955
class B

0 commit comments

Comments
 (0)