Skip to content
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

Allow block style in multiline scalars with trailing spaces #540

Merged
merged 2 commits into from Nov 19, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions YamlDotNet.Test/Core/EmitterTests.cs
Expand Up @@ -208,6 +208,19 @@ public void FoldedStyleIsSelectedWhenNewLinesAreFoundInLiteral()
yaml.Should().Contain(">");
}


[Fact]
[Trait("motive", "pr #540")]
public void AllowBlockStyleInMultilineScalarsWithTrailingSpaces()
{
var events = SequenceWith(Scalar("hello \nworld").ImplicitPlain);

var yaml = EmittedTextFrom(StreamedDocumentWith(events));

yaml.Should().Contain("\n");
}


[Fact]
public void FoldedStyleDoesNotGenerateExtraLineBreaks()
{
Expand Down
12 changes: 12 additions & 0 deletions YamlDotNet/Core/Emitter.cs
Expand Up @@ -319,11 +319,13 @@ private void AnalyzeScalar(Scalar scalar)
var spaceBreak = false;
var previousSpace = false;
var previousBreak = false;
var lineOfSpaces = false;

var lineBreaks = false;

var specialCharacters = !ValueIsRepresentableInOutputEncoding(value);
var singleQuotes = false;
var linesOfSpaces = false;

var isFirst = true;
while (!buffer.EndOfInput)
Expand Down Expand Up @@ -403,6 +405,7 @@ private void AnalyzeScalar(Scalar scalar)
if (previousBreak)
{
breakSpace = true;
lineOfSpaces = true;
}

previousSpace = true;
Expand All @@ -425,13 +428,19 @@ private void AnalyzeScalar(Scalar scalar)
spaceBreak = true;
}

if (lineOfSpaces)
{
linesOfSpaces = true;
}

previousSpace = false;
previousBreak = true;
}
else
{
previousSpace = false;
previousBreak = false;
lineOfSpaces = false;
}

preceededByWhitespace = buffer.IsWhiteBreakOrZero();
Expand Down Expand Up @@ -472,6 +481,9 @@ private void AnalyzeScalar(Scalar scalar)
scalarData.isFlowPlainAllowed = false;
scalarData.isBlockPlainAllowed = false;
scalarData.isSingleQuotedAllowed = false;
}
if (linesOfSpaces)
{
scalarData.isBlockAllowed = false;
}

Expand Down