diff --git a/src/ast/Pair.js b/src/ast/Pair.js index d1f0e20d..e5be0008 100644 --- a/src/ast/Pair.js +++ b/src/ast/Pair.js @@ -90,7 +90,7 @@ export class Pair extends Node { throw new Error(msg) } } - const explicitKey = + let explicitKey = !simpleKeys && (!key || keyComment || @@ -117,6 +117,14 @@ export class Pair extends Node { } else if (chompKeep && !keyComment && onChompKeep) onChompKeep() return ctx.inFlow ? str : `? ${str}` } + if (!explicitKey && str.length > 1024) { + if (!simpleKeys) { + explicitKey = true + } else { + const msg = 'With simple keys, single line scalar must not span more than 1024 characters' + throw new Error(msg) + } + } str = explicitKey ? `? ${str}\n${indent}:` : `${str}:` if (this.comment) { // expected (but not strictly required) to be a single-line comment diff --git a/tests/doc/stringify.js b/tests/doc/stringify.js index 16a7d5da..274db28f 100644 --- a/tests/doc/stringify.js +++ b/tests/doc/stringify.js @@ -469,6 +469,18 @@ describe('simple keys', () => { /With simple keys, collection cannot be used as a key value/ ) }) + + test('key value lingth > 1024', () => { + let str = ` + ? ${new Array(1026).join('a')} + : longkey` + const doc = YAML.parseDocument(str) + expect(String(doc)).toBe(`? ${new Array(1026).join('a')}\n: longkey\n`) + doc.options.simpleKeys = true + expect(() => String(doc)).toThrow( + /With simple keys, single line scalar must not span more than 1024 characters/ + ) + }) }) test('eemeli/yaml#128: YAML node inside object', () => {