diff --git a/examples/protected-document.tsx b/examples/protected-document.tsx
new file mode 100644
index 0000000..9c6500e
--- /dev/null
+++ b/examples/protected-document.tsx
@@ -0,0 +1,24 @@
+/** @jsx Docx.jsx */
+import { Text } from '../lib/components/document/src/Text.ts';
+import type { DocumentProtectionProps } from '../lib/files/src/SettingsXml.ts';
+import { cm } from '../lib/utilities/src/length.ts';
+import Docx, { Paragraph, Section } from '../mod.ts';
+
+const docxFile = Docx.fromNothing();
+
+const documentProtection: DocumentProtectionProps = {
+ edit: 'readOnly',
+ enforcement: true,
+};
+
+docxFile.document.settings.set('documentProtection', documentProtection);
+
+docxFile.document.set(
+
+
+ This is some protected text
+
+
+);
+
+await docxFile.toFile('protected-document.docx');
diff --git a/lib/files/src/SettingsXml.ts b/lib/files/src/SettingsXml.ts
index 7e87bd2..aa4b6e3 100644
--- a/lib/files/src/SettingsXml.ts
+++ b/lib/files/src/SettingsXml.ts
@@ -205,9 +205,9 @@ export class SettingsXml extends XmlFileWithContentTypes {
if ($attachedTemplate) then element ${QNS.w}attachedTemplate {
attribute ${QNS.r}id { $attachedTemplate }
} else (),
- if ($documentProtection) then element ${QNS.w}documentProtection {
- attribute ${QNS.w}edit { map:get($documentProtection, 'edit') },
- attribute ${QNS.w}enforcement { map:get($documentProtection, 'enforcement') }
+ if (exists($documentProtection)) then element ${QNS.w}documentProtection {
+ if ($documentProtection('edit')) then attribute ${QNS.w}edit { $documentProtection('edit')} else (),
+ attribute ${QNS.w}enforcement { $documentProtection('enforcement') }
} else (),
if (exists($footnoteProperties)) then (
element ${QNS.w}footnotePr {
@@ -228,8 +228,8 @@ export class SettingsXml extends XmlFileWithContentTypes {
}
}
) else (),
- if (exists($defaultTabStop)) then element ${QNS.w}defaultTabStop {
- attribute ${QNS.w}val { map:get($defaultTabStop, 'twip') }
+ if (exists($defaultTabStop)) then element ${QNS.w}defaultTabStop {
+ attribute ${QNS.w}val { $defaultTabStop('twip') }
} else ()
}
`,
@@ -280,7 +280,17 @@ export class SettingsXml extends XmlFileWithContentTypes {
`/${QNS.w}settings/map {
"isTrackChangesEnabled": docxml:ct-on-off(./${QNS.w}trackChanges),
"evenAndOddHeaders": docxml:ct-on-off(./${QNS.w}evenAndOddHeaders),
- "documentProtection": docxml:ct-on-off(./${QNS.w}documentProtection)
+ "documentProtection": if (./${QNS.w}documentProtection) then (
+ let $edit := string(./${QNS.w}documentProtection/@${QNS.w}edit)
+ let $enforcement := docxml:st-on-off(./${QNS.w}documentProtection/@${QNS.w}enforcement)
+ return if ($edit)
+ then map {
+ "edit": $edit,
+ "enforcement": $enforcement
+ } else map {
+ "enforcement": $enforcement
+ }
+ ) else ()
}`,
xml
);
diff --git a/lib/files/test/SettingsXml.test.ts b/lib/files/test/SettingsXml.test.ts
index 2f6a6ae..28f51a2 100644
--- a/lib/files/test/SettingsXml.test.ts
+++ b/lib/files/test/SettingsXml.test.ts
@@ -1,9 +1,15 @@
import { expect } from 'std/expect';
import { describe, it } from 'std/testing/bdd';
+import Docx from '@fontoxml/docxml';
+
import { RelationshipType } from '../../enums.ts';
+import { serialize } from '../../utilities/src/dom.ts';
import { pt } from '../../utilities/src/length.ts';
-import { SettingsXml } from '../src/SettingsXml.ts';
+import {
+ type DocumentProtectionProps,
+ SettingsXml,
+} from '../src/SettingsXml.ts';
describe('SettingsXml', () => {
it('evenAndOddHeaders', () => {
@@ -51,7 +57,7 @@ describe('SettingsXml', () => {
position: 'beneathText',
});
});
- it('DocumentProtectionProps', () => {
+ it('documentProtection with edit property', async () => {
const settings = new SettingsXml('test');
expect(settings.get('documentProtection')).toBe(null);
settings.set('documentProtection', {
@@ -62,5 +68,44 @@ describe('SettingsXml', () => {
edit: 'readOnly',
enforcement: true,
});
+
+ expect(serialize(await settings.$$$toNode())).toEqual(
+ ``
+ );
+
+ const docx = Docx.fromNothing();
+ const customSettings: DocumentProtectionProps = {
+ edit: 'trackedChanges',
+ enforcement: false,
+ };
+ docx.document.settings.set('documentProtection', customSettings);
+ const docxFromArchive = await Docx.fromArchive(await docx.toArchive());
+ expect(
+ docxFromArchive.document.settings.get('documentProtection')
+ ).toEqual(customSettings);
+ });
+ it('documentProtection without edit', async () => {
+ const settings = new SettingsXml('test');
+ expect(settings.get('documentProtection')).toBe(null);
+ settings.set('documentProtection', {
+ enforcement: true,
+ });
+ expect(settings.get('documentProtection')).toEqual({
+ enforcement: true,
+ });
+
+ expect(serialize(await settings.$$$toNode())).toEqual(
+ ``
+ );
+
+ const docx = Docx.fromNothing();
+ const customSettings: DocumentProtectionProps = {
+ enforcement: false,
+ };
+ docx.document.settings.set('documentProtection', customSettings);
+ const docxFromArchive = await Docx.fromArchive(await docx.toArchive());
+ expect(
+ docxFromArchive.document.settings.get('documentProtection')
+ ).toEqual(customSettings);
});
});