All files / src/web/flowable/roundTrip managedCollections.ts

98.66% Statements 74/75
96% Branches 48/50
100% Functions 16/16
98.59% Lines 70/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186                                                              216x 4x     620x 212x 183x     29x 1x 2x     28x       32x 13x     19x 19x                 216x                       216x 93x 73x   93x     123x 54x 54x     69x 259x     69x 20x 49x 11x                 900x 900x 900x 900x   900x 216x   216x 216x 216x 216x     900x 193x 4x           30x 30x       226x 226x               226x 91x   91x 23x 23x 23x 23x 20x   3x   23x       91x 6x         46x 46x 38x     8x 8x 8x   8x 3x   5x   8x       91x 192x 103x   89x    
import type { Element as XmlElement, Node as XmlNode } from '@xmldom/xmldom';
import type { XmlIdentified } from '../types';
import { BPMN_MODEL_NAMESPACE, managedExtensionChildNames } from './constants';
import {
	buildXmlIdentity,
	detachNode,
	findDirectChild,
	getElementChildren,
	getLocalName,
	getNodeDocument,
	insertAfter,
	insertBeforeNode,
	isActivitiElement,
	parseXmlFragment,
	replaceNode,
} from './xmlUtils';
 
interface CollectionOptions<T extends XmlIdentified> {
	isManagedNode: (node: XmlElement) => boolean;
	createNode: (item: T) => XmlElement;
	patchNode: (node: XmlElement, item: T) => void;
	matchFallback?: (node: XmlElement, item: T) => boolean;
	insertBefore?: (parent: XmlElement) => XmlNode | null;
}
 
function findFallbackMatch<T extends XmlIdentified>(
	item: T,
	existingNodes: XmlElement[],
	matchedNodes: Set<XmlElement>,
	matchFallback: ((node: XmlElement, item: T) => boolean) | undefined,
): XmlElement | undefined {
	if (!matchFallback) {
		return undefined;
	}
 
	const fallbackMatches = existingNodes.filter((node) => !matchedNodes.has(node) && matchFallback(node, item));
	if (fallbackMatches.length === 1) {
		return fallbackMatches[0];
	}
 
	if (fallbackMatches.length > 1 && item.xmlIdentity) {
		const scope = item.xmlIdentity.slice(0, item.xmlIdentity.indexOf(':'));
		return fallbackMatches.find((node) => item.xmlIdentity === buildXmlIdentity(scope, node));
	}
 
	return undefined;
}
 
function findIdentityMatch<T extends XmlIdentified>(item: T, existingNodes: XmlElement[], matchedNodes: Set<XmlElement>): XmlElement | undefined {
	if (!item.xmlIdentity) {
		return undefined;
	}
 
	const scope = item.xmlIdentity.slice(0, item.xmlIdentity.indexOf(':'));
	return existingNodes.find((node) => !matchedNodes.has(node) && item.xmlIdentity === buildXmlIdentity(scope, node));
}
 
function resolveNodeToPlace<T extends XmlIdentified>(
	item: T,
	existingNodes: XmlElement[],
	matchedNodes: Set<XmlElement>,
	options: CollectionOptions<T>,
): XmlElement {
	return findFallbackMatch(item, existingNodes, matchedNodes, options.matchFallback)
		?? findIdentityMatch(item, existingNodes, matchedNodes)
		?? options.createNode(item);
}
 
function placeManagedNode(
	parent: XmlElement,
	node: XmlElement,
	lastPlacedNode: XmlNode | null,
	insertionAnchor: XmlNode | null,
	isManagedNode: (node: XmlElement) => boolean,
): void {
	if (lastPlacedNode?.parentNode === parent) {
		if (lastPlacedNode.nextSibling !== node) {
			insertAfter(parent, node, lastPlacedNode);
		}
		return;
	}
 
	if (insertionAnchor?.parentNode === parent && insertionAnchor !== node) {
		insertBeforeNode(parent, node, insertionAnchor);
		return;
	}
 
	const firstManagedSibling = Array.from(parent.childNodes).find((child): child is XmlElement => {
		return child !== node && child.nodeType === child.ELEMENT_NODE && isManagedNode(child as XmlElement);
	});
 
	if (firstManagedSibling) {
		insertBeforeNode(parent, node, firstManagedSibling);
	} else if (node.parentNode !== parent) {
		parent.appendChild(node);
	}
}
 
export function reconcileManagedCollection<T extends XmlIdentified>(
	parent: XmlElement,
	items: T[],
	options: CollectionOptions<T>,
): void {
	const existingNodes = getElementChildren(parent).filter(options.isManagedNode);
	const matchedNodes = new Set<XmlElement>();
	let lastPlacedNode: XmlNode | null = null;
	const insertionAnchor = options.insertBefore?.(parent) || null;
 
	for (const item of items) {
		const nodeToPlace = resolveNodeToPlace(item, existingNodes, matchedNodes, options);
 
		options.patchNode(nodeToPlace, item);
		placeManagedNode(parent, nodeToPlace, lastPlacedNode, insertionAnchor, options.isManagedNode);
		lastPlacedNode = nodeToPlace;
		matchedNodes.add(nodeToPlace);
	}
 
	for (const existingNode of existingNodes) {
		if (!matchedNodes.has(existingNode)) {
			detachNode(existingNode);
		}
	}
}
 
export function createElementFromFragment(parent: XmlElement, xml: string, namespaces: Record<string, string>): XmlElement {
	const fragment = parseXmlFragment(xml, namespaces)[0];
	return getNodeDocument(parent).importNode(fragment, true) as XmlElement;
}
 
function isManagedExtensionChild(node: XmlElement): boolean {
	const localName = getLocalName(node);
	return managedExtensionChildNames.has(localName) && isActivitiElement(node, localName);
}
 
export function appendPreservedExtensionElements(
	extensionElements: XmlElement,
	preservedExtensionElements: string[],
	namespaces: Record<string, string>,
): void {
	const existingPreservedNodes = getElementChildren(extensionElements).filter((child) => !isManagedExtensionChild(child));
	let preservedIndex = 0;
 
	for (const preservedXml of preservedExtensionElements) {
		for (const fragment of parseXmlFragment(preservedXml, namespaces)) {
			const importedFragment = getNodeDocument(extensionElements).importNode(fragment, true) as XmlElement;
			const existingNode = existingPreservedNodes[preservedIndex];
			if (existingNode) {
				replaceNode(existingNode, importedFragment);
			} else {
				extensionElements.appendChild(importedFragment);
			}
			preservedIndex += 1;
		}
	}
 
	for (const staleNode of existingPreservedNodes.slice(preservedIndex)) {
		detachNode(staleNode);
	}
}
 
export function ensureExtensionElements(element: XmlElement): XmlElement {
	const existing = findDirectChild(element, 'extensionElements');
	if (existing) {
		return existing;
	}
 
	const extensionElements = getNodeDocument(element).createElementNS(BPMN_MODEL_NAMESPACE, 'extensionElements');
	const documentation = findDirectChild(element, 'documentation');
	Iif (documentation) {
		insertAfter(element, extensionElements, documentation);
	} else if (element.firstChild) {
		element.insertBefore(extensionElements, element.firstChild);
	} else {
		element.appendChild(extensionElements);
	}
	return extensionElements;
}
 
export function hasMeaningfulChildren(element: XmlElement): boolean {
	return Array.from(element.childNodes).some((child) => {
		if (child.nodeType === child.TEXT_NODE) {
			return (child.textContent || '').trim().length > 0;
		}
		return true;
	});
}