bug #16864 [Yaml] fix indented line handling in folded blocks (xabbuh)

This PR was merged into the 2.3 branch.

Discussion
----------

[Yaml] fix indented line handling in folded blocks

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #9105
| License       | MIT
| Doc PR        |

Commits
-------

756834c [Yaml] fix indented line handling in folded blocks
This commit is contained in:
Fabien Potencier 2015-12-26 13:21:40 +01:00
commit 7660fb8c89
2 changed files with 88 additions and 15 deletions

View File

@ -468,13 +468,13 @@ class Parser
}
$isCurrentLineBlank = $this->isCurrentLineBlank();
$text = '';
$blockLines = array();
// leading blank lines are consumed before determining indentation
while ($notEOF && $isCurrentLineBlank) {
// newline only if not EOF
if ($notEOF = $this->moveToNextLine()) {
$text .= "\n";
$blockLines[] = '';
$isCurrentLineBlank = $this->isCurrentLineBlank();
}
}
@ -495,37 +495,59 @@ class Parser
preg_match($pattern, $this->currentLine, $matches)
)
) {
if ($isCurrentLineBlank) {
$text .= substr($this->currentLine, $indentation);
if ($isCurrentLineBlank && strlen($this->currentLine) > $indentation) {
$blockLines[] = substr($this->currentLine, $indentation);
} elseif ($isCurrentLineBlank) {
$blockLines[] = '';
} else {
$text .= $matches[1];
$blockLines[] = $matches[1];
}
// newline only if not EOF
if ($notEOF = $this->moveToNextLine()) {
$text .= "\n";
$isCurrentLineBlank = $this->isCurrentLineBlank();
}
}
} elseif ($notEOF) {
$text .= "\n";
$blockLines[] = '';
}
if ($notEOF) {
$blockLines[] = '';
$this->moveToPreviousLine();
}
// folded style
if ('>' === $style) {
// folded lines
// replace all non-leading/non-trailing single newlines with spaces
preg_match('/(\n*)$/', $text, $matches);
$text = preg_replace('/(?<!\n|^)\n(?!\n)/', ' ', rtrim($text, "\n"));
$text .= $matches[1];
$text = '';
$previousLineIndented = false;
$previousLineBlank = false;
// empty separation lines
// remove one newline from each group of non-leading/non-trailing newlines
$text = preg_replace('/[^\n]\n+\K\n(?=[^\n])/', '', $text);
for ($i = 0; $i < count($blockLines); $i++) {
if ('' === $blockLines[$i]) {
$text .= "\n";
$previousLineIndented = false;
$previousLineBlank = true;
} elseif (' ' === $blockLines[$i][0]) {
$text .= "\n".$blockLines[$i];
$previousLineIndented = true;
$previousLineBlank = false;
} elseif ($previousLineIndented) {
$text .= "\n".$blockLines[$i];
$previousLineIndented = false;
$previousLineBlank = false;
} elseif ($previousLineBlank || 0 === $i) {
$text .= $blockLines[$i];
$previousLineIndented = false;
$previousLineBlank = false;
} else {
$text .= ' '.$blockLines[$i];
$previousLineIndented = false;
$previousLineBlank = false;
}
}
} else {
$text = implode("\n", $blockLines);
}
// deal with trailing newlines

View File

@ -820,6 +820,57 @@ EOT
array($yaml2, $expected2),
);
}
public function testBlankLinesAreParsedAsNewLinesInFoldedBlocks()
{
$yaml = <<<EOT
test: >
<h2>A heading</h2>
<ul>
<li>a list</li>
<li>may be a good example</li>
</ul>
EOT;
$this->assertSame(
array(
'test' => <<<EOT
<h2>A heading</h2>
<ul> <li>a list</li> <li>may be a good example</li> </ul>
EOT
,
),
$this->parser->parse($yaml)
);
}
public function testAdditionallyIndentedLinesAreParsedAsNewLinesInFoldedBlocks()
{
$yaml = <<<EOT
test: >
<h2>A heading</h2>
<ul>
<li>a list</li>
<li>may be a good example</li>
</ul>
EOT;
$this->assertSame(
array(
'test' => <<<EOT
<h2>A heading</h2>
<ul>
<li>a list</li>
<li>may be a good example</li>
</ul>
EOT
,
),
$this->parser->parse($yaml)
);
}
}
class B