@@ -56,6 +56,9 @@ function centralOffset(bytes: Uint8Array): number {
5656
5757function visualPresentationParts ( ) : Record < string , Uint8Array > {
5858 const parts : Record < string , Uint8Array > = presentationParts ( ) ;
59+ parts [ "[Content_Types].xml" ] = strToU8 (
60+ '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="png" ContentType="image/png"/></Types>' ,
61+ ) ;
5962 const placeholder = ( idx : number , text : string , props = "" , runs = "" ) =>
6063 `<p:sp><p:nvSpPr><p:cNvPr id="${ idx } " name="Shape ${ idx } "/><p:nvPr><p:ph idx="${ idx } "/></p:nvPr></p:nvSpPr><p:spPr>${ props } </p:spPr><p:txBody><a:p><a:r>${ runs } <a:t>${ text } </a:t></a:r></a:p></p:txBody></p:sp>` ;
6164 parts [ "ppt/slides/slide2.xml" ] =
@@ -84,6 +87,24 @@ function visualPresentationParts(): Record<string, Uint8Array> {
8487 return parts ;
8588}
8689
90+ function presentationWithBitmap ( name : string , bytes : Uint8Array , mime : string , override = true ) {
91+ const parts = visualPresentationParts ( ) ;
92+ delete parts [ "ppt/media/picture.png" ] ;
93+ parts [ `ppt/media/${ name } ` ] = bytes ;
94+ const relationships = "ppt/slides/_rels/slide2.xml.rels" ;
95+ parts [ relationships ] = strToU8 (
96+ strFromU8 ( parts [ relationships ] ! ) . replace ( "../media/picture.png" , `../media/${ name } ` ) ,
97+ ) ;
98+ parts [ "[Content_Types].xml" ] = strToU8 (
99+ `<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">${
100+ override
101+ ? `<Override PartName="/ppt/media/${ name } " ContentType="${ mime } "/>`
102+ : `<Default Extension="${ name . slice ( name . lastIndexOf ( "." ) + 1 ) } " ContentType="${ mime } "/>`
103+ } </Types>`,
104+ ) ;
105+ return parts ;
106+ }
107+
87108const xmlEncodings = [
88109 { label : "UTF-16LE with BOM" , littleEndian : true , bom : true , declaration : "UTF-16" } ,
89110 { label : "UTF-16BE with BOM" , littleEndian : false , bom : true , declaration : "UTF-16" } ,
@@ -280,6 +301,161 @@ describe("normal OOXML namespace and encoding compatibility", () => {
280301} ) ;
281302
282303describe ( "parseOfficeDocument spreadsheets" , ( ) => {
304+ it ( "preserves a canonical workbook declared through a Default content type" , ( ) => {
305+ const original = workbookBytes ( "xlsx" ) ;
306+ const parts = unzipSync ( original ) ;
307+ let contentTypes = strFromU8 ( parts [ "[Content_Types].xml" ] ! )
308+ . replace ( / < O v e r r i d e \b [ ^ > ] * P a r t N a m e = " \/ x l \/ w o r k b o o k \. x m l " [ ^ > ] * \/ > / , "" )
309+ . replace (
310+ / < D e f a u l t \b [ ^ > ] * E x t e n s i o n = " x m l " [ ^ > ] * \/ > / ,
311+ '<Default Extension="xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>' ,
312+ ) ;
313+ for ( const name of Object . keys ( parts ) ) {
314+ if ( name === "[Content_Types].xml" || name === "xl/workbook.xml" || ! name . endsWith ( ".xml" ) )
315+ continue ;
316+ if ( ! contentTypes . includes ( `PartName="/${ name } "` ) ) {
317+ contentTypes = contentTypes . replace (
318+ "</Types>" ,
319+ `<Override PartName="/${ name } " ContentType="application/xml"/></Types>` ,
320+ ) ;
321+ }
322+ }
323+ parts [ "[Content_Types].xml" ] = strToU8 ( contentTypes ) ;
324+ const document = parseOfficeDocument ( zipSync ( parts ) , "xlsx" ) ;
325+ expect ( document ) . toEqual ( parseOfficeDocument ( original , "xlsx" ) ) ;
326+ if ( document . kind !== "spreadsheet" ) throw new Error ( "Expected spreadsheet" ) ;
327+ expect ( document . sheets [ 0 ] ) . toMatchObject ( {
328+ name : "売上" ,
329+ rows : [
330+ [ "品名" , "価格" ] ,
331+ [ "りんご" , "125" ] ,
332+ ] ,
333+ } ) ;
334+ } ) ;
335+
336+ it . each ( [
337+ { prefix : "book/" , location : "book/main.xml" , moveTree : true } ,
338+ { prefix : "" , location : "rootmain.xml" , moveTree : true } ,
339+ { prefix : "book/" , location : "book/main.xml" , moveTree : false } ,
340+ { prefix : "" , location : "rootmain.xml" , moveTree : false } ,
341+ { prefix : "book/" , location : "book/main.xml" , moveTree : false , strictUtf16 : true } ,
342+ ] ) (
343+ "reads relocated workbook $location (move subtree: $moveTree, Strict UTF-16: $strictUtf16) through the relationship graph" ,
344+ ( {
345+ prefix,
346+ location,
347+ moveTree,
348+ strictUtf16,
349+ } : {
350+ prefix : string ;
351+ location : string ;
352+ moveTree : boolean ;
353+ strictUtf16 ?: boolean ;
354+ } ) => {
355+ const workbook = XLSX . utils . book_new ( ) ;
356+ const visible = XLSX . utils . aoa_to_sheet ( [
357+ [ "品名 🐈" , "数量" , "単価" , "合計" ] ,
358+ [ "りんご café" , 12 , 150 , 1800 ] ,
359+ ] ) ;
360+ visible . D2 = { t : "n" , f : "B2*C2" , v : 1800 } ;
361+ XLSX . utils . book_append_sheet ( workbook , visible , "売上 日本語" ) ;
362+ XLSX . utils . book_append_sheet ( workbook , XLSX . utils . aoa_to_sheet ( [ [ "secret" ] ] ) , "Hidden" ) ;
363+ XLSX . utils . book_append_sheet (
364+ workbook ,
365+ XLSX . utils . aoa_to_sheet ( [ [ "very secret" ] ] ) ,
366+ "Very hidden" ,
367+ ) ;
368+ XLSX . utils . book_set_sheet_visibility ( workbook , "Hidden" , 1 ) ;
369+ XLSX . utils . book_set_sheet_visibility ( workbook , "Very hidden" , 2 ) ;
370+ const original = new Uint8Array (
371+ XLSX . write ( workbook , { type : "array" , bookType : "xlsx" , bookSST : true } ) ,
372+ ) ;
373+ const originalParts = unzipSync ( original ) ;
374+ const baseName = location . slice ( location . lastIndexOf ( "/" ) + 1 ) ;
375+ const parts = Object . fromEntries (
376+ Object . entries ( originalParts ) . map ( ( [ name , bytes ] ) => [
377+ name === "xl/workbook.xml"
378+ ? location
379+ : name === "xl/_rels/workbook.xml.rels"
380+ ? `${ prefix } _rels/${ baseName } .rels`
381+ : moveTree && name . startsWith ( "xl/" )
382+ ? prefix + name . slice ( 3 )
383+ : name ,
384+ bytes ,
385+ ] ) ,
386+ ) ;
387+ parts [ "_rels/.rels" ] = strToU8 (
388+ strFromU8 ( parts [ "_rels/.rels" ] ! ) . replace (
389+ 'Target="xl/workbook.xml"' ,
390+ `Target="/${ location } "` ,
391+ ) ,
392+ ) ;
393+ let contentTypes = strFromU8 ( parts [ "[Content_Types].xml" ] ! ) ;
394+ if ( moveTree )
395+ contentTypes = contentTypes . replaceAll ( 'PartName="/xl/' , `PartName="/${ prefix } ` ) ;
396+ parts [ "[Content_Types].xml" ] = strToU8 (
397+ contentTypes . replace (
398+ `PartName="/${ moveTree ? prefix : "xl/" } workbook.xml"` ,
399+ `PartName="/${ location } "` ,
400+ ) ,
401+ ) ;
402+ if ( ! moveTree ) {
403+ const sidecar = `${ prefix } _rels/${ baseName } .rels` ;
404+ parts [ sidecar ] = strToU8 (
405+ strFromU8 ( parts [ sidecar ] ! ) . replace (
406+ / T a r g e t = " ( [ ^ " ] + ) " / g,
407+ ( _attribute : string , target : string ) =>
408+ `Target="${ target . startsWith ( "/" ) ? target : `${ prefix ? "../" : "" } xl/${ target } ` } "` ,
409+ ) ,
410+ ) ;
411+ }
412+ const encoded = strictUtf16
413+ ? utf16Parts (
414+ transformXmlParts ( parts , ( xml ) =>
415+ strToU8 (
416+ xml
417+ . replaceAll (
418+ "http://schemas.openxmlformats.org/spreadsheetml/2006/main" ,
419+ "http://purl.oclc.org/ooxml/spreadsheetml/main" ,
420+ )
421+ . replaceAll ( R , "http://purl.oclc.org/ooxml/officeDocument/relationships" ) ,
422+ ) ,
423+ ) ,
424+ xmlEncodings [ 1 ] ,
425+ )
426+ : parts ;
427+ const document = parseOfficeDocument ( zipSync ( encoded ) , "xlsx" ) ;
428+ expect ( document ) . toEqual ( parseOfficeDocument ( original , "xlsx" ) ) ;
429+ if ( document . kind !== "spreadsheet" ) throw new Error ( "Expected spreadsheet" ) ;
430+ expect ( document . sheets . map ( ( sheet ) => sheet . name ) ) . toEqual ( [ "売上 日本語" ] ) ;
431+ expect ( document . sheets [ 0 ] ?. rows ) . toEqual ( [
432+ [ "品名 🐈" , "数量" , "単価" , "合計" ] ,
433+ [ "りんご café" , "12" , "150" , "1800" ] ,
434+ ] ) ;
435+ } ,
436+ ) ;
437+
438+ it . each ( [
439+ { label : "missing" , entries : "" } ,
440+ {
441+ label : "external" ,
442+ entries : `<Relationship Id="workbook" Type="${ R } /officeDocument" Target="https://example.com/workbook.xml" TargetMode="External"/>` ,
443+ } ,
444+ {
445+ label : "ambiguous" ,
446+ entries : `<Relationship Id="one" Type="${ R } /officeDocument" Target="xl/workbook.xml"/><Relationship Id="two" Type="${ R } /officeDocument" Target="xl/workbook.xml"/>` ,
447+ } ,
448+ ] ) (
449+ "rejects a $label workbook root relationship instead of guessing its conventional path" ,
450+ ( { entries } : { entries : string } ) => {
451+ const parts = unzipSync ( workbookBytes ( "xlsx" ) ) ;
452+ parts [ "_rels/.rels" ] = strToU8 ( `<Relationships xmlns="${ REL } ">${ entries } </Relationships>` ) ;
453+ expect ( ( ) => parseOfficeDocument ( zipSync ( parts ) , "xlsx" ) ) . toThrow (
454+ / o f f i c e d o c u m e n t | r e l a t i o n s h i p / ,
455+ ) ;
456+ } ,
457+ ) ;
458+
283459 it . each ( [ "xlsx" , "xls" ] as const ) (
284460 "previews visible sheets after twenty hidden sheets in %s" ,
285461 ( format ) => {
@@ -547,6 +723,94 @@ describe("parseOfficeDocument spreadsheets", () => {
547723} ) ;
548724
549725describe ( "parseOfficeDocument presentation outlines" , ( ) => {
726+ it . each ( [
727+ { name : "hero.bin" , override : true } ,
728+ { name : "hero.data" , override : false } ,
729+ { name : "hero.jpg" , override : true } ,
730+ ] ) (
731+ "preserves PNG bytes and the whole slide model when named $name (override: $override)" ,
732+ ( { name, override } : { name : string ; override : boolean } ) => {
733+ const original = visualPresentationParts ( ) ;
734+ const bytes = original [ "ppt/media/picture.png" ] ! ;
735+ const document = parseOfficeDocument (
736+ zipSync ( presentationWithBitmap ( name , bytes , "image/png" , override ) ) ,
737+ "pptx" ,
738+ ) ;
739+ expect ( document ) . toEqual ( parseOfficeDocument ( zipSync ( original ) , "pptx" ) ) ;
740+ if ( document . kind !== "presentation" ) throw new Error ( "Expected presentation" ) ;
741+ const image = document . slides [ 0 ] ?. elements . find ( ( element ) => element . kind === "image" ) ;
742+ if ( image ?. kind !== "image" ) throw new Error ( "Expected slide bitmap" ) ;
743+ expect ( image ) . toMatchObject ( {
744+ x : 4000000 ,
745+ y : 3000000 ,
746+ width : 1000000 ,
747+ height : 1000000 ,
748+ alt : "テスト画像" ,
749+ } ) ;
750+ expect ( image . src . startsWith ( "data:image/png;base64," ) ) . toBe ( true ) ;
751+ expect (
752+ Uint8Array . from ( atob ( image . src . split ( "," ) [ 1 ] ! ) , ( character ) => character . charCodeAt ( 0 ) ) ,
753+ ) . toEqual ( bytes ) ;
754+ } ,
755+ ) ;
756+
757+ it ( "keeps actual JPEG bytes and MIME when a declared JPEG image uses an arbitrary part name" , ( ) => {
758+ // A normal Pillow RGB 2x2 JPEG, independently verified before embedding this fixture.
759+ const bytes = Uint8Array . from (
760+ atob (
761+ "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEPERETFhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/2wBDAQUFBQcGBw4ICA4eFBEUHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh7/wAARCAACAAIDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDmqKKK/VT80P/Z" ,
762+ ) ,
763+ ( character ) => character . charCodeAt ( 0 ) ,
764+ ) ;
765+ const expected = parseOfficeDocument (
766+ zipSync ( presentationWithBitmap ( "picture.jpg" , bytes , "image/jpeg" ) ) ,
767+ "pptx" ,
768+ ) ;
769+ const document = parseOfficeDocument (
770+ zipSync ( presentationWithBitmap ( "hero.bin" , bytes , "image/jpeg" ) ) ,
771+ "pptx" ,
772+ ) ;
773+ expect ( document ) . toEqual ( expected ) ;
774+ if ( document . kind !== "presentation" ) throw new Error ( "Expected presentation" ) ;
775+ const image = document . slides [ 0 ] ?. elements . find ( ( element ) => element . kind === "image" ) ;
776+ if ( image ?. kind !== "image" ) throw new Error ( "Expected JPEG bitmap" ) ;
777+ expect ( image . src ) . toBe ( `data:image/jpeg;base64,${ btoa ( String . fromCharCode ( ...bytes ) ) } ` ) ;
778+ } ) ;
779+
780+ it ( "omits non-bitmap bytes even when their part declares a supported image MIME" , ( ) => {
781+ const document = parseOfficeDocument (
782+ zipSync ( presentationWithBitmap ( "hero.bin" , strToU8 ( "ordinary text content" ) , "image/png" ) ) ,
783+ "pptx" ,
784+ ) ;
785+ if ( document . kind !== "presentation" ) throw new Error ( "Expected presentation" ) ;
786+ expect ( document . slides [ 0 ] ?. elements . some ( ( element ) => element . kind === "image" ) ) . toBe ( false ) ;
787+ expect ( document . slides [ 0 ] ?. text ) . toEqual ( [ "日本語の見出し" , "本文はマスターから継承" ] ) ;
788+ expect ( document . warnings . length ) . toBeGreaterThan ( 0 ) ;
789+ } ) ;
790+
791+ it ( "still omits an ordinary animated GIF when its declared GIF part has a png filename" , ( ) => {
792+ const bytes = Uint8Array . from (
793+ atob (
794+ "R0lGODlhAgACAIEAACh4yAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQACgAAACwAAAAAAgACAAAIBgABCAQQEAAh+QQBCgABACwAAAAAAgACAIHIeCgAAAAAAAAAAAAIBgABCAQQEAA7" ,
795+ ) ,
796+ ( character ) => character . charCodeAt ( 0 ) ,
797+ ) ;
798+ const document = parseOfficeDocument (
799+ zipSync ( presentationWithBitmap ( "hero.png" , bytes , "image/gif" ) ) ,
800+ "pptx" ,
801+ ) ;
802+ const expected = parseOfficeDocument ( zipSync ( visualPresentationParts ( ) ) , "pptx" ) ;
803+ if ( document . kind !== "presentation" || expected . kind !== "presentation" )
804+ throw new Error ( "Expected presentation" ) ;
805+ expect ( document . slides [ 0 ] ?. elements ) . toEqual (
806+ expected . slides [ 0 ] ?. elements . filter ( ( element ) => element . kind !== "image" ) ,
807+ ) ;
808+ expect ( document . slides . map ( ( slide ) => slide . text ) ) . toEqual (
809+ expected . slides . map ( ( slide ) => slide . text ) ,
810+ ) ;
811+ expect ( document . warnings . join ( " " ) ) . toContain ( "Only static PNG, JPEG, GIF, and WebP images" ) ;
812+ } ) ;
813+
550814 it . each ( [
551815 { location : "deck/main.xml" , strict : false } ,
552816 { location : "main.xml" , strict : false } ,
0 commit comments