[YAML] Fix one-liners to work with multiple new lines

This commit is contained in:
Alex Pott 2015-01-12 11:05:05 +00:00 committed by Fabien Potencier
parent b97e5437c7
commit 4d22bf7f72
2 changed files with 14 additions and 7 deletions

View File

@ -51,13 +51,13 @@ class Parser
*/ */
public function parse($value, $exceptionOnInvalidType = false, $objectSupport = false) public function parse($value, $exceptionOnInvalidType = false, $objectSupport = false)
{ {
$this->currentLineNb = -1;
$this->currentLine = '';
$this->lines = explode("\n", $this->cleanup($value));
if (!preg_match('//u', $value)) { if (!preg_match('//u', $value)) {
throw new ParseException('The YAML value does not appear to be valid UTF-8.'); throw new ParseException('The YAML value does not appear to be valid UTF-8.');
} }
$this->currentLineNb = -1;
$this->currentLine = '';
$value = $this->cleanup($value);
$this->lines = explode("\n", $value);
if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) { if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
$mbEncoding = mb_internal_encoding(); $mbEncoding = mb_internal_encoding();
@ -197,9 +197,8 @@ class Parser
throw new ParseException('Multiple documents are not supported.'); throw new ParseException('Multiple documents are not supported.');
} }
// 1-liner optionally followed by newline // 1-liner optionally followed by newline(s)
$lineCount = count($this->lines); if ($this->lines[0] === trim($value)) {
if (1 === $lineCount || (2 === $lineCount && empty($this->lines[1]))) {
try { try {
$value = Inline::parse($this->lines[0], $exceptionOnInvalidType, $objectSupport, $this->refs); $value = Inline::parse($this->lines[0], $exceptionOnInvalidType, $objectSupport, $this->refs);
} catch (ParseException $e) { } catch (ParseException $e) {

View File

@ -135,6 +135,14 @@ EOF;
); );
$tests['Literal block chomping strip with multiple trailing newlines'] = array($expected, $yaml); $tests['Literal block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
$yaml = <<<'EOF'
{}
EOF;
$expected = array();
$tests['Literal block chomping strip with multiple trailing newlines after a 1-liner'] = array($expected, $yaml);
$yaml = <<<'EOF' $yaml = <<<'EOF'
foo: |- foo: |-
one one