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

99.31% Statements 144/145
94.17% Branches 97/103
100% Functions 9/9
99.31% Lines 144/145
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   4363× 4363× 4363× 4363×   1212× 1212× 1190×   22× 22× 22×   20× 40×   20× 20×   44× 44×   42× 108× 108× 66×   42×   18×   12×     11×       24×       231× 231× 221×   10× 10× 10×   18×     100× 100×   98×   92× 126× 126× 44×   82× 82×       21× 21× 44× 44× 44×     37× 37×       21× 128× 128× 43× 43×   26×         37× 37× 37× 37×   35× 148× 12× 12× 12×   12× 12×   136× 97× 97×       97×     97× 13×     84×       39× 14×       25× 15×         14×     10× 10× 10× 10×   10×     35×        
define(["require", "exports", "@syncfusion/ej2-base", "../../common/util"], function (require, exports, ej2_base_1, util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var TableSelection = (function () {
        function TableSelection(root, currentDocument) {
            this.BLOCK_TAGS = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'li', 'pre', 'td', 'th', 'div', 'hr', 'section', 'figure'];
            this.BASIC_FORMATS = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'pre'];
            this.root = root;
            this.currentDocument = currentDocument;
        }
        TableSelection.prototype.getBlockNodes = function () {
            var blockNodes = [];
            if (ej2_base_1.isNullOrUndefined(this.root.querySelector('.e-cell-select'))) {
                return blockNodes;
            }
            var currentTable = this.root.querySelector('.e-cell-select').closest('table');
            var cellSelectNode = currentTable.querySelectorAll('.e-cell-select');
            if (ej2_base_1.isNullOrUndefined(cellSelectNode) || cellSelectNode.length < 2) {
                return blockNodes;
            }
            for (var i = 0; i < cellSelectNode.length; i++) {
                this.addBlockNodes(cellSelectNode[i], blockNodes);
            }
            this.wrapParagraphNodes(blockNodes);
            return blockNodes;
        };
        TableSelection.prototype.addBlockNodes = function (node, blockNodes) {
            var nodes = node.childNodes;
            if (nodes.length === 0) {
                blockNodes.push(node);
                return;
            }
            for (var j = 0; j < nodes.length; j++) {
                var currentNode = nodes[j];
                if (blockNodes.indexOf(currentNode.parentElement) >= 0) {
                    continue;
                }
                if (currentNode.parentElement && (currentNode.parentElement.nodeName === 'TD' || currentNode.parentElement.nodeName === 'TH')
                    && currentNode.parentElement.childNodes.length === 1) {
                    if (currentNode.nodeName === 'BR') {
                        blockNodes.push(currentNode.parentElement);
                    }
                    else if (currentNode.nodeType === Node.TEXT_NODE) {
                        blockNodes.push(currentNode.parentElement);
                    }
                    else {
                        blockNodes.push(currentNode.parentElement);
                    }
                }
                else {
                    blockNodes.push(currentNode.parentElement);
                }
            }
        };
        TableSelection.prototype.getTextNodes = function () {
            var textNodes = [];
            if (ej2_base_1.isNullOrUndefined(this.root.querySelector('.e-cell-select'))) {
                return textNodes;
            }
            var currentTable = this.root.querySelector('.e-cell-select').closest('table');
            var cellSelectNode = currentTable.querySelectorAll('.e-cell-select');
            if (ej2_base_1.isNullOrUndefined(cellSelectNode) || cellSelectNode.length < 2) {
                return textNodes;
            }
            for (var i = 0; i < cellSelectNode.length; i++) {
                this.addTextNodes(cellSelectNode[i], textNodes);
            }
            return textNodes;
        };
        TableSelection.prototype.addTextNodes = function (parent, textNodes) {
            var nodes = parent.childNodes;
            if (nodes.length === 0 && (parent.nodeName === 'TD' || parent.nodeName === 'TH')) {
                var text = this.currentDocument.createTextNode('\u200B');
                parent.appendChild(text);
                textNodes.push(text);
                return;
            }
            else if (nodes.length === 1 && (parent.nodeName === 'TD' || parent.nodeName === 'TH') && nodes[0].nodeName === 'BR') {
                var text = this.currentDocument.createTextNode('\u200B');
                parent.insertBefore(text, nodes[0]);
                textNodes.push(text);
                return;
            }
            for (var j = 0; j < nodes.length; j++) {
                var currentNode = nodes[j];
                if (currentNode.nodeType === Node.TEXT_NODE) {
                    textNodes.push(currentNode);
                }
                else Eif (currentNode.nodeType === Node.ELEMENT_NODE) {
                    this.addTextNodes(currentNode, textNodes);
                }
            }
        };
        TableSelection.prototype.wrapParagraphNodes = function (blockNodes) {
            var blockNodesArry = Array.from(blockNodes);
            for (var i = 0; i < blockNodesArry.length; i++) {
                var node = blockNodesArry[i];
                Eif (node.nodeName === 'TD' || node.nodeName === 'TH') {
                    if (node.childNodes.length === 1 && (node.childNodes[0].nodeName === 'BR' || node.childNodes[0].nodeType === Node.TEXT_NODE)) {
                        var childNode = node.childNodes[0];
                        var paragraph = ej2_base_1.createElement('p');
                        childNode.parentElement.insertBefore(paragraph, childNode);
                        paragraph.appendChild(childNode);
                        var index = blockNodes.indexOf(node);
                        blockNodes[index] = paragraph;
                    }
                    else {
                        var newIndex = blockNodes.indexOf(node);
                        this.wrapInlineNodes(node, blockNodes, newIndex);
                    }
                }
            }
            for (var i = 0; i < blockNodes.length; i++) {
                var currentNode = blockNodes[i];
                if (currentNode.nodeName === 'LI' && currentNode.childNodes.length === 1) {
                    var firstChild = currentNode.childNodes[0];
                    if (firstChild.nodeType === Node.ELEMENT_NODE && this.BASIC_FORMATS.indexOf(firstChild.nodeName.toLocaleLowerCase()) >= 0
                        && firstChild.textContent === currentNode.textContent) {
                        blockNodes[i] = firstChild;
                    }
                }
            }
        };
        TableSelection.prototype.wrapInlineNodes = function (node, blockNodes, index) {
            var child = node.childNodes[0];
            var wrapperElement = ej2_base_1.createElement('p');
            var tempBlockNodes = [];
            if (ej2_base_1.isNullOrUndefined(child)) {
                node.appendChild(wrapperElement);
                tempBlockNodes.push(wrapperElement);
                util_1.insertItemsAtIndex(blockNodes, tempBlockNodes, index);
                return;
            }
            while (child) {
                if (child.nodeName === 'BR') {
                    child.parentNode.insertBefore(wrapperElement, child);
                    wrapperElement.appendChild(child);
                    if (wrapperElement.childNodes.length > 0 && tempBlockNodes.indexOf(wrapperElement) < 0) {
                        tempBlockNodes.push(wrapperElement);
                    }
                    child = wrapperElement.nextSibling;
                    wrapperElement = ej2_base_1.createElement('p');
                }
                else if (this.BLOCK_TAGS.indexOf(child.nodeName.toLocaleLowerCase()) >= 0) {
                    tempBlockNodes.push(child);
                    Iif (wrapperElement.childNodes.length > 0) {
                        child = wrapperElement.nextSibling;
                    }
                    else {
                        if (child && child.nodeName === 'LI' && child.querySelectorAll('li').length > 0) {
                            var listNodes = child.querySelectorAll('li');
                            for (var i = 0; i < listNodes.length; i++) {
                                tempBlockNodes.push(listNodes[i]);
                            }
                        }
                        if (child.nodeName === 'LI' && ej2_base_1.isNullOrUndefined(child.nextSibling)) {
                            child = child.parentElement.nextSibling;
                        }
                        else {
                            child = child.nextSibling;
                        }
                    }
                }
                else if (child.nodeType === Node.TEXT_NODE) {
                    if (child.textContent.trim() === '' && child.textContent.indexOf('\u200B') < 0) {
                        var nextSibling = child.nextSibling;
                        ej2_base_1.detach(child);
                        child = nextSibling;
                        continue;
                    }
                    child.parentNode.insertBefore(wrapperElement, child);
                    var textNode = child;
                    wrapperElement.appendChild(textNode);
                    if (wrapperElement.childNodes.length > 0 && tempBlockNodes.indexOf(wrapperElement) < 0) {
                        tempBlockNodes.push(wrapperElement);
                    }
                    child = wrapperElement.nextSibling;
                }
                else if (child.nodeName === 'TABLE' || child.nodeName === 'UL' || child.nodeName === 'OL') {
                    if (child.nodeName === 'TABLE') {
                        var nestedBlockNodes = [];
                        var cellSelectNode = child.querySelectorAll('td, th');
                        for (var i = 0; i < cellSelectNode.length; i++) {
                            this.addBlockNodes(cellSelectNode[i], nestedBlockNodes);
                        }
                        this.wrapParagraphNodes(nestedBlockNodes);
                        for (var i = 0; i < nestedBlockNodes.length; i++) {
                            tempBlockNodes.push(nestedBlockNodes[i]);
                        }
                        child = child.nextSibling;
                    }
                    else {
                        child = child.firstElementChild;
                    }
                }
                else Eif (this.BLOCK_TAGS.indexOf(child.nodeName.toLocaleLowerCase()) < 0) {
                    child.parentNode.insertBefore(wrapperElement, child);
                    wrapperElement.appendChild(child);
                    if (wrapperElement.childNodes.length > 0 && tempBlockNodes.indexOf(wrapperElement) < 0) {
                        tempBlockNodes.push(wrapperElement);
                    }
                    child = wrapperElement.nextSibling;
                }
            }
            util_1.insertItemsAtIndex(blockNodes, tempBlockNodes, index);
        };
        return TableSelection;
    }());
    exports.TableSelection = TableSelection;
});