All files / src/models expression.model.ts

43.18% Statements 155/359
27.05% Branches 66/244
58% Functions 29/50
42.31% Lines 143/338

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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755                              9x         9x   9x 1x 9x   9x 9x         112x     9x 5x         9x   9x 9x   65x 65x     9x 73x     9x 68x   9x   9x 9x     47x 47x 47x 94x 47x   47x 47x 47x 47x     47x 10x 10x                     37x           47x 5x                     47x       9x 53x     53x             53x     53x         13x   40x           53x       9x 49x 49x 49x 49x               49x                       9x             9x   9x 18x 18x 18x     18x 18x   18x   9x           9x         5x     9x 2x     9x 18x     9x 18x     9x             9x                           9x                                                                                             9x 18x                             9x           18x 18x 18x 18x                                   18x 18x 18x                                                                                                                                                                                                                                     9x   1x                                                               1x 4x   1x 1x       1x                   1x 1x 1x 1x     6x         1x 1x   1x 1x       1x   1x     1x 1x 1x                                                                           1x 1x                 9x                                 9x                                                                                                                       9x           9x 25x     25x     9x             9x                       9x 19x 117x 19x 19x 117x 19x                 9x 18x 18x 18x   18x 47x 47x 47x   47x 47x 47x 47x                           47x                     47x 5x   47x   47x   18x 18x   18x     9x                         9x  
/*
    Copyright (C) 2017 Red Hat, Inc.
 
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
 
            http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/
import { ErrorInfo, ErrorLevel, ErrorScope, ErrorType } from './error.model';
import { MappedField, MappingModel } from './mapping.model';
 
import { ConfigModel } from './config.model';
import { IExpressionNode } from '../contracts/expression';
import { Subject } from 'rxjs';
 
export class ExpressionUpdatedEvent {
  constructor(public node?: ExpressionNode, public offset?: number) {}
}
 
export abstract class ExpressionNode implements IExpressionNode {
  protected static sequence = 0;
  public readonly uuid: string;
  public readonly str: string;
 
  constructor(prefix: string) {
    this.uuid = prefix + ExpressionNode.sequence++;
  }
 
  getUuid() {
    return this.uuid;
  }
 
  abstract toText(): string;
  abstract toHTML(): string;
}
 
export class TextNode extends ExpressionNode {
  static readonly PREFIX = 'expression-text-';
 
  constructor(public str: string) {
    super(TextNode.PREFIX);
  }
 
  toText(): string {
    return this.str;
  }
 
  toHTML(): string {
    return `<span id="${this.uuid}">${this.str.replace(/ /g, '&nbsp;')}</span>`;
  }
}
 
export class FieldNode extends ExpressionNode {
  static readonly PREFIX = 'expression-field-';
 
  constructor(
    private mapping: MappingModel,
    public mappedField?: MappedField | null,
    public metaStr?: string,
    Eindex: number = 0,
    public collectionContextPath?: string
  ) {
    super(FieldNode.PREFIX);
    Eif (!mappedField) {
      Eif (metaStr) {
        const fieldParts = metaStr.split(':');
 
        // Relative paths will not have the full field path in the meta data.
        if (fieldParts.length === 1) {
          Eif (collectionContextPath) {
            this.mappedField = mapping.getMappedFieldByPath(
              collectionContextPath + fieldParts[0],
              true
            )!;
          } else {
            this.mappedField = mapping.getMappedFieldByPath(
              fieldParts[0],
              true
            )!;
          }
        } else {
          this.mappedField = mapping.getMappedFieldByPath(
            fieldParts[1],
            true,
            fieldParts[0]
          )!;
        }
        if (!this.mappedField) {
          this.mappedField = mapping.getReferenceField(
            fieldParts[0],
            fieldParts[1]
          );
        }
      } else {
        this.mappedField = mapping.getMappedFieldForIndex(
          (index + 1)?.toString(),
          true
        )!;
      }
      mappedField = this.mappedField;
    }
  }
 
  toText(): string {
    Iif (!this.mappedField || !this.mappedField.field) {
      return '';
    }
    Iif (this.mappedField.field.enumeration) {
      // Convert enumeration field/index pairs into a string literal.
      const enumIdxVal = this.mappedField.field.enumIndexValue
        ? this.mappedField.field.enumIndexValue
        : 0;
      return '"' + this.mappedField.field.enumValues[enumIdxVal].name + '"';
    } else {
      let textStr = '${';
 
      // If the mapped field's parent is a complex reference field then only use the leaf.
      if (
        this.mappedField.field.parentField &&
        this.mappedField.field.parentField ===
          this.mapping.referenceFields[0]?.field
      ) {
        textStr += '/' + this.mappedField.field.name + '}';
      } else {
        textStr +=
          this.mappedField.field.docDef.id +
          ':' +
          this.mappedField.field.path +
          '}';
      }
      return textStr;
    }
  }
 
  toHTML(): string {
    Eif (this.mappedField && this.mappedField.field) {
      let titleAddendum = '';
      let mappedFieldName = this.mappedField.field.name;
      Iif (this.mappedField.field.enumeration) {
        const enumIdxVal = this.mappedField.field.enumIndexValue
          ? this.mappedField.field.enumIndexValue
          : 0;
        mappedFieldName +=
          '.' + this.mappedField.field.enumValues[enumIdxVal].name;
        titleAddendum = ':  Click to select an enumeration value.';
      }
      return `<span style="font-weight:bold" contenteditable="false" id="${this.uuid}" title="${this.mappedField.field.docDef.name}:${this.mappedField.field.path}${titleAddendum}"
        class="expressionFieldLabel label label-default">${mappedFieldName}</span>`;
    } else {
      // TODO: check this non null operator
      return `<span contenteditable="false" id="${this.uuid}"
        title="Field index '${
          this.mapping.getIndexForMappedField(this.mappedField!)! - 1
        }' is not available"
        class="expressionFieldLabel label label-danger">N/A</span>`;
    }
  }
 
  hasComplexField(): boolean {
    return (
      this.mappedField?.field?.documentField.fieldType === 'COMPLEX' &&
      (this.mappedField?.field?.documentField.status === 'SUPPORTED' ||
        this.mappedField?.field?.documentField.status === 'CACHED')
    );
  }
}
 
export class ExpressionModel {
  expressionHTML = '';
  expressionUpdatedSource = new Subject<ExpressionUpdatedEvent>();
  expressionUpdated$ = this.expressionUpdatedSource.asObservable();
 
  private _hasComplexField: boolean;
  private _nodes: ExpressionNode[] = [];
  private textCache = '';
 
  constructor(private mapping: MappingModel, private cfg: ConfigModel) {}
 
  generateInitialExpression() {
    this.mapping
      .getUserMappedFields(true)
      .forEach((f) => this.appendFieldNode(f));
  }
 
  get hasComplexField(): boolean {
    return this._hasComplexField;
  }
 
  set hasComplexField(value: boolean) {
    this._hasComplexField = value;
  }
 
  get nodes(): ReadonlyArray<ExpressionNode> {
    return this._nodes;
  }
 
  getLastNodeIndex() {
    return this._nodes.length - 1;
  }
 
  getLastNode() {
    return this._nodes[this.getLastNodeIndex()];
  }
 
  getNode(nodeId?: string): any {
    if (!nodeId) {
      return this.getLastNode();
    }
    return this._nodes.find((n) => n.getUuid() === nodeId);
  }
 
  setConfigModel(cfg: ConfigModel) {
    this.cfg = cfg;
  }
 
  /**
   * Clear all text from the specified TextNode offset range or from the '@' to
   * the end of the text node if no node ID is specified.
   *
   * Return the new UUID position indicator string or null.
   *
   * @param nodeId
   * @param startOffset
   * @param endOffset
   */
  clearText(
    nodeId?: string,
    startOffset?: number,
    endOffset?: number
  ): TextNode | null {
    let targetNode: TextNode | null = null;
    if (!nodeId) {
      const lastNode = this.getLastNode();
      if (!(lastNode instanceof TextNode)) {
        return null;
      }
      const keyPos = lastNode.str.indexOf('@');
      if (keyPos !== -1) {
        targetNode = lastNode;
        targetNode.str = targetNode.str.substring(0, keyPos);
      }
    } else {
      const node = this._nodes.find((n) => n.getUuid() === nodeId);
      if (!(node instanceof TextNode) || !endOffset) {
        return null;
      }
      targetNode = node;
      // TODO: check this non null operator
      const cleanStr = targetNode.str.replace(
        targetNode.str.substring(startOffset!, endOffset),
        ''
      );
      targetNode.str = cleanStr;
    }
    this.updateCache();
    this.expressionUpdatedSource.next();
    return targetNode;
  }
 
  /**
   * Insert text into expression at specified position. If nodeId is not specified,
   * it will be added to the end of expression. It parses the string
   * and insert a set of TextNode & FieldNode if it contains field reference like ${0},
   * otherwise just one TextNode.
   * This emits ExpressionUpdatedEvent which contains the latest node and offset it
   * worked on, so that the subscriber can determine where to put the caret in
   * the expression input widget. If ExpressionUpdatedEvent is undefined, it means that
   * it worked on the end of the expression.
   * @param str string to insert
   * @param nodeId target node to insert the string
   * @param offset position offset in the target node to insert the string
   */
  insertText(str: string, nodeId?: string, offset?: number) {
    this.insertNodes(this.createNodesFromText(str), nodeId, offset);
  }
 
  /**
   * Insert an array of ExpressionNodes at the specified position. If insertPosition is
   * not specified the nodes will be appended to the end of the expression.
   * This emits an ExpressionUpdatedEvent which contains the latest node and offset it
   * worked on, so that the subscriber can determine where to put the caret in
   * the expression input widget. If ExpressionUpdatedEvent is undefined, it means that
   * it worked on the end of the expression.
   *
   * @param newNodes an array of ExpressionNode to add
   * @param insertPosition target node to insert the string
   * @param offset position offset in the target node to insert the string
   */
  insertNodes(
    newNodes: ExpressionNode[],
    insertPosition?: string,
    offset?: number
  ) {
    // No position was specified - append to the end
    Eif (!insertPosition) {
      const last = this.getLastNode();
      Eif (!last) {
        this._nodes.push(...newNodes);
      } else if (last instanceof TextNode && newNodes[0] instanceof TextNode) {
        (last as TextNode).str += (newNodes[0] as TextNode).str;
        newNodes.splice(0, 1, last);
        this._nodes.splice(this.getLastNodeIndex(), 1, ...newNodes);
      } else if (
        last instanceof FieldNode &&
        newNodes[0] instanceof FieldNode
      ) {
        this._nodes.splice(
          this.getLastNodeIndex(),
          0,
          new TextNode(' + '),
          ...newNodes
        );
      } else {
        this._nodes.push(...newNodes);
      }
      this.updateCache();
      this.expressionUpdatedSource.next();
      return;
    }
 
    // Requires position handling
    const updatedEvent = new ExpressionUpdatedEvent();
    const targetNode = this._nodes.find((n) => n.getUuid() === insertPosition);
    // TODO: check this non null operator
    const targetNodeIndex = this._nodes.indexOf(targetNode!);
 
    if (targetNode instanceof TextNode) {
      if (offset === undefined || offset === null || offset < 0) {
        offset = targetNode.str.length;
      }
      const pre = targetNode.str.substring(0, offset);
      const post = targetNode.str.substring(offset);
      if (pre.length > 0) {
        if (newNodes[0] instanceof TextNode) {
          targetNode.str = pre + (newNodes[0] as TextNode).str;
          newNodes.splice(0, 1, targetNode);
        } else {
          targetNode.str = pre;
          newNodes.splice(0, 0, targetNode);
        }
      }
      if (post.length > 0) {
        const lastNewNodeIndex = newNodes.length - 1;
        if (newNodes[lastNewNodeIndex] instanceof TextNode) {
          let mergedTextNode: TextNode;
          if (pre.length > 0) {
            mergedTextNode = newNodes[lastNewNodeIndex] as TextNode;
            mergedTextNode.str += post;
          } else {
            mergedTextNode = targetNode;
            mergedTextNode.str =
              (newNodes[lastNewNodeIndex] as TextNode).str + post;
          }
          newNodes.splice(lastNewNodeIndex, 1, mergedTextNode);
        } else {
          if (pre.length > 0) {
            newNodes.push(new TextNode(post));
          } else {
            targetNode.str = post;
            newNodes.push(targetNode);
          }
        }
      }
      this._nodes.splice(targetNodeIndex, 1, ...newNodes);
      const lastAddedIndex = targetNodeIndex + newNodes.length - 1;
      if (
        this._nodes[lastAddedIndex] instanceof FieldNode &&
        this.nodes[lastAddedIndex + 1] instanceof FieldNode
      ) {
        // insert a glue in between FieldNodes so that it won't break syntax and caret can go into
        const space = new TextNode(' + ');
        this._nodes.splice(lastAddedIndex + 1, 0, space);
        updatedEvent.node = space;
        updatedEvent.offset = 1;
      } else if (this._nodes[lastAddedIndex] instanceof FieldNode) {
        updatedEvent.node = this._nodes[lastAddedIndex + 1];
        updatedEvent.offset = 0;
      } else {
        updatedEvent.node = this._nodes[lastAddedIndex];
        updatedEvent.offset =
          (this._nodes[lastAddedIndex] as TextNode).str.length - post.length;
      }
      this.updateCache();
      this.expressionUpdatedSource.next(updatedEvent);
      return;
    }
 
    // targetNode is a FieldNode - insert the text before it if offset is 0, otherwise after it
    if (offset !== 0 && newNodes[0] instanceof FieldNode) {
      // insert a glue in between FieldNodes so that it won't break syntax and caret can go into
      newNodes.splice(0, 0, new TextNode(' + '));
    }
    const nextNodeIndex = offset === 0 ? targetNodeIndex : targetNodeIndex + 1;
    const nextNode = this._nodes[nextNodeIndex];
    if (
      nextNode instanceof TextNode &&
      newNodes[newNodes.length - 1] instanceof TextNode
    ) {
      updatedEvent.offset = (
        newNodes[newNodes.length - 1] as TextNode
      ).str.length;
      nextNode.str =
        (newNodes[newNodes.length - 1] as TextNode).str +
        (nextNode as TextNode).str;
      newNodes.pop();
      this._nodes.splice(nextNodeIndex, 1, ...newNodes);
      updatedEvent.node = nextNode;
    } else if (
      nextNode instanceof FieldNode &&
      newNodes[newNodes.length - 1] instanceof FieldNode
    ) {
      // insert a glue in between FieldNodes so that it won't break syntax and caret can go into
      const space = new TextNode(' + ');
      this._nodes.splice(nextNodeIndex, 0, ...newNodes, space);
      updatedEvent.node = space;
      updatedEvent.offset = 1;
    } else {
      this._nodes.splice(nextNodeIndex, 0, ...newNodes);
      if (nextNode instanceof TextNode) {
        updatedEvent.node = nextNode;
        updatedEvent.offset = 0;
      } else {
        updatedEvent.node = newNodes[newNodes.length - 1];
        updatedEvent.offset = (
          newNodes[newNodes.length - 1] as TextNode
        ).str.length;
      }
    }
    this.updateCache();
    this.expressionUpdatedSource.next(updatedEvent);
  }
 
  removeToken(tokenPosition?: string, offset?: number, removeNext?: boolean) {
    // No position was specified - remove from the end
    Iif (!tokenPosition) {
      const last = this.getLastNode();
      if (!last) {
        return;
      }
      if (last instanceof FieldNode) {
        const removed = this._nodes.pop() as FieldNode;
        if (
          !this._nodes.find(
            (n) =>
              n instanceof FieldNode && n.mappedField === removed.mappedField
          )
        ) {
          // TODO: check this non null operator
          this.mapping.removeField(removed.mappedField!.field!);
          this.cfg.mappingService.updateMappedField(this.mapping);
        }
      } else if (last instanceof TextNode) {
        if (last.str.length > 0) {
          last.str = last.str.substring(0, last.str.length - 1);
        }
        if (last.str.length === 0) {
          this._nodes.pop();
        }
      }
      this.updateCache();
      this.expressionUpdatedSource.next();
      return;
    }
 
    // Requires position handling
    let updatedEvent: ExpressionUpdatedEvent | undefined =
      new ExpressionUpdatedEvent();
    let targetNode = this._nodes.find((n) => n.getUuid() === tokenPosition);
    // TODO: check this non null operator
    let targetNodeIndex = this._nodes.indexOf(targetNode!);
    Iif (removeNext) {
      targetNodeIndex++;
      targetNode = this._nodes[targetNodeIndex];
    }
    Iif (!targetNode || offset === -1) {
      if (targetNodeIndex < 1) {
        return;
      }
      targetNode = this._nodes[--targetNodeIndex];
      offset =
        targetNode instanceof TextNode
          ? (targetNode as TextNode).str.length
          : 1;
    }
    Eif (targetNode instanceof FieldNode) {
      const removed = this._nodes.splice(targetNodeIndex, 1);
      const targetFieldNode: FieldNode = removed[0] as FieldNode;
      Eif (
        !this._nodes.find(
          (n) =>
            n instanceof FieldNode &&
            n.mappedField === targetFieldNode.mappedField
        )
      ) {
        // TODO: check this non null operator
        this.mapping.removeField(targetFieldNode.mappedField!.field!);
        this.cfg.mappingService.updateMappedField(this.mapping);
      }
      Eif (this._nodes.length > targetNodeIndex) {
        Eif (
          this._nodes[targetNodeIndex - 1] instanceof TextNode &&
          this._nodes[targetNodeIndex] instanceof TextNode
        ) {
          const newOffset = (this._nodes[targetNodeIndex - 1] as TextNode).str
            .length;
          (this._nodes[targetNodeIndex - 1] as TextNode).str += (
            this._nodes[targetNodeIndex] as TextNode
          ).str;
          this._nodes.splice(targetNodeIndex, 1);
          updatedEvent.node = this._nodes[targetNodeIndex - 1];
          updatedEvent.offset = newOffset;
        } else if (
          this._nodes[targetNodeIndex - 1] instanceof FieldNode &&
          this._nodes[targetNodeIndex] instanceof FieldNode
        ) {
          const glue = new TextNode(' + ');
          this._nodes.splice(targetNodeIndex, 0, glue);
          updatedEvent.node = glue;
          updatedEvent.offset = 3;
        } else if (this._nodes[targetNodeIndex - 1] instanceof TextNode) {
          updatedEvent.node = this._nodes[targetNodeIndex - 1];
          updatedEvent.offset = (
            this._nodes[targetNodeIndex - 1] as TextNode
          ).str.length;
        } else if (this._nodes[targetNodeIndex] instanceof TextNode) {
          updatedEvent.node = this._nodes[targetNodeIndex];
          updatedEvent.offset = 0;
        }
      } else {
        // end of line
        updatedEvent = undefined;
      }
    } else {
      const targetString = (targetNode as TextNode).str;
 
      // Remove empty string text node.
      if (targetString.length === 1) {
        this._nodes.splice(targetNodeIndex, 1);
      } else {
        (targetNode as TextNode).str =
          offset === 0
            ? targetString.substr(1)
            : targetString.substring(0, offset) +
              targetString.substring(offset! + 1);
        updatedEvent.node = targetNode;
        updatedEvent.offset = offset;
      }
    }
    this.updateCache();
    this.expressionUpdatedSource.next(updatedEvent);
  }
 
  /**
   * Replace the content of the last text node with a substring terminating at the
   * specified index.
   *
   * @param index
   */
  clearToEnd(index: number): void {
    const last = this.getLastNode();
    if (!(last instanceof TextNode)) {
      return;
    }
    last.str = last.str.substring(0, index);
  }
 
  /**
   * Reflect mapped source fields to the field references in the expression.
   * Selected source fields are inserted into or appended to the expression,
   * and unselected source fields are removed from expression.
   *
   * @param mapping Corresponding MappingModel object
   * @param insertPosition
   * @param offset
   */
  updateFieldReference(
    mapping: MappingModel,
    insertPosition?: string,
    offset?: number
  ) {
    const mappedFields = mapping.getUserMappedFields(true);
    const referenceFields = mapping.getReferenceMappedFields();
    let fieldNodes = this._nodes.filter(
      (n) => n instanceof FieldNode
    ) as FieldNode[];
 
    // Remove non-reference fields from the expression if unmapped.
    for (const node of fieldNodes) {
      // TODO: check this non null operator
      if (
        mappedFields.includes(node.mappedField!) ||
        referenceFields.includes(node.mappedField!) ||
        node.hasComplexField()
      ) {
        continue;
      }
      const index = this._nodes.indexOf(node);
      this._nodes.splice(index, 1);
      if (
        this._nodes.length > index &&
        this._nodes[index - 1] instanceof TextNode &&
        this._nodes[index] instanceof TextNode
      ) {
        (this._nodes[index - 1] as TextNode).str += (
          this._nodes[index] as TextNode
        ).str;
        this._nodes.splice(index, 1);
      }
    }
 
    // Add any non-reference mapped fields into the expression - append if no insert
    // position is specified.
    fieldNodes = this._nodes.filter(
      (n) => n instanceof FieldNode
    ) as FieldNode[];
    for (const mfield of mappedFields) {
      if (
        !fieldNodes.find((n) => n.mappedField === mfield) &&
        !referenceFields.find((r) => r.field === mfield.field)
      ) {
        if (insertPosition) {
          this.insertNodes(
            [new FieldNode(this.mapping, mfield)],
            insertPosition,
            offset
          );
        } else {
          this.appendFieldNode(mfield);
        }
      }
    }
    this.updateCache();
    this.expressionUpdatedSource.next();
  }
 
  clear() {
    this._nodes = [];
    this.updateCache();
    this.expressionUpdatedSource.next();
  }
 
  toText() {
    Iif (this.textCache.length === 0) {
      this.updateCache();
    }
    return this.textCache;
  }
 
  toHTML() {
    if (this.expressionHTML.length === 0) {
      this.updateCache();
    }
    return this.expressionHTML;
  }
 
  addConditionalExpressionNode(
    mappedField: MappedField,
    nodeId: string,
    offset: number
  ): void {
    this.insertNodes(
      [new FieldNode(this.mapping, mappedField)],
      nodeId,
      offset
    );
  }
 
  private updateCache() {
    let answer = '';
    this._nodes.forEach((node) => (answer += node.toText()));
    this.textCache = answer;
    answer = '';
    this._nodes.forEach((node) => (answer += node.toHTML()));
    this.expressionHTML = answer; // trigger expr box render
  }
 
  /**
   * Translate an expression string into an array of text nodes and field nodes.  Handle
   * relative paths for collection-based field references.
   *
   * @param text
   */
  private createNodesFromText(text: string): ExpressionNode[] {
    const answer = [];
    let position = -1;
    let collectionContextFieldNode = null;
 
    while (text.search(/\$\{[a-zA-Z0-9.:/<>[\]_-]+\}/) !== -1) {
      position = text.search(/\$/);
      Eif (position !== 0) {
        answer.push(new TextNode(text.substring(0, position)));
      }
      const nodeMetaVal = text.substring(position + 2, text.indexOf('}'));
      let fn = null;
      Eif (isNaN(Number(nodeMetaVal))) {
        fn = collectionContextFieldNode
          ? new FieldNode(
              this.mapping,
              undefined,
              nodeMetaVal,
              undefined,
              collectionContextFieldNode.mappedField?.field?.path!
            )
          : new FieldNode(this.mapping, undefined, nodeMetaVal);
      } else {
        const index = parseInt(nodeMetaVal, 10);
        fn = new FieldNode(this.mapping, undefined, undefined, index);
      }
 
      Iif (!fn || !fn.mappedField) {
        this.cfg.errorService.addError(
          new ErrorInfo({
            message: `Unable to map expression element '${nodeMetaVal}' to a field node.`,
            level: ErrorLevel.ERROR,
            scope: ErrorScope.MAPPING,
            type: ErrorType.INTERNAL,
            mapping: this.mapping,
          })
        );
      } else {
        if (fn.mappedField?.field?.isCollection) {
          collectionContextFieldNode = fn;
        }
        answer.push(fn);
      }
      text = text.substring(text.indexOf('}') + 1);
    }
    Eif (text.length > 0) {
      answer.push(new TextNode(text));
    }
    return answer;
  }
 
  private appendFieldNode(mfield: MappedField) {
    const lastNode = this._nodes.pop();
    if (lastNode instanceof FieldNode) {
      this._nodes.push(lastNode, new TextNode(' + '));
    } else if (lastNode instanceof TextNode) {
      if (lastNode.str.length === 0) {
        this._nodes.push(new TextNode(' + '));
      } else {
        this._nodes.push(lastNode);
      }
    }
    this._nodes.push(new FieldNode(this.mapping, mfield));
  }
}