From edf51d9a68c73ae838175c6cf56d04ce96640ed1 Mon Sep 17 00:00:00 2001 From: Steffen Becker Date: Fri, 10 Jul 2026 00:11:14 +0200 Subject: [PATCH] fix: show linked images --- src/lib/import/docx.ts | 74 ++++++++++++++-------- src/lib/import/imageFormats.ts | 72 +++++++++++++++++++++ src/lib/import/odt.ts | 103 +++++++++++++++++++++---------- tests/unit/image-formats.test.ts | 47 ++++++++++++++ 4 files changed, 238 insertions(+), 58 deletions(-) create mode 100644 src/lib/import/imageFormats.ts create mode 100644 tests/unit/image-formats.test.ts diff --git a/src/lib/import/docx.ts b/src/lib/import/docx.ts index 240966c..947ffdd 100644 --- a/src/lib/import/docx.ts +++ b/src/lib/import/docx.ts @@ -6,6 +6,7 @@ import { HEADER_SHADE } from '../editor/extensions/tableHeaderRow'; import { orderedTypeFromFormat, orderedTypeAttrAt, childCycle, ROOT_ORDERED_CYCLE, type OrderedCycle } from '../utils/orderedListTypes'; import { bulletCharAttr, bulletCharFromDocx } from '../utils/bulletListTypes'; import { DATE_FORMATS, TIME_FORMATS, docxPicture, toDateValue } from '../utils/dateTime'; +import { imageDataUrl } from './imageFormats'; import { PX_PER_CM, type PageMargins } from '../storage/pageMargins'; import type { Orientation } from '../storage/pageOrientation'; import { formatFromCm, type PageFormat } from '../storage/pageFormat'; @@ -51,10 +52,6 @@ const LIST_LEFT_STEP_CM = 1.27; // matches export/docx.ts const LIST_INDENT_EPS_CM = 0.05; const LINK_BLUE = '#0563C1'; // the visual the exporter paints on hyperlink runs -const MIME_BY_EXT: Record = { - png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', gif: 'image/gif', bmp: 'image/bmp', svg: 'image/svg+xml', webp: 'image/webp', -}; - // ---- small DOM helpers ------------------------------------------------------ function fc(el: Element | null, localName: string): Element | null { if (!el) return null; @@ -83,13 +80,6 @@ function hexColor(v: string | null | undefined): string | undefined { return /^[0-9a-fA-F]{6}$/.test(v) ? normalizeColor(`#${v}`) : normalizeColor(v); } -function bytesToBase64(bytes: Uint8Array): string { - let bin = ''; - const chunk = 0x8000; - for (let i = 0; i < bytes.length; i += chunk) bin += String.fromCharCode(...bytes.subarray(i, i + chunk)); - return btoa(bin); -} - // ---- entry ------------------------------------------------------------------ export function importDocx(bytes: Uint8Array): OdtImportResult { let files: Record; @@ -716,8 +706,8 @@ function loadImageDataUrl(path: string, ctx: Ctx): string | null { if (cached) return cached; const bytes = ctx.files[path]; if (!bytes) return null; - const ext = path.split('.').pop()?.toLowerCase() ?? ''; - const url = `data:${MIME_BY_EXT[ext] ?? 'image/png'};base64,${bytesToBase64(bytes)}`; + const url = imageDataUrl(bytes, path); + if (!url) return null; ctx.imageCache.set(path, url); return url; } @@ -731,11 +721,25 @@ function convertDrawing(drawing: Element, ctx: Ctx): Node | null { if (wsp) return convertWpsShape(wsp, root, !!anchor, ctx); const blip = drawing.getElementsByTagNameNS(A, 'blip')[0]; if (!blip) { ctx.warnings.add('Drawings were removed'); return null; } - const embed = blip.getAttributeNS(R, 'embed'); - const rel = embed ? ctx.rels.get(embed) : undefined; - if (!rel || rel.external) { ctx.warnings.add('Some images could not be read and were skipped'); return null; } - const src = loadImageDataUrl(`word/${rel.target.replace(/^\/+/, '')}`, ctx); - if (!src) { ctx.warnings.add('Some images could not be read and were skipped'); return null; } + // Candidate media: the primary blip plus any SVG alternative (Word 2016+ stores an + // svgBlip beside a raster fallback) — prefer whichever the browser can display. + const candidates: string[] = []; + const pushEmbed = (id: string | null) => { + const rel = id ? ctx.rels.get(id) : undefined; + if (rel && !rel.external) candidates.push(`word/${rel.target.replace(/^\/+/, '')}`); + }; + pushEmbed(blip.getAttributeNS(R, 'embed')); + for (const sb of Array.from(blip.getElementsByTagName('*'))) + if (sb.localName === 'svgBlip') pushEmbed(sb.getAttributeNS(R, 'embed')); + if (!candidates.length) { ctx.warnings.add('Some images could not be read and were skipped'); return null; } + const path = candidates.find(p => loadImageDataUrl(p, ctx)) ?? candidates[0]; + const src = loadImageDataUrl(path, ctx); + if (!src) { + ctx.warnings.add(ctx.files[path] + ? 'Images in a format the browser can’t display (e.g. WMF, EMF, TIFF) were removed' + : 'Some images could not be read and were skipped'); + return null; + } const attrs: Record = { src }; const extent = root.getElementsByTagNameNS(WP, 'extent')[0]; @@ -827,14 +831,7 @@ function convertPict(pict: Element, ctx: Ctx): Node | null { const shape = Array.from(pict.children).find( (c) => c.namespaceURI === VML && ['shape', 'rect', 'oval', 'roundrect'].includes(c.localName), ); - const txbxContent = shape - ? nsChild(shape.getElementsByTagNameNS(VML, 'textbox')[0] ?? null, W, 'txbxContent') - : null; - if (!shape || !txbxContent) { ctx.warnings.add('Drawings were removed'); return null; } - - const attrs: Record = {}; - const kind = shape.localName === 'oval' ? 'ellipse' : shape.localName === 'roundrect' ? 'roundRect' : 'textbox'; - if (kind !== 'textbox') attrs.shapeKind = kind; + if (!shape) { ctx.warnings.add('Drawings were removed'); return null; } const style = shape.getAttribute('style') ?? ''; const dimPx = (key: string): number | null => { @@ -847,6 +844,31 @@ function convertPict(pict: Element, ctx: Ctx): Node | null { }; const w = dimPx('width'); const h = dimPx('height'); + + // A v:imagedata is a legacy inline picture (no text box) → an image node. + const imagedata = shape.getElementsByTagNameNS(VML, 'imagedata')[0]; + const imgRel = imagedata ? ctx.rels.get(imagedata.getAttributeNS(R, 'id') ?? '') : undefined; + if (imagedata && imgRel && !imgRel.external) { + const path = `word/${imgRel.target.replace(/^\/+/, '')}`; + const src = loadImageDataUrl(path, ctx); + if (!src) { + ctx.warnings.add(ctx.files[path] + ? 'Images in a format the browser can’t display (e.g. WMF, EMF, TIFF) were removed' + : 'Some images could not be read and were skipped'); + return null; + } + const imgAttrs: Record = { src }; + if (w) imgAttrs.width = w; + if (h) imgAttrs.height = h; + return { type: 'image', attrs: imgAttrs }; + } + + const txbxContent = nsChild(shape.getElementsByTagNameNS(VML, 'textbox')[0] ?? null, W, 'txbxContent'); + if (!txbxContent) { ctx.warnings.add('Drawings were removed'); return null; } + + const attrs: Record = {}; + const kind = shape.localName === 'oval' ? 'ellipse' : shape.localName === 'roundrect' ? 'roundRect' : 'textbox'; + if (kind !== 'textbox') attrs.shapeKind = kind; if (w) attrs.width = w; if (h) attrs.height = h; diff --git a/src/lib/import/imageFormats.ts b/src/lib/import/imageFormats.ts new file mode 100644 index 0000000..da0d6f8 --- /dev/null +++ b/src/lib/import/imageFormats.ts @@ -0,0 +1,72 @@ +// Image-format handling shared by the ODF and DOCX importers. An can only +// render a fixed set of formats; anything else (WMF/EMF/SVM/TIFF/…) has to be skipped +// with a warning rather than emitted as a broken image. The format is resolved from +// the file extension first, then confirmed/recovered from the bytes' magic number so a +// mislabelled or extensionless entry still works. + +const MIME_BY_EXT: Record = { + png: 'image/png', + apng: 'image/apng', + jpg: 'image/jpeg', + jpeg: 'image/jpeg', + jfif: 'image/jpeg', + pjpeg: 'image/jpeg', + pjp: 'image/jpeg', + gif: 'image/gif', + bmp: 'image/bmp', + svg: 'image/svg+xml', + webp: 'image/webp', + avif: 'image/avif', + ico: 'image/x-icon', +}; + +export function imageExtOf(path: string): string { + const bare = path.split(/[?#]/)[0]; + return bare.split('.').pop()?.toLowerCase() ?? ''; +} + +// A renderable mime from the magic number, or null (unknown/unrenderable). Only the +// formats browsers display are recognised — a metafile/TIFF signature returns null. +function sniffImageMime(bytes: Uint8Array): string | null { + const b = bytes; + if (b.length < 4) return null; + if (b[0] === 0x89 && b[1] === 0x50 && b[2] === 0x4e && b[3] === 0x47) return 'image/png'; + if (b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff) return 'image/jpeg'; + if (b[0] === 0x47 && b[1] === 0x49 && b[2] === 0x46 && b[3] === 0x38) return 'image/gif'; + if (b[0] === 0x42 && b[1] === 0x4d) return 'image/bmp'; + if (b[0] === 0x00 && b[1] === 0x00 && b[2] === 0x01 && b[3] === 0x00) return 'image/x-icon'; + if (b.length >= 12 && b[0] === 0x52 && b[1] === 0x49 && b[2] === 0x46 && b[3] === 0x46 + && b[8] === 0x57 && b[9] === 0x45 && b[10] === 0x42 && b[11] === 0x50) return 'image/webp'; + if (b.length >= 12 && b[4] === 0x66 && b[5] === 0x74 && b[6] === 0x79 && b[7] === 0x70) { + const brand = String.fromCharCode(b[8], b[9], b[10], b[11]); + if (brand === 'avif' || brand === 'avis') return 'image/avif'; + } + // SVG is XML text: look for an = { - png: 'image/png', - jpg: 'image/jpeg', - jpeg: 'image/jpeg', - gif: 'image/gif', - svg: 'image/svg+xml', - webp: 'image/webp', - bmp: 'image/bmp', -}; - -function bytesToBase64(bytes: Uint8Array): string { - let bin = ''; - const chunk = 0x8000; // chunk so String.fromCharCode doesn't blow the call stack - for (let i = 0; i < bytes.length; i += chunk) { - bin += String.fromCharCode(...bytes.subarray(i, i + chunk)); - } - return btoa(bin); -} - -// Read a Pictures/ entry from the archive into a base64 data-URI (mime by extension). +// Read a Pictures/ entry into a base64 data-URI; null when it's missing or in a format +// the browser can't display (the caller distinguishes the two for its warning). function loadImageDataUrl(href: string, ctx: Ctx): string | null { const cached = ctx.imageCache.get(href); if (cached) return cached; const bytes = ctx.files[href]; if (!bytes) return null; - const ext = href.split('.').pop()?.toLowerCase() ?? ''; - const url = `data:${MIME_BY_EXT[ext] ?? 'image/png'};base64,${bytesToBase64(bytes)}`; + const url = imageDataUrl(bytes, href); + if (!url) return null; ctx.imageCache.set(href, url); return url; } @@ -125,13 +108,18 @@ function applyFrameRotationAndWrap(el: Element, attrs: Record, // geometry (cm → px). as-char frames stay inline; paragraph/page-anchored frames // with a wrap become floating (wrap mode + svg:x/svg:y position). function convertFrame(frame: Element, ctx: Ctx): Node | null { - const image = frame.getElementsByTagNameNS(NS.draw, 'image')[0]; - if (!image) return null; - const href = (image.getAttributeNS(NS.xlink, 'href') ?? '').replace(/^\.\//, ''); - if (!href) return null; + // A frame may list several alternatives (e.g. a metafile + a bitmap + // fallback) — prefer the first the browser can display. + const hrefs = Array.from(frame.getElementsByTagNameNS(NS.draw, 'image')) + .map(im => (im.getAttributeNS(NS.xlink, 'href') ?? '').replace(/^\.\//, '')) + .filter(Boolean); + if (!hrefs.length) return null; + const href = hrefs.find(h => loadImageDataUrl(h, ctx)) ?? hrefs[0]; const src = loadImageDataUrl(href, ctx); if (!src) { - ctx.warnings.add('Some images could not be read and were skipped'); + ctx.warnings.add(ctx.files[href] + ? 'Images in a format the browser can’t display (e.g. WMF, EMF, SVM, TIFF) were removed' + : 'Some images could not be read and were skipped'); return null; } const attrs: Record = { src }; @@ -237,6 +225,16 @@ function convertDrawElement(e: Element, ctx: Ctx): { inline?: Node; block?: Node const shape = convertShape(e, ctx); return shape ? { block: shape } : null; } + if (e.localName === 'a') { + // draw:a wraps a drawing in a hyperlink; the editor has no image link, so unwrap + // to the inner drawing (the link is dropped, like other flattened hyperlinks). + for (const child of Array.from(e.children)) { + if (child.namespaceURI !== NS.draw) continue; + const conv = convertDrawElement(child, ctx); + if (conv) return conv; + } + return null; + } ctx.warnings.add('Drawings were removed'); return null; } @@ -438,8 +436,15 @@ function convertBlocks(elements: Element[], ctx: Ctx, kind: BlockKind, boldByDef if (el.localName === 'p' || el.localName === 'h') { pushWithPending(convertParaLike(el, ctx, kind, boldByDefault)); } else if (el.localName === 'list') { - const list = convertList(el, ctx, null, 1); - if (list) out.push(list); + // A list wrapping only headings is ODF outline (chapter) numbering, not a real + // list — unwrap it to plain headings instead of empty nested list levels. + const headingEls = kind === 'body' ? outlineHeadingEls(el) : null; + if (headingEls) { + for (const h of headingEls) out.push(convertParaLike(h, ctx, 'body')); + } else { + const list = convertList(el, ctx, null, 1); + if (list) out.push(list); + } pushWithPending(null); } else if (el.localName === 'section') { const inner = convertBlocks(Array.from(el.children), ctx, kind, boldByDefault); @@ -659,15 +664,26 @@ function fieldValue(kind: 'date' | 'time', raw: string | null): string { // A / whose data style matches a known format → a live field // node; returns null (keep the shown text) for an unrecognised format. function convertDateTimeField(e: Element, ctx: Ctx): Node | null { - const kind: 'date' | 'time' = e.localName === 'time' ? 'time' : 'date'; const styleEl = ctx.resolver.numberStyle(e.getAttributeNS(NS.style, 'data-style-name')); - const format = styleEl ? matchFormat(parseNumberStyleTokens(styleEl), kind) : null; + if (!styleEl) return null; + const tokens = parseNumberStyleTokens(styleEl); + // Kind follows the number style's actual tokens, not the element name: OpenOffice + // can wrap a date field in yet reference a date-style, and vice versa. + const kind = numberStyleKind(tokens) ?? (e.localName === 'time' ? 'time' : 'date'); + const format = matchFormat(tokens, kind); if (!format) return null; const fixed = e.getAttributeNS(NS.text, 'fixed') === 'true'; - const raw = e.getAttributeNS(NS.text, kind === 'time' ? 'time-value' : 'date-value'); + const raw = e.getAttributeNS(NS.text, 'date-value') ?? e.getAttributeNS(NS.text, 'time-value'); return { type: 'dateTimeField', attrs: { kind, format, fixed, value: fieldValue(kind, raw) } }; } +// A number style with any calendar token is a date style; hours/minutes → time; else null. +function numberStyleKind(tokens: Token[]): 'date' | 'time' | null { + if (tokens.some(t => t.t === 'year' || t.t === 'month' || t.t === 'day' || t.t === 'weekday')) return 'date'; + if (tokens.some(t => t.t === 'hour24' || t.t === 'hour12' || t.t === 'minute' || t.t === 'second' || t.t === 'ampm')) return 'time'; + return null; +} + // ---- inline conversion -------------------------------------------------------- function convertInline(root: Element, ctx: Ctx, baseProps: PropMap, headingLevel: number | null, hfFields = false, boldByDefault = false): Node[] { @@ -865,6 +881,29 @@ function formatPt(v: number): string { // ---- lists ----------------------------------------------------------------------- +// The heading elements of a whose leaves are all headings (each list-item +// holds only a text:h and/or nested such lists) — ODF outline/chapter numbering, not a +// real list, so they import as plain headings. null when it's a genuine list. +function outlineHeadingEls(listEl: Element): Element[] | null { + const out: Element[] = []; + for (const item of Array.from(listEl.children)) { + if (item.namespaceURI !== NS.text || (item.localName !== 'list-item' && item.localName !== 'list-header')) continue; + for (const child of Array.from(item.children)) { + if (child.namespaceURI !== NS.text) return null; + if (child.localName === 'h') { + out.push(child); + } else if (child.localName === 'list') { + const nested = outlineHeadingEls(child); + if (!nested) return null; + out.push(...nested); + } else { + return null; // a paragraph or other content ⇒ a genuine list + } + } + } + return out.length ? out : null; +} + // `inheritedStyleName`: nested text:list elements usually carry no style-name of // their own — the outermost list's style governs, with one level def per depth. // `govMultilevel`: this list sits inside a multilevel (display-levels) chain, so an diff --git a/tests/unit/image-formats.test.ts b/tests/unit/image-formats.test.ts new file mode 100644 index 0000000..de0b676 --- /dev/null +++ b/tests/unit/image-formats.test.ts @@ -0,0 +1,47 @@ +import { describe, it, expect } from 'vitest'; +import { displayableImageMime, imageDataUrl, imageExtOf } from '../../src/lib/import/imageFormats'; + +const bytesOf = (...b: number[]) => new Uint8Array(b); +const PNG = bytesOf(0x89, 0x50, 0x4e, 0x47, 0, 0, 0, 0); +const JPEG = bytesOf(0xff, 0xd8, 0xff, 0xe0, 0, 0, 0, 0); +const BMP = bytesOf(0x42, 0x4d, 0, 0, 0, 0, 0, 0); +const SVM = bytesOf(0x56, 0x43, 0x4c, 0x4d, 0x54, 0x46, 1, 0); // "VCLMTF" +const TIFF = bytesOf(0x49, 0x49, 0x2a, 0x00, 0, 0, 0, 0); +const WEBP = bytesOf(0x52, 0x49, 0x46, 0x46, 0, 0, 0, 0, 0x57, 0x45, 0x42, 0x50); + +describe('imageExtOf', () => { + it('lowercases and strips query/fragment', () => { + expect(imageExtOf('Pictures/A.PNG')).toBe('png'); + expect(imageExtOf('word/media/image1.jpeg?x=1')).toBe('jpeg'); + expect(imageExtOf('noext')).toBe('noext'); + }); +}); + +describe('displayableImageMime', () => { + it('resolves renderable formats by extension', () => { + expect(displayableImageMime(PNG, 'a.png')).toBe('image/png'); + expect(displayableImageMime(JPEG, 'a.jfif')).toBe('image/jpeg'); + expect(displayableImageMime(new Uint8Array(4), 'a.avif')).toBe('image/avif'); + }); + it('returns null for formats the browser cannot display', () => { + expect(displayableImageMime(SVM, 'a.svm')).toBeNull(); + expect(displayableImageMime(TIFF, 'a.tiff')).toBeNull(); + expect(displayableImageMime(bytesOf(0,0,0,0), 'a.wmf')).toBeNull(); + expect(displayableImageMime(bytesOf(0,0,0,0), 'a.emf')).toBeNull(); + }); + it('recovers a renderable format from magic bytes when the extension is unknown', () => { + expect(displayableImageMime(PNG, 'blob')).toBe('image/png'); + expect(displayableImageMime(BMP, 'Pictures/x')).toBe('image/bmp'); + expect(displayableImageMime(WEBP, 'x')).toBe('image/webp'); + }); + it('does not mislabel a metafile as a bitmap when the extension is unknown', () => { + expect(displayableImageMime(SVM, 'Pictures/x')).toBeNull(); + }); +}); + +describe('imageDataUrl', () => { + it('emits a data-URI for renderable bytes, null otherwise', () => { + expect(imageDataUrl(PNG, 'a.png')).toMatch(/^data:image\/png;base64,/); + expect(imageDataUrl(SVM, 'a.svm')).toBeNull(); + }); +});