8000 [Yaml] Improve newline handling in folded scalar blocks by teohhanhui · Pull Request #15482 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Yaml] Improve newline handling in folded scalar blocks #15482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
*/
class Parser
{
const FOLDED_SCALAR_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';
const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';
// BC - wrongly named
const FOLDED_SCALAR_PATTERN = self::BLOCK_SCALAR_HEADER_PATTERN;

private $offset = 0;
private $lines = array();
Expand Down Expand Up @@ -332,8 +334,8 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)

$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem($this->currentLine);

// Comments must not be removed inside a string block (ie. after a line ending with "|")
$removeCommentsPattern = '~'.self::FOLDED_SCALAR_PATTERN.'$~';
// Comments must not be removed inside a block scalar
$removeCommentsPattern = '~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~';
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);

while ($this->moveToNextLine()) {
Expand Down Expand Up @@ -422,10 +424,10 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
return $this->refs[$value];
}

if (preg_match('/^'.self::FOLDED_SCALAR_PATTERN.'$/', $value, $matches)) {
if (preg_match('/^'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
$modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';

return $this->parseFoldedScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
return $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
}

try {
Expand All @@ -439,15 +441,15 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
}

/**
* Parses a folded scalar.
* Parses a block scalar.
*
* @param string $separator The separator that was used to begin this folded scalar (| or >)
* @param string $indicator The indicator that was used to begin this folded scalar (+ or -)
* @param int $indentation The indentation that was used to begin this folded scalar
* @param string $style The style indicator that was used to begin this block scalar (| or >)
* @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -)
* @param int $indentation The indentation indicator that was used to begin this block scalar
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why indicator in this comment ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think that $separator should be renamed $style (i.e. style indicator)

*
* @return string The text value
*/
private function parseFoldedScalar($separator, $indicator = '', $indentation = 0)
private function parseBlockScalar($style, $chomping = '', $indentation = 0)
{
$notEOF = $this->moveToNextLine();
if (!$notEOF) {
Expand Down Expand Up @@ -502,17 +504,23 @@ private function parseFoldedScalar($separator, $indicator = '', $indentation = 0
$this->moveToPreviousLine();
}

// replace all non-trailing single newlines with spaces in folded blocks
if ('>' === $separator) {
// 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 = preg_replace('/(?<!\n|^)\n(?!\n)/', ' ', rtrim($text, "\n"));
$text .= $matches[1];

// empty separation lines
// remove one newline from each group of non-leading/non-trailing newlines
$text = preg_replace('/[^\n]\n+\K\n(?=[^\n])/', '', $text);
}

// deal with trailing newlines as indicated
if ('' === $indicator) {
// deal with trailing newlines
if ('' === $chomping) {
$text = preg_replace('/\n+$/', "\n", $text);
} elseif ('-' === $indicator) {
} elseif ('-' === $chomping) {
$text = preg_replace('/\n+$/', '', $text);
}

Expand Down
18 changes: 16 additions & 2 deletions src/Symfony/Component/Yaml/Tests/Fixtures/sfTests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ php: |
'~',
)
---
test: Empty lines in folded blocks
test: Empty lines in literal blocks
brief: >
Empty lines in folded blocks
Empty lines in literal blocks
yaml: |
foo:
bar: |
Expand All @@ -65,6 +65,20 @@ yaml: |
php: |
array('foo' => array('bar' => "foo\n\n\n \nbar\n"))
---
test: Empty lines in folded blocks
brief: >
Empty lines in folded blocks
yaml: |
foo:
bar: >

foo


bar
php: |
array('foo' => array('bar' => "\nfoo\n\nbar\n"))
---
test: IP addresses
brief: >
IP addresses
Expand Down
0