@@ -45,7 +45,7 @@ function buildPptx(slideBodyXml: string, macroEnabled = false): Promise<Buffer>
4545 return zip . generateAsync ( { type : 'nodebuffer' } ) as Promise < Buffer >
4646}
4747
48- function buildDocx ( bodyXml : string , macroEnabled = false ) : Promise < Buffer > {
48+ function buildDocx ( bodyXml : string , macroEnabled = false , stylesXml ?: string ) : Promise < Buffer > {
4949 const mainType = macroEnabled
5050 ? 'application/vnd.ms-word.document.macroEnabled.main+xml'
5151 : 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml'
@@ -62,6 +62,16 @@ function buildDocx(bodyXml: string, macroEnabled = false): Promise<Buffer> {
6262 'word/document.xml' ,
6363 `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body>${ bodyXml } </w:body></w:document>`
6464 )
65+ if ( stylesXml ) {
66+ zip . file (
67+ 'word/_rels/document.xml.rels' ,
68+ `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/></Relationships>`
69+ )
70+ zip . file (
71+ 'word/styles.xml' ,
72+ `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">${ stylesXml } </w:styles>`
73+ )
74+ }
6575 return zip . generateAsync ( { type : 'nodebuffer' } ) as Promise < Buffer >
6676}
6777
@@ -138,6 +148,24 @@ describe('DocxParser', () => {
138148 expect ( error ) . toBeInstanceOf ( FileParserError )
139149 expect ( error ) . toMatchObject ( { code : 'no_extractable_text' } )
140150 } )
151+
152+ it ( 'ignores a style definition with an unrecognized type' , async ( ) => {
153+ const key = 'unrecognizedStyleType'
154+ const buffer = await buildDocx (
155+ '<w:p><w:r><w:t>Quarterly plan</w:t></w:r></w:p>' ,
156+ false ,
157+ `<w:style w:type="__proto__" w:styleId="${ key } "><w:name w:val="Custom"/></w:style>`
158+ )
159+
160+ try {
161+ const result = await new DocxParser ( ) . parseBuffer ( buffer )
162+
163+ expect ( result . content ) . toContain ( 'Quarterly plan' )
164+ expect ( Object . hasOwn ( Object . prototype , key ) ) . toBe ( false )
165+ } finally {
166+ delete ( Object . prototype as Record < string , unknown > ) [ key ]
167+ }
168+ } )
141169} )
142170
143171describe ( 'OpenDocumentParser' , ( ) => {
0 commit comments