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
  • Loading branch information
zoncoen committed Jan 26, 2024
1 parent 42fb764 commit 4618994
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
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,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 @@ -856,6 +864,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
Original file line number Diff line number Diff line change
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 4618994

Please sign in to comment.