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
24 changes: 24 additions & 0 deletions examples/protected-document.tsx
Original file line number Diff line number Diff line change
@@ -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(
<Section pageWidth={cm(20)} pageHeight={cm(20)}>
<Paragraph>
<Text>This is some protected text</Text>
</Paragraph>
</Section>
);

await docxFile.toFile('protected-document.docx');
22 changes: 16 additions & 6 deletions lib/files/src/SettingsXml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 ()
}
</w:settings>`,
Expand Down Expand Up @@ -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
);
Expand Down
49 changes: 47 additions & 2 deletions lib/files/test/SettingsXml.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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', {
Expand All @@ -62,5 +68,44 @@ describe('SettingsXml', () => {
edit: 'readOnly',
enforcement: true,
});

expect(serialize(await settings.$$$toNode())).toEqual(
`<w:settings xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:documentProtection w:edit="readOnly" w:enforcement="true"/></w:settings>`
);

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(
`<w:settings xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:documentProtection w:enforcement="true"/></w:settings>`
);

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);
});
});