From def167cd22b3242ec9b8cde7f34bfd92a0386fd0 Mon Sep 17 00:00:00 2001 From: Eemeli Aro Date: Mon, 6 Sep 2021 14:31:44 +0300 Subject: [PATCH] fix(compose): Handle CRLF line endings in double-quoted scalars (#306) - Trailing whitespace - Escaped newlines --- src/compose/resolve-flow-scalar.ts | 7 ++++++- tests/doc/parse.js | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/compose/resolve-flow-scalar.ts b/src/compose/resolve-flow-scalar.ts index cc8106ab..ca800b44 100644 --- a/src/compose/resolve-flow-scalar.ts +++ b/src/compose/resolve-flow-scalar.ts @@ -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) @@ -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 } diff --git a/tests/doc/parse.js b/tests/doc/parse.js index 5f3fe63f..708a7750 100644 --- a/tests/doc/parse.js +++ b/tests/doc/parse.js @@ -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') + }) +})