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

94.7% Statements 143/151
85.1% Branches 80/94
100% Functions 41/41
94.7% Lines 143/151

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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331                                                                              1510x 27180x 27180x 27180x 159x 159x   27021x         1510x 19x 2x           1510x                       66x 62x   4x             1x 1x 1x               66x 158x 4x 50x 154x   66x 150x 1x   3x   66x 152x 1x   1x   66x 152x 1x   2x   66x 157x 1x   5x   66x 157x 1x   2x   66x 155x 2x         4x   66x 159x 1x   3x   66x               1510x 1510x 44x   1510x 1444x   66x 66x 2x         1x 1x 1x                   1510x 1510x 1458x     1458x   52x 1510x 1510x 2x   52x 1x                 1510x 1510x 1496x 1x   1496x   14x 1510x 1510x 1510x 3x   14x 1x                 1510x 1510x 1509x     1509x   1x 1510x 1510x 1x   1x           1510x 1510x 2x 2x 2x 2x         1508x 1x                 1510x 1510x 1499x     1499x   11x 1510x 11x   11x 11x 11x 11x           1510x 1510x 10x   1510x 1510x 14x   1510x 1510x 10x   1510x 1510x 7x 3x   4x     1510x 1510x 3x         1510x 1x 1x   1509x                 1510x 1510x 1510x 1510x 1510x 1510x 1510x 1510x 1510x 1510x    
import type { Element as XmlElement } from '@xmldom/xmldom';
import {
	buildFieldExtensionNode,
	buildFormPropertyNode,
	buildIOParameterNode,
	buildListenerNode,
	buildMultiInstanceNode,
} from './xmlBuilders';
import { BPMN_MODEL_NAMESPACE, XSI_NAMESPACE } from './constants';
import {
	appendPreservedExtensionElements,
	createElementFromFragment,
	ensureExtensionElements,
	hasMeaningfulChildren,
	reconcileManagedCollection,
} from './managedCollections';
import {
	patchExceptionMapNode,
	patchFieldExtensionNode,
	patchFormPropertyNode,
	patchIoParameterNode,
	patchListenerNode,
	setOptionalAttribute,
} from './patchingHelpers';
import { type FlowableElementState, FLOWABLE_ATTRIBUTE_KEYS } from '../types';
import {
	detachNode,
	escapeXml,
	findDirectChild,
	getElementChildren,
	getNodeDocument,
	isActivitiElement,
	removeActivitiAttribute,
	replaceNode,
	setActivitiAttribute,
	setTextContentPreservingComments,
} from './xmlUtils';
 
function applyEditableActivitiAttributes(element: XmlElement, elementState: FlowableElementState): void {
	for (const key of FLOWABLE_ATTRIBUTE_KEYS) {
		element.removeAttribute(key);
		const value = elementState.activitiAttributes[key]?.trim();
		if (value) {
			setActivitiAttribute(element, key, value);
			continue;
		}
		removeActivitiAttribute(element, key);
	}
}
 
function applyPreservedAttributes(element: XmlElement, elementState: FlowableElementState): void {
	for (const [attributeName, value] of Object.entries(elementState.preservedAttributes)) {
		if (!element.hasAttribute(attributeName)) {
			element.setAttribute(attributeName, value);
		}
	}
}
 
function countManagedExtensionItems(elementState: FlowableElementState): number {
	return elementState.fieldExtensions.length
		+ elementState.inputParameters.length
		+ elementState.outputParameters.length
		+ elementState.taskListeners.length
		+ elementState.executionListeners.length
		+ elementState.formProperties.length
		+ elementState.exceptionMaps.length
		+ elementState.preservedExtensionElements.length
		+ (elementState.failedJobRetryTimeCycle ? 1 : 0);
}
 
function buildRetryItems(elementState: FlowableElementState): Array<{ xmlIdentity: string; value: string }> {
	if (!elementState.failedJobRetryTimeCycle) {
		return [];
	}
	return [{
		xmlIdentity: 'failedJobRetryTimeCycle:http://activiti.org/bpmn:failedJobRetryTimeCycle:0',
		value: elementState.failedJobRetryTimeCycle,
	}];
}
 
function buildExceptionMapFragment(item: FlowableElementState['exceptionMaps'][number]): string {
	const errorCodeAttribute = item.errorCode ? ` errorCode="${escapeXml(item.errorCode)}"` : '';
	const includeChildAttribute = item.includeChildExceptions ? ' includeChildExceptions="true"' : '';
	return `<activiti:mapException${errorCodeAttribute}${includeChildAttribute}>${escapeXml(item.className)}</activiti:mapException>`;
}
 
