diff --git a/YamlDotNet.Test/Core/EmitterTests.cs b/YamlDotNet.Test/Core/EmitterTests.cs index 277099577..d01d1ec50 100644 --- a/YamlDotNet.Test/Core/EmitterTests.cs +++ b/YamlDotNet.Test/Core/EmitterTests.cs @@ -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() { diff --git a/YamlDotNet/Core/Emitter.cs b/YamlDotNet/Core/Emitter.cs index 9364f837f..53542f72a 100644 --- a/YamlDotNet/Core/Emitter.cs +++ b/YamlDotNet/Core/Emitter.cs @@ -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) @@ -403,6 +405,7 @@ private void AnalyzeScalar(Scalar scalar) if (previousBreak) { breakSpace = true; + lineOfSpaces = true; } previousSpace = true; @@ -425,6 +428,11 @@ private void AnalyzeScalar(Scalar scalar) spaceBreak = true; } + if (lineOfSpaces) + { + linesOfSpaces = true; + } + previousSpace = false; previousBreak = true; } @@ -432,6 +440,7 @@ private void AnalyzeScalar(Scalar scalar) { previousSpace = false; previousBreak = false; + lineOfSpaces = false; } preceededByWhitespace = buffer.IsWhiteBreakOrZero(); @@ -472,6 +481,9 @@ private void AnalyzeScalar(Scalar scalar) scalarData.isFlowPlainAllowed = false; scalarData.isBlockPlainAllowed = false; scalarData.isSingleQuotedAllowed = false; + } + if (linesOfSpaces) + { scalarData.isBlockAllowed = false; }