Preserve QWord strikethrough formatting

This commit is contained in:
Курнат Андрей
2026-06-01 07:14:24 +03:00
parent 5a968dea72
commit ea26f5c55d
5 changed files with 63 additions and 3 deletions
+28
View File
@@ -248,6 +248,34 @@ describe("wordOffice helpers", () => {
expect(documentXml).toContain('w:ascii="Arial"');
});
it("сохраняет зачеркнутый текст из HTML для DOCX", () => {
const blocks = htmlToWordBlocks('<p><s>Deleted</s> <span style="text-decoration-line: line-through;">Styled</span></p>');
expect(blocks).toEqual([
{
type: "paragraph",
text: "Deleted Styled",
runs: [
{ text: "Deleted", strike: true },
{ text: " " },
{ text: "Styled", strike: true }
]
}
]);
});
it("преобразует зачеркнутый inline run в параметр docx strike", () => {
expect(wordInlineRunsToTextRunOptions([{ text: "Deleted", strike: true }])).toEqual([{ text: "Deleted", strike: true }]);
});
it("записывает зачеркнутый текст в XML экспортированного DOCX", async () => {
const blob = await exportDocxBlob("Зачеркнутый.docx", "<p><s>Удаленный текст</s></p>");
const zip = await JSZip.loadAsync(await blob.arrayBuffer());
const documentXml = await zip.file("word/document.xml")?.async("string");
expect(documentXml).toContain("<w:strike/>");
});
it("нормализует HTML после импорта DOCX", () => {
expect(normalizeImportedDocxHtml(" <p>Готово</p>\n")).toBe("<p>Готово</p>");
});
+12 -1
View File
@@ -10,6 +10,7 @@ export interface WordInlineRun {
bold?: boolean;
italics?: boolean;
underline?: boolean;
strike?: boolean;
href?: string;
color?: string;
highlightColor?: string;
@@ -353,6 +354,9 @@ function wordInlineRunToTextRunOption(run: WordInlineRun) {
if (run.underline) {
options.underline = {};
}
if (run.strike) {
options.strike = true;
}
if (run.color) {
options.color = run.color;
}
@@ -681,7 +685,8 @@ function docxRunToHtml(run: unknown) {
const highlightColor = docxRunHighlightColor(properties);
const fontSize = docxRunFontSize(properties);
const fontFamily = docxRunFontFamily(properties);
const hasInlineColorOrHighlight = Boolean(color || highlightColor || fontSize || fontFamily);
const hasStrike = hasDocxBoolean(properties, "w:strike") || hasDocxBoolean(properties, "w:dstrike");
const hasInlineColorOrHighlight = Boolean(color || highlightColor || fontSize || fontFamily || hasStrike);
let html = escapeHtml(text);
if (!text) {
@@ -697,6 +702,9 @@ function docxRunToHtml(run: unknown) {
if (hasDocxBoolean(properties, "w:u")) {
html = `<u>${html}</u>`;
}
if (hasStrike) {
html = `<s>${html}</s>`;
}
const styles = [
color ? `color: #${color};` : "",
@@ -870,6 +878,7 @@ function formatForElement(element: Element, inherited: InlineFormat): InlineForm
bold: inherited.bold || tagName === "b" || tagName === "strong" || /font-weight\s*:\s*(bold|[6-9]00)\b/i.test(style),
italics: inherited.italics || tagName === "i" || tagName === "em" || /font-style\s*:\s*italic\b/i.test(style),
underline: inherited.underline || tagName === "u" || /text-decoration(?:-line)?\s*:[^;]*underline/i.test(style),
strike: inherited.strike || tagName === "s" || tagName === "strike" || tagName === "del" || /text-decoration(?:-line)?\s*:[^;]*line-through/i.test(style),
...(href ? { href } : {}),
...(color ? { color } : {}),
...(highlightColor ? { highlightColor } : {}),
@@ -910,6 +919,7 @@ function sameInlineFormat(first: InlineFormat, second: InlineFormat) {
Boolean(first.bold) === Boolean(second.bold) &&
Boolean(first.italics) === Boolean(second.italics) &&
Boolean(first.underline) === Boolean(second.underline) &&
Boolean(first.strike) === Boolean(second.strike) &&
(first.href ?? "") === (second.href ?? "") &&
(first.color ?? "") === (second.color ?? "") &&
(first.highlightColor ?? "") === (second.highlightColor ?? "") &&
@@ -923,6 +933,7 @@ function cleanInlineFormat(format: InlineFormat): InlineFormat {
...(format.bold ? { bold: true } : {}),
...(format.italics ? { italics: true } : {}),
...(format.underline ? { underline: true } : {}),
...(format.strike ? { strike: true } : {}),
...(format.href ? { href: format.href } : {}),
...(format.color ? { color: format.color } : {}),
...(format.highlightColor ? { highlightColor: format.highlightColor } : {}),
+17
View File
@@ -74,4 +74,21 @@ describe("wordOffice DOCX import", () => {
}
]);
});
it("imports DOCX strikethrough from OOXML", async () => {
const blob = await exportDocxBlob("Strike.docx", "<p><s>Deleted text</s></p>");
const file = new File([blob], "Strike.docx", {
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
});
const imported = await importDocxFile(file);
expect(imported.html).toContain("<s>Deleted text</s>");
expect(htmlToWordBlocks(imported.html)).toEqual([
{
type: "paragraph",
text: "Deleted text",
runs: [{ text: "Deleted text", strike: true }]
}
]);
});
});