Skip to content

Commit 72fd901

Browse files
committed
fix(image): add suffix for created rId-attributes to prevent messed svg insertions
1 parent 020a456 commit 72fd901

File tree

7 files changed

+48
-23
lines changed

7 files changed

+48
-23
lines changed

src/automizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default class Automizer implements IPresentationProps {
7373

7474
setStatusTracker(statusTracker: StatusTracker['next']): void {
7575
const defaultStatusTracker = (status: StatusTracker) => {
76-
console.log(status.info + '(' + status.share + '%)');
76+
console.log(status.info + ' (' + status.share + '%)');
7777
};
7878

7979
this.status = {

src/classes/shape.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,21 @@ export class Shape {
121121
);
122122
}
123123

124-
async updateElementRelId(): Promise<void> {
124+
async updateElementsRelId(): Promise<void> {
125125
const targetSlideXml = await XmlHelper.getXmlFromArchive(
126126
this.targetArchive,
127127
this.targetSlideFile,
128128
);
129-
const targetElement = await this.getElementByRid(
129+
const targetElements = await this.getElementsByRid(
130130
targetSlideXml,
131131
this.sourceRid,
132132
);
133133

134-
targetElement
135-
.getElementsByTagName(this.relRootTag)[0]
136-
.setAttribute(this.relAttribute, this.createdRid);
134+
targetElements.forEach((targetElement) => {
135+
this.relParent(targetElement)
136+
.getElementsByTagName(this.relRootTag)[0]
137+
.setAttribute(this.relAttribute, this.createdRid);
138+
});
137139

138140
await XmlHelper.writeXmlToArchive(
139141
this.targetArchive,
@@ -142,23 +144,24 @@ export class Shape {
142144
);
143145
}
144146

145-
async updateTargetElementRelId(): Promise<void> {
146-
this.targetElement
147-
.getElementsByTagName(this.relRootTag)[0]
148-
.setAttribute(this.relAttribute, this.createdRid);
149-
}
150-
151-
async getElementByRid(slideXml: Document, rId: string): Promise<Element> {
147+
async getElementsByRid(slideXml: Document, rId: string): Promise<Element[]> {
152148
const sourceList = slideXml
153149
.getElementsByTagName('p:spTree')[0]
154150
.getElementsByTagName(this.relRootTag);
155-
const sourceElement = XmlHelper.findByAttributeValue(
151+
152+
const sourceElements = XmlHelper.findByAttributeValue(
156153
sourceList,
157154
this.relAttribute,
158155
rId,
159156
);
160157

161-
return this.relParent(sourceElement);
158+
return sourceElements;
159+
}
160+
161+
async updateTargetElementRelId(): Promise<void> {
162+
this.targetElement
163+
.getElementsByTagName(this.relRootTag)[0]
164+
.setAttribute(this.relAttribute, this.createdRid);
162165
}
163166

164167
applyCallbacks(

src/classes/slide.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,6 @@ export class Slide implements ISlide {
467467
const length = drop.length;
468468
if (length && length > 0) {
469469
XmlHelper.sliceCollection(drop, 0);
470-
console.log('Removed ' + length + ' unsupported ' + tag + ' elements.');
471470
}
472471
});
473472
await XmlHelper.writeXmlToArchive(this.targetArchive, this.targetPath, xml);

src/helper/modify-xml-helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default class ModifyXmlHelper {
4141

4242
if (element === false) {
4343
if (isRequired === true) {
44-
vd('Could not assert required tag: ' + tag + '@index:' + index);
44+
// vd('Could not assert required tag: ' + tag + '@index:' + index);
4545
}
4646
return;
4747
}

src/helper/xml-helper.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class XmlHelper {
9191
const relationNodes = presentationRelsXml.documentElement.childNodes;
9292
const rid = XmlHelper.getMaxId(relationNodes, 'Id', true);
9393

94-
return increment(rid);
94+
return increment(rid) + '-created';
9595
}
9696

9797
static getMaxId(
@@ -104,7 +104,12 @@ export class XmlHelper {
104104
for (const i in rels) {
105105
const rel = rels[i] as Element;
106106
if (rel.getAttribute !== undefined) {
107-
const id = Number(rel.getAttribute(attribute).replace('rId', ''));
107+
const id = Number(
108+
rel
109+
.getAttribute(attribute)
110+
.replace('rId', '')
111+
.replace('-created', ''),
112+
);
108113
max = id > max ? id : max;
109114
}
110115
}
@@ -293,7 +298,7 @@ export class XmlHelper {
293298
return null;
294299
}
295300

296-
static findByAttributeValue(
301+
static findFirstByAttributeValue(
297302
nodes: NodeListOf<ChildNode> | HTMLCollectionOf<Element>,
298303
attributeName: string,
299304
attributeValue: string,
@@ -310,6 +315,24 @@ export class XmlHelper {
310315
return null;
311316
}
312317

318+
static findByAttributeValue(
319+
nodes: NodeListOf<ChildNode> | HTMLCollectionOf<Element>,
320+
attributeName: string,
321+
attributeValue: string,
322+
): Element[] {
323+
const matchingNodes = <Element[]>[];
324+
for (const i in nodes) {
325+
const node = <Element>nodes[i];
326+
if (
327+
node.getAttribute &&
328+
node.getAttribute(attributeName) === attributeValue
329+
) {
330+
matchingNodes.push(node);
331+
}
332+
}
333+
return matchingNodes;
334+
}
335+
313336
static createContentTypeChild(
314337
archive: JSZip,
315338
attributes: OverrideAttribute | DefaultAttribute,

src/shapes/chart.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class Chart extends Shape implements IChart {
7070
targetSlideNumber: number,
7171
): Promise<Chart> {
7272
await this.prepare(targetTemplate, targetSlideNumber);
73-
await this.updateElementRelId();
73+
await this.updateElementsRelId();
7474

7575
return this;
7676
}

src/shapes/image.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class Image extends Shape implements IImage {
3838
targetSlideNumber: number,
3939
): Promise<Image> {
4040
await this.prepare(targetTemplate, targetSlideNumber);
41-
await this.updateElementRelId();
41+
await this.updateElementsRelId();
4242

4343
return this;
4444
}
@@ -48,7 +48,7 @@ export class Image extends Shape implements IImage {
4848
targetSlideNumber: number,
4949
): Promise<Image> {
5050
await this.prepare(targetTemplate, targetSlideNumber);
51-
await this.updateElementRelId();
51+
await this.updateElementsRelId();
5252

5353
return this;
5454
}

0 commit comments

Comments
 (0)