Skip to content

Commit 6fbc4e2

Browse files
Merge branch '2.7' into 2.8
* 2.7: [SecurityBundle] Removing test insulations for a huge perf win [Validator] Use the new interface in the README [Filesystem] fix tests on 2.3 [Filesystem] Recursivly widen non-executable directories [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 [Console][Table] fixed render row with multiple cells. [Yaml] do not remove "comments" in scalar blocks Conflicts: src/Symfony/Component/Console/Application.php src/Symfony/Component/Console/Tests/Fixtures/application_renderexception1.txt src/Symfony/Component/Console/Tests/Fixtures/application_renderexception2.txt src/Symfony/Component/Console/Tests/Fixtures/application_renderexception4.txt src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php src/Symfony/Component/Yaml/Tests/ParserTest.php
2 parents aceecdb + 9101150 commit 6fbc4e2

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

@@ -719,4 +725,14 @@ private function isStringUnIndentedCollectionItem()
719725
{
720726
return 0 === strpos($this->currentLine, '- ');
721727
}
728+
729+
/**
730+
* Tests whether or not the current line is the header of a block scalar.
731+
*
732+
* @return bool
733+
*/
734+
private function isBlockScalarHeader()
735+
{
736+
return (bool) preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine);
737+
}
722738
}

Tests/ParserTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,100 @@ public function testColonInMappingValueExceptionNotTriggeredByColonInComment()
818818

819819
$this->assertSame(array('foo' => array('bar' => 'foobar')), $this->parser->parse($yaml));
820820
}
821+
822+
/**
823+
* @dataProvider getCommentLikeStringInScalarBlockData
824+
*/
825+
public function testCommentLikeStringsAreNotStrippedInBlockScalars($yaml, $expectedParserResult)
826+
{
827+
$this->assertSame($expectedParserResult, $this->parser->parse($yaml));
828+
}
829+
830+
public function getCommentLikeStringInScalarBlockData()
831+
{
832+
$yaml1 = <<<EOT
833+
pages:
834+
-
835+
title: some title
836+
content: |
837+
# comment 1
838+
header
839+
840+
# comment 2
841+
<body>
842+
<h1>title</h1>
843+
</body>
844+
845+
footer # comment3
846+
EOT;
847+
$expected1 = array(
848+
'pages' => array(
849+
array(
850+
'title' => 'some title',
851+
'content' => <<<EOT
852+
# comment 1
853+
header
854+
855+
# comment 2
856+
<body>
857+
<h1>title</h1>
858+
</body>
859+
860+
footer # comment3
861+
EOT
862+
,
863+
),
864+
),
865+
);
866+
867+
$yaml2 = <<<EOT
868+
test: |
869+
foo
870+
# bar
871+
baz
872+
collection:
873+
- one: |
874+
foo
875+
# bar
876+
baz
877+
- two: |
878+
foo
879+
# bar
880+
baz
881+
EOT;
882+
$expected2 = array(
883+
'test' => <<<EOT
884+
foo
885+
# bar
886+
baz
887+
888+
EOT
889+
,
890+
'collection' => array(
891+
array(
892+
'one' => <<<EOT
893+
foo
894+
# bar
895+
baz
896+
EOT
897+
,
898+
),
899+
array(
900+
'two' => <<<EOT
901+
foo
902+
# bar
903+
baz
904+
EOT
905+
,
906+
),
907+
),
908+
);
909+
910+
return array(
911+
array($yaml1, $expected1),
912+
array($yaml2, $expected2),
913+
);
914+
}
821915
}
822916

823917
class B

0 commit comments

Comments
 (0)