From 4f06d760a317ef1f682e9672cabd13ec39db6f72 Mon Sep 17 00:00:00 2001 From: p10y Date: Wed, 28 Jun 2023 23:34:16 +0800 Subject: [PATCH 1/2] Fix patch doc add duplicate content type --- src/patcher/content-types-manager.spec.ts | 26 +++++++++++++++++++++++ src/patcher/content-types-manager.ts | 11 ++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/patcher/content-types-manager.spec.ts b/src/patcher/content-types-manager.spec.ts index 5bcebe8c9d..7e4554cf5b 100644 --- a/src/patcher/content-types-manager.spec.ts +++ b/src/patcher/content-types-manager.spec.ts @@ -47,5 +47,31 @@ describe("content-types-manager", () => { type: "element", }); }); + + it("should not append duplicate content type", () => { + const element = { + type: "element", + name: "xml", + elements: [ + { + type: "element", + name: "Types", + elements: [ + { + type: "element", + name: "Default", + attributes: { + ContentType: "image/png", + Extension: "png", + }, + }, + ], + }, + ], + }; + appendContentType(element, "image/png", "png"); + + expect(element.elements.length).toBe(1); + }); }); }); diff --git a/src/patcher/content-types-manager.ts b/src/patcher/content-types-manager.ts index 8032073bfc..120322cab1 100644 --- a/src/patcher/content-types-manager.ts +++ b/src/patcher/content-types-manager.ts @@ -4,6 +4,17 @@ import { getFirstLevelElements } from "./util"; export const appendContentType = (element: Element, contentType: string, extension: string): void => { const relationshipElements = getFirstLevelElements(element, "Types"); + + const exist = relationshipElements.some(el => + el.type === "element" + && el.name === "Default" + && el?.attributes?.ContentType === contentType + && el?.attributes?.Extension === extension + ); + if (exist) { + return; + } + // eslint-disable-next-line functional/immutable-data relationshipElements.push({ attributes: { From b0d60109c9b857b60489058de297a5cb51623be2 Mon Sep 17 00:00:00 2001 From: p10y Date: Thu, 29 Jun 2023 10:40:42 +0800 Subject: [PATCH 2/2] Fix prettier issue --- src/patcher/content-types-manager.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/patcher/content-types-manager.ts b/src/patcher/content-types-manager.ts index 120322cab1..8829c3349b 100644 --- a/src/patcher/content-types-manager.ts +++ b/src/patcher/content-types-manager.ts @@ -5,11 +5,12 @@ import { getFirstLevelElements } from "./util"; export const appendContentType = (element: Element, contentType: string, extension: string): void => { const relationshipElements = getFirstLevelElements(element, "Types"); - const exist = relationshipElements.some(el => - el.type === "element" - && el.name === "Default" - && el?.attributes?.ContentType === contentType - && el?.attributes?.Extension === extension + const exist = relationshipElements.some( + (el) => + el.type === "element" && + el.name === "Default" && + el?.attributes?.ContentType === contentType && + el?.attributes?.Extension === extension, ); if (exist) { return;