Skip to content

Commit 535b006

Browse files
committed
fix: address PR review feedback for TIFF support
- Use mimeTypeForExt mapping for .tif data URIs (image/tiff not image/tif) - Remove unused size arg from convertTiffToPng call - Add happy-path test asserting valid TIFF produces PNG data URI
1 parent 498aa80 commit 535b006

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

packages/super-editor/src/core/DocxZipper.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ class DocxZipper {
6464
const extension = this.getFileExtension(name)?.toLowerCase();
6565
// Only build data URIs for images; keep raw base64 for other binaries (e.g., xlsx)
6666
const imageTypes = new Set(['png', 'jpg', 'jpeg', 'gif', 'bmp', 'tiff', 'tif', 'emf', 'wmf', 'svg', 'webp']);
67+
const mimeTypeForExt = { tif: 'tiff' };
6768
if (imageTypes.has(extension)) {
68-
this.mediaFiles[name] = `data:image/${extension};base64,${fileBase64}`;
69+
const mimeSubtype = mimeTypeForExt[extension] || extension;
70+
this.mediaFiles[name] = `data:image/${mimeSubtype};base64,${fileBase64}`;
6971
const blob = await zipEntry.async('blob');
7072
const fileObj = new File([blob], name, { type: blob.type });
7173
const imageUrl = URL.createObjectURL(fileObj);

packages/super-editor/src/core/super-converter/v3/handlers/wp/helpers/encode-image-node-helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ export function handleImageNode(node, params, isAnchor) {
403403
if (converter?.domEnvironment) {
404404
setTiffDomEnvironment(converter.domEnvironment);
405405
}
406-
const conversionResult = convertTiffToPng(mediaData, size);
406+
const conversionResult = convertTiffToPng(mediaData);
407407
if (conversionResult?.dataUri) {
408408
finalSrc = conversionResult.dataUri;
409409
finalExtension = conversionResult.format || 'png';

packages/super-editor/src/core/super-converter/v3/handlers/wp/helpers/tiff-converter.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,37 @@ describe('tiff-converter', () => {
6060
expect(result).toBeNull();
6161
});
6262

63+
it('returns a PNG data URI for valid TIFF input', () => {
64+
const fakeRgba = new Uint8Array(2 * 2 * 4); // 2x2 image, RGBA
65+
vi.doMock('utif2', () => ({
66+
decode: () => [{ t256: [2], t257: [2] }],
67+
decodeImage: (_buf, ifd) => {
68+
ifd.width = 2;
69+
ifd.height = 2;
70+
},
71+
toRGBA8: () => fakeRgba,
72+
}));
73+
74+
const mockCanvas = {
75+
width: 0,
76+
height: 0,
77+
getContext: () => ({
78+
createImageData: (w, h) => ({ data: new Uint8Array(w * h * 4), width: w, height: h }),
79+
putImageData: () => {},
80+
}),
81+
toDataURL: () => 'data:image/png;base64,iVBORw0KGgo=',
82+
};
83+
const spy = vi.spyOn(document, 'createElement').mockReturnValue(mockCanvas);
84+
85+
return import('./tiff-converter.js?happy').then(({ convertTiffToPng: fn }) => {
86+
const result = fn(new Uint8Array([0x49, 0x49, 0x2a, 0x00]));
87+
expect(result).toEqual({ dataUri: 'data:image/png;base64,iVBORw0KGgo=', format: 'png' });
88+
89+
spy.mockRestore();
90+
vi.doUnmock('utif2');
91+
});
92+
});
93+
6394
it('returns null for TIFF with dimensions exceeding pixel limit', () => {
6495
// Mock utif2 to return oversized dimensions via raw IFD tags
6596
// (t256=ImageWidth, t257=ImageLength — 100k × 10k = 1 billion pixels)

0 commit comments

Comments
 (0)