Skip to content

Commit a192df0

Browse files
fix(mime): specify charset parameter per MIME type instead of mechanical detection (#4912)
Per #4510: - Inline `; charset=utf-8` in `_baseMimes` for all text/* entries plus `image/svg+xml`, `application/xhtml+xml`, and `application/xml`, which are XML-based formats that benefit from an explicit charset. - Drop the `if (mimeType.startsWith("text"))` block in `getMimeType`; the charset now lives in the data, not in the lookup logic. Output for text types is unchanged (still "text/<sub>; charset=utf-8"). - Update `getExtension` to compare the base type (the part before the first `;`), so callers can pass either a raw type ("text/html") or one with parameters ("text/html; charset=utf-8") and still get the correct extension back. Tests: 5 mime.test.ts cases pass (3 existing + 2 new) covering the new charset values for svg/xhtml/xml/ics/mjs/css/csv and getExtension with parameters. Closes #4510
1 parent cf6ef70 commit a192df0

2 files changed

Lines changed: 36 additions & 17 deletions

File tree

‎src/utils/mime.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ describe('mime', () => {
1818
expect(getMimeType('IMAGE.JPG')).toBe('image/jpeg')
1919
})
2020

21+
it('getMimeType returns charset parameter for text and XML-based formats', () => {
22+
expect(getMimeType('icon.svg')).toBe('image/svg+xml; charset=utf-8')
23+
expect(getMimeType('page.xhtml')).toBe('application/xhtml+xml; charset=utf-8')
24+
expect(getMimeType('feed.xml')).toBe('application/xml; charset=utf-8')
25+
expect(getMimeType('events.ics')).toBe('text/calendar; charset=utf-8')
26+
expect(getMimeType('script.mjs')).toBe('text/javascript; charset=utf-8')
27+
expect(getMimeType('style.css')).toBe('text/css; charset=utf-8')
28+
expect(getMimeType('data.csv')).toBe('text/csv; charset=utf-8')
29+
})
30+
2131
it('getMimeType with custom mime', () => {
2232
expect(getMimeType('morning-routine.m3u8', mime)).toBe('application/vnd.apple.mpegurl')
2333
expect(getMimeType('morning-routine1.ts', mime)).toBe('video/mp2t')
@@ -40,4 +50,15 @@ describe('mime', () => {
4050
expect(getExtension('application/zip')).toBe('zip')
4151
expect(getExtension('non/existent')).toBeUndefined()
4252
})
53+
54+
it('getExtension matches MIME types regardless of charset/parameters', () => {
55+
expect(getExtension('text/html; charset=utf-8')).toBe('htm')
56+
expect(getExtension('text/plain; charset=utf-8')).toBe('txt')
57+
expect(getExtension('image/svg+xml; charset=utf-8')).toBe('svg')
58+
expect(getExtension('application/xml; charset=utf-8')).toBe('xml')
59+
expect(getExtension('application/xhtml+xml; charset=utf-8')).toBe('xhtml')
60+
expect(getExtension('text/css; charset=utf-16')).toBe('css')
61+
expect(getExtension('image/svg+xml')).toBe('svg')
62+
expect(getExtension('application/xml')).toBe('xml')
63+
})
4364
})

‎src/utils/mime.ts‎

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@ export const getMimeType = (
1212
if (!match) {
1313
return
1414
}
15-
let mimeType = mimes[match[1].toLowerCase()]
16-
if (mimeType && mimeType.startsWith('text')) {
17-
mimeType += '; charset=utf-8'
18-
}
19-
return mimeType
15+
return mimes[match[1].toLowerCase()]
2016
}
2117

2218
export const getExtension = (mimeType: string): string | undefined => {
19+
const baseType = mimeType.split(';', 1)[0].trim()
2320
for (const ext in baseMimes) {
24-
if (baseMimes[ext] === mimeType) {
21+
const stored = baseMimes[ext]
22+
if (stored === mimeType || stored.split(';', 1)[0].trim() === baseType) {
2523
return ext
2624
}
2725
}
@@ -41,25 +39,25 @@ const _baseMimes = {
4139
av1: 'video/av1',
4240
bin: 'application/octet-stream',
4341
bmp: 'image/bmp',
44-
css: 'text/css',
45-
csv: 'text/csv',
42+
css: 'text/css; charset=utf-8',
43+
csv: 'text/csv; charset=utf-8',
4644
eot: 'application/vnd.ms-fontobject',
4745
epub: 'application/epub+zip',
4846
gif: 'image/gif',
4947
gz: 'application/gzip',
50-
htm: 'text/html',
51-
html: 'text/html',
48+
htm: 'text/html; charset=utf-8',
49+
html: 'text/html; charset=utf-8',
5250
ico: 'image/x-icon',
53-
ics: 'text/calendar',
51+
ics: 'text/calendar; charset=utf-8',
5452
jpeg: 'image/jpeg',
5553
jpg: 'image/jpeg',
56-
js: 'text/javascript',
54+
js: 'text/javascript; charset=utf-8',
5755
json: 'application/json',
5856
jsonld: 'application/ld+json',
5957
map: 'application/json',
6058
mid: 'audio/x-midi',
6159
midi: 'audio/x-midi',
62-
mjs: 'text/javascript',
60+
mjs: 'text/javascript; charset=utf-8',
6361
mp3: 'audio/mpeg',
6462
mp4: 'video/mp4',
6563
mpeg: 'video/mpeg',
@@ -71,21 +69,21 @@ const _baseMimes = {
7169
pdf: 'application/pdf',
7270
png: 'image/png',
7371
rtf: 'application/rtf',
74-
svg: 'image/svg+xml',
72+
svg: 'image/svg+xml; charset=utf-8',
7573
tif: 'image/tiff',
7674
tiff: 'image/tiff',
7775
ts: 'video/mp2t',
7876
ttf: 'font/ttf',
79-
txt: 'text/plain',
77+
txt: 'text/plain; charset=utf-8',
8078
wasm: 'application/wasm',
8179
webm: 'video/webm',
8280
weba: 'audio/webm',
8381
webmanifest: 'application/manifest+json',
8482
webp: 'image/webp',
8583
woff: 'font/woff',
8684
woff2: 'font/woff2',
87-
xhtml: 'application/xhtml+xml',
88-
xml: 'application/xml',
85+
xhtml: 'application/xhtml+xml; charset=utf-8',
86+
xml: 'application/xml; charset=utf-8',
8987
zip: 'application/zip',
9088
'3gp': 'video/3gpp',
9189
'3g2': 'video/3gpp2',

0 commit comments

Comments
 (0)