Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/emotion/src/source-map.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, it, expect } from 'vitest'
import { createSourceMap } from './source-map'

describe('createSourceMap', () => {
it('handles source content with non-Latin-1 characters', () => {
// Regression: btoa() only supports Latin-1 (U+0000–U+00FF).
// Source files containing characters outside that range caused
// "Invalid character" errors when generating inline source maps.
const sourceWithUnicode = `
import { css } from '@emotion/react'
// Em dash: —
// Arrow: ➜
// Emoji: 🔁
const style = css\`color: red;\`
`
// With the old btoa() implementation, this would throw:
// "Invalid character" at btoa (node:buffer)
expect(() =>
createSourceMap(sourceWithUnicode, 'test.tsx', { line: 6, column: 20 }),
).not.toThrow()
})

it('returns a valid base64-encoded source map comment', () => {
const result = createSourceMap('const x = 1', 'test.tsx', { line: 0, column: 0 })
expect(result).toMatch(
/^\/\*# sourceMappingURL=data:application\/json;charset=utf-8;base64,[A-Za-z0-9+/=]+ \*\/$/,
)
})
})
2 changes: 1 addition & 1 deletion packages/emotion/src/source-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function createSourceMap(
setSourceContent(map, filename, sourceContent)
addSegment(map, 0, 0, filename, pos.line, pos.column)

const encoded = btoa(JSON.stringify(toEncodedMap(map)))
const encoded = Buffer.from(JSON.stringify(toEncodedMap(map))).toString('base64')
return `/*# sourceMappingURL=data:application/json;charset=utf-8;base64,${encoded} */`
}

Expand Down
Loading