Skip to content

Commit 9101150

Browse files
committed
Merge branch '2.3' into 2.7
* 2.3: [Form] fix #15544 when a collection type attribute "required" is false, "prototype" should too updated validators.bg.xlf [Security] Enable bcrypt validation and result length tests on all PHP versions [Security] Verify if a password encoded with bcrypt is no longer than 72 characters [Console] Avoid extra blank lines when rendering exceptions [Yaml] do not remove "comments" in scalar blocks
2 parents 0717f9a + 5f77b9a commit 9101150

File tree

2 files changed

+116
-6
lines changed

2 files changed

+116
-6
lines changed

Parser.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ private function getCurrentLineIndentation()
339339
private function getNextEmbedBlock($indentation = null, $inSequence = false)
340340
{
341341
$oldLineIndentation = $this->getCurrentLineIndentation();
342+
$insideBlockScalar = $this->isBlockScalarHeader();
342343

343344
if (!$this->moveToNextLine()) {
344345
return;
@@ -375,17 +376,21 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
375376

376377
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
377378

378-
// Comments must not be removed inside a block scalar
379-
$removeCommentsPattern = '~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~';
380-
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
379+
if (!$insideBlockScalar) {
380+
$insideBlockScalar = $this->isBlockScalarHeader();
381+
}
382+
383+
$previousLineIndentation = $this->getCurrentLineIndentation();
381384

382385
while ($this->moveToNextLine()) {
383386
$indent = $this->getCurrentLineIndentation();
384387

385-
if ($indent === $newIndent) {
386-
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
388+
if (!$insideBlockScalar && $indent === $previousLineIndentation) {
389+
$insideBlockScalar = $this->isBlockScalarHeader();
387390
}
388391

392+
$previousLineIndentation = $indent;
393+
389394
if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
390395
$this->moveToPreviousLine();
391396
break;
@@ -396,7 +401,8 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
396401
continue;
397402
}
398403

399-
if ($removeComments && $this->isCurrentLineComment()) {
404+
// we ignore "comment" lines only when we are not inside a scalar block
405+
if (!$insideBlockScalar && $this->isCurrentLineComment()) {
400406
continue;
401407
}
402408

@@ -709,4 +715,14 @@ private function isStringUnIndentedCollectionItem()
709715
{
710716
return 0 === strpos($this->currentLine, '- ');
711717
}
718+
719+
/**
720+
* Tests whether or not the current line is the header of a block scalar.
721+
*
722+
* @return bool
723+
*/
724+
private function isBlockScalarHeader()
725+
{
726+
return (bool) preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine);
727+
}
712728
}

Tests/ParserTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,100 @@ public function testFloatKeys()
783783

784784
$this->assertEquals($expected, $this->parser->parse($yaml));
785785
}
786+
787+
/**
788+
* @dataProvider getCommentLikeStringInScalarBlockData
789+
*/
790+
public function testCommentLikeStringsAreNotStrippedInBlockScalars($yaml, $expectedParserResult)
791+
{
792+
$this->assertSame($expectedParserResult, $this->parser->parse($yaml));
793+
}
794+
795+
public function getCommentLikeStringInScalarBlockData()
796+
{
797+
$yaml1 = <<<EOT
798+
pages:
799+
-
800+
title: some title
801+
content: |
802+
# comment 1
803+
header
804+
805+
# comment 2
806+
<body>
807+
<h1>title</h1>
808+
</body>
809+
810+
footer # comment3
811+
EOT;
812+
$expected1 = array(
813+
'pages' => array(
814+
array(
815+
'title' => 'some title',
816+
'content' => <<<EOT
817+
# comment 1
818+
header
819+
820+
# comment 2
821+
<body>
822+
<h1>title</h1>
823+
</body>
824+
825+
footer # comment3
826+
EOT
827+
,
828+
),
829+
),
830+
);
831+
832+
$yaml2 = <<<EOT
833+
test: |
834+
foo
835+
# bar
836+
baz
837+
collection:
838+
- one: |
839+
foo
840+
# bar
841+
baz
842+
- two: |
843+
foo
844+
# bar
845+
baz
846+
EOT;
847+
$expected2 = array(
848+
'test' => <<<EOT
849+
foo
850+
# bar
851+
baz
852+
853+
EOT
854+
,
855+
'collection' => array(
856+
array(
857+
'one' => <<<EOT
858+
foo
859+
# bar
860+
baz
861+
EOT
862+
,
863+
),
864+
array(
865+
'two' => <<<EOT
866+
foo
867+
# bar
868+
baz
869+
EOT
870+
,
871+
),
872+
),
873+
);
874+
875+
return array(
876+
array($yaml1, $expected1),
877+
array($yaml2, $expected2),
878+
);
879+
}
786880
}
787881

788882
class B

0 commit comments

Comments
 (0)