function reconcileExtensionElements(
	extensionElements: XmlElement,
	elementState: FlowableElementState,
	namespaces: Record<string, string>,
): void {
	reconcileManagedCollection(extensionElements, elementState.fieldExtensions, {
		isManagedNode: (node) => isActivitiElement(node, 'field'),
		createNode: (item) => createElementFromFragment(extensionElements, buildFieldExtensionNode(item), namespaces),
		patchNode: (node, item) => patchFieldExtensionNode(node, item, namespaces),
		matchFallback: (node, item) => node.getAttribute('name') === item.name,
	});
	reconcileManagedCollection(extensionElements, elementState.inputParameters, {
		isManagedNode: (node) => isActivitiElement(node, 'in'),
		createNode: (item) => createElementFromFragment(extensionElements, buildIOParameterNode('activiti:in', item), namespaces),
		patchNode: patchIoParameterNode,
		matchFallback: (node, item) => node.getAttribute('target') === item.target,
	});
	reconcileManagedCollection(extensionElements, elementState.outputParameters, {
		isManagedNode: (node) => isActivitiElement(node, 'out'),
		createNode: (item) => createElementFromFragment(extensionElements, buildIOParameterNode('activiti:out', item), namespaces),
		patchNode: patchIoParameterNode,
		matchFallback: (node, item) => node.getAttribute('target') === item.target,
	});
	reconcileManagedCollection(extensionElements, elementState.taskListeners, {
		isManagedNode: (node) => isActivitiElement(node, 'taskListener'),
		createNode: (item) => createElementFromFragment(extensionElements, buildListenerNode('activiti:taskListener', item), namespaces),
		patchNode: patchListenerNode,
		matchFallback: (node, item) => node.getAttribute('event') === item.event,
	});
	reconcileManagedCollection(extensionElements, elementState.executionListeners, {
		isManagedNode: (node) => isActivitiElement(node, 'executionListener'),
		createNode: (item) => createElementFromFragment(extensionElements, buildListenerNode('activiti:executionListener', item), namespaces),
		patchNode: patchListenerNode,
		matchFallback: (node, item) => node.getAttribute('event') === item.event,
	});
	reconcileManagedCollection(extensionElements, elementState.formProperties, {
		isManagedNode: (node) => isActivitiElement(node, 'formProperty'),
		createNode: (item) => createElementFromFragment(extensionElements, buildFormPropertyNode(item), namespaces),
		patchNode: patchFormPropertyNode,
		matchFallback: (node, item) => node.getAttribute('id') === item.id,
	});
	reconcileManagedCollection(extensionElements, buildRetryItems(elementState), {
		isManagedNode: (node) => isActivitiElement(node, 'failedJobRetryTimeCycle'),
		createNode: (item) => createElementFromFragment(
			extensionElements,
			`<activiti:failedJobRetryTimeCycle>${escapeXml(item.value)}</activiti:failedJobRetryTimeCycle>`,
			namespaces,
		),
		patchNode: (node, item) => setTextContentPreservingComments(node, item.value),
	});
	reconcileManagedCollection(extensionElements, elementState.exceptionMaps, {
		isManagedNode: (node) => isActivitiElement(node, 'mapException'),
		createNode: (item) => createElementFromFragment(extensionElements, buildExceptionMapFragment(item), namespaces),
		patchNode: patchExceptionMapNode,
		matchFallback: (node, item) => node.textContent === item.className,
	});
	appendPreservedExtensionElements(extensionElements, elementState.preservedExtensionElements, namespaces);
}
 
function syncExtensionElements(
	element: XmlElement,
	elementState: FlowableElementState,
	namespaces: Record<string, string>,
): void {
	let extensionElements = findDirectChild(element, 'extensionElements');
	if (countManagedExtensionItems(elementState) > 0) {
		extensionElements = ensureExtensionElements(element);
	}
	if (!extensionElements) {
		return;
	}
	reconcileExtensionElements(extensionElements, elementState, namespaces);
	if (!hasMeaningfulChildren(extensionElements)) {
		detachNode(extensionElements);
	}
}
 
function prependChild(parent: XmlElement, child: XmlElement): void {
	Eif (parent.firstChild) {
		parent.insertBefore(child, parent.firstChild);
		return;
	}
	parent.appendChild(child);
}
 
function syncDocumentation(
	element: XmlElement,
	elementState: FlowableElementState,
	originalElementState: FlowableElementState | undefined,
): void {
	const existingDocumentation = findDirectChild(element, 'documentation');
	if (!elementState.documentation) {
		Iif (existingDocumentation) {
			detachNode(existingDocumentation);
		}
		return;
	}
	const documentation = existingDocumentation || getNodeDocument(element).createElementNS(BPMN_MODEL_NAMESPACE, 'documentation');
	const documentationChanged = elementState.documentation !== (originalElementState?.documentation || '');
	if (documentationChanged || !existingDocumentation) {
		setTextContentPreservingComments(documentation, elementState.documentation);
	}
	if (!existingDocumentation) {
		prependChild(element, documentation);
	}
}
 
function syncConditionExpression(
	element: XmlElement,
	elementState: FlowableElementState,
	originalElementState: FlowableElementState | undefined,
): void {
	const existingCondition = findDirectChild(element, 'conditionExpression');
	if (!elementState.conditionExpression) {
		if (existingCondition) {
			detachNode(existingCondition);
		}
		return;
	}
	const condition = existingCondition || getNodeDocument(element).createElementNS(BPMN_MODEL_NAMESPACE, 'conditionExpression');
	condition.setAttributeNS(XSI_NAMESPACE, 'xsi:type', 'tFormalExpression');
	const conditionChanged = elementState.conditionExpression !== (originalElementState?.conditionExpression || '');
	if (conditionChanged || !existingCondition) {
		setTextContentPreservingComments(condition, elementState.conditionExpression);
	}
	if (!existingCondition) {
		element.appendChild(condition);
	}
}
 
