8000 bug #15482 [Yaml] Improve newline handling in folded scalar blocks (t… · symfony/symfony@c0ff4bf · GitHub
[go: up one dir, main page]

Skip to content

Commit c0ff4bf

Browse files
committed
bug #15482 [Yaml] Improve newline handling in folded scalar blocks (teohhanhui)
This PR was merged into the 2.3 branch. Discussion ---------- [Yaml] Improve newline handling in folded scalar blocks | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #15477 | License | MIT | Doc PR | N/A Commits ------- 73366d5 [Yaml] Improve newline handling in folded scalar blocks
2 parents d8dc8f2 + 73366d5 commit c0ff4bf

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();
@@ -337,8 +339,8 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
337339

338340
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem($this->currentLine);
339341

340-
// Comments must not be removed inside a string block (ie. after a line ending with "|")
341-
$removeCommentsPattern = '~'.self::FOLDED_SCALAR_PATTERN.'$~';
342+
// Comments must not be removed inside a block scalar
343+
$removeCommentsPattern = '~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~';
342344
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
343345

344346
while ($this->moveToNextLine()) {
@@ -427,10 +429,10 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
427429
return $this->refs[$value];
428430
}
429431

430-
if (preg_match('/^'.self::FOLDED_SCALAR_PATTERN.'$/', $value, $matches)) {
432+
if (preg_match('/^'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
431433
$modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
432434

433-
return $this->parseFoldedScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
435+
return $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
434436
}
435437

436438
try {
@@ -444,15 +446,15 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
444446
}
445447

446448
/**
447-
* Parses a folded scalar.
449+
* Parses a block scalar.
448450
*
449-
* @param string $separator The separator that was used to begin this folded scalar (| or >)
450-
* @param string $indicator The indicator that was used to begin this folded scalar (+ or -)
451-
* @param int $indentation The indentation that was used to begin this folded scalar
451+
* @param string $style The style indicator that was used to begin this block scalar (| or >)
452+
* @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -)
453+
* @param int $indentation The indentation indicator that was used to begin this block scalar
452454
*
453455
* @return string The text value
454456
*/
455-
private function parseFoldedScalar($separator, $indicator = '', $indentation = 0)
457+
private function parseBlockScalar($style, $chomping = '', $indentation = 0)
456458
{
457459
$notEOF = $this->moveToNextLine();
458460
if (!$notEOF) {
@@ -507,17 +509,23 @@ private function parseFoldedScalar($separator, $indicator = '', $indentation = 0
507509
$this->moveToPreviousLine();
508510
}
509511

510-
// replace all non-trailing single newlines with spaces in folded blocks
511-
if ('>' === $separator) {
512+
// folded style
513+
if ('>' === $style) {
514+
// folded lines
515+
// replace all non-leading/non-trailing single newlines with spaces
512516
preg_match('/(\n*)$/', $text, $matches);
513-
$text = preg_replace('/(?<!\n)\n(?!\n)/', ' ', rtrim($text, "\n"));
517+
$text = preg_replace('/(?<!\n|^)\n(?!\n)/', ' ', rtrim($text, "\n"));
514518
$text .= $matches[1];
519+
520+
// empty separation lines
521+
// remove one newline from each group of non-leading/non-trailing newlines
522+
$text = preg_replace('/[^\n]\n+\K\n(?=[^\n])/', '', $text);
515523
}
516524

517-
// deal with trailing newlines as indicated
518-
if ('' === $indicator) {
525+
// deal with trailing newlines
526+
if ('' === $chomping) {
519527
$text = preg_replace('/\n+$/', "\n", $text);
520-
} elseif ('-' === $indicator) {
528+
} elseif ('-' === $chomping) {
521529
$text = preg_replace('/\n+$/', '', $text);
522530
}
523531

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