Skip to content

Commit 33bf087

Browse files
committed
bug symfony#13375 [YAML] Fix one-liners to work with multiple new lines (Alex Pott)
This PR was submitted for the 2.7 branch but it was merged into the 2.3 branch instead (closes symfony#13375). Discussion ---------- [YAML] Fix one-liners to work with multiple new lines | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | none | License | MIT | Doc PR | n/a Commits ------- 4d22bf7 [YAML] Fix one-liners to work with multiple new lines
2 parents b97e543 + 4d22bf7 commit 33bf087

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ public function __construct($offset = 0)
5151
*/
5252
public function parse($value, $exceptionOnInvalidType = false, $objectSupport = false)
5353
{
54-
$this->currentLineNb = -1;
55-
$this->currentLine = '';
56-
$this->lines = explode("\n", $this->cleanup($value));
57-
5854
if (!preg_match('//u', $value)) {
5955
throw new ParseException('The YAML value does not appear to be valid UTF-8.');
6056
}
57+
$this->currentLineNb = -1;
58+
$this->currentLine = '';
59+
$value = $this->cleanup($value);
60+
$this->lines = explode("\n", $value);
6161

6262
if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
6363
$mbEncoding = mb_internal_encoding();
@@ -197,9 +197,8 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
197197
throw new ParseException('Multiple documents are not supported.');
198198
}
199199

200-
// 1-liner optionally followed by newline
201-
$lineCount = count($this->lines);
202-
if (1 === $lineCount || (2 === $lineCount && empty($this->lines[1]))) {
200+
// 1-liner optionally followed by newline(s)
201+
if ($this->lines[0] === trim($value)) {
203202
try {
204203
$value = Inline::parse($this->lines[0], $exceptionOnInvalidType, $objectSupport, $this->refs);
205204
} catch (ParseException $e) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ public function getBlockChompingTests()
135135
);
136136
$tests['Literal block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
137137

138+
$yaml = <<<'EOF'
139+
{}
140+
141+
142+
EOF;
143+
$expected = array();
144+
$tests['Literal block chomping strip with multiple trailing newlines after a 1-liner'] = array($expected, $yaml);
145+
138146
$yaml = <<<'EOF'
139147
foo: |-
140148
one

0 commit comments

Comments
 (0)