fix(docx-engine): read paired protection tags and on enforcement (#312)
This commit is contained in:
@@ -121,7 +121,7 @@ export async function parseProtection(zip: JSZip): Promise<DocProtection | null>
|
||||
const file = zip.file('word/settings.xml')
|
||||
if (!file) return null
|
||||
const xml = await file.async('string')
|
||||
const tag = /<w:documentProtection[^>]*\/>/.exec(xml)?.[0]
|
||||
const tag = /<w:documentProtection\b[^>]*?(?:\/>|>)/.exec(xml)?.[0]
|
||||
if (!tag) return null
|
||||
const edit = /w:edit="([^"]+)"/.exec(tag)?.[1]
|
||||
if (!edit || edit === 'none') return null
|
||||
@@ -132,7 +132,7 @@ export async function parseProtection(zip: JSZip): Promise<DocProtection | null>
|
||||
const sid = /w:cryptAlgorithmSid="(\d+)"/.exec(tag)?.[1]
|
||||
return {
|
||||
edit,
|
||||
enforced: enforcement === '1' || enforcement === 'true',
|
||||
enforced: enforcement === '1' || enforcement === 'true' || enforcement === 'on',
|
||||
...(hash ? { hash } : {}),
|
||||
...(salt ? { salt } : {}),
|
||||
...(spin ? { spinCount: parseInt(spin, 10) } : {}),
|
||||
@@ -144,7 +144,7 @@ export async function parseProtection(zip: JSZip): Promise<DocProtection | null>
|
||||
export async function parseWriteProtection(zip: JSZip): Promise<WriteProtection | null> {
|
||||
const file = zip.file('word/settings.xml')
|
||||
if (!file) return null
|
||||
const tag = /<w:writeProtection[^>]*\/>/.exec(await file.async('string'))?.[0]
|
||||
const tag = /<w:writeProtection\b[^>]*?(?:\/>|>)/.exec(await file.async('string'))?.[0]
|
||||
if (!tag) return null
|
||||
const recommended = /w:recommended="(?:1|true|on)"/.test(tag)
|
||||
const hash = /w:hash="([^"]+)"/.exec(tag)?.[1]
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import JSZip from 'jszip'
|
||||
import { hashProtectionPassword, parseDocx, saveDocx, verifyProtectionPassword } from '../src/index'
|
||||
import { parseProtection, parseWriteProtection } from '../src/parse-package'
|
||||
import { buildDocx } from './helpers/build-docx'
|
||||
|
||||
const XML_DECL = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
|
||||
@@ -348,3 +349,31 @@ describe('removePersonalInformation', () => {
|
||||
expect(reparsed.removePersonalInfo).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('protection tag forms', () => {
|
||||
const settingsZip = async (settingsInner: string): Promise<JSZip> => {
|
||||
const zip = new JSZip()
|
||||
zip.file(
|
||||
'word/settings.xml',
|
||||
`${XML_DECL}<w:settings xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">${settingsInner}</w:settings>`,
|
||||
)
|
||||
return zip
|
||||
}
|
||||
|
||||
it('reads paired-form documentProtection tags', async () => {
|
||||
const zip = await settingsZip(
|
||||
'<w:documentProtection w:edit="readOnly" w:enforcement="1"></w:documentProtection>',
|
||||
)
|
||||
expect(await parseProtection(zip)).toMatchObject({ edit: 'readOnly', enforced: true })
|
||||
})
|
||||
|
||||
it('treats enforcement="on" as enforced', async () => {
|
||||
const zip = await settingsZip('<w:documentProtection w:edit="comments" w:enforcement="on"/>')
|
||||
expect(await parseProtection(zip)).toMatchObject({ edit: 'comments', enforced: true })
|
||||
})
|
||||
|
||||
it('reads paired-form writeProtection tags', async () => {
|
||||
const zip = await settingsZip('<w:writeProtection w:recommended="1"></w:writeProtection>')
|
||||
expect(await parseWriteProtection(zip)).toMatchObject({ recommended: true })
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user