Skip to content

Commit

Permalink
fix: remove any trailing empty lines if the block scalar has strip in…
Browse files Browse the repository at this point in the history
…dicator (#421)
  • Loading branch information
zoncoen committed Jan 26, 2024
1 parent 0640a15 commit 31fe1ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
16 changes: 16 additions & 0 deletions decode_test.go
Expand Up @@ -860,6 +860,14 @@ func TestDecoder(t *testing.T) {
},
},
},
{
"v:\n- A\n- |-\n B\n C\n\n\n",
map[string][]string{
"v": {
"A", "B\nC",
},
},
},
{
"v:\n- A\n- >-\n B\n C\n",
map[string][]string{
Expand All @@ -868,6 +876,14 @@ func TestDecoder(t *testing.T) {
},
},
},
{
"v:\n- A\n- >-\n B\n C\n\n\n",
map[string][]string{
"v": {
"A", "B C",
},
},
},
{
"a: b\nc: d\n",
struct {
Expand Down
13 changes: 10 additions & 3 deletions scanner/context.go
Expand Up @@ -196,9 +196,16 @@ func (c *Context) existsBuffer() bool {

func (c *Context) bufferedSrc() []rune {
src := c.buf[:c.notSpaceCharPos]
if len(src) > 0 && src[len(src)-1] == '\n' && c.isDocument() && c.literalOpt == "-" {
// remove end '\n' character
src = src[:len(src)-1]
if c.isDocument() && c.literalOpt == "-" {
// remove end '\n' character and trailing empty lines
// https://yaml.org/spec/1.2.2/#8112-block-chomping-indicator
for {
if len(src) > 0 && src[len(src)-1] == '\n' {
src = src[:len(src)-1]
continue
}
break
}
}
return src
}
Expand Down

0 comments on commit 31fe1ba

Please sign in to comment.