Merge pull request #2201 from performonkey/master

Fix patch doc add duplicate content type
This commit is contained in:
Dolan
2023-06-29 15:05:44 +01:00
committed by GitHub
2 changed files with 38 additions and 0 deletions

View File

@ -47,5 +47,31 @@ describe("content-types-manager", () => {
type: "element", 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);
});
}); });
}); });

View File

@ -4,6 +4,18 @@ import { getFirstLevelElements } from "./util";
export const appendContentType = (element: Element, contentType: string, extension: string): void => { export const appendContentType = (element: Element, contentType: string, extension: string): void => {
const relationshipElements = getFirstLevelElements(element, "Types"); 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 // eslint-disable-next-line functional/immutable-data
relationshipElements.push({ relationshipElements.push({
attributes: { attributes: {