Skip to content

Commit 8dde76b

Browse files
committed
Merge branch '2.3' into 2.7
2 parents cb5204a + b2cbedb commit 8dde76b

File tree

2 files changed

+67
-15
lines changed

2 files changed

+67
-15
lines changed

Parser.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,11 @@ private function getCurrentLineIndentation()
343343
private function getNextEmbedBlock($indentation = null, $inSequence = false)
344344
{
345345
$oldLineIndentation = $this->getCurrentLineIndentation();
346-
$insideBlockScalar = $this->isBlockScalarHeader();
346+
$blockScalarIndentations = array();
347+
348+
if ($this->isBlockScalarHeader()) {
349+
$blockScalarIndentations[] = $this->getCurrentLineIndentation();
350+
}
347351

348352
if (!$this->moveToNextLine()) {
349353
return;
@@ -380,17 +384,26 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
380384

381385
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
382386

383-
if (!$insideBlockScalar) {
384-
$insideBlockScalar = $this->isBlockScalarHeader();
387+
if (empty($blockScalarIndentations) && $this->isBlockScalarHeader()) {
388+
$blockScalarIndentations[] = $this->getCurrentLineIndentation();
385389
}
386390

387391
$previousLineIndentation = $this->getCurrentLineIndentation();
388392

389393
while ($this->moveToNextLine()) {
390394
$indent = $this->getCurrentLineIndentation();
391395

392-
if (!$insideBlockScalar && $indent === $previousLineIndentation) {
393-
$insideBlockScalar = $this->isBlockScalarHeader();
396+
// terminate all block scalars that are more indented than the current line
397+
if (!empty($blockScalarIndentations) && $indent < $previousLineIndentation && trim($this->currentLine) !== '') {
398+
foreach ($blockScalarIndentations as $key => $blockScalarIndentation) {
399+
if ($blockScalarIndentation >= $this->getCurrentLineIndentation()) {
400+
unset($blockScalarIndentations[$key]);
401+
}
402+
}
403+
}
404+
405+
if (empty($blockScalarIndentations) && !$this->isCurrentLineComment() && $this->isBlockScalarHeader()) {
406+
$blockScalarIndentations[] = $this->getCurrentLineIndentation();
394407
}
395408

396409
$previousLineIndentation = $indent;
@@ -406,7 +419,7 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
406419
}
407420

408421
// we ignore "comment" lines only when we are not inside a scalar block
409-
if (!$insideBlockScalar && $this->isCurrentLineComment()) {
422+
if (empty($blockScalarIndentations) && $this->isCurrentLineComment()) {
410423
continue;
411424
}
412425

@@ -564,7 +577,7 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
564577
$previousLineIndented = false;
565578
$previousLineBlank = false;
566579

567-
for ($i = 0; $i < count($blockLines); $i++) {
580+
for ($i = 0; $i < count($blockLines); ++$i) {
568581
if ('' === $blockLines[$i]) {
569582
$text .= "\n";
570583
$previousLineIndented = false;
@@ -659,7 +672,7 @@ private function isCurrentLineComment()
659672
//checking explicitly the first char of the trim is faster than loops or strpos
660673
$ltrimmedLine = ltrim($this->currentLine, ' ');
661674

662-
return $ltrimmedLine[0] === '#';
675+
return '' !== $ltrimmedLine && $ltrimmedLine[0] === '#';
663676
}
664677

665678
/**

Tests/ParserTest.php

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,9 @@ public function testCommentLikeStringsAreNotStrippedInBlockScalars($yaml, $expec
816816

817817
public function getCommentLikeStringInScalarBlockData()
818818
{
819-
$yaml1 = <<<'EOT'
819+
$tests = array();
820+
821+
$yaml = <<<'EOT'
820822
pages:
821823
-
822824
title: some title
@@ -831,7 +833,7 @@ public function getCommentLikeStringInScalarBlockData()
831833
832834
footer # comment3
833835
EOT;
834-
$expected1 = array(
836+
$expected = array(
835837
'pages' => array(
836838
array(
837839
'title' => 'some title',
@@ -850,8 +852,9 @@ public function getCommentLikeStringInScalarBlockData()
850852
),
851853
),
852854
);
855+
$tests[] = array($yaml, $expected);
853856

854-
$yaml2 = <<<'EOT'
857+
$yaml = <<<'EOT'
855858
test: |
856859
foo
857860
# bar
@@ -866,7 +869,7 @@ public function getCommentLikeStringInScalarBlockData()
866869
# bar
867870
baz
868871
EOT;
869-
$expected2 = array(
872+
$expected = array(
870873
'test' => <<<'EOT'
871874
foo
872875
# bar
@@ -893,11 +896,47 @@ public function getCommentLikeStringInScalarBlockData()
893896
),
894897
),
895898
);
899+
$tests[] = array($yaml, $expected);
896900

897-
return array(
898-
array($yaml1, $expected1),
899-
array($yaml2, $expected2),
901+
$yaml = <<<EOT
902+
foo:
903+
bar:
904+
scalar-block: >
905+
line1
906+
line2>
907+
baz:
908+
# comment
909+
foobar: ~
910+
EOT;
911+
$expected = array(
912+
'foo' => array(
913+
'bar' => array(
914+
'scalar-block' => 'line1 line2>',
915+
),
916+
'baz' => array(
917+
'foobar' => null,
918+
),
919+
),
900920
);
921+
$tests[] = array($yaml, $expected);
922+
923+
$yaml = <<<'EOT'
924+
a:
925+
b: hello
926+
# c: |
927+
# first row
928+
# second row
929+
d: hello
930+
EOT;
931+
$expected = array(
932+
'a' => array(
933+
'b' => 'hello',
934+
'd' => 'hello',
935+
),
936+
);
937+
$tests[] = array($yaml, $expected);
938+
939+
return $tests;
901940
}
902941

903942
public function testBlankLinesAreParsedAsNewLinesInFoldedBlocks()

0 commit comments

Comments
 (0)