function syncScript(
	element: XmlElement,
	elementState: FlowableElementState,
	originalElementState: FlowableElementState | undefined,
): void {
	const existingScript = findDirectChild(element, 'script');
	if (!elementState.script) {
		Iif (existingScript) {
			detachNode(existingScript);
		}
		return;
	}
	const script = existingScript || getNodeDocument(element).createElementNS(BPMN_MODEL_NAMESPACE, 'script');
	const scriptChanged = elementState.script !== (originalElementState?.script || '');
	if (scriptChanged || !existingScript) {
		setTextContentPreservingComments(script, elementState.script);
	}
	Iif (!existingScript) {
		element.appendChild(script);
	}
}
 
function syncMultiInstance(element: XmlElement, elementState: FlowableElementState, namespaces: Record<string, string>): void {
	const existingMultiInstance = findDirectChild(element, 'multiInstanceLoopCharacteristics');
	if (elementState.multiInstance) {
		const replacement = createElementFromFragment(element, buildMultiInstanceNode(elementState.multiInstance), namespaces);
		Eif (existingMultiInstance) {
			replaceNode(existingMultiInstance, replacement);
			return;
		}
		element.appendChild(replacement);
		return;
	}
	if (existingMultiInstance && elementState.multiInstance === null) {
		detachNode(existingMultiInstance);
	}
}
 
function syncTimerDefinition(
	element: XmlElement,
	elementState: FlowableElementState,
	originalElementState: FlowableElementState | undefined,
): void {
	const existingTimerDefinition = findDirectChild(element, 'timerEventDefinition');
	if (!elementState.timerDefinition?.value) {
		Iif (existingTimerDefinition && originalElementState?.timerDefinition) {
			detachNode(existingTimerDefinition);
		}
		return;
	}
	const timerDefinition = existingTimerDefinition || getNodeDocument(element).createElementNS(BPMN_MODEL_NAMESPACE, 'timerEventDefinition');
	for (const child of getElementChildren(timerDefinition)) {
		detachNode(child);
	}
	const timerChild = getNodeDocument(element).createElementNS(BPMN_MODEL_NAMESPACE, elementState.timerDefinition.type);
	setTextContentPreservingComments(timerChild, elementState.timerDefinition.value);
	timerDefinition.appendChild(timerChild);
	Iif (!existingTimerDefinition) {
		element.appendChild(timerDefinition);
	}
}
 
function syncEventDefinitionAttributes(element: XmlElement, elementState: FlowableElementState): void {
	const existingErrorDefinition = findDirectChild(element, 'errorEventDefinition');
	if (existingErrorDefinition) {
		setOptionalAttribute(existingErrorDefinition, 'errorRef', elementState.errorRef);
	}
	const existingSignalDefinition = findDirectChild(element, 'signalEventDefinition');
	if (existingSignalDefinition) {
		setOptionalAttribute(existingSignalDefinition, 'signalRef', elementState.signalRef);
	}
	const existingMessageDefinition = findDirectChild(element, 'messageEventDefinition');
	if (existingMessageDefinition) {
		setOptionalAttribute(existingMessageDefinition, 'messageRef', elementState.messageRef);
	}
	const existingTerminateDefinition = findDirectChild(element, 'terminateEventDefinition');
	if (existingTerminateDefinition) {
		if (elementState.terminateAll === 'true') {
			setActivitiAttribute(existingTerminateDefinition, 'terminateAll', 'true');
		} else {
			removeActivitiAttribute(existingTerminateDefinition, 'terminateAll');
		}
	}
	const existingCompensateDefinition = findDirectChild(element, 'compensateEventDefinition');
	if (existingCompensateDefinition) {
		setOptionalAttribute(existingCompensateDefinition, 'activityRef', elementState.compensateActivityRef);
	}
}
 
function syncCompensationAttribute(element: XmlElement, elementState: FlowableElementState): void {
	if (elementState.isForCompensation === 'true') {
		element.setAttribute('isForCompensation', 'true');
		return;
	}
	element.removeAttribute('isForCompensation');
}
 
export function applyElementOverlay(
	element: XmlElement,
	elementState: FlowableElementState,
	originalElementState: FlowableElementState | undefined,
	namespaces: Record<string, string>,
): void {
	applyEditableActivitiAttributes(element, elementState);
	applyPreservedAttributes(element, elementState);
	syncExtensionElements(element, elementState, namespaces);
	syncDocumentation(element, elementState, originalElementState);
	syncConditionExpression(element, elementState, originalElementState);
	syncScript(element, elementState, originalElementState);
	syncMultiInstance(element, elementState, namespaces);
	syncTimerDefinition(element, elementState, originalElementState);
	syncEventDefinitionAttributes(element, elementState);
	syncCompensationAttribute(element, elementState);
}