|
| 1 | +import Automizer from '../src/index'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as JSZip from 'jszip'; |
| 5 | +import { DOMParser } from '@xmldom/xmldom'; |
| 6 | + |
| 7 | +test('Add and modify hyperlinks', async () => { |
| 8 | + const automizer = new Automizer({ |
| 9 | + templateDir: `${__dirname}/pptx-templates`, |
| 10 | + outputDir: `${__dirname}/pptx-output`, |
| 11 | + }); |
| 12 | + |
| 13 | + const pres = automizer |
| 14 | + .loadRoot(`RootTemplate.pptx`) |
| 15 | + .load(`EmptySlide.pptx`, 'empty') |
| 16 | + .load(`SlideWithLink.pptx`, 'link'); |
| 17 | + |
| 18 | + // Track if the hyperlink was added |
| 19 | + const outputFile = `modify-hyperlink.test.pptx`; |
| 20 | + const outputPath = path.join(`${__dirname}/pptx-output`, outputFile); |
| 21 | + |
| 22 | + const result = await pres |
| 23 | + // Add the slide with the existing hyperlink |
| 24 | + .addSlide('empty', 1, (slide) => { |
| 25 | + // Add the element with the hyperlink from the source slide |
| 26 | + slide.addElement('link', 1, 'ExternalLink'); |
| 27 | + }) |
| 28 | + .write(outputFile); |
| 29 | + |
| 30 | + // Verify the number of slides |
| 31 | + expect(result.slides).toBe(2); |
| 32 | + |
| 33 | + // Now verify that the hyperlink was actually copied by checking the PPTX file |
| 34 | + // Read the generated PPTX file |
| 35 | + const fileData = fs.readFileSync(outputPath); |
| 36 | + const zip = await JSZip.loadAsync(fileData); |
| 37 | + |
| 38 | + // The second slide should be slide2.xml (index starts at 1 in PPTX) |
| 39 | + // Check its relationships file for hyperlink entries |
| 40 | + const slideRelsPath = 'ppt/slides/_rels/slide2.xml.rels'; |
| 41 | + const slideRelsFile = zip.file(slideRelsPath); |
| 42 | + |
| 43 | + // Make sure the file exists |
| 44 | + expect(slideRelsFile).not.toBeNull(); |
| 45 | + |
| 46 | + // Get the file content |
| 47 | + const slideRelsXml = await slideRelsFile!.async('text'); |
| 48 | + |
| 49 | + // Parse the XML |
| 50 | + const parser = new DOMParser(); |
| 51 | + const xmlDoc = parser.parseFromString(slideRelsXml, 'application/xml'); |
| 52 | + |
| 53 | + // Look for hyperlink relationships |
| 54 | + const relationships = xmlDoc.getElementsByTagName('Relationship'); |
| 55 | + let hasHyperlink = false; |
| 56 | + let hyperlinkId = ''; |
| 57 | + |
| 58 | + for (let i = 0; i < relationships.length; i++) { |
| 59 | + const relationship = relationships[i]; |
| 60 | + const type = relationship.getAttribute('Type'); |
| 61 | + if (type === 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink') { |
| 62 | + hasHyperlink = true; |
| 63 | + const id = relationship.getAttribute('Id'); |
| 64 | + hyperlinkId = id || ''; |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Verify that a hyperlink relationship exists |
| 70 | + expect(hasHyperlink).toBe(true); |
| 71 | + expect(hyperlinkId).not.toBe(''); |
| 72 | + |
| 73 | + // Now check if the slide XML contains the hyperlink reference |
| 74 | + const slidePath = 'ppt/slides/slide2.xml'; |
| 75 | + const slideFile = zip.file(slidePath); |
| 76 | + |
| 77 | + // Make sure the file exists |
| 78 | + expect(slideFile).not.toBeNull(); |
| 79 | + |
| 80 | + // Get the file content |
| 81 | + const slideXml = await slideFile!.async('text'); |
| 82 | + |
| 83 | + // Verify that the hyperlink ID is referenced in the slide content |
| 84 | + expect(slideXml.includes(`r:id="${hyperlinkId}"`)).toBe(true); |
| 85 | +}); |
| 86 | + |
| 87 | +test('Copy full slide with hyperlinks', async () => { |
| 88 | + const automizer = new Automizer({ |
| 89 | + templateDir: `${__dirname}/pptx-templates`, |
| 90 | + outputDir: `${__dirname}/pptx-output`, |
| 91 | + }); |
| 92 | + |
| 93 | + const pres = automizer |
| 94 | + .loadRoot(`RootTemplate.pptx`) |
| 95 | + .load(`SlideWithLink.pptx`, 'link'); |
| 96 | + |
| 97 | + const outputFile = `copy-slide-with-hyperlink.test.pptx`; |
| 98 | + const outputPath = path.join(`${__dirname}/pptx-output`, outputFile); |
| 99 | + |
| 100 | + const result = await pres |
| 101 | + .addSlide('link', 1) |
| 102 | + .write(outputFile); |
| 103 | + |
| 104 | + // Verify the number of slides |
| 105 | + expect(result.slides).toBe(2); |
| 106 | + |
| 107 | + // Read the generated PPTX file |
| 108 | + const fileData = fs.readFileSync(outputPath); |
| 109 | + const zip = await JSZip.loadAsync(fileData); |
| 110 | + |
| 111 | + // Check relationships file for slide 2 |
| 112 | + const slideRelsPath = 'ppt/slides/_rels/slide2.xml.rels'; |
| 113 | + const slideRelsFile = zip.file(slideRelsPath); |
| 114 | + expect(slideRelsFile).not.toBeNull(); |
| 115 | + |
| 116 | + const slideRelsXml = await slideRelsFile!.async('text'); |
| 117 | + const parser = new DOMParser(); |
| 118 | + const xmlDoc = parser.parseFromString(slideRelsXml, 'application/xml'); |
| 119 | + |
| 120 | + // Look for hyperlink relationships |
| 121 | + const relationships = xmlDoc.getElementsByTagName('Relationship'); |
| 122 | + let hasExternalHyperlink = false; |
| 123 | + let hasInternalHyperlink = false; |
| 124 | + let externalHyperlinkId = ''; |
| 125 | + let internalHyperlinkId = ''; |
| 126 | + |
| 127 | + for (let i = 0; i < relationships.length; i++) { |
| 128 | + const relationship = relationships[i]; |
| 129 | + const type = relationship.getAttribute('Type'); |
| 130 | + if (type === 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink') { |
| 131 | + const targetMode = relationship.getAttribute('TargetMode'); |
| 132 | + const id = relationship.getAttribute('Id') || ''; |
| 133 | + |
| 134 | + if (targetMode === 'External') { |
| 135 | + hasExternalHyperlink = true; |
| 136 | + externalHyperlinkId = id; |
| 137 | + } else if (!targetMode) { |
| 138 | + hasInternalHyperlink = true; |
| 139 | + internalHyperlinkId = id; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // Verify that hyperlink relationships exist |
| 145 | + expect(hasExternalHyperlink).toBe(true); |
| 146 | + expect(externalHyperlinkId).not.toBe(''); |
| 147 | + |
| 148 | + // Check the slide XML content |
| 149 | + const slidePath = 'ppt/slides/slide2.xml'; |
| 150 | + const slideFile = zip.file(slidePath); |
| 151 | + expect(slideFile).not.toBeNull(); |
| 152 | + |
| 153 | + const slideXml = await slideFile!.async('text'); |
| 154 | + |
| 155 | + // Verify hyperlink references in slide content |
| 156 | + expect(slideXml.includes(`r:id="${externalHyperlinkId}"`)).toBe(true); |
| 157 | + |
| 158 | + if (hasInternalHyperlink) { |
| 159 | + expect(slideXml.includes(`r:id="${internalHyperlinkId}"`)).toBe(true); |
| 160 | + } |
| 161 | +}); |
0 commit comments