10000 [Yaml] Improve newline handling in folded scalar blocks · symfony/symfony@3fc7774 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3fc7774

Browse files
committed
[Yaml] Improve newline handling in folded scalar blocks
1 parent 7ebda1e commit 3fc7774

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
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_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';
2424

2525
private $offset = 0;
2626
private $lines = array();
@@ -331,7 +331,7 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
331331
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem($this->currentLine);
332332

333333
// Comments must not be removed inside a string block (ie. after a line ending with "|")
334-
$removeCommentsPattern = '~'.self::FOLDED_SCALAR_PATTERN.'$~';
334+
$removeCommentsPattern = '~'.self::BLOCK_SCALAR_PATTERN.'$~';
335335
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
336336

337337
while ($this->moveToNextLine()) {
@@ -420,10 +420,10 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
420420
return $this->refs[$value];
421421
}
422422

423-
if (preg_match('/^'.self::FOLDED_SCALAR_PATTERN.'$/', $value, $matches)) {
423+
if (preg_match('/^'.self::BLOCK_SCALAR_PATTERN.'$/', $value, $matches)) {
424424
$modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
425425

426-
return $this->parseFoldedScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
426+
return $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
427427
}
428428

429429
try {
@@ -437,15 +437,15 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
437437
}
438438

439439
/**
440-
* Parses a folded scalar.
440+
* Parses a block scalar.
441441
*
442-
* @param string $separator The separator that was used to begin this folded scalar (| or >)
443-
* @param string $indicator The indicator that was used to begin this folded scalar (+ or -)
444-
* @param int $indentation The indentation that was used to begin this folded scalar
442+
* @param string $separator The separator that was used to begin this block scalar (| or >)
443+
* @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -)
444+
* @param int $indentation The indentation indicator that was used to begin this block scalar
445445
*
446446
* @return string The text value
447447
*/
448-
private function parseFoldedScalar($separator, $indicator = '', $indentation = 0)
448+
private function parseBlockScalar($separator, $chomping = '', $indentation = 0)
449449
{
450450
$notEOF = $this->moveToNextLine();
451451
if (!$notEOF) {
@@ -500,17 +500,25 @@ private function parseFoldedScalar($separator, $indicator = '', $indentation = 0
500500
$this->moveToPreviousLine();
501501
}
502502

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

510-
// deal with trailing newlines as indicated
511-
if ('' === $indicator) {
518+
// deal with trailing newlines
519+
if ('' === $chomping) {
512520
$text = preg_replace('/\n+$/', "\n", $text);
513-
} elseif ('-' === $indicator) {
521+
} elseif ('-' === $chomping) {
514522
$text = preg_replace('/\n+$/', '', $text);
515523
}
516524

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)
0