Skip to content

Commit 1e6c9e1

Browse files
authored
fix: omit empty descr/title in <wp:docPr> for Word 2007 compatibility (#3073)
1 parent c38d0a2 commit 1e6c9e1

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

src/file/drawing/doc-properties/doc-properties.ts

+28-23
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import { createHyperlinkClick } from "./doc-properties-children";
1919

2020
export type DocPropertiesOptions = {
2121
readonly name: string;
22-
readonly description: string;
23-
readonly title: string;
22+
readonly description?: string;
23+
readonly title?: string;
2424
};
2525

2626
export class DocProperties extends XmlComponent {
@@ -29,26 +29,32 @@ export class DocProperties extends XmlComponent {
2929
public constructor({ name, description, title }: DocPropertiesOptions = { name: "", description: "", title: "" }) {
3030
super("wp:docPr");
3131

32-
this.root.push(
33-
new NextAttributeComponent({
34-
id: {
35-
key: "id",
36-
value: this.docPropertiesUniqueNumericId(),
37-
},
38-
name: {
39-
key: "name",
40-
value: name,
41-
},
42-
description: {
43-
key: "descr",
44-
value: description,
45-
},
46-
title: {
47-
key: "title",
48-
value: title,
49-
},
50-
}),
51-
);
32+
const attributes: Record<string, { readonly key: string; readonly value: string | number }> = {
33+
id: {
34+
key: "id",
35+
value: this.docPropertiesUniqueNumericId(),
36+
},
37+
name: {
38+
key: "name",
39+
value: name,
40+
},
41+
};
42+
43+
if (description !== null && description !== undefined) {
44+
attributes.description = {
45+
key: "descr",
46+
value: description,
47+
};
48+
}
49+
50+
if (title !== null && title !== undefined) {
51+
attributes.title = {
52+
key: "title",
53+
value: title,
54+
};
55+
}
56+
57+
this.root.push(new NextAttributeComponent(attributes));
5258
}
5359

5460
public prepForXml(context: IContext): IXmlableObject | undefined {
@@ -59,7 +65,6 @@ export class DocProperties extends XmlComponent {
5965
}
6066

6167
this.root.push(createHyperlinkClick(element.linkId, true));
62-
6368
break;
6469
}
6570

0 commit comments

Comments
 (0)