all files / editor-manager/plugin/ inserthtml.js

90.93% Statements 391/430
88.69% Branches 439/495
100% Functions 24/24
91.12% Lines 390/428
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     432× 432× 32× 32× 32×     400×       394×     432× 432× 432× 432×     15× 15×   432×       432×   10× 10× 10×   432×   432× 432× 432× 432×   430× 13×   430×   226× 226×   204×       63× 63× 63× 63× 56× 56×         63×     62×   63× 42×   63× 140× 36×   34×     63× 21×     42× 42×           42×     42×                 37× 28×         63×   60× 53×           141× 141× 78×   141× 43× 43× 43×   98×                     98×       98×     141× 82×   59× 47×   12×       12×       30×   21× 21× 21× 21×         226×   226×   226× 226× 226× 226× 226×       18× 18× 17× 17×     11× 11× 11×   11× 11×       226× 226× 226× 226× 399× 168× 168×     226× 226× 226× 58× 11× 11×                 49× 49× 49×               49×       49×   49× 162× 162×   49×       168× 168× 213×   168×         168× 168× 168× 168× 168× 168×       168× 825× 270× 270×   555×   23× 23× 13× 13×   23×       23×       532× 532×   13× 10× 10× 10×       13× 13×     519× 519×   532×     168×     226× 19× 19× 19× 19×   226× 14×   212× 10× 10× 10× 10×       10×       10×     202×   226× 226× 226×   226× 38× 38× 38× 101×   101× 106×   101×           216×   216×   297×         297×   216× 216× 24×     192×   216×   766× 766×       419×     347×   766×   176×                 176×       176×               170× 170×   170×   170×       170×     167× 167× 167× 167×     57× 57×       57×     110×     14× 14×     14×   14× 14× 14×     96×   152×       202× 202× 202× 202× 202× 202× 202× 44×       74× 74×     71×   74×   187× 191×   187×   226× 226× 226×   243× 243× 243× 235×   235×   69×       166×           243×   266× 266× 266× 708×   266× 707× 707×     707× 177× 177× 169×         47× 47×   45× 61×     44×   17×                               226× 226× 15× 15× 15× 15× 15× 15× 25× 25× 21×     15×                                  
define(["require", "exports", "./../../selection/index", "./nodecutter", "./../base/constant", "@syncfusion/ej2-base", "./insert-methods", "./../../common/util"], function (require, exports, index_1, nodecutter_1, CONSTANT, ej2_base_1, insert_methods_1, util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var InsertHtml = (function () {
        function InsertHtml() {
        }
        InsertHtml.Insert = function (docElement, insertNode, editNode, isExternal, enterAction) {
            var node;
            if (typeof insertNode === 'string') {
                var divNode = document.createElement('div');
                divNode.innerHTML = insertNode;
                node = isExternal ? divNode : divNode.firstChild;
            }
            else {
                if (isExternal && !(!ej2_base_1.isNullOrUndefined(insertNode) && !ej2_base_1.isNullOrUndefined(insertNode.classList) &&
                    insertNode.classList.contains('pasteContent'))) {
                    var divNode = document.createElement('div');
                    divNode.appendChild(insertNode);
                    node = divNode;
                }
                else {
                    node = insertNode;
                }
            }
            var nodeSelection = new index_1.NodeSelection(editNode);
            var nodeCutter = new nodecutter_1.NodeCutter();
            var range = nodeSelection.getRange(docElement);
            if (range.startContainer === editNode && range.startContainer === range.endContainer && range.startOffset === 0 &&
                range.startOffset === range.endOffset && editNode.textContent.length === 0 &&
                (editNode.children[0].tagName === 'P' || editNode.children[0].tagName === 'DIV' || (editNode.children[0].tagName === 'BR'))) {
                nodeSelection.setSelectionText(docElement, range.startContainer.children[0], range.startContainer.children[0], 0, 0);
                range = nodeSelection.getRange(docElement);
            }
            if (range.startContainer === editNode && range.startContainer === range.endContainer && range.startOffset === 0 &&
                range.startOffset === range.endOffset && editNode.textContent.trim().length > 0) {
                var focusNode = this.findFirstTextNode(range.startContainer);
                Eif (!ej2_base_1.isNullOrUndefined(focusNode)) {
                    nodeSelection.setSelectionText(docElement, focusNode, focusNode, 0, 0);
                    range = nodeSelection.getRange(docElement);
                }
            }
            if (range.startContainer.nodeName === 'BR' && range.startOffset === 0 && range.startOffset === range.endOffset &&
                range.startContainer === range.endContainer) {
                var currentIndex = Array.prototype.slice.call(range.startContainer.parentElement.childNodes).indexOf(range.startContainer);
                nodeSelection.setSelectionText(docElement, range.startContainer.parentElement, range.startContainer.parentElement, currentIndex, currentIndex);
                range = nodeSelection.getRange(docElement);
            }
            var isCursor = range.startOffset === range.endOffset && range.startOffset === 0 &&
                range.startContainer === range.endContainer;
            var isCollapsed = range.collapsed;
            var nodes = this.getNodeCollection(range, nodeSelection, node);
            var closestParentNode = (node.nodeName.toLowerCase() === 'table') ? (!ej2_base_1.isNullOrUndefined(nodes[0]) ? this.closestEle(nodes[0].parentNode, editNode) : range.startContainer) : nodes[0];
            if (closestParentNode && closestParentNode.nodeName === 'LI' && node.nodeName.toLowerCase() === 'table') {
                this.insertTableInList(range, node, closestParentNode, nodes[0], nodeCutter);
                return;
            }
            if (isCursor && range.startContainer.textContent === '' && range.startContainer.nodeName !== 'BR' && enterAction !== 'BR' && node.nodeName !== '#text' && !ej2_base_1.isNullOrUndefined(node.children[0]) && !ej2_base_1.isNullOrUndefined(node.children[0].tagName) && node.children[0].tagName === 'IMG' && node.children.length === 1) {
                range.startContainer.innerHTML = '';
            }
            if (isExternal || (!ej2_base_1.isNullOrUndefined(node) && !ej2_base_1.isNullOrUndefined(node.classList) &&
                node.classList.contains('pasteContent'))) {
                this.pasteInsertHTML(nodes, node, range, nodeSelection, nodeCutter, docElement, isCollapsed, closestParentNode, editNode, enterAction);
                return;
            }
            if (editNode !== range.startContainer && ((!isCollapsed && !(closestParentNode.nodeType === Node.ELEMENT_NODE &&
                CONSTANT.TABLE_BLOCK_TAGS.indexOf(closestParentNode.tagName.toLocaleLowerCase()) !== -1))
                || (node.nodeName.toLowerCase() === 'table' && closestParentNode &&
                    CONSTANT.TABLE_BLOCK_TAGS.indexOf(closestParentNode.tagName.toLocaleLowerCase()) === -1))) {
                var preNode = nodeCutter.GetSpliceNode(range, closestParentNode);
                var sibNode = preNode.previousSibling;
                var parentNode = preNode.parentNode;
                if (nodes.length === 1 || (node.nodeName.toLowerCase() === 'table' && preNode.childElementCount === 0)) {
                    nodeSelection.setSelectionContents(docElement, preNode);
                    range = nodeSelection.getRange(docElement);
                }
                else {
                    var lasNode = nodeCutter.GetSpliceNode(range, nodes[nodes.length - 1].parentElement);
                    lasNode = ej2_base_1.isNullOrUndefined(lasNode) ? preNode : lasNode;
                    nodeSelection.setSelectionText(docElement, preNode, lasNode, 0, (lasNode.nodeType === 3) ?
                        lasNode.textContent.length : lasNode.childNodes.length);
                    range = nodeSelection.getRange(docElement);
                }
                if (range.startContainer.parentElement.closest('ol,ul') !== null && range.endContainer.parentElement.closest('ol,ul') !== null) {
                    util_1.nestedListCleanUp(range);
                }
                else {
                    range.extractContents();
                }
                if (insertNode.tagName === 'TABLE') {
                    this.removeEmptyElements(editNode);
                }
                for (var index = 0; index < nodes.length; index++) {
                    if (nodes[index].nodeType !== 3 && nodes[index].parentNode != null) {
                        if (nodes[index].nodeName === 'IMG') {
                            continue;
                        }
                        nodes[index].parentNode.removeChild(nodes[index]);
                    }
                }
                if (!ej2_base_1.isNullOrUndefined(sibNode) && !ej2_base_1.isNullOrUndefined(sibNode.parentNode)) {
                    insert_methods_1.InsertMethods.AppendBefore(node, sibNode, true);
                }
                else {
                    var previousNode = null;
                    while (parentNode !== editNode && parentNode.firstChild &&
                        (parentNode.textContent.trim() === '')) {
                        var parentNode1 = parentNode.parentNode;
                        previousNode = parentNode;
                        parentNode = parentNode1;
                    }
                    Iif (previousNode !== null) {
                        parentNode = previousNode;
                    }
                    if (parentNode.firstChild && (parentNode !== editNode ||
                        (node.nodeName === 'TABLE' && isCursor && parentNode === range.startContainer &&
                            parentNode === range.endContainer))) {
                        Iif (parentNode.textContent.trim() === '' && parentNode !== editNode) {
                            insert_methods_1.InsertMethods.AppendBefore(node, parentNode, false);
                            ej2_base_1.detach(parentNode);
                        }
                        else {
                            insert_methods_1.InsertMethods.AppendBefore(node, parentNode.firstChild, false);
                        }
                    }
                    else if (ej2_base_1.isNullOrUndefined(preNode.previousSibling) && insertNode.tagName === 'TABLE') {
                        parentNode.prepend(node);
                    }
                    else {
                        parentNode.appendChild(node);
                    }
                }
                if (node.nodeName === 'IMG') {
                    this.imageFocus(node, nodeSelection, docElement);
                }
                else if (node.nodeType !== 3) {
                    nodeSelection.setSelectionText(docElement, node, node, 0, node.childNodes.length);
                }
                else {
                    nodeSelection.setSelectionText(docElement, node, node, 0, node.textContent.length);
                }
            }
            else {
                range.deleteContents();
                if (isCursor && range.startContainer.textContent === '' && range.startContainer.nodeName !== 'BR') {
                    range.startContainer.innerHTML = '';
                }
                if (ej2_base_1.Browser.isIE) {
                    var frag = docElement.createDocumentFragment();
                    frag.appendChild(node);
                    range.insertNode(frag);
                }
                else Iif (range.startContainer.nodeType === 1 && range.startContainer.nodeName.toLowerCase() === 'hr'
                    && range.endContainer.nodeName.toLowerCase() === 'hr') {
                    var paraElem = range.startContainer.nextElementSibling;
                    if (paraElem) {
                        if (paraElem.querySelector('br')) {
                            ej2_base_1.detach(paraElem.querySelector('br'));
                        }
                        paraElem.appendChild(node);
                    }
                }
                else {
                    Iif (range.startContainer.nodeName === 'BR') {
                        range.startContainer.parentElement.insertBefore(node, range.startContainer);
                    }
                    else {
                        range.insertNode(node);
                    }
                }
                if (node.nodeType !== 3 && node.childNodes.length > 0) {
                    nodeSelection.setSelectionText(docElement, node, node, 1, 1);
                }
                else if (node.nodeName === 'IMG') {
                    this.imageFocus(node, nodeSelection, docElement);
                }
                else Iif (node.nodeType !== 3) {
                    nodeSelection.setSelectionContents(docElement, node);
                }
                else {
                    nodeSelection.setSelectionText(docElement, node, node, node.textContent.length, node.textContent.length);
                }
            }
        };
        InsertHtml.findFirstTextNode = function (node) {
            if (node.nodeType === Node.TEXT_NODE) {
                return node;
            }
            for (var i = 0; i < node.childNodes.length; i++) {
                var textNode = this.findFirstTextNode(node.childNodes[i]);
                Eif (!ej2_base_1.isNullOrUndefined(textNode)) {
                    return textNode;
                }
            }
            return null;
        };
        InsertHtml.pasteInsertHTML = function (nodes, node, range, nodeSelection, nodeCutter, docElement, isCollapsed, closestParentNode, editNode, enterAction) {
            var isCursor = range.startOffset === range.endOffset &&
                range.startContainer === range.endContainer;
            if (isCursor && range.startContainer === editNode && editNode.textContent === '') {
                var currentBlockNode = this.getImmediateBlockNode(nodes[nodes.length - 1], editNode);
                nodeSelection.setSelectionText(docElement, currentBlockNode, currentBlockNode, 0, 0);
                range = nodeSelection.getRange(docElement);
            }
            var lasNode;
            var sibNode;
            var isSingleNode;
            var preNode;
            if (editNode !== range.startContainer && ((!isCollapsed && !(closestParentNode.nodeType === Node.ELEMENT_NODE &&
                CONSTANT.TABLE_BLOCK_TAGS.indexOf(closestParentNode.tagName.toLocaleLowerCase()) !== -1))
                || (node.nodeName.toLowerCase() === 'table' && closestParentNode &&
                    CONSTANT.TABLE_BLOCK_TAGS.indexOf(closestParentNode.tagName.toLocaleLowerCase()) === -1))) {
                preNode = nodeCutter.GetSpliceNode(range, closestParentNode);
                if (!ej2_base_1.isNullOrUndefined(preNode)) {
                    sibNode = ej2_base_1.isNullOrUndefined(preNode.previousSibling) ? preNode.parentNode.previousSibling : preNode.previousSibling;
                    if (nodes.length === 1) {
                        nodeSelection.setSelectionContents(docElement, preNode);
                        range = nodeSelection.getRange(docElement);
                        isSingleNode = true;
                    }
                    else {
                        lasNode = nodeCutter.GetSpliceNode(range, nodes[nodes.length - 1].parentElement);
                        lasNode = ej2_base_1.isNullOrUndefined(lasNode) ? preNode : lasNode;
                        nodeSelection.setSelectionText(docElement, preNode, lasNode, 0, (lasNode.nodeType === 3) ?
                            lasNode.textContent.length : lasNode.childNodes.length);
                        range = nodeSelection.getRange(docElement);
                        isSingleNode = false;
                    }
                }
            }
            var containsBlockNode = false;
            this.removingComments(node);
            var allChildNodes = node.childNodes;
            for (var i = 0; i < allChildNodes.length; i++) {
                if (CONSTANT.BLOCK_TAGS.indexOf(allChildNodes[i].nodeName.toLocaleLowerCase()) >= 0) {
                    containsBlockNode = true;
                    break;
                }
            }
            var lastSelectionNode;
            var fragment = document.createDocumentFragment();
            if (!containsBlockNode) {
                if (!isCursor) {
                    while (node.firstChild) {
                        lastSelectionNode = node.firstChild;
                        fragment.appendChild(node.firstChild);
                    }
                    if (isSingleNode) {
                        range.deleteContents();
                        this.removeEmptyElements(editNode, true);
                        range.insertNode(fragment);
                    }
                    else {
                        var startContainerParent = editNode === range.startContainer ?
                            range.startContainer : range.startContainer.parentNode;
                        var startIndex = Array.prototype.indexOf.call(startContainerParent.childNodes, (ej2_base_1.Browser.userAgent.indexOf('Firefox') !== -1 && editNode === range.startContainer) ? range.startContainer.firstChild : range.startContainer);
                        range.deleteContents();
                        range.setStart(startContainerParent, startIndex);
                        range.setEnd(startContainerParent, startIndex);
                        if (!ej2_base_1.isNullOrUndefined(lasNode) && lasNode !== editNode) {
                            ej2_base_1.detach(lasNode);
                            this.removeEmptyElements(editNode, true);
                        }
                        !ej2_base_1.isNullOrUndefined(sibNode) ? (sibNode.parentNode === editNode ? sibNode.appendChild(fragment) : sibNode.parentNode.appendChild(fragment)) : range.insertNode(fragment);
                    }
                }
                else {
                    var tempSpan = ej2_base_1.createElement('span', { className: 'tempSpan' });
                    var nearestAnchor = ej2_base_1.closest(range.startContainer.parentElement, 'a');
                    Iif (range.startContainer.nodeType === 3 && nearestAnchor && ej2_base_1.closest(nearestAnchor, 'span')) {
                        var immediateBlockNode = this.getImmediateBlockNode(range.startContainer, editNode);
                        if (immediateBlockNode.querySelectorAll('br').length > 0) {
                            ej2_base_1.detach(immediateBlockNode.querySelector('br'));
                        }
                        var rangeElement = ej2_base_1.closest(nearestAnchor, 'span');
                        rangeElement.appendChild(tempSpan);
                    }
                    else Iif (nodes[0].nodeName === '#text' && nodes[0].nodeValue.includes('\u200B') && !ej2_base_1.isNullOrUndefined(nodes[0].parentElement) && !ej2_base_1.isNullOrUndefined(nodes[0].parentElement.previousElementSibling) && nodes[0].parentElement.previousElementSibling.classList.contains('e-mention-chip')) {
                        range.startContainer.parentElement.insertAdjacentElement('afterend', tempSpan);
                    }
                    else {
                        range.insertNode(tempSpan);
                    }
                    while (node.firstChild) {
                        lastSelectionNode = node.firstChild;
                        fragment.appendChild(node.firstChild);
                    }
                    tempSpan.parentNode.replaceChild(fragment, tempSpan);
                }
            }
            else {
                var parentElem = range.startContainer;
                while (!ej2_base_1.isNullOrUndefined(parentElem) && parentElem.nodeName !== 'PRE' && parentElem !== editNode) {
                    parentElem = parentElem.parentElement;
                }
                Iif (!ej2_base_1.isNullOrUndefined(node) && !ej2_base_1.isNullOrUndefined(parentElem) && parentElem.nodeName === 'PRE') {
                    range.insertNode(node);
                    lastSelectionNode = node.lastChild;
                }
                else {
                    this.insertTempNode(range, node, nodes, nodeCutter, editNode);
                    var isFirstTextNode = true;
                    var isPreviousInlineElem = void 0;
                    var paraElm = void 0;
                    var previousParent = void 0;
                    if (!this.contentsDeleted) {
                        Iif (!isCollapsed && range.startContainer.parentElement.textContent.length === 0 && range.startContainer.nodeName === 'BR' && range.startContainer.parentElement.nodeName === 'P') {
                            editNode.removeChild(range.startContainer.parentElement);
                        }
                        range.deleteContents();
                    }
                    while (node.firstChild) {
                        if (node.firstChild.nodeName === '#text' && node.firstChild.textContent.trim() === '') {
                            ej2_base_1.detach(node.firstChild);
                            continue;
                        }
                        if (node.firstChild.nodeName === '#text' && isFirstTextNode ||
                            (this.inlineNode.indexOf(node.firstChild.nodeName.toLocaleLowerCase()) >= 0 && isFirstTextNode)) {
                            lastSelectionNode = node.firstChild;
                            if (ej2_base_1.isNullOrUndefined(node.previousElementSibling)) {
                                var firstParaElm = enterAction === 'DIV' ? ej2_base_1.createElement('div') : ej2_base_1.createElement('p');
                                node.parentElement.insertBefore(firstParaElm, node);
                            }
                            Iif (node.previousElementSibling.nodeName === 'BR') {
                                node.parentElement.insertBefore(node.firstChild, node);
                            }
                            else {
                                node.previousElementSibling.appendChild(node.firstChild);
                            }
                        }
                        else {
                            lastSelectionNode = node.firstChild;
                            if (node.firstChild.nodeName === '#text' ||
                                (this.inlineNode.indexOf(node.firstChild.nodeName.toLocaleLowerCase()) >= 0)) {
                                if (!isPreviousInlineElem) {
                                    paraElm = enterAction === 'DIV' ? ej2_base_1.createElement('div') : ej2_base_1.createElement('p');
                                    paraElm.appendChild(node.firstChild);
                                    fragment.appendChild(paraElm);
                                }
                                else {
                                    previousParent.appendChild(node.firstChild);
                                    fragment.appendChild(previousParent);
                                }
                                previousParent = paraElm;
                                isPreviousInlineElem = true;
                            }
                            else {
                                fragment.appendChild(node.firstChild);
                                isPreviousInlineElem = false;
                            }
                            isFirstTextNode = false;
                        }
                    }
                    node.parentNode.replaceChild(fragment, node);
                }
            }
            if (lastSelectionNode.nodeName === 'TABLE') {
                var pTag = ej2_base_1.createElement('p');
                pTag.appendChild(ej2_base_1.createElement('br'));
                lastSelectionNode.parentElement.insertBefore(pTag, lastSelectionNode.nextSibling);
                lastSelectionNode = pTag;
            }
            if (lastSelectionNode.nodeName === '#text') {
                this.placeCursorEnd(lastSelectionNode, node, nodeSelection, docElement, editNode);
            }
            else if (lastSelectionNode.nodeName === 'HR') {
                var nextSiblingNode = lastSelectionNode.nextSibling ? lastSelectionNode.nextSibling : null;
                var siblingTag = enterAction === 'DIV' ? ej2_base_1.createElement('div') : ej2_base_1.createElement('p');
                siblingTag.appendChild(ej2_base_1.createElement('br'));
                Iif (!ej2_base_1.isNullOrUndefined(nextSiblingNode) && nextSiblingNode.nodeName === 'HR') {
                    lastSelectionNode.parentNode.insertBefore(siblingTag, nextSiblingNode);
                    lastSelectionNode = siblingTag;
                }
                else if (!ej2_base_1.isNullOrUndefined(nextSiblingNode)) {
                    lastSelectionNode = nextSiblingNode;
                }
                else {
                    lastSelectionNode.parentNode.appendChild(siblingTag);
                    lastSelectionNode.parentNode.insertBefore(lastSelectionNode, siblingTag);
                    lastSelectionNode = siblingTag;
                }
                nodeSelection.setSelectionText(docElement, lastSelectionNode, lastSelectionNode, 0, 0);
            }
            else {
                this.cursorPos(lastSelectionNode, node, nodeSelection, docElement, editNode, enterAction);
            }
            this.alignCheck(editNode);
            var currentRange = nodeSelection.getRange(docElement);
            this.listCleanUp(currentRange);
        };
        InsertHtml.listCleanUp = function (range) {
            if (range.startContainer.parentElement.closest('ol,ul') !== null && range.endContainer.parentElement.closest('ol,ul') !== null) {
                var liElems = range.startContainer.parentElement.closest('ol,ul').querySelectorAll('li');
                Eif (liElems.length > 0) {
                    liElems.forEach(function (item) {
                        if (!ej2_base_1.isNullOrUndefined(item.firstChild) && (item.firstChild.nodeName === 'OL' || item.firstChild.nodeName === 'UL')) {
                            item.style.listStyleType = 'none';
                        }
                        var nestedLi = Array.from(item.children).find(function (child) {
                            return child.tagName === 'LI' && (child.parentElement && child.parentElement.tagName !== 'OL' && child.parentElement.tagName !== 'UL');
                        });
                        if (nestedLi) {
                            item.parentNode.replaceChild(nestedLi, item);
                        }
                    });
                }
            }
        };
        InsertHtml.placeCursorEnd = function (lastSelectionNode, node, nodeSelection, docElement, editNode) {
            lastSelectionNode = lastSelectionNode.nodeName === 'BR' ? (ej2_base_1.isNullOrUndefined(lastSelectionNode.previousSibling) ? lastSelectionNode.parentNode
                : lastSelectionNode.previousSibling) : lastSelectionNode;
            while (!ej2_base_1.isNullOrUndefined(lastSelectionNode) && lastSelectionNode.nodeName !== '#text' && lastSelectionNode.nodeName !== 'IMG' &&
                lastSelectionNode.nodeName !== 'BR' && lastSelectionNode.nodeName !== 'HR') {
                Iif (!ej2_base_1.isNullOrUndefined(lastSelectionNode.lastChild) && (lastSelectionNode.lastChild.nodeName === 'P' &&
                    lastSelectionNode.lastChild.innerHTML === '')) {
                    var lineBreak = ej2_base_1.createElement('br');
                    lastSelectionNode.lastChild.appendChild(lineBreak);
                }
                lastSelectionNode = lastSelectionNode.lastChild;
            }
            lastSelectionNode = ej2_base_1.isNullOrUndefined(lastSelectionNode) ? node : lastSelectionNode;
            if (lastSelectionNode.nodeName === 'IMG') {
                this.imageFocus(lastSelectionNode, nodeSelection, docElement);
            }
            else {
                nodeSelection.setSelectionText(docElement, lastSelectionNode, lastSelectionNode, lastSelectionNode.textContent.length, lastSelectionNode.textContent.length);
            }
            this.removeEmptyElements(editNode);
        };
        InsertHtml.getNodeCollection = function (range, nodeSelection, node) {
            var nodes = [];
            if (range.startOffset === range.endOffset && range.startContainer === range.endContainer &&
                range.startContainer.nodeName !== 'BR' && range.startContainer.childNodes.length > 0 &&
                (range.startContainer.nodeName === 'TD' || (range.startContainer.nodeType !== 3 &&
                    node.classList && node.classList.contains('pasteContent')))) {
                nodes.push(range.startContainer.childNodes[range.endOffset]);
            }
            else {
                nodes = nodeSelection.getInsertNodeCollection(range);
            }
            return nodes;
        };
        InsertHtml.insertTempNode = function (range, node, nodes, nodeCutter, editNode) {
            Iif (range.startContainer === editNode && !ej2_base_1.isNullOrUndefined(range.startContainer.childNodes[range.endOffset - 1]) &&
                range.startContainer.childNodes[range.endOffset - 1].nodeName === 'TABLE') {
                if (ej2_base_1.isNullOrUndefined(range.startContainer.childNodes[range.endOffset - 1].nextSibling)) {
                    range.startContainer.appendChild(node);
                }
                else {
                    range.startContainer.insertBefore(node, range.startContainer.childNodes[range.endOffset - 1].nextSibling);
                }
            }
            else Iif (range.startContainer === editNode && !ej2_base_1.isNullOrUndefined(range.startContainer.childNodes[range.endOffset]) &&
                range.startContainer.childNodes[range.endOffset].nodeName === 'TABLE') {
                range.startContainer.insertBefore(node, range.startContainer.childNodes[range.endOffset]);
            }
            else if (range.startContainer === range.endContainer && range.startContainer.nodeType !== 3
                && node.firstChild.nodeName === 'HR') {
                Iif (range.startContainer.classList.contains('e-content') || range.startContainer.nodeName === 'BODY') {
                    range.startContainer.appendChild(node);
                }
                else {
                    range.startContainer.parentNode.insertBefore(node, range.startContainer);
                }
            }
            else {
                var blockNode = this.getImmediateBlockNode(nodes[nodes.length - 1], editNode);
                if ((ej2_base_1.isNullOrUndefined(blockNode) || ej2_base_1.isNullOrUndefined(blockNode.parentElement)) && range.endContainer.nodeType !== 3) {
                    blockNode = range.endContainer;
                    range.setEnd(blockNode, range.endContainer.textContent.length);
                }
                if (blockNode && blockNode.nodeName === 'BODY' || blockNode.nodeName === 'DIV' && range.startContainer === range.endContainer && range.startContainer.nodeType === 1) {
                    blockNode = range.startContainer;
                }
                if (blockNode && blockNode.closest('LI') && editNode.contains(blockNode.closest('LI')) && blockNode.nodeName !== 'TD' && blockNode.nodeName !== 'TH' && blockNode.nodeName !== 'TR' && node && node.firstElementChild &&
                    ((node).firstElementChild.tagName === 'OL' || node.firstElementChild.tagName === 'UL')) {
                    var liNode = void 0;
                    while (node.firstElementChild.lastElementChild && node.firstElementChild.lastElementChild.tagName === 'LI') {
                        liNode = node.firstElementChild.lastElementChild;
                        liNode.style.removeProperty('margin-left');
                        liNode.style.removeProperty('margin-top');
                        liNode.style.removeProperty('margin-bottom');
                        node.firstElementChild.insertAdjacentElement('afterend', liNode);
                    }
                }
                if (blockNode && blockNode.nodeName === 'TD' || blockNode.nodeName === 'TH' || blockNode.nodeName === 'TR') {
                    var tempSpan = ej2_base_1.createElement('span', { className: 'tempSpan' });
                    range.insertNode(tempSpan);
                    tempSpan.parentNode.replaceChild(node, tempSpan);
                }
                else {
                    var nodeSelection = new index_1.NodeSelection(editNode);
                    var currentNode = this.getNodeCollection(range, nodeSelection, node)[this.getNodeCollection(range, nodeSelection, node).length - 1];
                    var splitedElm = void 0;
                    if (currentNode && ((currentNode.nodeName === 'BR' || currentNode.nodeName === 'HR' ||
                        (currentNode.nodeName === '#text' && !ej2_base_1.isNullOrUndefined(currentNode.parentElement) && currentNode.parentElement.nodeName === 'LI')) &&
                        (!ej2_base_1.isNullOrUndefined(currentNode.parentElement) && currentNode.parentElement.textContent.trim().length === 0))) {
                        splitedElm = currentNode;
                        Iif (currentNode.parentElement.nodeName === 'LI' && !ej2_base_1.isNullOrUndefined(currentNode.nextSibling) &&
                            currentNode.nextSibling.nodeName === 'BR') {
                            ej2_base_1.detach(currentNode.nextSibling);
                        }
                        if (currentNode.parentElement.nodeName === 'LI' && currentNode.parentElement.textContent === '') {
                            this.removeListfromPaste(range);
                            range.insertNode(node);
                            this.contentsDeleted = true;
                            return;
                        }
                    }
                    else if (currentNode && ((currentNode.nodeName === '#text' || currentNode.nodeName === 'BR') && !ej2_base_1.isNullOrUndefined(currentNode.parentElement) &&
                        (currentNode.parentElement.nodeName === 'LI' || currentNode.parentElement.closest('LI') || (blockNode === editNode && currentNode.parentElement === blockNode)) &&
                        currentNode.parentElement.textContent.trim().length > 0)) {
                        splitedElm = currentNode;
                        if (currentNode.parentElement.nodeName === 'LI' && !ej2_base_1.isNullOrUndefined(currentNode.nextSibling) &&
                            currentNode.nextSibling.nodeName === 'BR') {
                            ej2_base_1.detach(currentNode.nextSibling);
                        }
                        if (!range.collapsed) {
                            var startContainer = range.startContainer;
                            var startOffset = range.startOffset;
                            this.removeListfromPaste(range);
                            range.setStart(startContainer, startOffset);
                            range.setEnd(startContainer, startOffset);
                        }
                        range.insertNode(node);
                        this.contentsDeleted = true;
                        return;
                    }
                    else {
                        splitedElm = nodeCutter.GetSpliceNode(range, blockNode);
                    }
                    splitedElm.parentNode.replaceChild(node, splitedElm);
                }
            }
        };
        InsertHtml.cursorPos = function (lastSelectionNode, node, nodeSelection, docElement, editNode, enterAction) {
            lastSelectionNode.classList.add('lastNode');
            editNode.innerHTML = util_1.updateTextNode(editNode.innerHTML, enterAction);
            lastSelectionNode = editNode.querySelector('.lastNode');
            Eif (!ej2_base_1.isNullOrUndefined(lastSelectionNode)) {
                this.placeCursorEnd(lastSelectionNode, node, nodeSelection, docElement, editNode);
                lastSelectionNode.classList.remove('lastNode');
                if (lastSelectionNode.classList.length === 0) {
                    lastSelectionNode.removeAttribute('class');
                }
            }
        };
        InsertHtml.imageFocus = function (node, nodeSelection, docElement) {
            var focusNode = document.createTextNode(' ');
            if (node.parentNode && node.parentNode.nodeName === 'A') {
                var anchorTag = node.parentNode;
                var parentNode = anchorTag.parentNode;
                parentNode.insertBefore(focusNode, anchorTag.nextSibling);
                parentNode.insertBefore(node, focusNode);
            }
            else {
                node.parentNode.insertBefore(focusNode, node.nextSibling);
            }
            nodeSelection.setSelectionText(docElement, node.nextSibling, node.nextSibling, 0, 0);
        };
        InsertHtml.getImmediateBlockNode = function (node, editNode) {
            while (node && CONSTANT.BLOCK_TAGS.indexOf(node.nodeName.toLocaleLowerCase()) < 0) {
                node = node.parentNode;
            }
            return node;
        };
        InsertHtml.removingComments = function (elm) {
            var innerElement = elm.innerHTML;
            innerElement = innerElement.replace(/<!--[\s\S]*?-->/g, '');
            elm.innerHTML = innerElement;
        };
        InsertHtml.findDetachEmptyElem = function (element, ignoreBlockNodes) {
            Iif (ignoreBlockNodes === void 0) { ignoreBlockNodes = false; }
            var removableElement;
            if (!ej2_base_1.isNullOrUndefined(element.parentElement)) {
                var hasNbsp = element.parentElement.textContent.length > 0 && element.parentElement.textContent.match(/\u00a0/g)
                    && element.parentElement.textContent.match(/\u00a0/g).length > 0;
                if (!hasNbsp && element.parentElement.textContent.trim() === '' && element.parentElement.contentEditable !== 'true' &&
                    ej2_base_1.isNullOrUndefined(element.parentElement.querySelector('img')) && element.parentElement.nodeName !== 'TD' && element.parentElement.nodeName !== 'TH') {
                    removableElement = ignoreBlockNodes && CONSTANT.BLOCK_TAGS.indexOf(element.parentElement.tagName.toLowerCase()) !== -1 ?
                        element : this.findDetachEmptyElem(element.parentElement, ignoreBlockNodes);
                }
                else {
                    removableElement = ignoreBlockNodes && CONSTANT.BLOCK_TAGS.indexOf(element.tagName.toLowerCase()) !== -1 ? null :
                        element;
                }
            }
            else {
                removableElement = null;
            }
            return removableElement;
        };
        InsertHtml.removeEmptyElements = function (element, ignoreBlockNodes) {
            if (ignoreBlockNodes === void 0) { ignoreBlockNodes = false; }
            var emptyElements = element.querySelectorAll(':empty');
            var nonSvgEmptyElements = Array.from(emptyElements).filter(function (element) {
                return !element.closest('svg') && !element.closest('canvas');
            });
            for (var i = 0; i < nonSvgEmptyElements.length; i++) {
                var lineWithDiv = true;
                if (nonSvgEmptyElements[i].tagName === 'DIV') {
                    lineWithDiv = nonSvgEmptyElements[i].style.borderBottom === 'none' ||
                        nonSvgEmptyElements[i].style.borderBottom === '' ? true : false;
                }
                if (CONSTANT.SELF_CLOSING_TAGS.indexOf(nonSvgEmptyElements[i].tagName.toLowerCase()) < 0 && lineWithDiv) {
                    var detachableElement = this.findDetachEmptyElem(nonSvgEmptyElements[i], ignoreBlockNodes);
                    if (!ej2_base_1.isNullOrUndefined(detachableElement)) {
                        ej2_base_1.detach(detachableElement);
                    }
                }
            }
        };
        InsertHtml.closestEle = function (element, editNode) {
            var el = element;
            if (ej2_base_1.closest(el, 'li')) {
                return ej2_base_1.closest(el, 'li');
            }
            while (el && el.nodeType === 1) {
                if (el.parentNode === editNode ||
                    (!ej2_base_1.isNullOrUndefined(el.parentNode.tagName) &&
                        CONSTANT.IGNORE_BLOCK_TAGS.indexOf(el.parentNode.tagName.toLocaleLowerCase()) !== -1)) {
                    return el;
                }
                el = el.parentNode;
            }
            return null;
        };
        InsertHtml.insertTableInList = function (range, insertNode, parentNode, currentNode, nodeCutter) {
            Eif (range.collapsed) {
                var isStart = range.startOffset === 0;
                var isEnd = range.startContainer.textContent.trimEnd().length === range.startOffset;
                if (isStart || isEnd) {
                    Iif (isStart) {
                        insert_methods_1.InsertMethods.AppendBefore(insertNode, currentNode, false);
                    }
                    else {
                        insert_methods_1.InsertMethods.AppendBefore(insertNode, currentNode, true);
                    }
                }
                else {
                    var preNode = nodeCutter.SplitNode(range, parentNode, true);
                    var sibNode = preNode.previousSibling;
                    sibNode.appendChild(insertNode);
                }
            }
            else {
                range.deleteContents();
                parentNode.appendChild(insertNode);
            }
            insertNode.classList.add('ignore-table');
        };
        InsertHtml.alignCheck = function (editNode) {
            var spanAligns = editNode.querySelectorAll('span[style*="text-align"]');
            for (var i = 0; i < spanAligns.length; i++) {
                var spanAlign = spanAligns[i];
                Eif (spanAlign) {
                    var blockAlign = this.getImmediateBlockNode(spanAlign, null);
                    Eif (blockAlign) {
                        var totalSpanText = '';
                        for (var j = 0; j < spanAligns.length; j++) {
                            var span = spanAligns[j];
                            if (blockAlign.contains(span)) {
                                totalSpanText += span.textContent;
                            }
                        }
                        if (blockAlign.textContent.trim() === totalSpanText.trim()) {
                            blockAlign.style.textAlign = spanAlign.style.textAlign;
                        }
                    }
                }
            }
        };
        InsertHtml.removeListfromPaste = function (range) {
            range.deleteContents();
            var value = range.startContainer;
            if (!ej2_base_1.isNullOrUndefined(value) && value.nodeName === 'LI' && !ej2_base_1.isNullOrUndefined(value.parentElement) && (value.parentElement.nodeName === 'OL' || value.parentElement.nodeName === 'UL') && value.textContent.trim() === '') {
                value.parentElement.querySelectorAll('li').forEach(function (item) {
                    Iif (item.textContent.trim() === '' && item !== value) {
                        item.remove();
                    }
                });
            }
        };
        InsertHtml.inlineNode = ['a', 'abbr', 'acronym', 'audio', 'b', 'bdi', 'bdo', 'big', 'br', 'button',
            'canvas', 'cite', 'code', 'data', 'datalist', 'del', 'dfn', 'em', 'embed', 'font', 'i', 'iframe', 'img', 'input',
            'ins', 'kbd', 'label', 'map', 'mark', 'meter', 'noscript', 'object', 'output', 'picture', 'progress',
            'q', 'ruby', 's', 'samp', 'script', 'select', 'slot', 'small', 'span', 'strong', 'sub', 'sup', 'svg',
            'template', 'textarea', 'time', 'u', 'tt', 'var', 'video', 'wbr'];
        InsertHtml.contentsDeleted = false;
        return InsertHtml;
    }());
    exports.InsertHtml = InsertHtml;
});