|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { isXmlLike, hex, sniffEncoding, stripBOM, ensureXmlString } from './encoding-helpers.js'; |
| 3 | + |
| 4 | +function utf16leWithBOM(str) { |
| 5 | + const bom = Buffer.from([0xff, 0xfe]); |
| 6 | + const body = Buffer.from(str, 'utf16le'); |
| 7 | + return Buffer.concat([bom, body]); |
| 8 | +} |
| 9 | + |
| 10 | +function utf16beWithBOM(str) { |
| 11 | + const le = Buffer.from(str, 'utf16le'); |
| 12 | + const swapped = Buffer.alloc(le.length); |
| 13 | + for (let i = 0; i < le.length; i += 2) { |
| 14 | + swapped[i] = le[i + 1]; |
| 15 | + swapped[i + 1] = le[i]; |
| 16 | + } |
| 17 | + const bom = Buffer.from([0xfe, 0xff]); |
| 18 | + return Buffer.concat([bom, swapped]); |
| 19 | +} |
| 20 | + |
| 21 | +function noBOMUtf16leBytes(str) { |
| 22 | + // UTF-16LE WITHOUT a BOM (to trigger the NUL-heuristic) |
| 23 | + return Buffer.from(str, 'utf16le'); |
| 24 | +} |
| 25 | + |
| 26 | +describe('isXmlLike', () => { |
| 27 | + it('matches .xml and .rels', () => { |
| 28 | + expect(isXmlLike('word/document.xml')).toBe(true); |
| 29 | + expect(isXmlLike('word/_rels/document.xml.rels')).toBe(true); |
| 30 | + expect(isXmlLike('docProps/core.xml')).toBe(true); |
| 31 | + }); |
| 32 | + it('rejects non-xml', () => { |
| 33 | + expect(isXmlLike('word/media/image1.png')).toBe(false); |
| 34 | + expect(isXmlLike('customXml/item1.xml.bin')).toBe(false); |
| 35 | + expect(isXmlLike('word/fonts/font1.odttf')).toBe(false); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +describe('hex', () => { |
| 40 | + it('renders hex dump of first N bytes', () => { |
| 41 | + const u8 = new Uint8Array([0xff, 0xfe, 0x3c, 0x00, 0x3f, 0x00]); |
| 42 | + expect(hex(u8, 6)).toBe('ff fe 3c 00 3f 00'); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe('sniffEncoding', () => { |
| 47 | + it('detects UTF-16LE by BOM', () => { |
| 48 | + const u8 = utf16leWithBOM('<?xml version="1.0"?>'); |
| 49 | + expect(sniffEncoding(u8)).toBe('utf-16le'); |
| 50 | + }); |
| 51 | + it('detects UTF-16BE by BOM', () => { |
| 52 | + const u8 = utf16beWithBOM('<?xml version="1.0"?>'); |
| 53 | + expect(sniffEncoding(u8)).toBe('utf-16be'); |
| 54 | + }); |
| 55 | + it('defaults to utf-8 for plain ASCII/UTF-8', () => { |
| 56 | + const u8 = new TextEncoder().encode('<?xml version="1.0"?><a/>'); |
| 57 | + expect(sniffEncoding(u8)).toBe('utf-8'); |
| 58 | + }); |
| 59 | + it('heuristically detects UTF-16 (no BOM) via NUL density', () => { |
| 60 | + const u8 = noBOMUtf16leBytes('<?xml version="1.0"?><root/>'); |
| 61 | + // Our heuristic returns 'utf-16le' for lots of NULs |
| 62 | + expect(sniffEncoding(u8)).toBe('utf-16le'); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +describe('stripBOM', () => { |
| 67 | + it('removes U+FEFF if present', () => { |
| 68 | + const s = '\uFEFF<?xml?><r/>'; |
| 69 | + expect(stripBOM(s)).toBe('<?xml?><r/>'); |
| 70 | + }); |
| 71 | + it('no-ops when no BOM present', () => { |
| 72 | + const s = '<?xml?><r/>'; |
| 73 | + expect(stripBOM(s)).toBe(s); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +describe('ensureXmlString', () => { |
| 78 | + it('returns same string when given a plain XML string', () => { |
| 79 | + const s = '<?xml version="1.0"?><r/>'; |
| 80 | + expect(ensureXmlString(s)).toBe(s); |
| 81 | + }); |
| 82 | + |
| 83 | + it('strips leading BOM from a decoded string', () => { |
| 84 | + const s = '\uFEFF<?xml version="1.0"?><r/>'; |
| 85 | + expect(ensureXmlString(s)).toBe('<?xml version="1.0"?><r/>'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('decodes UTF-8 bytes', () => { |
| 89 | + const u8 = new TextEncoder().encode('<?xml version="1.0"?><root>héllo</root>'); |
| 90 | + const out = ensureXmlString(u8); |
| 91 | + expect(out).toContain('<?xml'); |
| 92 | + expect(out).toContain('héllo'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('decodes UTF-16LE with BOM bytes', () => { |
| 96 | + const u8 = utf16leWithBOM('<?xml version="1.0" encoding="utf-16"?><props><k>v</k></props>'); |
| 97 | + const out = ensureXmlString(u8); |
| 98 | + expect(out.toLowerCase()).toContain('encoding="utf-16"'); |
| 99 | + expect(out).toContain('<props>'); |
| 100 | + expect(out).not.toMatch(/\u0000/); |
| 101 | + }); |
| 102 | + |
| 103 | + it('decodes UTF-16BE with BOM bytes', () => { |
| 104 | + const u8 = utf16beWithBOM('<?xml version="1.0" encoding="utf-16"?><props><k>v</k></props>'); |
| 105 | + const out = ensureXmlString(u8); |
| 106 | + expect(out.toLowerCase()).toContain('encoding="utf-16"'); |
| 107 | + expect(out).toContain('<props>'); |
| 108 | + expect(out).not.toMatch(/\u0000/); |
| 109 | + }); |
| 110 | + |
| 111 | + it('decodes UTF-16 (no BOM) via heuristic', () => { |
| 112 | + const u8 = noBOMUtf16leBytes('<?xml version="1.0"?><root>NOBOM</root>'); |
| 113 | + const out = ensureXmlString(u8); |
| 114 | + expect(out).toContain('<root>'); |
| 115 | + expect(out).toContain('NOBOM'); |
| 116 | + expect(out).not.toMatch(/\u0000/); |
| 117 | + }); |
| 118 | + |
| 119 | + it('accepts ArrayBuffer input', () => { |
| 120 | + const u8 = new TextEncoder().encode('<?xml version="1.0"?><r/>'); |
| 121 | + const out = ensureXmlString(u8.buffer); |
| 122 | + expect(out).toContain('<r/>'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('throws on unsupported content types', () => { |
| 126 | + expect(() => ensureXmlString(12345)).toThrow(/Unsupported content type/); |
| 127 | + }); |
| 128 | + |
| 129 | + it('decodes from Node Buffer (utf-8)', () => { |
| 130 | + const buf = Buffer.from('<?xml version="1.0"?><root/>', 'utf8'); |
| 131 | + const out = ensureXmlString(buf); |
| 132 | + expect(out).toContain('<root/>'); |
| 133 | + }); |
| 134 | +}); |
| 135 | + |
| 136 | +describe('ensureXmlString cross-env', () => { |
| 137 | + it('decodes from Node Buffer (utf-8)', () => { |
| 138 | + const buf = Buffer.from('<?xml version="1.0"?><root/>', 'utf8'); |
| 139 | + const out = ensureXmlString(buf); |
| 140 | + expect(out).toContain('<root/>'); |
| 141 | + }); |
| 142 | +}); |
0 commit comments