Skip to content

Commit bde6eae

Browse files
fix: reject a raw string whose comment marker follows a byte-order mark
1 parent 6e822b3 commit bde6eae

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

‎packages/toon/src/encode/raw-string.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { JsonPrimitive } from '../types.ts'
2-
import { COMMENT_MARKER } from '../constants.ts'
2+
import { BYTE_ORDER_MARK, COMMENT_MARKER } from '../constants.ts'
33

4-
// Decoders silently strip a line whose first non-space character is the comment marker.
5-
const COMMENT_LINE_PATTERN = new RegExp(`(?:^|\\n) *${COMMENT_MARKER}`)
4+
// Decoders silently strip a line whose first non-space character is the comment marker,
5+
// and they remove a leading byte-order mark before making that test.
6+
const COMMENT_LINE_PATTERN = new RegExp(`(?:^${BYTE_ORDER_MARK}?|\\n) *${COMMENT_MARKER}`)
67

78
/**
89
* Pre-formatted string that the encoder emits verbatim at a primitive value

‎packages/toon/test/raw-string.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ describe('rawString', () => {
8787
expect(() => rawString('note\n# hidden')).toThrowError(TypeError)
8888
})
8989

90+
it('throws when a byte-order mark precedes the comment marker', () => {
91+
expect(() => rawString('\u{FEFF}#hi')).toThrowError(TypeError)
92+
})
93+
9094
it('allows the comment marker after other content on the same line', () => {
9195
expect(encode({ tag: rawString('a #tag') })).toBe('tag: a #tag')
9296
})

0 commit comments

Comments
 (0)