Skip to content

Commit

Permalink
fix(compose): Handle CRLF line endings in double-quoted scalars (#306)
Browse files Browse the repository at this point in the history
- Trailing whitespace
- Escaped newlines
  • Loading branch information
eemeli committed Sep 6, 2021
1 parent ef0d1e0 commit def167c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/compose/resolve-flow-scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ function doubleQuotedValue(source: string, onError: FlowScalarErrorHandler) {
// skip escaped newlines, but still trim the following line
next = source[i + 1]
while (next === ' ' || next === '\t') next = source[++i + 1]
} else if (next === '\r' && source[i + 1] === '\n') {
// skip escaped CRLF newlines, but still trim the following line
next = source[++i + 1]
while (next === ' ' || next === '\t') next = source[++i + 1]
} else if (next === 'x' || next === 'u' || next === 'U') {
const length = { x: 2, u: 4, U: 8 }[next]
res += parseCharCode(source, i + 1, length, onError)
Expand All @@ -173,7 +177,8 @@ function doubleQuotedValue(source: string, onError: FlowScalarErrorHandler) {
const wsStart = i
let next = source[i + 1]
while (next === ' ' || next === '\t') next = source[++i + 1]
if (next !== '\n') res += i > wsStart ? source.slice(wsStart, i + 1) : ch
if (next !== '\n' && !(next === '\r' && source[i + 2] === '\n'))
res += i > wsStart ? source.slice(wsStart, i + 1) : ch
} else {
res += ch
}
Expand Down
12 changes: 12 additions & 0 deletions tests/doc/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -918,3 +918,15 @@ describe('reviver', () => {
])
})
})

describe('CRLF line endings', () => {
test('trailing space in double-quoted scalar', () => {
const res = YAML.parse('"foo \r\nbar"')
expect(res).toBe('foo bar')
})

test('escaped newline in double-quoted scalar', () => {
const res = YAML.parse('"foo \\\r\nbar"')
expect(res).toBe('foo bar')
})
})

0 comments on commit def167c

Please sign in to comment.