Skip to content

Commit

Permalink
Merge pull request #540 from Himura2la/patch-1
Browse files Browse the repository at this point in the history
Allow block style in multiline scalars with trailing spaces
  • Loading branch information
aaubry committed Nov 19, 2020
2 parents c4bd6c6 + 048b027 commit 108c192
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
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

0 comments on commit 108c192

Please sign in to comment.