Preserve QWord images in styled DOCX import

This commit is contained in:
Курнат Андрей
2026-06-01 08:19:00 +03:00
parent 46e04cb9ab
commit cf3d6b0b01
3 changed files with 282 additions and 46 deletions
+35
View File
@@ -1,6 +1,9 @@
import { describe, expect, it } from "vitest";
import { exportDocxBlob, htmlToWordBlocks, importDocxFile } from "./wordOffice";
const tinyPngBase64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/p9sAAAAASUVORK5CYII=";
const tinyPngDataUri = `data:image/png;base64,${tinyPngBase64}`;
describe("wordOffice DOCX import", () => {
it("imports DOCX text color and highlight from OOXML", async () => {
const blob = await exportDocxBlob(
@@ -138,4 +141,36 @@ describe("wordOffice DOCX import", () => {
{ type: "paragraph", text: "After table", runs: [{ text: "After table" }] }
]);
});
it("imports styled DOCX content when the document also contains images", async () => {
const blob = await exportDocxBlob(
"Mixed.docx",
`<p><span style="color: #0f5fae;">Color fragment</span></p><img src="${tinyPngDataUri}" alt="Logo" width="32" height="32"><table><tr><td>Stage</td><td>Done</td></tr></table>`
);
const file = new File([blob], "Mixed.docx", {
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
});
const imported = await importDocxFile(file);
const blocks = htmlToWordBlocks(imported.html);
expect(imported.html).toContain("color: #0F5FAE;");
expect(imported.html).toContain("<table>");
expect(imported.html).toContain(`src="${tinyPngDataUri}"`);
expect(blocks[0]).toEqual({
type: "paragraph",
text: "Color fragment",
runs: [{ text: "Color fragment", color: "0F5FAE" }]
});
expect(blocks[1]).toMatchObject({
type: "image",
src: tinyPngDataUri,
alt: "Logo",
width: 32,
height: 32
});
expect(blocks[2]).toEqual({
type: "table",
rows: [["Stage", "Done"]]
});
});
});