Skip to content

Commit 73366d5

Browse files
committed
[Yaml] Improve newline handling in folded scalar blocks
1 parent 620a3d4 commit 73366d5

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
*/
2121
class Parser
2222
{
23-
const FOLDED_SCALAR_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';
23+
const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';
24+
// BC - wrongly named
25+
const FOLDED_SCALAR_PATTERN = self::BLOCK_SCALAR_HEADER_PATTERN;
2426

2527
private $offset = 0;
2628
private $lines = array();
@@ -332,8 +334,8 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
332334

333335
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem($this->currentLine);
334336

335-
// Comments must not be removed inside a string block (ie. after a line ending with "|")
336-
$removeCommentsPattern = '~'.self::FOLDED_SCALAR_PATTERN.'$~';
337+
// Comments must not be removed inside a block scalar
338+
$removeCommentsPattern = '~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~';
337339
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
338340

339341
while ($this->moveToNextLine()) {
@@ -422,10 +424,10 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
422424
return $this->refs[$value];
423425
}
424426

425-
if (preg_match('/^'.self::FOLDED_SCALAR_PATTERN.'$/', $value, $matches)) {
427+
if (preg_match('/^'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
426428
$modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
427429

428-
return $this->parseFoldedScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
430+
return $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
429431
}
430432

431433
try {
@@ -439,15 +441,15 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
439441
}
440442

441443
/**
442-
* Parses a folded scalar.
444+
* Parses a block scalar.
443445
*
444-
* @param string $separator The separator that was used to begin this folded scalar (| or >)
445-
* @param string $indicator The indicator that was used to begin this folded scalar (+ or -)
446-
* @param int $indentation The indentation that was used to begin this folded scalar
446+
* @param string $style The style indicator that was used to begin this block scalar (| or >)
447+
* @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -)
448+
* @param int $indentation The indentation indicator that was used to begin this block scalar
447449
*
448450
* @return string The text value
449451
*/
450-
private function parseFoldedScalar($separator, $indicator = '', $indentation = 0)
452+
private function parseBlockScalar($style, $chomping = '', $indentation = 0)
451453
{
452454
$notEOF = $this->moveToNextLine();
453455
if (!$notEOF) {
@@ -502,17 +504,23 @@ private function parseFoldedScalar($separator, $indicator = '', $indentation = 0
502504
$this->moveToPreviousLine();
503505
}
504506

505-
// replace all non-trailing single newlines with spaces in folded blocks
506-
if ('>' === $separator) {
507+
// folded style
508+
if ('>' === $style) {
509+
// folded lines
510+
// replace all non-leading/non-trailing single newlines with spaces
507511
preg_match('/(\n*)$/', $text, $matches);
508-
$text = preg_replace('/(?<!\n)\n(?!\n)/', ' ', rtrim($text, "\n"));
512+
$text = preg_replace('/(?<!\n|^)\n(?!\n)/', ' ', rtrim($text, "\n"));
509513
$text .= $matches[1];
514+
515+
// empty separation lines
516+
// remove one newline from each group of non-leading/non-trailing newlines
517+
$text = preg_replace('/[^\n]\n+\K\n(?=[^\n])/', '', $text);
510518
}
511519

512-
// deal with trailing newlines as indicated
513-
if ('' === $indicator) {
520+
// deal with trailing newlines
521+
if ('' === $chomping) {
514522
$text = preg_replace('/\n+$/', "\n", $text);
515-
} elseif ('-' === $indicator) {
523+
} elseif ('-' === $chomping) {
516524
$text = preg_replace('/\n+$/', '', $text);
517525
}
518526

src/Symfony/Component/Yaml/Tests/Fixtures/sfTests.yml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ php: |
5151
'~',
5252
)
5353
---
54-
test: Empty lines in folded blocks
54+
test: Empty lines in literal blocks
5555
brief: >
56-
Empty lines in folded blocks
56+
Empty lines in literal blocks
5757
yaml: |
5858
foo:
5959
bar: |
@@ -65,6 +65,20 @@ yaml: |
6565
php: |
6666
array('foo' => array('bar' => "foo\n\n\n \nbar\n"))
6767
---
68+
test: Empty lines in folded blocks
69+
brief: >
70+
Empty lines in folded blocks
71+
yaml: |
72+
foo:
73+
bar: >
74+
75+
foo
76+
77+
78+
bar
79+
php: |
80+
array('foo' => array('bar' => "\nfoo\n\nbar\n"))
81+
---
6882
test: IP addresses
6983
brief: >
7084
IP addresses

0 commit comments

Comments
 (0)