Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion lib/files/src/SettingsXml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export type SettingsI = {
footnoteProperties?: FootnoteProps | null;

documentProtection?: DocumentProtectionProps | null;

/**
* Word compatibility mode version. Common values: 11 (2003), 12 (2007), 14 (2010), 15 (2013+).
* Set to `null` to omit the compatibility block entirely.
*/
compatibilityMode?: 11 | 12 | 14 | 15 | null;
};

export type DocumentProtectionProps = {
Expand All @@ -54,13 +60,15 @@ const DEFAULT_SETTINGS: SettingsI = {
defaultTabStop: null,
footnoteProperties: null,
documentProtection: null,
compatibilityMode: null,
};

enum SettingType {
Length,
OnOff,
Relationship,
Formatting,
Custom,
}

type SettingMeta =
Expand All @@ -79,6 +87,11 @@ type SettingMeta =
ooxmlLocalName: string;
ooxmlType: SettingType.Formatting;
}
| {
docxmlName: keyof SettingsI;
ooxmlLocalName: string;
ooxmlType: SettingType.Custom;
}
| {
docxmlName: keyof SettingsI;
ooxmlLocalName: string;
Expand Down Expand Up @@ -117,6 +130,11 @@ const settingsMeta: Array<SettingMeta> = [
ooxmlLocalName: 'documentProtection',
ooxmlType: SettingType.OnOff,
},
{
docxmlName: 'compatibilityMode',
ooxmlLocalName: 'compat',
ooxmlType: SettingType.Custom,
},
];

export class SettingsXml extends XmlFileWithContentTypes {
Expand Down Expand Up @@ -230,6 +248,13 @@ export class SettingsXml extends XmlFileWithContentTypes {
) else (),
if (exists($defaultTabStop)) then element ${QNS.w}defaultTabStop {
attribute ${QNS.w}val { $defaultTabStop('twip') }
} else (),
if (exists($compatibilityMode)) then element ${QNS.w}compat {
element ${QNS.w}compatSetting {
attribute ${QNS.w}name { "compatibilityMode" },
attribute ${QNS.w}uri { "http://schemas.microsoft.com/office/word" },
attribute ${QNS.w}val { $compatibilityMode }
}
} else ()
}
</w:settings>`,
Expand Down Expand Up @@ -290,8 +315,14 @@ export class SettingsXml extends XmlFileWithContentTypes {
} else map {
"enforcement": $enforcement
}
) else ()
) else (),
"compatibilityMode": let $cs := ./${QNS.w}compat/${QNS.w}compatSetting[
@${QNS.w}name = "compatibilityMode" and
@${QNS.w}uri = "http://schemas.microsoft.com/office/word"
]
return if ($cs) then number($cs/@${QNS.w}val) else ()
}`,

xml
);

Expand Down
37 changes: 37 additions & 0 deletions lib/files/test/SettingsXml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,41 @@ describe('SettingsXml', () => {
docxFromArchive.document.settings.get('documentProtection')
).toEqual(customSettings);
});

describe('compatibilityMode', () => {
it('compatibilityMode default is null', async () => {
const settings = new SettingsXml('test');
expect(settings.get('compatibilityMode')).toBe(null);
expect(serialize(await settings.$$$toNode())).toEqual(
`<w:settings xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"/>`
);
});
it('compatibilityMode set and get', async () => {
const settings = new SettingsXml('test');
settings.set('compatibilityMode', 15);
expect(settings.get('compatibilityMode')).toBe(15);
expect(serialize(await settings.$$$toNode())).toEqual(
`<w:settings xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:compat><w:compatSetting w:name="compatibilityMode" w:uri="http://schemas.microsoft.com/office/word" w:val="15"/></w:compat></w:settings>`
);
});
it('compatibilityMode roundtrips through archive', async () => {
const docx = Docx.fromNothing();
docx.document.settings.set('compatibilityMode', 15);
const docxFromArchive = await Docx.fromArchive(
await docx.toArchive()
);
expect(
docxFromArchive.document.settings.get('compatibilityMode')
).toBe(15);
});
it('compatibilityMode null roundtrips through archive', async () => {
const docx = Docx.fromNothing();
const docxFromArchive = await Docx.fromArchive(
await docx.toArchive()
);
expect(
docxFromArchive.document.settings.get('compatibilityMode')
).toBe(null);
});
});
});
Loading