From 46189947976046147a093b3738cd9341454dc84e Mon Sep 17 00:00:00 2001 From: zoncoen Date: Fri, 26 Jan 2024 16:25:58 +0900 Subject: [PATCH] fix: remove any trailing empty lines if the block scalar has strip indicator --- decode_test.go | 16 ++++++++++++++++ scanner/context.go | 13 ++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/decode_test.go b/decode_test.go index 0ac4a7fa..cf64c046 100644 --- a/decode_test.go +++ b/decode_test.go @@ -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{ @@ -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 { diff --git a/scanner/context.go b/scanner/context.go index 09d0a2da..3aaec561 100644 --- a/scanner/context.go +++ b/scanner/context.go @@ -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 }