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

98.46% Statements 64/65
77.19% Branches 44/57
100% Functions 27/27
98.36% Lines 60/61

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                                                                                              2x 2x       6x 6x                       106x         106x 106x 106x   106x 354x     106x 106x     106x 106x 1528x 1528x 18x   1510x     106x 236x 2x   43x 106x     106x 237x 1x   33x 106x     106x 106x 106x 110x 110x 110x 571x 6x 14x 11x 110x 151x 151x       110x 110x 110x 110x 110x 110x 25x     110x 25x 25x 76x 3x   114x     25x 75x 3x   25x     25x   25x          
import type { Document as XmlDocument, Element as XmlElement } from '@xmldom/xmldom';
import { buildEventListenerNode, buildLocalizationNode } from './xmlBuilders';
import { applyElementOverlay } from './elementOverlay';
 
import { processPreFlowChildNames, XMLNS_NAMESPACE } from './constants';
import {
	appendPreservedExtensionElements,
	createElementFromFragment,
	ensureExtensionElements,
	hasMeaningfulChildren,
	reconcileManagedCollection,
} from './managedCollections';
import {
	patchDataObjectNode,
	patchLocalizationNode,
	patchMessageDefinitionNode,
	patchProcessEventListenerNode,
	patchSignalDefinitionNode,
} from './patchingHelpers';
import { buildIdMap } from './structuralIndexing';
import {
	reconcileStructuralChildren,
	syncDocumentLexicalNodes,
	syncStructuralAttributes,
} from './structuralMerge';
import type { FlowableDocumentState } from '../types';
import {
	escapeXml,
	detachNode,
	findDirectChild,
	getElementChildren,
	getElementsByLocalName,
	getLocalName,
	isActivitiElement,
} from './xmlUtils';
 
interface ApplyMergedStateParams {
	originalDocument: XmlDocument;
	serializedDocument: XmlDocument;
	originalDefinitions: XmlElement;
	serializedDefinitions: XmlElement;
	originalState: FlowableDocumentState;
	mergedState: FlowableDocumentState;
	preserveUnmatchedLexicalNodes: boolean;
}
 
function buildSignalDefinitionXml(id: string, name: string, scope: string): string {
	const scopeAttribute = scope ? ` activiti:scope="${escapeXml(scope)}"` : '';
	return `<signal id="${escapeXml(id)}" name="${escapeXml(name)}"${scopeAttribute}/>`;
}
 
function buildDataObjectXml(id: string, name: string, itemSubjectRef: string): string {
	const typeAttribute = itemSubjectRef ? ` itemSubjectRef="${escapeXml(itemSubjectRef)}"` : '';
	return `<dataObject id="${escapeXml(id)}" name="${escapeXml(name)}"${typeAttribute}/>`;
}
 
