all files / editor-manager/plugin/ selection-commands.js

94.09% Statements 589/626
91.67% Branches 649/708
95.83% Functions 23/24
94.09% Lines 589/626
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 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999     438× 438×   438× 437×   437× 437× 437× 437× 437× 437× 437×                   437×             437× 437× 437× 437× 224×   437×     435×     437× 437× 437× 437× 437× 437× 153× 153×     153× 75× 75×   78×         10× 10× 10×     68× 68× 68× 68×     63×   68×       437× 437× 437× 725× 725× 529× 12× 12×   517× 18× 18×     725× 278×   725× 198×     527×   725× 713×     437×     437× 430×   437×       68× 68× 68×       68× 68× 38× 38× 27× 27× 27× 15× 30× 15×       27× 27×           27×           27× 27×               38× 24×     24×   21× 20×       24×     14×       30×       30× 14×   30×   12× 12× 12×               12× 12×   18×   56×   19× 19× 41×     19×   198× 198×     198× 198× 198×     165× 165× 165× 165× 231× 231×   165× 150× 10×   150× 144×         165× 143×   156× 13× 13×     143× 143×     143× 128× 15×   128× 128×                                   22× 22× 23×   22× 22×     33×             196× 196× 17×   196× 196× 28× 28×     196× 196× 196× 196×   195×                       191× 191× 191× 188×   191×   49×   44×     142×   24×     196× 131× 131× 131× 478× 478× 115×       363×     478×                             475×       196× 64× 75×   68× 68× 68× 68×   68×   19×       68×     68× 18× 18×               64× 64×   64×   64×           64× 45× 45× 45× 253× 253×   45× 45× 45×   18× 18×   45× 45× 45× 45× 17× 22×       45×   13× 10×         32×                             196×   527× 499× 474× 430× 430×   474×     466× 464× 464× 238× 238× 238× 1254× 1254×   238× 126× 126× 126× 194× 59× 59× 35×   24× 17×   59× 52×   19× 19×     24× 24×           194× 194× 16×                     238×   57× 14×   43×   34×     238× 94×   144× 144× 144× 144× 144×   144× 135× 181×   181× 181×   46×     135×       144× 144× 36× 36×       144×     144× 144× 144×   28×               226× 226× 226× 226×   226×   36×   29×     190×   25×           25× 24×         28× 25× 25× 25×         433×   384× 384×     384×   384×   146× 146× 146× 146× 146× 146× 143× 143× 136× 136× 129× 129× 128×                           43× 43× 43×           43×   43×   384×   384×   713× 216× 216× 216× 216×   497× 85× 85× 85× 85×   412× 65× 65×   347× 65× 65×   713×   589× 589×   103×   63×   52× 52× 52× 52×   17× 17× 17× 17×   10×   12×   56× 56× 56× 56× 56×   29× 29× 29× 29×   101× 101× 101× 101×     143× 143× 143× 143×     398× 64×     94×   94× 94× 42×     94× 29×   94× 94× 34× 34× 34× 34× 22×     12× 12×     34× 34× 34× 34× 34×   94×   29× 29×     15×         25×       25× 58× 58× 77× 77× 33×     58× 55×   58×       194× 194× 194× 87× 149× 149× 112×       194×   83× 83× 83× 83× 83×         10×   10×   10×   10× 12× 12× 11×     10×   10×   25× 60× 60× 24×     25× 12×   25×                  
define(["require", "exports", "./../../selection/index", "./nodecutter", "./insert-methods", "./isformatted", "../../common/util", "@syncfusion/ej2-base", "./dom-node"], function (require, exports, index_1, nodecutter_1, insert_methods_1, isformatted_1, util_1, ej2_base_1, dom_node_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var SelectionCommands = (function () {
        function SelectionCommands() {
        }
        SelectionCommands.applyFormat = function (docElement, format, endNode, enterAction, tableCellSelection, value, selector, painterValues) {
            this.enterAction = enterAction;
            var validFormats = ['bold', 'italic', 'underline', 'strikethrough', 'superscript',
                'subscript', 'uppercase', 'lowercase', 'fontcolor', 'fontname', 'fontsize', 'backgroundcolor', 'inlinecode'];
            if (validFormats.indexOf(format) > -1 || value === 'formatPainter') {
                if (format === 'backgroundcolor' && value === '') {
                    value = 'transparent';
                }
                var domSelection = new index_1.NodeSelection(endNode);
                var domNode = new dom_node_1.DOMNode(endNode, docElement);
                var nodeCutter = new nodecutter_1.NodeCutter();
                var isFormatted = new isformatted_1.IsFormatted();
                var range = domSelection.getRange(docElement);
                var currentAnchorNode = range.startContainer.parentElement;
                if (range.collapsed && !ej2_base_1.isNullOrUndefined(currentAnchorNode) &&
                    currentAnchorNode.tagName === 'A' &&
                    (range.startOffset === currentAnchorNode.textContent.length || range.startOffset === 0)) {
                    var emptyTextNode = document.createTextNode('');
                    if (range.startOffset === 0) {
                        currentAnchorNode.parentNode.insertBefore(emptyTextNode, currentAnchorNode);
                    }
                    else {
                        if (!ej2_base_1.isNullOrUndefined(currentAnchorNode.nextSibling)) {
                            currentAnchorNode.parentElement.insertBefore(emptyTextNode, currentAnchorNode.nextSibling);
                        }
                        else {
                            currentAnchorNode.parentNode.appendChild(emptyTextNode);
                        }
                    }
                    var newRange = docElement.createRange();
                    range.setStart(emptyTextNode, 0);
                    range.setEnd(emptyTextNode, 0);
                    range.collapse(true);
                    domSelection.setRange(docElement, newRange);
                }
                if (ej2_base_1.Browser.userAgent.indexOf('Firefox') !== -1 && range.startContainer === range.endContainer && !ej2_base_1.isNullOrUndefined(endNode) && range.startContainer === endNode) {
                    var startChildNodes = range.startContainer.childNodes;
                    var startNode = ((startChildNodes[(range.startOffset > 0) ? (range.startOffset - 1) :
                        range.startOffset]) || range.startContainer);
                    var endNode_1 = (range.endContainer.childNodes[(range.endOffset > 0) ? (range.endOffset - 1) :
                        range.endOffset] || range.endContainer);
                    var lastSelectionNode = (endNode_1.lastChild.nodeName === 'BR' ? (ej2_base_1.isNullOrUndefined(endNode_1.lastChild.previousSibling) ? endNode_1
                        : endNode_1.lastChild.previousSibling) : endNode_1.firstChild);
                    while (!ej2_base_1.isNullOrUndefined(lastSelectionNode) && lastSelectionNode.nodeName !== '#text' && lastSelectionNode.nodeName !== 'IMG' &&
                        lastSelectionNode.nodeName !== 'BR' && lastSelectionNode.nodeName !== 'HR') {
                        lastSelectionNode = lastSelectionNode.lastChild;
                    }
                    domSelection.setSelectionText(docElement, startNode, lastSelectionNode, 0, 0);
                    range = domSelection.getRange(docElement);
                }
                var save = domSelection.save(range, docElement);
                var nodes = void 0;
                var isTableSelect = false;
                if (endNode && tableCellSelection && endNode.nodeName !== '#text') {
                    nodes = tableCellSelection.getTextNodes();
                }
                if (nodes && nodes.length > 0) {
                    isTableSelect = true;
                }
                else {
                    nodes = range.collapsed ? domSelection.getSelectionNodeCollection(range) :
                        domSelection.getSelectionNodeCollectionBr(range);
                }
                var isCollapsed = false;
                var isFormat = false;
                var isCursor = false;
                var preventRestore = false;
                var isFontStyle = (['fontcolor', 'fontname', 'fontsize', 'backgroundcolor'].indexOf(format) > -1);
                if (!isTableSelect && range.collapsed) {
                    var currentFormatNode = isFormatted.getFormattedNode(range.startContainer, format, endNode);
                    var currentSelector = !ej2_base_1.isNullOrUndefined(currentFormatNode) ?
                        (currentFormatNode.getAttribute('style') === null ? currentFormatNode.nodeName :
                            currentFormatNode.nodeName + '[style=\'' + currentFormatNode.getAttribute('style') + '\']') : null;
                    if (nodes.length > 0) {
                        isCollapsed = true;
                        range = nodeCutter.GetCursorRange(docElement, range, nodes[0]);
                    }
                    else if (range.startContainer.nodeType === 3 && ((range.startContainer.parentElement.childElementCount > 0 &&
                        range.startOffset > 0 && range.startContainer.parentElement.firstElementChild.tagName.toLowerCase() !== 'br') ||
                        !ej2_base_1.isNullOrUndefined(currentFormatNode) && currentFormatNode ===
                            (range.startContainer.parentElement.closest(currentSelector)) &&
                            ((range.startContainer.parentElement.closest(currentSelector)).textContent.replace(new RegExp('\u200B', 'g'), '').trim().length !== 0))) {
                        isCollapsed = true;
                        range = nodeCutter.GetCursorRange(docElement, range, range.startContainer);
                        nodes.push(range.startContainer);
                    }
                    else {
                        var cursorNode = this.insertCursorNode(docElement, domSelection, range, isFormatted, nodeCutter, format, value, endNode);
                        domSelection.endContainer = domSelection.startContainer = domSelection.getNodeArray(cursorNode, true);
                        var childNodes = cursorNode.nodeName === 'BR' && cursorNode.parentNode.childNodes;
                        if (!ej2_base_1.isNullOrUndefined(childNodes) && childNodes.length === 1 && childNodes[0].nodeName === 'BR' && nodes.length === 0) {
                            domSelection.setSelectionText(docElement, range.startContainer, range.endContainer, 0, 0);
                            preventRestore = true;
                        }
                        else {
                            domSelection.endOffset = domSelection.startOffset = 1;
                        }
                        if (cursorNode.nodeName === 'BR' && cursorNode.parentNode.textContent.length === 0) {
                            preventRestore = true;
                        }
                    }
                }
                isCursor = isTableSelect ? false : range.collapsed;
                var isSubSup = false;
                for (var index = 0; index < nodes.length; index++) {
                    var formatNode = isFormatted.getFormattedNode(nodes[index], format, endNode);
                    if (formatNode === null) {
                        if (format === 'subscript') {
                            formatNode = isFormatted.getFormattedNode(nodes[index], 'superscript', endNode);
                            isSubSup = formatNode === null ? false : true;
                        }
                        else if (format === 'superscript') {
                            formatNode = isFormatted.getFormattedNode(nodes[index], 'subscript', endNode);
                            isSubSup = formatNode === null ? false : true;
                        }
                    }
                    if (index === 0 && formatNode === null) {
                        isFormat = true;
                    }
                    if (formatNode !== null && (!isFormat || isFontStyle)) {
                        nodes[index] = this.removeFormat(nodes, index, formatNode, isCursor, isTableSelect, isFormat, isFontStyle, range, nodeCutter, format, value, domSelection, endNode, domNode);
                    }
                    else {
                        nodes[index] = this.insertFormat(docElement, nodes, index, formatNode, isCursor, isTableSelect, isFormat, isFontStyle, range, nodeCutter, format, value, painterValues, domNode, endNode);
                    }
                    if (!isTableSelect) {
                        domSelection = this.applySelection(nodes, domSelection, nodeCutter, index, isCollapsed);
                    }
                }
                Iif (util_1.isIDevice()) {
                    util_1.setEditFrameFocus(endNode, selector);
                }
                if (!preventRestore && !isTableSelect) {
                    save.restore();
                }
                if (isSubSup) {
                    this.applyFormat(docElement, format, endNode, enterAction);
                }
            }
        };
        SelectionCommands.insertCursorNode = function (docElement, domSelection, range, isFormatted, nodeCutter, format, value, endNode) {
            var cursorNodes = domSelection.getNodeCollection(range);
            var domNode = new dom_node_1.DOMNode(endNode, docElement);
            var cursorFormat = (cursorNodes.length > 0) ?
                (cursorNodes.length > 1 && range.startContainer === range.endContainer) ?
                    this.getCursorFormat(isFormatted, cursorNodes, format, endNode) :
                    ((value === '' && format === 'fontsize' && isFormatted.getFormattedNode(cursorNodes[0], format, endNode) == null && cursorNodes[0].parentElement.nodeName === 'SPAN') ? cursorNodes[0].parentElement : isFormatted.getFormattedNode(cursorNodes[0], format, endNode)) : null;
            var cursorNode = null;
            if (cursorFormat) {
                cursorNode = cursorNodes[0];
                if (cursorFormat.firstChild.textContent.charCodeAt(0) === 8203 && cursorFormat.firstChild.nodeType === 3) {
                    var regEx = new RegExp('\u200B', 'g');
                    var emptySpaceNode = void 0;
                    if (cursorNode.nodeName !== '#text') {
                        for (var i = 0; i < cursorNodes.length; i++) {
                            if (cursorNodes[i].nodeType === Node.TEXT_NODE) {
                                cursorNode = cursorNodes[i];
                            }
                        }
                    }
                    Eif (cursorFormat.firstChild === cursorNode) {
                        cursorNode.textContent = (cursorFormat.parentElement && (domNode.isBlockNode(cursorFormat.parentElement) &&
                            cursorFormat.parentElement.textContent.length <= 1 ? cursorFormat.parentElement.childElementCount > 1 :
                            cursorFormat.childElementCount === 0) &&
                            (cursorFormat.parentElement.textContent.length > 1 ||
                                cursorFormat.parentElement.firstChild && cursorFormat.parentElement.firstChild.nodeType === 1) ?
                            cursorNode.textContent : cursorNode.textContent.replace(regEx, ''));
                        emptySpaceNode = cursorNode;
                    }
                    else {
                        cursorFormat.firstChild.textContent = cursorFormat.firstChild.textContent.replace(regEx, '');
                        emptySpaceNode = cursorFormat.firstChild;
                    }
                    var pointer = void 0;
                    if (emptySpaceNode.textContent.length === 0) {
                        Iif (!ej2_base_1.isNullOrUndefined(emptySpaceNode.previousSibling)) {
                            cursorNode = emptySpaceNode.previousSibling;
                            pointer = emptySpaceNode.textContent.length - 1;
                            domSelection.setCursorPoint(docElement, emptySpaceNode, pointer);
                        }
                        else Eif (!ej2_base_1.isNullOrUndefined(emptySpaceNode.parentElement) && emptySpaceNode.parentElement.textContent.length === 0) {
                            var brElem = document.createElement('BR');
                            emptySpaceNode.parentElement.appendChild(brElem);
                            ej2_base_1.detach(emptySpaceNode);
                            cursorNode = brElem;
                            domSelection.setCursorPoint(docElement, cursorNode.parentElement, 0);
                        }
                    }
                }
                if ((['fontcolor', 'fontname', 'fontsize', 'backgroundcolor'].indexOf(format) > -1)) {
                    Iif (format === 'fontcolor') {
                        cursorFormat.style.color = value;
                    }
                    else if (format === 'fontname') {
                        cursorFormat.style.fontFamily = value;
                    }
                    else if (format === 'fontsize') {
                        cursorFormat.style.fontSize = value;
                    }
                    else {
                        cursorFormat.style.backgroundColor = value;
                    }
                    cursorNode = cursorFormat;
                }
                else {
                    insert_methods_1.InsertMethods.unwrap(cursorFormat);
                }
            }
            else {
                Iif (cursorNodes.length > 1 && range.startOffset > 0 && (cursorNodes[0].firstElementChild &&
                    cursorNodes[0].firstElementChild.tagName.toLowerCase() === 'br')) {
                    cursorNodes[0].innerHTML = '';
                }
                if (cursorNodes.length === 1 && range.startOffset === 0 && (cursorNodes[0].nodeName === 'BR' || (ej2_base_1.isNullOrUndefined(cursorNodes[0].nextSibling) ? false : cursorNodes[0].nextSibling.nodeName === 'BR'))) {
                    ej2_base_1.detach(cursorNodes[0].nodeName === '#text' ? cursorNodes[0].nextSibling : cursorNodes[0]);
                }
                if (!ej2_base_1.isNullOrUndefined(cursorNodes[0] && cursorNodes[0].parentElement) && isformatted_1.IsFormatted.inlineTags.
                    indexOf((cursorNodes[0].parentElement).tagName.toLowerCase()) !== -1 && cursorNodes[0].textContent.includes('\u200B')) {
                    var element = this.GetFormatNode(format, value);
                    var tempNode = cursorNodes[0];
                    if (format === 'fontsize') {
                        var currentFormatNode = cursorNodes[0];
                        while (currentFormatNode) {
                            var isSameTextContent = currentFormatNode.parentElement.textContent.trim()
                                === cursorNodes[0].textContent.trim();
                            var previousElement = currentFormatNode.parentElement;
                            if (!domNode.isBlockNode(previousElement) && isSameTextContent &&
                                !(previousElement.nodeName === 'SPAN' && previousElement.classList.contains('e-img-inner'))) {
                                currentFormatNode = previousElement;
                            }
                            else {
                                break;
                            }
                            cursorNodes[0] = currentFormatNode;
                        }
                    }
                    this.applyStyles(cursorNodes, 0, element);
                    return tempNode;
                }
                cursorNode = this.getInsertNode(docElement, range, format, value).firstChild;
            }
            return cursorNode;
        };
        SelectionCommands.getCursorFormat = function (isFormatted, cursorNodes, format, endNode) {
            var currentNode;
            for (var index = 0; index < cursorNodes.length; index++) {
                currentNode = cursorNodes[index].lastElementChild ?
                    cursorNodes[index].lastElementChild : cursorNodes[index];
            }
            return (format === 'fontsize' && isFormatted.getFormattedNode(currentNode, format, endNode) == null && currentNode.parentElement.nodeName === 'SPAN') ? currentNode.parentElement : isFormatted.getFormattedNode(currentNode, format, endNode);
        };
        SelectionCommands.removeFormat = function (nodes, index, formatNode, isCursor, isTableCell, isFormat, isFontStyle, range, nodeCutter, format, value, domSelection, endNode, domNode) {
            var splitNode = null;
            var startText = range.startContainer.nodeName === '#text' ?
                range.startContainer.textContent.substring(range.startOffset, range.startContainer.textContent.length) :
                range.startContainer.textContent;
            var nodeText = nodes[index].textContent;
            var isParentNodeSameAsParentElement = nodes[0].parentElement.nodeName === nodes[0].parentElement.parentElement.nodeName;
            if (!(range.startContainer === range.endContainer && range.startOffset === 0
                && range.endOffset === range.startContainer.length
                && (range.startContainer.textContent === formatNode.textContent || isParentNodeSameAsParentElement))) {
                var nodeIndex = [];
                var cloneNode = nodes[index];
                var clonedElement = cloneNode;
                do {
                    nodeIndex.push(domSelection.getIndex(cloneNode));
                    cloneNode = cloneNode.parentNode;
                } while (cloneNode && (cloneNode !== formatNode));
                if (nodes[index].nodeName !== 'BR') {
                    if (clonedElement.nodeName === '#text' && clonedElement.textContent.includes('\u200B')) {
                        clonedElement.remove();
                    }
                    if (!isTableCell) {
                        cloneNode = splitNode = (isCursor && (formatNode.textContent.length - 1) === range.startOffset) ?
                            nodeCutter.SplitNode(range, formatNode, true)
                            : nodeCutter.GetSpliceNode(range, formatNode);
                    }
                }
                if (!isCursor) {
                    while (cloneNode && cloneNode.childNodes.length > 0 && ((nodeIndex.length - 1) >= 0)
                        && (cloneNode.childNodes.length > nodeIndex[nodeIndex.length - 1])) {
                        if (cloneNode.childNodes.length > 1 && nodeIndex.length > 1) {
                            cloneNode = cloneNode.childNodes[nodeIndex[nodeIndex.length - 2]];
                            break;
                        }
                        else {
                            cloneNode = cloneNode.childNodes[nodeIndex[nodeIndex.length - 1]];
                            nodeIndex.pop();
                        }
                    }
                    if (nodes[index].nodeName !== 'BR') {
                        while (cloneNode.nodeType === 1 && cloneNode.childNodes.length > 0) {
                            cloneNode = cloneNode.childNodes[0];
                        }
                        Eif (cloneNode.nodeType === 3 && !(isCursor && cloneNode.nodeValue === '')) {
                            nodes[index] = cloneNode;
                        }
                        else {
                            var divNode = document.createElement('div');
                            divNode.innerHTML = '&#8203;';
                            if (cloneNode.nodeType !== 3) {
                                cloneNode.insertBefore(divNode.firstChild, cloneNode.firstChild);
                                nodes[index] = cloneNode.firstChild;
                            }
                            else {
                                cloneNode.parentNode.insertBefore(divNode.firstChild, cloneNode);
                                nodes[index] = cloneNode.previousSibling;
                                cloneNode.parentNode.removeChild(cloneNode);
                            }
                        }
                    }
                }
                else {
                    var lastNode = splitNode;
                    for (; lastNode.firstChild !== null && lastNode.firstChild.nodeType !== 3; null) {
                        lastNode = lastNode.firstChild;
                    }
                    lastNode.innerHTML = '&#8203;';
                    nodes[index] = lastNode.firstChild;
                }
            }
            else if (isFontStyle && !nodes[index].contains(formatNode) && nodes[index].nodeType === 3 &&
                nodes[index].textContent !== formatNode.textContent) {
                var isFullNodeSelected = nodes[index].textContent === nodes[index].wholeText;
                var nodeTraverse = nodes[index];
                var styleElement = this.GetFormatNode(format, value);
                while (nodeTraverse && nodeTraverse.textContent === nodeTraverse.parentElement.textContent) {
                    nodeTraverse = nodeTraverse.parentElement;
                }
                Eif (isFullNodeSelected && formatNode.textContent !== nodeTraverse.textContent) {
                    var nodeArray = [];
                    var priorityNode = this.getPriorityFormatNode(nodeTraverse, endNode);
                    Iif (priorityNode && priorityNode.textContent === nodeTraverse.textContent) {
                        nodeTraverse = priorityNode;
                    }
                    nodeArray.push(nodeTraverse);
                    this.applyStyles(nodeArray, 0, styleElement);
                    return nodes[index];
                }
            }
            var fontStyle;
            if (format === 'backgroundcolor') {
                fontStyle = formatNode.style.fontSize;
            }
            var bgStyle;
            if (format === 'fontsize') {
                var bg = ej2_base_1.closest(nodes[index].parentElement, 'span[style*=' + 'background-color' + ']');
                if (!ej2_base_1.isNullOrUndefined(bg)) {
                    bgStyle = bg.style.backgroundColor;
                }
            }
            var formatNodeStyles = formatNode.getAttribute('style');
            var formatNodeTagName = formatNode.tagName;
            var child;
            if (formatNodeTagName === 'A' && format === 'underline') {
                formatNode.style.textDecoration = 'none';
                child = [formatNode];
            }
            else if (isformatted_1.IsFormatted.inlineTags.indexOf(formatNodeTagName.toLowerCase()) !== -1 && isFontStyle && formatNodeTagName.toLocaleLowerCase() !== 'span') {
                var fontNodeStyle = formatNode.style;
                if (fontNodeStyle.color && format === 'fontcolor') {
                    if (formatNode.nodeName === 'A') {
                        fontNodeStyle.color = value;
                    }
                    else {
                        fontNodeStyle.color = '';
                    }
                }
                else if (fontNodeStyle.backgroundColor && format === 'backgroundcolor') {
                    fontNodeStyle.backgroundColor = '';
                }
                else Iif (fontNodeStyle.fontSize && format === 'fontsize') {
                    fontNodeStyle.fontSize = '';
                }
                else Eif (fontNodeStyle.fontFamily && format === 'fontname') {
                    fontNodeStyle.fontFamily = '';
                }
                if (formatNode.getAttribute('style') === '') {
                    formatNode.removeAttribute('style');
                }
                child = [formatNode];
            }
            else {
                child = insert_methods_1.InsertMethods.unwrap(formatNode);
                var liElement = nodes[index].parentElement;
                if (!ej2_base_1.isNullOrUndefined(liElement) && liElement.tagName.toLowerCase() !== 'li') {
                    liElement = ej2_base_1.closest(liElement, 'li');
                }
                if (!ej2_base_1.isNullOrUndefined(liElement) && liElement.tagName.toLowerCase() === 'li' &&
                    liElement.textContent.trim() === nodes[index].textContent.trim()) {
                    if (format === 'bold') {
                        liElement.style.fontWeight = 'normal';
                    }
                    else if (format === 'italic') {
                        liElement.style.fontStyle = 'normal';
                    }
                }
                else if (!ej2_base_1.isNullOrUndefined(liElement) && liElement.tagName.toLowerCase() === 'li'
                    && liElement.textContent.trim() !== nodes[index].textContent.trim()) {
                    SelectionCommands.conCatenateTextNode(liElement, format, '', 'normal', value);
                }
            }
            if (child[0] && !isFontStyle) {
                var nodeTraverse = child[index] ? child[index] : child[0];
                var textNode = nodeTraverse;
                for (; nodeTraverse && nodeTraverse.parentElement && nodeTraverse.parentElement !== endNode; nodeTraverse = nodeTraverse) {
                    var nodeTraverseCondition = void 0;
                    if (formatNode.nodeName === 'SPAN') {
                        nodeTraverseCondition = nodeTraverse.parentElement.tagName.toLocaleLowerCase()
                            === formatNode.tagName.toLocaleLowerCase() && nodeTraverse.parentElement.getAttribute('style') === formatNodeStyles;
                    }
                    else {
                        nodeTraverseCondition = nodeTraverse.parentElement.tagName.toLocaleLowerCase()
                            === formatNode.tagName.toLocaleLowerCase();
                    }
                    if (nodeTraverse.parentElement && nodeTraverseCondition &&
                        (nodeTraverse.parentElement.childElementCount > 1 || range.startOffset > 1)) {
                        if (textNode.parentElement && textNode.parentElement.tagName.toLocaleLowerCase()
                            === formatNode.tagName.toLocaleLowerCase()) {
                            if ((range.startOffset === range.endOffset) && textNode.nodeType !== 1 &&
                                !ej2_base_1.isNullOrUndefined(textNode.textContent) && textNode.parentElement.childElementCount > 1) {
                                range.setStart(textNode, 0);
                                range.setEnd(textNode, textNode.textContent.length);
                                nodeCutter.SplitNode(range, textNode.parentElement, false);
                            }
                        }
                        Iif (nodeTraverse.parentElement.tagName.toLocaleLowerCase() === 'span') {
                            if (formatNode.style.textDecoration === 'underline' &&
                                nodeTraverse.parentElement.style.textDecoration !== 'underline') {
                                nodeTraverse = nodeTraverse.parentElement;
                                continue;
                            }
                        }
                        insert_methods_1.InsertMethods.unwrap(nodeTraverse.parentElement);
                        nodeTraverse = !ej2_base_1.isNullOrUndefined(nodeTraverse.parentElement) && !domNode.isBlockNode(nodeTraverse.parentElement) ? textNode :
                            nodeTraverse.parentElement;
                    }
                    else {
                        nodeTraverse = nodeTraverse.parentElement;
                    }
                }
            }
            if (child.length > 0 && isFontStyle && !((format === 'fontname' && value === '') || (format === 'fontsize' && value === ''))) {
                for (var num = 0; num < child.length; num++) {
                    if (child[num].nodeType !== 3 || (child[num].textContent &&
                        child[num].textContent.trim().length > 0)) {
                        child[num] = insert_methods_1.InsertMethods.Wrap(child[num], this.GetFormatNode(format, value, formatNodeTagName, formatNodeStyles));
                        var liElement = nodes[index].parentElement;
                        Eif (!ej2_base_1.isNullOrUndefined(liElement) && liElement.tagName.toLowerCase() !== 'li') {
                            liElement = ej2_base_1.closest(liElement, 'li');
                        }
                        if (!ej2_base_1.isNullOrUndefined(liElement) && liElement.tagName.toLowerCase() === 'li' &&
                            liElement.textContent.trim() === nodes[index].textContent.trim()) {
                            Iif (format === 'fontname') {
                                liElement.style.fontFamily = value;
                            }
                        }
                        if (!ej2_base_1.isNullOrUndefined(liElement) && liElement.tagName.toLowerCase() === 'li'
                            && liElement.textContent.trim() !== nodes[index].textContent.trim()) {
                            SelectionCommands.conCatenateTextNode(liElement, format, liElement.textContent, format, value);
                        }
                        if (child[num].textContent === startText) {
                            Eif (num === 0) {
                                range.setStartBefore(child[num]);
                            }
                            else if (num === child.length - 1) {
                                range.setEndAfter(child[num]);
                            }
                        }
                    }
                }
                var currentNodeElem = nodes[index].parentElement;
                if (!ej2_base_1.isNullOrUndefined(fontStyle) && fontStyle !== '') {
                    currentNodeElem.style.fontSize = fontStyle;
                }
                if (!ej2_base_1.isNullOrUndefined(bgStyle) && bgStyle !== '') {
                    currentNodeElem.style.backgroundColor = bgStyle;
                }
                Iif ((format === 'backgroundcolor' && !ej2_base_1.isNullOrUndefined(fontStyle) && fontStyle !== '') &&
                    currentNodeElem.parentElement.innerHTML === currentNodeElem.outerHTML) {
                    var curParentElem = currentNodeElem.parentElement;
                    curParentElem.parentElement.insertBefore(currentNodeElem, curParentElem);
                    ej2_base_1.detach(curParentElem);
                }
                if (format === 'fontsize' || format === 'fontcolor') {
                    var liElement = nodes[index].parentElement;
                    var parentElement = nodes[index].parentElement;
                    while (!ej2_base_1.isNullOrUndefined(parentElement) && parentElement.tagName.toLowerCase() !== 'li') {
                        parentElement = parentElement.parentElement;
                        liElement = parentElement;
                    }
                    var num = index;
                    var liChildContent = '';
                    while (num >= 0 && !ej2_base_1.isNullOrUndefined(liElement) && liElement.tagName.toLowerCase() === 'li' && liElement.contains(nodes[num]) &&
                        liElement.textContent.replace('/\u200B/g', '').trim().includes(nodes[num].textContent.trim())) {
                        liChildContent = ' ' + nodes[num].textContent.trim() + liChildContent;
                        num--;
                    }
                    var isNestedList = false;
                    var nestedListCount = 0;
                    var isNestedListItem = false;
                    if (!ej2_base_1.isNullOrUndefined(liElement) && liElement.childNodes) {
                        for (var num_1 = 0; num_1 < liElement.childNodes.length; num_1++) {
                            if (liElement.childNodes[num_1].nodeName === ('OL' || 'UL')) {
                                nestedListCount++;
                                isNestedList = true;
                            }
                        }
                    }
                    if (!ej2_base_1.isNullOrUndefined(liElement) && liElement.tagName.toLowerCase() === 'li' &&
                        liElement.textContent.split('\u200B').join('').trim() === liChildContent.split('\u200B').join('').trim()) {
                        if (format === 'fontsize') {
                            liElement.style.fontSize = value;
                        }
                        else {
                            liElement.style.color = value;
                            liElement.style.textDecoration = 'inherit';
                        }
                    }
                    else if (!ej2_base_1.isNullOrUndefined(liElement) && liElement.tagName.toLowerCase() === 'li' && isNestedList) {
                        Eif (isNestedList && nestedListCount > 0) {
                            for (var num_2 = 0; num_2 < liElement.childNodes.length; num_2++) {
                                if (nodes[index].textContent === liElement.childNodes[num_2].textContent && nodes[index].textContent === nodeText && liElement.textContent.replace('/\u200B/g', '').trim().includes(liChildContent.split('\u200B').join('').trim())) {
                                    isNestedListItem = true;
                                }
                            }
                        }
                        Eif (isNestedListItem) {
                            for (var num_3 = 0; num_3 < liElement.childNodes.length; num_3++) {
                                if (liElement.childNodes[num_3].nodeName === ('OL' || 'UL')) {
                                    liElement.childNodes[num_3].style.fontSize = 'initial';
                                }
                            }
                            Eif (format === 'fontsize') {
                                liElement.style.fontSize = value;
                            }
                            else {
                                liElement.style.color = value;
                                liElement.style.textDecoration = 'inherit';
                            }
                        }
                    }
                }
            }
            return nodes[index];
        };
        SelectionCommands.insertFormat = function (docElement, nodes, index, formatNode, isCursor, isTableSelect, isFormat, isFontStyle, range, nodeCutter, format, value, painterValues, domNode, endNode) {
            if (!isCursor) {
                if ((formatNode === null && isFormat) || isFontStyle) {
                    if (!isTableSelect && nodes[index].nodeName !== 'BR') {
                        nodes[index] = nodeCutter.GetSpliceNode(range, nodes[index]);
                        nodes[index].textContent = nodeCutter.TrimLineBreak(nodes[index].textContent);
                    }
                    if (format === 'uppercase' || format === 'lowercase') {
                        nodes[index].textContent = (format === 'uppercase') ? nodes[index].textContent.toLocaleUpperCase()
                            : nodes[index].textContent.toLocaleLowerCase();
                    }
                    else if (!(isFontStyle === true && value === '')) {
                        var element = this.GetFormatNode(format, value);
                        if (value === 'formatPainter' || isFontStyle) {
                            var liElement = nodes[index].parentElement;
                            var parentElement = nodes[index].parentElement;
                            while (!ej2_base_1.isNullOrUndefined(parentElement) && parentElement.tagName.toLowerCase() !== 'li') {
                                parentElement = parentElement.parentElement;
                                liElement = parentElement;
                            }
                            if (format === 'fontcolor' || format === 'fontname' || format === 'fontsize') {
                                var parentElem = nodes[index].parentElement;
                                Eif (!ej2_base_1.isNullOrUndefined(parentElem) && parentElem.childNodes) {
                                    for (var i = 0; i < parentElem.childNodes.length; i++) {
                                        if (this.concatenateTextExcludingList(nodes, index) === nodes[index].textContent) {
                                            var liElement_1 = void 0;
                                            if (parentElem.tagName === 'LI') {
                                                liElement_1 = parentElem;
                                            }
                                            else if (parentElem.closest('li')) {
                                                liElement_1 = parentElem.closest('li');
                                            }
                                            if (!ej2_base_1.isNullOrUndefined(liElement_1)) {
                                                switch (format) {
                                                    case 'fontcolor':
                                                        liElement_1.style.color = value;
                                                        break;
                                                    case 'fontname':
                                                        liElement_1.style.fontFamily = value;
                                                        break;
                                                    case 'fontsize':
                                                        liElement_1.style.fontSize = value;
                                                        break;
                                                    default:
                                                        break;
                                                }
                                            }
                                        }
                                        var childElement = parentElem.childNodes[i];
                                        if (childElement.tagName === 'OL' || childElement.tagName === 'UL') {
                                            switch (format) {
                                                case 'fontcolor':
                                                    childElement.style.color = 'initial';
                                                    break;
                                                case 'fontname':
                                                    childElement.style.fontFamily = 'initial';
                                                    break;
                                                case 'fontsize':
                                                    childElement.style.fontSize = 'initial';
                                                    break;
                                                default:
                                                    break;
                                            }
                                        }
                                    }
                                }
                            }
                            if (!ej2_base_1.isNullOrUndefined(liElement) && liElement.tagName.toLowerCase() === 'li' &&
                                liElement.textContent.trim() === nodes[index].textContent.trim()) {
                                if (format === 'fontsize') {
                                    liElement.style.fontSize = value;
                                }
                                else if (format === 'fontcolor') {
                                    liElement.style.color = value;
                                    liElement.style.textDecoration = 'inherit';
                                }
                                else if (format === 'fontname') {
                                    liElement.style.fontFamily = value;
                                }
                            }
                            if (value === 'formatPainter') {
                                return this.insertFormatPainterElem(nodes, index, range, nodeCutter, painterValues, domNode);
                            }
                            var currentNode = nodes[index];
                            var priorityNode = this.getPriorityFormatNode(currentNode, endNode);
                            Eif (!ej2_base_1.isNullOrUndefined(priorityNode) || isFontStyle) {
                                var currentFormatNode = ej2_base_1.isNullOrUndefined(priorityNode) ? currentNode : priorityNode;
                                currentFormatNode = !ej2_base_1.isNullOrUndefined(priorityNode) && priorityNode.style.fontSize !== '' ?
                                    currentFormatNode.firstChild : currentFormatNode;
                                if (ej2_base_1.isNullOrUndefined(priorityNode) || format === 'fontsize') {
                                    while (currentFormatNode) {
                                        var isSameTextContent = currentFormatNode.parentElement.textContent.trim()
                                            === nodes[index].textContent.trim();
                                        var parent_1 = currentFormatNode.parentElement;
                                        if (!domNode.isBlockNode(parent_1) && isSameTextContent &&
                                            !(parent_1.nodeName === 'SPAN' && parent_1.classList.contains('e-img-inner'))) {
                                            currentFormatNode = parent_1;
                                        }
                                        else {
                                            break;
                                        }
                                    }
                                }
                                var nodeList = [];
                                if (format === 'fontcolor') {
                                    var closestAnchor = ej2_base_1.closest(nodes[index].parentElement, 'A');
                                    if (!ej2_base_1.isNullOrUndefined(closestAnchor) && closestAnchor.firstChild.textContent.trim()
                                        === nodes[index].textContent.trim()) {
                                        currentFormatNode = nodes[index];
                                    }
                                }
                                Iif (nodes[index].textContent.trim() !== currentFormatNode.textContent.trim()) {
                                    currentFormatNode = nodes[index];
                                }
                                nodeList[0] = currentFormatNode;
                                this.applyStyles(nodeList, 0, element);
                                if (!ej2_base_1.isNullOrUndefined(liElement) && liElement.tagName.toLowerCase() === 'li'
                                    && liElement.textContent.trim() !== nodes[index].textContent.trim()) {
                                    SelectionCommands.conCatenateTextNode(liElement, format, liElement.textContent, format, value);
                                }
                            }
                            else {
                                nodes[index] = this.applyStyles(nodes, index, element);
                            }
                        }
                        else {
                            nodes[index] = this.applyStyles(nodes, index, element);
                            var liElement = nodes[index].parentElement;
                            Eif (!ej2_base_1.isNullOrUndefined(liElement) && liElement.tagName.toLowerCase() !== 'li') {
                                liElement = ej2_base_1.closest(liElement, 'li');
                            }
                            if (!ej2_base_1.isNullOrUndefined(liElement) && liElement.tagName.toLowerCase() === 'li' &&
                                liElement.textContent.trim() === nodes[index].textContent.trim()) {
                                if (format === 'bold') {
                                    liElement.style.fontWeight = 'bold';
                                }
                                else if (format === 'italic') {
                                    liElement.style.fontStyle = 'italic';
                                }
                            }
                            else if (!ej2_base_1.isNullOrUndefined(liElement) && liElement.tagName.toLowerCase() === 'li'
                                && liElement.textContent.trim() !== nodes[index].textContent.trim()) {
                                SelectionCommands.conCatenateTextNode(liElement, format, liElement.textContent, format);
                            }
                        }
                    }
                }
                else {
                    if (!isTableSelect) {
                        nodes[index] = nodeCutter.GetSpliceNode(range, nodes[index]);
                    }
                }
            }
            else {
                if (format !== 'uppercase' && format !== 'lowercase') {
                    var element = this.getInsertNode(docElement, range, format, value);
                    nodes[index] = element.firstChild;
                    nodeCutter.position = 1;
                }
                else {
                    nodeCutter.position = range.startOffset;
                }
            }
            return nodes[index];
        };
        SelectionCommands.applyStyles = function (nodes, index, element) {
            Eif (!(nodes[index].nodeName === 'BR' && this.enterAction === 'BR')) {
                nodes[index] = (index === (nodes.length - 1)) || nodes[index].nodeName === 'BR' ?
                    insert_methods_1.InsertMethods.Wrap(nodes[index], element)
                    : insert_methods_1.InsertMethods.WrapBefore(nodes[index], element, true);
                nodes[index] = this.getChildNode(nodes[index], element);
            }
            return nodes[index];
        };
        SelectionCommands.getPriorityFormatNode = function (node, endNode) {
            var isFormatted = new isformatted_1.IsFormatted();
            var fontSizeNode = isFormatted.getFormattedNode(node, 'fontsize', endNode);
            var fontColorNode;
            var backgroundColorNode;
            var fontNameNode;
            if (ej2_base_1.isNullOrUndefined(fontSizeNode)) {
                backgroundColorNode = isFormatted.getFormattedNode(node, 'backgroundcolor', endNode);
                if (ej2_base_1.isNullOrUndefined(backgroundColorNode)) {
                    fontNameNode = isFormatted.getFormattedNode(node, 'fontname', endNode);
                    if (ej2_base_1.isNullOrUndefined(fontNameNode)) {
                        fontColorNode = isFormatted.getFormattedNode(node, 'fontcolor', endNode);
                        if (ej2_base_1.isNullOrUndefined(fontColorNode)) {
                            return null;
                        }
                        else {
                            return fontColorNode;
                        }
                    }
                    else {
                        return fontNameNode;
                    }
                }
                else {
                    return backgroundColorNode;
                }
            }
            else {
                return fontSizeNode;
            }
        };
        SelectionCommands.getInsertNode = function (docElement, range, format, value) {
            var element = this.GetFormatNode(format, value);
            element.innerHTML = '&#8203;';
            Iif (ej2_base_1.Browser.isIE) {
                var frag = docElement.createDocumentFragment();
                frag.appendChild(element);
                range.insertNode(frag);
            }
            else {
                range.insertNode(element);
            }
            return element;
        };
        SelectionCommands.getChildNode = function (node, element) {
            if (node === undefined || node === null) {
                element.innerHTML = '&#8203;';
                node = element.firstChild;
            }
            return node;
        };
        SelectionCommands.applySelection = function (nodes, domSelection, nodeCutter, index, isCollapsed) {
            if (nodes.length === 1 && !isCollapsed) {
                domSelection.startContainer = domSelection.getNodeArray(nodes[index], true);
                domSelection.endContainer = domSelection.startContainer;
                domSelection.startOffset = 0;
                domSelection.endOffset = nodes[index].textContent.length;
            }
            else if (nodes.length === 1 && isCollapsed) {
                domSelection.startContainer = domSelection.getNodeArray(nodes[index], true);
                domSelection.endContainer = domSelection.startContainer;
                domSelection.startOffset = nodeCutter.position;
                domSelection.endOffset = nodeCutter.position;
            }
            else if (index === 0) {
                domSelection.startContainer = domSelection.getNodeArray(nodes[index], true);
                domSelection.startOffset = 0;
            }
            else if (index === nodes.length - 1) {
                domSelection.endContainer = domSelection.getNodeArray(nodes[index], false);
                domSelection.endOffset = nodes[index].textContent.length;
            }
            return domSelection;
        };
        SelectionCommands.GetFormatNode = function (format, value, tagName, styles) {
            var node;
            switch (format) {
                case 'bold':
                    return document.createElement('strong');
                case 'italic':
                    return document.createElement('em');
                case 'underline':
                    node = document.createElement('span');
                    this.updateStyles(node, tagName, styles);
                    node.style.textDecoration = 'underline';
                    return node;
                case 'strikethrough':
                    node = document.createElement('span');
                    this.updateStyles(node, tagName, styles);
                    node.style.textDecoration = 'line-through';
                    return node;
                case 'superscript':
                    return document.createElement('sup');
                case 'subscript':
                    return document.createElement('sub');
                case 'fontcolor':
                    node = document.createElement('span');
                    this.updateStyles(node, tagName, styles);
                    node.style.color = value;
                    node.style.textDecoration = 'inherit';
                    return node;
                case 'fontname':
                    node = document.createElement('span');
                    this.updateStyles(node, tagName, styles);
                    node.style.fontFamily = value;
                    return node;
                case 'fontsize':
                    node = document.createElement('span');
                    this.updateStyles(node, tagName, styles);
                    node.style.fontSize = value;
                    return node;
                case 'inlinecode':
                    return document.createElement('code');
                default:
                    node = document.createElement('span');
                    this.updateStyles(node, tagName, styles);
                    node.style.backgroundColor = value;
                    return node;
            }
        };
        SelectionCommands.updateStyles = function (ele, tag, styles) {
            if (styles !== null && tag === 'SPAN') {
                ele.setAttribute('style', styles);
            }
        };
        SelectionCommands.insertFormatPainterElem = function (nodes, index, range, nodeCutter, painterValues, domNode) {
            var parent = !domNode.isBlockNode(nodes[index].parentElement) ?
                nodes[index].parentElement : nodes[index];
            Eif (!domNode.isBlockNode(parent)) {
                while (parent.textContent.trim() === parent.parentElement.textContent.trim() && !domNode.isBlockNode(parent.parentElement)) {
                    parent = parent.parentElement;
                }
            }
            if (!ej2_base_1.isNullOrUndefined(parent) && parent.nodeType === 1 && !(parent.classList.contains('e-rte-img-caption') || parent.classList.contains('e-img-inner'))) {
                this.formatPainterCleanup(index, nodes, parent, range, nodeCutter, domNode);
            }
            var elem = painterValues.element;
            if (!ej2_base_1.isNullOrUndefined(elem)) {
                var clonedElement = elem.cloneNode(true);
                var elemList = clonedElement.querySelectorAll(painterValues.lastChild.nodeName);
                var lastElement = void 0;
                if (elemList.length > 0) {
                    lastElement = elemList[elemList.length - 1];
                }
                else {
                    Eif (!ej2_base_1.isNullOrUndefined(clonedElement) && clonedElement.nodeName === painterValues.lastChild.nodeName) {
                        lastElement = clonedElement;
                    }
                }
                lastElement.textContent = nodes[index].textContent;
                var lastChild = lastElement.childNodes[0];
                nodes[index] = insert_methods_1.InsertMethods.Wrap(nodes[index], clonedElement);
                nodes[index].textContent = '';
                nodes[index] = lastChild;
            }
            return nodes[index];
        };
        SelectionCommands.formatPainterCleanup = function (index, nodes, parent, range, nodeCutter, domNode) {
            var INVALID_TAGS = ['A', 'AUDIO', 'IMG', 'VIDEO', 'IFRAME'];
            if (index === 0 && parent.textContent.trim() !== nodes[index].textContent.trim()) {
                nodeCutter.SplitNode(range, parent, true);
                var childELemList = nodes[index].parentElement.childNodes;
                for (var i = 0; i < childELemList.length; i++) {
                    Eif (childELemList[i].textContent.trim() === nodes[i].textContent.trim()) {
                        parent.parentNode.insertBefore(childELemList[i], parent);
                        break;
                    }
                }
                var blockChildNodes = parent.parentElement.childNodes;
                for (var k = 0; k < blockChildNodes.length; k++) {
                    if ((blockChildNodes[k].textContent.trim() === '' || blockChildNodes[k].textContent.length === 0) &&
                        blockChildNodes[k].textContent.charCodeAt(0) !== 160) {
                        ej2_base_1.detach(blockChildNodes[k]);
                    }
                }
            }
            else Iif (parent.textContent.trim() !== nodes[index].textContent.trim()) {
                parent.parentElement.insertBefore(nodes[index], parent);
            }
            else {
                while (!ej2_base_1.isNullOrUndefined(parent) && parent.nodeType !== 3 && !domNode.isBlockNode(parent)) {
                    var temp = void 0;
                    for (var i = 0; i < parent.childNodes.length; i++) {
                        var currentChild = parent.childNodes[i];
                        if (currentChild.textContent.trim().length !== 0 && currentChild.nodeType !== 3) {
                            temp = parent.childNodes[i];
                        }
                    }
                    if (INVALID_TAGS.indexOf(parent.tagName) === -1) {
                        insert_methods_1.InsertMethods.unwrap(parent);
                    }
                    parent = temp;
                }
            }
        };
        SelectionCommands.concatenateTextExcludingList = function (nodes, index) {
            var result = '';
            var parentNode = nodes[index].nodeName === '#text' ? ej2_base_1.closest(nodes[index].parentElement, 'li') : ej2_base_1.closest(nodes[index], 'li');
            if (!ej2_base_1.isNullOrUndefined(parentNode)) {
                for (var i = 0; i < parentNode.childNodes.length; i++) {
                    var childNode = parentNode.childNodes[i];
                    if ((childNode.nodeType === 3) || (childNode.nodeType === 1 && (childNode.tagName !== 'OL' && childNode.tagName !== 'UL'))) {
                        result += childNode.textContent;
                    }
                }
            }
            return result;
        };
        SelectionCommands.conCatenateTextNode = function (liElement, format, value, formatStr, constVal) {
            var result = '';
            var colorStyle = '';
            var fontSize = '';
            var fontFamily = '';
            switch (format) {
                case 'bold':
                    liElement.querySelectorAll('strong').forEach(function (e) {
                        result = result + e.textContent;
                    });
                    if (result === value) {
                        liElement.style.fontWeight = formatStr;
                    }
                    break;
                case 'italic':
                    liElement.querySelectorAll('em').forEach(function (e) {
                        result = result + e.textContent;
                    });
                    if (result === value) {
                        liElement.style.fontStyle = formatStr;
                    }
                    break;
                case 'fontcolor':
                    liElement.querySelectorAll('span').forEach(function (span) {
                        colorStyle = span.style.color;
                        if (colorStyle === constVal) {
                            result = result + span.textContent;
                        }
                    });
                    if (result === value) {
                        liElement.style.color = colorStyle;
                        liElement.style.textDecoration = 'inherit';
                    }
                    break;
                case 'fontsize':
                    liElement.querySelectorAll('span').forEach(function (span) {
                        fontSize = span.style.getPropertyValue('font-size');
                        if (fontSize === constVal) {
                            result = result + span.textContent;
                        }
                    });
                    if (result === value) {
                        liElement.style.fontSize = fontSize;
                    }
                    break;
                case 'fontname':
                    liElement.querySelectorAll('span').forEach(function (span) {
                        fontFamily = span.style.getPropertyValue('font-family');
                        fontFamily = fontFamily.replace(/ /g, '');
                        Eif (fontFamily === constVal) {
                            result = result + span.textContent;
                        }
                    });
                    if (result === value) {
                        liElement.style.fontFamily = fontFamily;
                    }
                    break;
            }
        };
        SelectionCommands.enterAction = 'P';
        return SelectionCommands;
    }());
    exports.SelectionCommands = SelectionCommands;
});