Skip to content

Commit

Permalink
fix(removeDataType): avoid pre remove
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobVogelsang committed Feb 6, 2025
1 parent a8a4778 commit df16680
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
4 changes: 4 additions & 0 deletions tDataTypeTemplates/removeDataType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ describe("Function to recursively remove data types ", () => {

expect(removes.length).equals(6);
expect(removes[0].node).equal(doType);

removes.forEach(remove => expect(remove.node.parentElement).to.not.be.null);
});

it("does not remove linked data types with force option set", () => {
const removes = removeDataType({ node: lNodeType });

expect(removes.length).equals(15);
expect(removes[0].node).equal(lNodeType);

removes.forEach(remove => expect(remove.node.parentElement).to.not.be.null);
});
});
41 changes: 32 additions & 9 deletions tDataTypeTemplates/removeDataType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,46 @@ function removeOrphans(orphans: Element[]): void {
orphans.forEach((orphan) => orphan.remove());
}

function isLinked(dataType: Element): boolean {
function isLNodeTypeReferenced(lNodeType:Element):boolean {
const dtt = lNodeType.closest("DataTypeTemplates");

const id = lNodeType.getAttribute("id");

const linkedAnyLn = dtt?.ownerDocument.querySelector(
`:root > IED *[lnType="${id}"]`
);
return !!linkedAnyLn;
}

function isDataTypeReferenced(dataType: Element): boolean {
const dtt = dataType.closest("DataTypeTemplates");

const id = dataType.getAttribute("id");

const linkedData = dtt?.ownerDocument.querySelector(
`:root > DataTypeTemplates *[type="${id}"], :root > IED *[lnType="${id}"]`
const linkedData = dtt?.querySelector(
`:scope *[type="${id}"]`
);
return !!linkedData;
}

function getOrphans(ddt: Element, saveOrphans: Element[] = []): Element[] {
return Array.from(ddt.querySelectorAll(":scope > *"))
.filter((dataType) => !isLinked(dataType))
.filter((dataType) => !isDataTypeReferenced(dataType))
.filter((orphan) => !saveOrphans.includes(orphan));
}

function clonedDataType(dtt:Element,dataType:Element):Element {

return dtt.querySelector(`:scope > *[id="${dataType.getAttribute('id')}"]`)!;

}

function isDataTypeLinked(dataType:Element): boolean {
if (dataType.tagName === 'LNodeType') return isLNodeTypeReferenced(dataType);

return isDataTypeReferenced(dataType);
}

/**
* Utility function to remove data types LNodeType, DOType, DAType or EnumType.
* The function makes sure to not leave new unlinked data types .
Expand All @@ -50,19 +73,19 @@ export function removeDataType(
const dataType = dtRemove.node as Element;

const dtt = dataType.closest("DataTypeTemplates");
const dttClone = dataType.closest("DataTypeTemplates")?.cloneNode(true);
const dttClone = dataType.closest("DataTypeTemplates")?.cloneNode(true) as Element;
if (!dttClone) return [];

if (isLinked(dataType) && !options.force) return [];
if (isDataTypeLinked(dataType) && !options.force) return [];

const saveOrphans = getOrphans(dtt!);
const saveOrphans = getOrphans(dttClone);

const removes: Remove[] = [];
let orphans = [dataType];
let orphans = [clonedDataType(dttClone,dataType)];
while (orphans.length > 0) {
removes.push(...orphanRemoves(dtt!, orphans));
removeOrphans(orphans);
orphans = getOrphans(dtt!, saveOrphans);
orphans = getOrphans(dttClone!, saveOrphans);
}

return removes;
Expand Down

0 comments on commit df16680

Please sign in to comment.