export function applyMergedStateToDocument({
	originalDocument,
	serializedDocument,
	originalDefinitions,
	serializedDefinitions,
	originalState,
	mergedState,
	preserveUnmatchedLexicalNodes,
}: ApplyMergedStateParams): void {
	const structuralContext = {
		originalById: buildIdMap(originalDocument),
		preserveUnmatchedLexicalNodes,
	};
 
	syncDocumentLexicalNodes(originalDocument, serializedDocument, structuralContext);
	syncStructuralAttributes(originalDefinitions, serializedDefinitions);
	reconcileStructuralChildren(originalDefinitions, serializedDefinitions, structuralContext);
 
	for (const [prefix, namespaceUri] of Object.entries(mergedState.namespaces)) {
		originalDefinitions.setAttributeNS(XMLNS_NAMESPACE, `xmlns:${prefix}`, namespaceUri);
	}
 
	Eif (mergedState.targetNamespace) {
		originalDefinitions.setAttribute('targetNamespace', mergedState.targetNamespace);
	}
 
	const currentElementsById = buildIdMap(originalDocument);
	for (const [id, elementState] of Object.entries(mergedState.elements)) {
		const element = currentElementsById.get(id);
		if (!element) {
			continue;
		}
		applyElementOverlay(element, elementState, originalState.elements[id], mergedState.namespaces);
	}
 
	reconcileManagedCollection(originalDefinitions, mergedState.signalDefinitions, {
		isManagedNode: (node) => getLocalName(node) === 'signal',
		createNode: (item) => createElementFromFragment(originalDefinitions, buildSignalDefinitionXml(item.id, item.name, item.scope), mergedState.namespaces),
		patchNode: patchSignalDefinitionNode,
		matchFallback: (node, item) => node.getAttribute('id') === item.id,
		insertBefore: (parent) => findDirectChild(parent, 'process') || findDirectChild(parent, 'collaboration') || null,
	});
 
	reconcileManagedCollection(originalDefinitions, mergedState.messageDefinitions, {
		isManagedNode: (node) => getLocalName(node) === 'message',
		createNode: (item) => createElementFromFragment(originalDefinitions, `<message id="${escapeXml(item.id)}" name="${escapeXml(item.name)}"/>`, mergedState.namespaces),
		patchNode: patchMessageDefinitionNode,
		matchFallback: (node, item) => node.getAttribute('id') === item.id,
		insertBefore: (parent) => findDirectChild(parent, 'process') || findDirectChild(parent, 'collaboration') || null,
	});
 
	const processElements = getElementsByLocalName(originalDocument, 'process');
	const singleProcessId = processElements.length === 1 ? (processElements[0]?.getAttribute('id') || '') : '';
	for (const processElement of processElements) {
		const processId = processElement.getAttribute('id') || '';
		const processDataObjects = mergedState.dataObjects.filter((item) => item.processId === processId || (!item.processId && singleProcessId === processId));
		reconcileManagedCollection(processElement, processDataObjects, {
			isManagedNode: (node) => getLocalName(node) === 'dataObject',
			createNode: (item) => createElementFromFragment(processElement, buildDataObjectXml(item.id, item.name, item.itemSubjectRef), mergedState.namespaces),
			patchNode: (node, item) => patchDataObjectNode(node, item, mergedState.namespaces),
			matchFallback: (node, item) => node.getAttribute('id') === item.id,
			insertBefore: () => getElementChildren(processElement).find((child) => {
				const localName = getLocalName(child);
				return localName !== 'dataObject' && !processPreFlowChildNames.has(localName);
			}) || null,
		});
 
		const processEventListeners = mergedState.eventListeners.filter((item) => item.processId === processId || (!item.processId && singleProcessId === processId));
		const processLocalizations = mergedState.localizations.filter((item) => item.processId === processId || (!item.processId && singleProcessId === processId));
		const processPreservedExtensionElements = mergedState.processExtensionElements.find((item) => item.processId === processId || (!item.processId && singleProcessId === processId))?.preservedExtensionElements || [];
		const needsProcessExtensionElements = processEventListeners.length > 0 || processLocalizations.length > 0 || processPreservedExtensionElements.length > 0;
		let processExtensionElements = findDirectChild(processElement, 'extensionElements');
		if (needsProcessExtensionElements) {
			processExtensionElements = processExtensionElements || ensureExtensionElements(processElement);
		}
 
		if (processExtensionElements) {
			const extensionTarget = processExtensionElements;
			reconcileManagedCollection(processExtensionElements, processEventListeners, {
				isManagedNode: (node) => isActivitiElement(node, 'eventListener'),
				createNode: (item) => createElementFromFragment(extensionTarget, buildEventListenerNode(item), mergedState.namespaces),
				patchNode: patchProcessEventListenerNode,
				matchFallback: (node, item) => node.getAttribute('events') === item.events && (node.getAttribute('class') || node.getAttribute('delegateExpression') || node.getAttribute('signalName') || node.getAttribute('messageName') || node.getAttribute('errorCode') || '') === item.implementation,
			});
 
			reconcileManagedCollection(processExtensionElements, processLocalizations, {
				isManagedNode: (node) => isActivitiElement(node, 'localization'),
				createNode: (item) => createElementFromFragment(extensionTarget, buildLocalizationNode(item), mergedState.namespaces),
				patchNode: patchLocalizationNode,
				matchFallback: (node, item) => node.getAttribute('locale') === item.locale,
			});
 
			appendPreservedExtensionElements(processExtensionElements, processPreservedExtensionElements, mergedState.namespaces);
 
			Iif (!hasMeaningfulChildren(processExtensionElements)) {
				detachNode(processExtensionElements);
			}
		}
	}
}