diff --git a/lib/files/src/SettingsXml.ts b/lib/files/src/SettingsXml.ts index aa4b6e3..2441fac 100644 --- a/lib/files/src/SettingsXml.ts +++ b/lib/files/src/SettingsXml.ts @@ -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 = { @@ -54,6 +60,7 @@ const DEFAULT_SETTINGS: SettingsI = { defaultTabStop: null, footnoteProperties: null, documentProtection: null, + compatibilityMode: null, }; enum SettingType { @@ -61,6 +68,7 @@ enum SettingType { OnOff, Relationship, Formatting, + Custom, } type SettingMeta = @@ -79,6 +87,11 @@ type SettingMeta = ooxmlLocalName: string; ooxmlType: SettingType.Formatting; } + | { + docxmlName: keyof SettingsI; + ooxmlLocalName: string; + ooxmlType: SettingType.Custom; + } | { docxmlName: keyof SettingsI; ooxmlLocalName: string; @@ -117,6 +130,11 @@ const settingsMeta: Array = [ ooxmlLocalName: 'documentProtection', ooxmlType: SettingType.OnOff, }, + { + docxmlName: 'compatibilityMode', + ooxmlLocalName: 'compat', + ooxmlType: SettingType.Custom, + }, ]; export class SettingsXml extends XmlFileWithContentTypes { @@ -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 () } `, @@ -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 ); diff --git a/lib/files/test/SettingsXml.test.ts b/lib/files/test/SettingsXml.test.ts index 28f51a2..86e35f7 100644 --- a/lib/files/test/SettingsXml.test.ts +++ b/lib/files/test/SettingsXml.test.ts @@ -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( + `` + ); + }); + 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( + `` + ); + }); + 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); + }); + }); });