From 280a861919b77a3e04e774ceca548732ac7e64ec Mon Sep 17 00:00:00 2001 From: Eemeli Aro Date: Sat, 8 Jun 2024 17:57:31 +0300 Subject: [PATCH] fix: Allow comment after top-level block scalar with explicit indent indicator (fixes #547) --- src/parse/lexer.ts | 5 ++++- tests/doc/parse.ts | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/parse/lexer.ts b/src/parse/lexer.ts index d4457df0..aee148a6 100644 --- a/src/parse/lexer.ts +++ b/src/parse/lexer.ts @@ -537,7 +537,10 @@ export class Lexer { if (!ch && !this.atEnd) return this.setNext('block-scalar') if (indent >= this.indentNext) { if (this.blockScalarIndent === -1) this.indentNext = indent - else this.indentNext += this.blockScalarIndent + else { + this.indentNext = + this.blockScalarIndent + (this.indentNext === 0 ? 1 : this.indentNext) + } do { const cs = this.continueScalar(nl + 1) if (cs === -1) break diff --git a/tests/doc/parse.ts b/tests/doc/parse.ts index 154db795..30a08ae4 100644 --- a/tests/doc/parse.ts +++ b/tests/doc/parse.ts @@ -412,6 +412,7 @@ describe('maps with no values', () => { describe('odd indentations', () => { test('Block map with empty explicit key (#551)', () => { const doc = YAML.parseDocument('?\n? a') + expect(doc.errors).toHaveLength(0) expect(doc.contents.items).toMatchObject([ { key: { value: null }, value: null }, { key: { value: 'a' }, value: null } @@ -432,6 +433,12 @@ describe('odd indentations', () => { const doc = YAML.parseDocument('a:\n{x}') expect(doc.errors).not.toHaveLength(0) }) + + test('comment after top-level block scalar with indentation indicator (#547)', () => { + const doc = YAML.parseDocument('|1\n x\n#c') + expect(doc.errors).toHaveLength(0) + expect(doc.contents).toMatchObject({ value: 'x\n' }) + }) }) describe('Excessive entity expansion attacks', () => {