8000 bug #18828 [Yaml] chomp newlines only at the end of YAML documents (x… · src-run/symfony@26b7922 · GitHub
[go: up one dir, main page]

Skip to content

Commit 26b7922

Browse files
committed
bug symfony#18828 [Yaml] chomp newlines only at the end of YAML documents (xabbuh)
This PR was merged into the 2.3 branch. Discussion ---------- [Yaml] chomp newlines only at the end of YAML documents | Q | A | ------------- | --- | Branch? | 2.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#18784 | License | MIT | Doc PR | Commits ------- a4b1fa6 chomp newlines only at the end of YAML documents
2 parents 76223b2 + a4b1fa6 commit 26b7922

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Parser
2525
const FOLDED_SCALAR_PATTERN = self::BLOCK_SCALAR_HEADER_PATTERN;
2626

2727
private $offset = 0;
28+
private $totalNumberOfLines;
2829
private $lines = array();
2930
private $currentLineNb = -1;
3031
private $currentLine = '';
@@ -33,11 +34,13 @@ class Parser
3334
/**
3435
* Constructor.
3536
*
36-
* @param int $offset The offset of YAML document (used for line numbers in error messages)
37+
* @param int $offset The offset of YAML document (used for line numbers in error messages)
38+
* @param int|null $totalNumberOfLines The overall number of lines being parsed
3739
*/
38-
public function __construct($offset = 0)
40+
public function __construct($offset = 0, $totalNumberOfLines = null)
3941
{
4042
$this->offset = $offset;
43+
$this->totalNumberOfLines = $totalNumberOfLines;
4144
}
4245

4346
/**
@@ -61,6 +64,10 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
6164
$value = $this->cleanup($value);
6265
$this->lines = explode("\n", $value);
6366

67+
if (null === $this->totalNumberOfLines) {
68+
$this->totalNumberOfLines = count($this->lines);
69+
}
70+
6471
if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
6572
$mbEncoding = mb_internal_encoding();
6673
mb_internal_encoding('UTF-8');
@@ -93,7 +100,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
93100
// array
94101
if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
95102
$c = $this->getRealCurrentLineNb() + 1;
96-
$parser = new self($c);
103+
$parser = new self($c, $this->totalNumberOfLines);
97104
$parser->refs = &$this->refs;
98105
$data[] = $parser->parse($this->getNextEmbedBlock(null, true), $exceptionOnInvalidType, $objectSupport);
99106
} else {
@@ -102,7 +109,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
102109
) {
103110
// this is a compact notation element, add to next block and parse
104111
$c = $this->getRealCurrentLineNb();
105-
$parser = new self($c);
112+
$parser = new self($c, $this->totalNumberOfLines);
106113
$parser->refs = &$this->refs;
107114

108115
$block = $values['value'];
@@ -153,7 +160,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
153160
$value = $this->getNextEmbedBlock();
154161
}
155162
$c = $this->getRealCurrentLineNb() + 1;
156-
$parser = new self($c);
163+
$parser = new self($c, $this->totalNumberOfLines);
157164
$parser->refs = &$this->refs;
158165
$parsed = $parser->parse($value, $exceptionOnInvalidType, $objectSupport);
159166

@@ -190,7 +197,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
190197
$data[$key] = null;
191198
} else {
192199
$c = $this->getRealCurrentLineNb() + 1;
193-
$parser = new self($c);
200+
$parser = new self($c, $this->totalNumberOfLines);
194201
$parser->refs = &$this->refs;
195202
$data[$key] = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport);
196203
}
@@ -528,6 +535,8 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
528535
if ($notEOF) {
529536
$blockLines[] = '';
530537
$this->moveToPreviousLine();
538+
} elseif (!$notEOF && !$this->isCurrentLineLastLineInDocument()) {
539+
$blockLines[] = '';
531540
}
532541

533542
// folded style
@@ -634,6 +643,11 @@ private function isCurrentLineComment()
634643
return '' !== $ltrimmedLine && $ltrimmedLine[0] === '#';
635644
}
636645

646+
private function isCurrentLineLastLineInDocument()
647+
{
648+
return ($this->offset + $this->currentLineNb) >= ($this->totalNumberOfLines - 1);
649+
}
650+
637651
/**
638652
* Cleanups a YAML string to be parsed.
639653
*

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@ public function getCommentLikeStringInScalarBlockData()
826826
foo
827827
# bar
828828
baz
829+
829830
EOT
830831
,
831832
),
@@ -854,7 +855,7 @@ public function getCommentLikeStringInScalarBlockData()
854855
$expected = array(
855856
'foo' => array(
856857
'bar' => array(
857-
'scalar-block' => 'line1 line2>',
858+
'scalar-block' => "line1 line2>\n",
858859
),
859860
'baz' => array(
860861
'foobar' => null,

0 commit comments

Comments
 (0)
0