Skip to content

Commit d847282

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: Added progressive jpeg to mime types guesser [Yaml] Fix wrong line number when comments are inserted in the middle of a block. Fixed singular of committee Do not inject web debug toolbar on attachments bumped Symfony version to 2.7.15 updated VERSION for 2.7.14 update CONTRIBUTORS for 2.7.14 updated CHANGELOG for 2.7.14 [Console] [SymfonyStyle] Replace long word wrapping test to directly test output
2 parents 815fabf + 23d85dd commit d847282

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

Parser.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
432432
}
433433

434434
// we ignore "comment" lines only when we are not inside a scalar block
435-
if (empty($blockScalarIndentations) && $this->isCurrentLineComment()) {
435+
if (empty($blockScalarIndentations) && $this->isCurrentLineComment() && false === $this->checkIfPreviousNonCommentLineIsCollectionItem()) {
436436
continue;
437437
}
438438

@@ -468,10 +468,18 @@ private function moveToNextLine()
468468

469469
/**
470470
* Moves the parser to the previous line.
471+
*
472+
* @return bool
471473
*/
472474
private function moveToPreviousLine()
473475
{
476+
if ($this->currentLineNb < 1) {
477+
return false;
478+
}
479+
474480
$this->currentLine = $this->lines[--$this->currentLineNb];
481+
482+
return true;
475483
}
476484

477485
/**
@@ -794,4 +802,44 @@ private function isBlockScalarHeader()
794802
{
795803
return (bool) preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine);
796804
}
805+
806+
/**
807+
* Returns true if the current line is a collection item.
808+
*
809+
* @return bool
810+
*/
811+
private function isCurrentLineCollectionItem()
812+
{
813+
$ltrimmedLine = ltrim($this->currentLine, ' ');
814+
815+
return '' !== $ltrimmedLine && '-' === $ltrimmedLine[0];
816+
}
817+
818+
/**
819+
* Tests whether the current comment line is in a collection.
820+
*
821+
* @return bool
822+
*/
823+
private function checkIfPreviousNonCommentLineIsCollectionItem()
824+
{
825+
$isCollectionItem = false;
826+
$moves = 0;
827+
while ($this->moveToPreviousLine()) {
828+
++$moves;
829+
// If previous line is a comment, move back again.
830+
if ($this->isCurrentLineComment()) {
831+
continue;
832+
}
833+
$isCollectionItem = $this->isCurrentLineCollectionItem();
834+
break;
835+
}
836+
837+
// Move parser back to previous line.
838+
while ($moves > 0) {
839+
$this->moveToNextLine();
840+
--$moves;
841+
}
842+
843+
return $isCollectionItem;
844+
}
797845
}

Tests/ParserTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,74 @@ public function testAdditionallyIndentedLinesAreParsedAsNewLinesInFoldedBlocks()
11281128
$this->parser->parse($yaml)
11291129
);
11301130
}
1131+
1132+
/**
1133+
* @param $lineNumber
1134+
* @param $yaml
1135+
* @dataProvider parserThrowsExceptionWithCorrectLineNumberProvider
1136+
*/
1137+
public function testParserThrowsExceptionWithCorrectLineNumber($lineNumber, $yaml)
1138+
{
1139+
$this->setExpectedException(
1140+
'\Symfony\Component\Yaml\Exception\ParseException',
1141+
sprintf('Unexpected characters near "," at line %d (near "bar: "123",").', $lineNumber)
1142+
);
1143+
1144+
$this->parser->parse($yaml);
1145+
}
1146+
1147+
public function parserThrowsExceptionWithCorrectLineNumberProvider()
1148+
{
1149+
return array(
1150+
array(
1151+
4,
1152+
<<<YAML
1153+
foo:
1154+
-
1155+
# bar
1156+
bar: "123",
1157+
YAML
1158+
),
1159+
array(
1160+
5,
1161+
<<<YAML
1162+
foo:
1163+
-
1164+
# bar
1165+
# bar
1166+
bar: "123",
1167+
YAML
1168+
),
1169+
array(
1170+
8,
1171+
<<<YAML
1172+
foo:
1173+
-
1174+
# foobar
1175+
baz: 123
1176+
bar:
1177+
-
1178+
# bar
1179+
bar: "123",
1180+
YAML
1181+
),
1182+
array(
1183+
10,
1184+
<<<YAML
1185+
foo:
1186+
-
1187+
# foobar
1188+
# foobar
1189+
baz: 123
1190+
bar:
1191+
-
1192+
# bar
1193+
# bar
1194+
bar: "123",
1195+
YAML
1196+
),
1197+
);
1198+
}
11311199
}
11321200

11331201
class B

0 commit comments

Comments
 (0)