Skip to content

Commit

Permalink
trim right spaces before adding carriage return or linefeed (#462)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew F Leader <[email protected]>
Co-authored-by: Webb Scales <[email protected]>
  • Loading branch information
mfleader and webbnh authored Jul 16, 2024
1 parent b2a8cc6 commit 56a6b54
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
3 changes: 3 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func TestParser(t *testing.T) {
}`,
"\"a\": a\n\"b\": b",
"'a': a\n'b': b",
"a: \r\n b: 1\r\n",
"a_ok: \r bc: 2\r",
"a_mk: \n bd: 3\n",
}
for _, src := range sources {
if _, err := parser.Parse(lexer.Tokenize(src), 0); err != nil {
Expand Down
19 changes: 10 additions & 9 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,16 @@ func (s *Scanner) scanNewLine(ctx *Context, c rune) {
}
}

// There is no problem that we ignore CR which followed by LF and normalize it to LF, because of following YAML1.2 spec.
// > Line breaks inside scalar content must be normalized by the YAML processor. Each such line break must be parsed into a single line feed character.
// > Outside scalar content, YAML allows any line break to be used to terminate lines.
// > -- https://yaml.org/spec/1.2/spec.html
if c == '\r' && ctx.nextChar() == '\n' {
ctx.addOriginBuf('\r')
ctx.progress(1)
c = '\n'
}

if ctx.isEOS() {
s.addBufferedTokenIfExists(ctx)
} else if s.isAnchor {
Expand Down Expand Up @@ -840,15 +850,6 @@ func (s *Scanner) scan(ctx *Context) (pos int) {
return
}
case '\r', '\n':
// There is no problem that we ignore CR which followed by LF and normalize it to LF, because of following YAML1.2 spec.
// > Line breaks inside scalar content must be normalized by the YAML processor. Each such line break must be parsed into a single line feed character.
// > Outside scalar content, YAML allows any line break to be used to terminate lines.
// > -- https://yaml.org/spec/1.2/spec.html
if c == '\r' && ctx.nextChar() == '\n' {
ctx.addOriginBuf('\r')
ctx.progress(1)
c = '\n'
}
s.scanNewLine(ctx, c)
continue
case ' ':
Expand Down

0 comments on commit 56a6b54

Please sign in to comment.