Skip to content

Commit

Permalink
Fix for handling implicit key after explicit key with no value
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Feb 28, 2021
1 parent a51a079 commit d02cc8d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/parse/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ function getFirstKeyStartProps(prev: SourceToken[]) {
break loop
}
}
while (prev[++i]?.type === 'space') { /* loop */ }
while (prev[++i]?.type === 'space') {
/* loop */
}
return prev.splice(i, prev.length)
}

Expand Down Expand Up @@ -322,7 +324,7 @@ export class Parser {
it.value = token
} else {
Object.assign(it, { key: token, sep: [] })
this.onKeyLine = true
this.onKeyLine = !includesToken(it.start, 'explicit-key-ind')
return
}
break
Expand Down Expand Up @@ -588,6 +590,12 @@ export class Parser {
default: {
const bv = this.startBlockValue(map)
if (bv) {
if (
atNextItem &&
bv.type !== 'block-seq' &&
includesToken(it.start, 'explicit-key-ind')
)
map.items.push({ start: [] })
this.stack.push(bv)
return
}
Expand Down
16 changes: 16 additions & 0 deletions tests/doc/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,22 @@ describe('maps with no values', () => {
doc.set('b', 'x')
expect(String(doc)).toBe(`{\n a: null,\n b: #c\n x\n}\n`)
})

test('implicit scalar key after explicit key with no value', () => {
const doc = YAML.parseDocument('? - 1\nx:\n')
expect(doc.contents.items).toMatchObject([
{ key: { items: [{ value: 1 }] }, value: null },
{ key: { value: 'x' }, value: { value: null } }
])
})

test('implicit flow collection key after explicit key with no value', () => {
const doc = YAML.parseDocument('? - 1\n[x]: y\n')
expect(doc.contents.items).toMatchObject([
{ key: { items: [{ value: 1 }] }, value: null },
{ key: { items: [{ value: 'x' }] }, value: { value: 'y' } }
])
})
})

describe('Excessive entity expansion attacks', () => {
Expand Down
2 changes: 2 additions & 0 deletions tests/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ aa: AA
DD
DD
? EE
[ ee , ff ] : {
"gg
gg":[GG]
Expand All @@ -43,6 +44,7 @@ describe('Input in parts', () => {
bb: 'BB BB',
cc: 'CC',
'dd\n': '\nDD\n DD\n\nDD\n',
EE: null,
'[ ee, ff ]': { 'gg gg': ['GG'] },
'{ hh }': ['HH', 'II\rII II']
})
Expand Down

0 comments on commit d02cc8d

Please sign in to comment.