Skip to content

Commit

Permalink
bug #18828 [Yaml] chomp newlines only at the end of YAML documents (x…
Browse files Browse the repository at this point in the history
…abbuh)

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 | #18784
| License       | MIT
| Doc PR        |

Commits
-------

a4b1fa6 chomp newlines only at the end of YAML documents
  • Loading branch information
fabpot committed May 23, 2016
2 parents 76223b2 + a4b1fa6 commit 26b7922
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
26 changes: 20 additions & 6 deletions src/Symfony/Component/Yaml/Parser.php
Expand Up @@ -25,6 +25,7 @@ class Parser
const FOLDED_SCALAR_PATTERN = self::BLOCK_SCALAR_HEADER_PATTERN;

private $offset = 0;
private $totalNumberOfLines;
private $lines = array();
private $currentLineNb = -1;
private $currentLine = '';
Expand All @@ -33,11 +34,13 @@ class Parser
/**
* Constructor.
*
* @param int $offset The offset of YAML document (used for line numbers in error messages)
* @param int $offset The offset of YAML document (used for line numbers in error messages)
* @param int|null $totalNumberOfLines The overall number of lines being parsed
*/
public function __construct($offset = 0)
public function __construct($offset = 0, $totalNumberOfLines = null)
{
$this->offset = $offset;
$this->totalNumberOfLines = $totalNumberOfLines;
}

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

if (null === $this->totalNumberOfLines) {
$this->totalNumberOfLines = count($this->lines);
}

if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
$mbEncoding = mb_internal_encoding();
mb_internal_encoding('UTF-8');
Expand Down Expand Up @@ -93,7 +100,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
// array
if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
$c = $this->getRealCurrentLineNb() + 1;
$parser = new self($c);
$parser = new self($c, $this->totalNumberOfLines);
$parser->refs = &$this->refs;
$data[] = $parser->parse($this->getNextEmbedBlock(null, true), $exceptionOnInvalidType, $objectSupport);
} else {
Expand All @@ -102,7 +109,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
) {
// this is a compact notation element, add to next block and parse
$c = $this->getRealCurrentLineNb();
$parser = new self($c);
$parser = new self($c, $this->totalNumberOfLines);
$parser->refs = &$this->refs;

$block = $values['value'];
Expand Down Expand Up @@ -153,7 +160,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
$value = $this->getNextEmbedBlock();
}
$c = $this->getRealCurrentLineNb() + 1;
$parser = new self($c);
$parser = new self($c, $this->totalNumberOfLines);
$parser->refs = &$this->refs;
$parsed = $parser->parse($value, $exceptionOnInvalidType, $objectSupport);

Expand Down Expand Up @@ -190,7 +197,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
$data[$key] = null;
} else {
$c = $this->getRealCurrentLineNb() + 1;
$parser = new self($c);
$parser = new self($c, $this->totalNumberOfLines);
$parser->refs = &$this->refs;
$data[$key] = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport);
}
Expand Down Expand Up @@ -528,6 +535,8 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
if ($notEOF) {
$blockLines[] = '';
$this->moveToPreviousLine();
} elseif (!$notEOF && !$this->isCurrentLineLastLineInDocument()) {
$blockLines[] = '';
}

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

private function isCurrentLineLastLineInDocument()
{
return ($this->offset + $this->currentLineNb) >= ($this->totalNumberOfLines - 1);
}

/**
* Cleanups a YAML string to be parsed.
*
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Yaml/Tests/ParserTest.php
Expand Up @@ -826,6 +826,7 @@ public function getCommentLikeStringInScalarBlockData()
foo
# bar
baz
EOT
,
),
Expand Down Expand Up @@ -854,7 +855,7 @@ public function getCommentLikeStringInScalarBlockData()
$expected = array(
'foo' => array(
'bar' => array(
'scalar-block' => 'line1 line2>',
'scalar-block' => "line1 line2>\n",
),
'baz' => array(
'foobar' => null,
Expand Down

0 comments on commit 26b7922

Please sign in to comment.