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

93.27% Statements 97/104
96.12% Branches 99/103
100% Functions 11/11
93.27% Lines 97/104
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   2819× 2819× 2819×   916× 916× 916×   1958× 1948× 1948× 1948× 1948× 1948× 1948× 1948× 1034× 1034× 1034× 603×   603× 154× 154×                         914×   914× 914× 491×   491× 221× 219×             1948×     10×     1089× 1089× 1089× 1089× 503×                   1089×   2609× 2609×   1608×     1001×   2609× 295×   2314× 366×   1948× 23×   2609×   70× 70× 70× 232×   70×   86× 86× 86× 86× 86× 86× 332× 228×   332×     332× 103×   332×   86× 86×       51× 51×     35× 35× 35×   35× 35×   86×     515×        
define(["require", "exports", "./../../selection/index", "@syncfusion/ej2-base", "./insert-methods"], function (require, exports, index_1, ej2_base_1, insert_methods_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var NodeCutter = (function () {
        function NodeCutter() {
            this.enterAction = 'P';
            this.position = -1;
            this.nodeSelection = new index_1.NodeSelection();
        }
        NodeCutter.prototype.GetSpliceNode = function (range, node) {
            node = this.SplitNode(range, node, true);
            node = this.SplitNode(range, node, false);
            return node;
        };
        NodeCutter.prototype.SplitNode = function (range, node, isCollapsed) {
            if (node) {
                var clone = range.cloneRange();
                var parent_1 = node.parentNode;
                var index = this.nodeSelection.getIndex(node);
                clone.collapse(isCollapsed);
                (isCollapsed) ? clone.setStartBefore(node) : clone.setEndAfter(node);
                var fragment = clone.extractContents();
                if (isCollapsed) {
                    node = parent_1.childNodes[index];
                    fragment = this.spliceEmptyNode(fragment, false);
                    if (fragment && fragment.childNodes.length > 0) {
                        var isEmpty = (fragment.childNodes.length === 1 && fragment.childNodes[0].nodeName !== 'IMG' && !(fragment.querySelectorAll('img').length > 0)
                            && this.isRteElm(fragment) && fragment.textContent.trim() === '' && fragment.textContent !== ' ' && fragment.textContent.charCodeAt(0) !== 160) ? true : false;
                        if (!isEmpty) {
                            Eif (node) {
                                insert_methods_1.InsertMethods.AppendBefore(fragment, node);
                            }
                            else {
                                parent_1.appendChild(fragment);
                                var divNode = document.createElement('div');
                                divNode.innerHTML = '';
                                node = divNode.firstChild;
                                parent_1.appendChild(node);
                            }
                        }
                    }
                }
                else {
                    node = parent_1.childNodes.length > 1 ? parent_1.childNodes[index] :
                        parent_1.childNodes[0];
                    fragment = this.spliceEmptyNode(fragment, true);
                    if (fragment && fragment.childNodes.length > 0) {
                        var isEmpty = (fragment.childNodes.length === 1 && fragment.childNodes[0].nodeName !== 'IMG'
                            && this.isRteElm(fragment) && fragment.textContent.trim() === '' && fragment.textContent !== ' ' && fragment.textContent.charCodeAt(0) !== 160) ? true : false;
                        if (!isEmpty) {
                            if (node) {
                                insert_methods_1.InsertMethods.AppendBefore(fragment, node, true);
                            }
                            else {
                                parent_1.appendChild(fragment);
                                var divNode = document.createElement('div');
                                divNode.innerHTML = '';
                                parent_1.insertBefore(divNode.firstChild, parent_1.firstChild);
                                node = parent_1.firstChild;
                            }
                        }
                    }
                }
                return node;
            }
            else {
                return null;
            }
        };
        NodeCutter.prototype.isRteElm = function (fragment) {
            var result = true;
            Eif (fragment.childNodes.length === 1 && fragment.childNodes[0].nodeName !== 'IMG') {
                var firstChild = fragment.childNodes[0];
                for (var i = 0; !ej2_base_1.isNullOrUndefined(firstChild.childNodes) && i < firstChild.childNodes.length; i++) {
                    if (firstChild.childNodes[i].nodeName === 'IMG' || (firstChild.childNodes[i].nodeName === 'SPAN' &&
                        (firstChild.childNodes[i].classList.contains('e-video-wrap') ||
                            firstChild.childNodes[i].classList.contains('e-embed-video-wrap') ||
                            firstChild.childNodes[i].classList.contains('e-audio-wrap')))) {
                        result = false;
                    }
                }
            }
            else {
                result = true;
            }
            return result;
        };
        NodeCutter.prototype.spliceEmptyNode = function (fragment, isStart) {
            var len;
            if (fragment.childNodes.length === 1 && fragment.childNodes[0].nodeName === '#text' &&
                fragment.childNodes[0].textContent === '' || fragment.textContent === '') {
                len = -1;
            }
            else {
                len = fragment.childNodes.length - 1;
            }
            if (len > -1 && !isStart) {
                this.spliceEmptyNode(fragment.childNodes[len], isStart);
            }
            else if (len > -1) {
                this.spliceEmptyNode(fragment.childNodes[0], isStart);
            }
            else if (fragment.nodeType !== 3 && fragment.nodeType !== 11 && fragment.nodeName !== 'IMG' && !(fragment.querySelectorAll('img').length > 0) && !(fragment.classList.contains('e-video-wrap')) && !(fragment.classList.contains('e-audio-wrap'))) {
                fragment.parentNode.removeChild(fragment);
            }
            return fragment;
        };
        NodeCutter.prototype.GetCursorStart = function (indexes, index, isStart) {
            indexes = (isStart) ? indexes : indexes.reverse();
            var position = indexes[0];
            for (var num = 0; num < indexes.length && ((isStart) ? (indexes[num] < index) : (indexes[num] >= index)); num++) {
                position = indexes[num];
            }
            return position;
        };
        NodeCutter.prototype.GetCursorRange = function (docElement, range, node) {
            var cursorRange = docElement.createRange();
            var indexes = [];
            indexes.push(0);
            var str = this.TrimLineBreak(node.data);
            var index = str.indexOf(' ', 0);
            while (index !== -1) {
                if (indexes.indexOf(index) < 0) {
                    indexes.push(index);
                }
                Iif (new RegExp('\\s').test(str[index - 1]) && (indexes.indexOf(index - 1) < 0)) {
                    indexes.push(index - 1);
                }
                if (new RegExp('\\s').test(str[index + 1])) {
                    indexes.push(index + 1);
                }
                index = str.indexOf(' ', (index + 1));
            }
            indexes.push(str.length);
            if ((indexes.indexOf(range.startOffset) >= 0)
                || ((indexes.indexOf(range.startOffset - 1) >= 0) && (range.startOffset !== 1
                    || (range.startOffset === 1 && new RegExp('\\s').test(str[0])))
                    || (((indexes[indexes.length - 1] - 1) === range.startOffset) && range.endOffset !== (str.length - 1) && !new RegExp('\\s').test(str[0])))) {
                cursorRange = range;
                this.position = 1;
            }
            else {
                var startOffset = this.GetCursorStart(indexes, range.startOffset, true);
                this.position = range.startOffset - startOffset;
                if (startOffset !== 0 && str[startOffset] && str[startOffset] === ' ') {
                    startOffset = startOffset + 1;
                }
                cursorRange.setStart(range.startContainer, startOffset);
                cursorRange.setEnd(range.startContainer, this.GetCursorStart(indexes, range.startOffset, false));
            }
            return cursorRange;
        };
        NodeCutter.prototype.GetCursorNode = function (docElement, range, node) {
            return this.GetSpliceNode(this.GetCursorRange(docElement, range, node), node);
        };
        NodeCutter.prototype.TrimLineBreak = function (line) {
            return line.replace(/(\r\n\t|\n|\r\t)/gm, ' ');
        };
        return NodeCutter;
    }());
    exports.NodeCutter = NodeCutter;
});