all files / selection/ selection.js

98.99% Statements 196/198
95% Branches 133/140
100% Functions 35/35
98.99% Lines 196/198
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   20363× 20363× 20363×   9728× 9728× 9728× 9728× 9728× 9728× 9728× 9728× 9728×   9728×   21749× 21749× 21749×         31295×   9728× 9728×   181952× 181952× 181952× 181952× 86596× 316072× 316072× 316072× 316072×     181952×   10747× 14462× 151×     10596×   27759× 220×   27539×     2037×   25502× 12×   25490× 9854×   15636× 6288×   9348× 17×     9331×     10700×   10700×   10700× 10700× 78× 78×     10622×   8336×   2286×     2286× 2286× 27759× 19883×   27759×   2286×   3735×   3735× 3735× 8891×       4561× 4561×   4330× 2054×     3735×   173×   1023×   211× 211× 211× 520×   311× 311×     211×   1023× 1023× 1023× 4847×     2832× 2832×     1023×   347×   401× 401× 1793×       790× 790×     401×   21124× 21124× 21124× 179742×   179738× 179738× 179738×   21124×   6446× 6446× 6446× 6446× 55310×   6446× 6366×     6366×   6446×   3223× 3223× 3223× 3223× 3223×   3224× 3224×   5346× 5346× 5346×   1612× 1612× 1612× 1612×   227× 227× 227×   100× 100× 100×   702×   446×     426× 426× 426× 426× 426× 426×   10700× 10700× 10700× 10700×     10700× 41×     10659×     10700× 10700× 10700× 10700×     10700× 45×     10655×     10700× 10700× 10700×        
define(["require", "exports", "@syncfusion/ej2-base"], function (require, exports, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var NodeSelection = (function () {
        function NodeSelection(editElement) {
            this.startNodeName = [];
            this.endNodeName = [];
            this.editableElement = editElement;
        }
        NodeSelection.prototype.saveInstance = function (range, body) {
            this.range = range.cloneRange();
            this.rootNode = this.documentFromRange(range);
            this.body = body;
            this.startContainer = this.getNodeArray(range.startContainer, true);
            this.endContainer = this.getNodeArray(range.endContainer, false);
            this.startOffset = range.startOffset;
            this.endOffset = range.endOffset;
            this.html = this.body.innerHTML;
            return this;
        };
        NodeSelection.prototype.documentFromRange = function (range) {
            return (9 === range.startContainer.nodeType) ? range.startContainer : range.startContainer.ownerDocument;
        };
        NodeSelection.prototype.getRange = function (docElement) {
            var select = this.get(docElement);
            var range = select && select.rangeCount > 0 ? select.getRangeAt(select.rangeCount - 1) : docElement.createRange();
            return (range.startContainer !== docElement || range.endContainer !== docElement
                || range.startOffset || range.endOffset || (range.setStart(docElement.body, 0),
                range.collapse(!0)),
                range);
        };
        NodeSelection.prototype.get = function (docElement) {
            return docElement.defaultView.getSelection();
        };
        NodeSelection.prototype.save = function (range, docElement) {
            range = (range) ? range.cloneRange() : this.getRange(docElement);
            return this.saveInstance(range, docElement.body);
        };
        NodeSelection.prototype.getIndex = function (node) {
            var index;
            var num = 0;
            node = !node.previousSibling && node.tagName === 'BR' ? node : node.previousSibling;
            if (node) {
                for (var type = node.nodeType; node; null) {
                    index = node.nodeType;
                    num++;
                    type = index;
                    node = node.previousSibling;
                }
            }
            return num;
        };
        NodeSelection.prototype.isChildNode = function (nodeCollection, parentNode) {
            for (var index = 0; index < parentNode.childNodes.length; index++) {
                if (nodeCollection.indexOf(parentNode.childNodes[index]) > -1) {
                    return true;
                }
            }
            return false;
        };
        NodeSelection.prototype.getNode = function (startNode, endNode, nodeCollection) {
            if (this.editableElement && (!this.editableElement.contains(startNode) || this.editableElement === startNode)) {
                return null;
            }
            if (endNode === startNode &&
                (startNode.nodeType === 3 || !startNode.firstChild || nodeCollection.indexOf(startNode.firstChild) !== -1
                    || this.isChildNode(nodeCollection, startNode))) {
                return null;
            }
            if (startNode.nodeType === 3 && startNode.previousSibling === endNode && endNode.nodeName === 'IMG') {
                return null;
            }
            if (nodeCollection.indexOf(startNode.firstChild) === -1 && startNode.firstChild && !this.isChildNode(nodeCollection, startNode)) {
                return startNode.firstChild;
            }
            if (startNode.nextSibling) {
                return startNode.nextSibling;
            }
            if (!startNode.parentNode) {
                return null;
            }
            else {
                return startNode.parentNode;
            }
        };
        NodeSelection.prototype.getNodeCollection = function (range) {
            var startNode = range.startContainer.childNodes[range.startOffset]
                || range.startContainer;
            var endNode = range.endContainer.childNodes[(range.endOffset > 0) ? (range.endOffset - 1) : range.endOffset]
                || range.endContainer;
            var tableCursor = this.processedTableImageCursor(range);
            if (tableCursor.start || tableCursor.end) {
                Eif (tableCursor.startName === 'TABLE' || tableCursor.endName === 'TABLE') {
                    return [];
                }
            }
            if ((startNode === endNode || (startNode.nodeName === 'BR' && startNode === range.endContainer.childNodes[range.endOffset])) &&
                startNode.childNodes.length === 0) {
                return [startNode];
            }
            Iif (range.startOffset === range.endOffset && range.startOffset !== 0 && range.startContainer.nodeName === 'PRE') {
                return [startNode.nodeName === 'BR' || startNode.nodeName === '#text' ? startNode : startNode.childNodes[0]];
            }
            var nodeCollection = [];
            do {
                if (nodeCollection.indexOf(startNode) === -1) {
                    nodeCollection.push(startNode);
                }
                startNode = this.getNode(startNode, endNode, nodeCollection);
            } while (startNode);
            return nodeCollection;
        };
        NodeSelection.prototype.getParentNodeCollection = function (range) {
            return this.getParentNodes(this.getNodeCollection(range), range);
        };
        NodeSelection.prototype.getParentNodes = function (nodeCollection, range) {
            nodeCollection = nodeCollection.reverse();
            for (var index = 0; index < nodeCollection.length; index++) {
                if ((nodeCollection.indexOf(nodeCollection[index].parentNode) !== -1)
                    || (nodeCollection[index].nodeType === 3 &&
                        range.startContainer !== range.endContainer &&
                        range.startContainer.parentNode !== range.endContainer.parentNode)) {
                    nodeCollection.splice(index, 1);
                    index--;
                }
                else if (nodeCollection[index].nodeType === 3) {
                    nodeCollection[index] = nodeCollection[index].parentNode;
                }
            }
            return nodeCollection;
        };
        NodeSelection.prototype.getSelectionNodeCollection = function (range) {
            return this.getSelectionNodes(this.getNodeCollection(range));
        };
        NodeSelection.prototype.getSelectionNodeCollectionBr = function (range) {
            return this.getSelectionNodesBr(this.getNodeCollection(range));
        };
        NodeSelection.prototype.getSelectionNodes = function (nodeCollection) {
            nodeCollection = nodeCollection.reverse();
            var regEx = new RegExp('\u200B', 'g');
            for (var index = 0; index < nodeCollection.length; index++) {
                if (nodeCollection[index].nodeType !== 3 || (nodeCollection[index].textContent.trim() === '' ||
                    (nodeCollection[index].textContent.length === 1 && nodeCollection[index].textContent.match(regEx)))) {
                    nodeCollection.splice(index, 1);
                    index--;
                }
            }
            return nodeCollection.reverse();
        };
        NodeSelection.prototype.getSelectionNodesBr = function (nodeCollection) {
            nodeCollection = nodeCollection.reverse();
            var regEx = new RegExp('\u200B', 'g');
            for (var index = 0; index < nodeCollection.length; index++) {
                if (nodeCollection[index].nodeName !== 'BR' &&
                    (nodeCollection[index].nodeType !== 3 || (nodeCollection[index].textContent.trim() === '' ||
                        (nodeCollection[index].textContent.length === 1 && nodeCollection[index].textContent.match(regEx))))) {
                    nodeCollection.splice(index, 1);
                    index--;
                }
            }
            return nodeCollection.reverse();
        };
        NodeSelection.prototype.getInsertNodeCollection = function (range) {
            return this.getInsertNodes(this.getNodeCollection(range));
        };
        NodeSelection.prototype.getInsertNodes = function (nodeCollection) {
            nodeCollection = nodeCollection.reverse();
            for (var index = 0; index < nodeCollection.length; index++) {
                if ((nodeCollection[index].childNodes.length !== 0 &&
                    nodeCollection[index].nodeType !== 3) ||
                    (nodeCollection[index].nodeType === 3 &&
                        nodeCollection[index].textContent === '')) {
                    nodeCollection.splice(index, 1);
                    index--;
                }
            }
            return nodeCollection.reverse();
        };
        NodeSelection.prototype.getNodeArray = function (node, isStart, root) {
            var array = [];
            ((isStart) ? (this.startNodeName = []) : (this.endNodeName = []));
            for (; node !== (root ? root : this.rootNode); null) {
                if (ej2_base_1.isNullOrUndefined(node)) {
                    break;
                }
                (isStart) ? this.startNodeName.push(node.nodeName.toLowerCase()) : this.endNodeName.push(node.nodeName.toLowerCase());
                array.push(this.getIndex(node));
                node = node.parentNode;
            }
            return array;
        };
        NodeSelection.prototype.setRangePoint = function (range, isvalid, num, size) {
            var node = this.rootNode;
            var index = num.length;
            var constant = size;
            for (; index--; null) {
                node = node && node.childNodes[num[index]];
            }
            if (node && constant >= 0 && node.nodeName !== 'html') {
                Iif (node.nodeType === 3 && node.nodeValue.replace(/\u00a0/g, '&nbsp;') === '&nbsp;') {
                    constant = node.textContent.length;
                }
                range[isvalid ? 'setStart' : 'setEnd'](node, constant);
            }
            return range;
        };
        NodeSelection.prototype.restore = function () {
            var range = this.range.cloneRange();
            range = this.setRangePoint(range, true, this.startContainer, this.startOffset);
            range = this.setRangePoint(range, false, this.endContainer, this.endOffset);
            this.selectRange(this.rootNode, range);
            return range;
        };
        NodeSelection.prototype.selectRange = function (docElement, range) {
            this.setRange(docElement, range);
            this.save(range, docElement);
        };
        NodeSelection.prototype.setRange = function (docElement, range) {
            var selection = this.get(docElement);
            selection.removeAllRanges();
            selection.addRange(range);
        };
        NodeSelection.prototype.setSelectionText = function (docElement, startNode, endNode, startIndex, endIndex) {
            var range = docElement.createRange();
            range.setStart(startNode, startIndex);
            range.setEnd(endNode, endIndex);
            this.setRange(docElement, range);
        };
        NodeSelection.prototype.setSelectionContents = function (docElement, element) {
            var range = docElement.createRange();
            range.selectNode(element);
            this.setRange(docElement, range);
        };
        NodeSelection.prototype.setSelectionNode = function (docElement, element) {
            var range = docElement.createRange();
            range.selectNodeContents(element);
            this.setRange(docElement, range);
        };
        NodeSelection.prototype.getSelectedNodes = function (docElement) {
            return this.getNodeCollection(this.getRange(docElement));
        };
        NodeSelection.prototype.Clear = function (docElement) {
            this.get(docElement).removeAllRanges();
        };
        NodeSelection.prototype.insertParentNode = function (docElement, newNode, range) {
            range.surroundContents(newNode);
            this.selectRange(docElement, range);
        };
        NodeSelection.prototype.setCursorPoint = function (docElement, element, point) {
            var range = docElement.createRange();
            var selection = docElement.defaultView.getSelection();
            range.setStart(element, point);
            range.collapse(true);
            selection.removeAllRanges();
            selection.addRange(range);
        };
        NodeSelection.prototype.isTableOrImageStart = function (range) {
            var customHandlerElements = ['TABLE'];
            var startContainer = range.startContainer;
            var startOffset = range.startOffset;
            var isCursorAtStart = range.collapsed && (startContainer.nodeType === 1) &&
                startContainer.isContentEditable && startContainer.childNodes[startOffset] &&
                (customHandlerElements.indexOf(startContainer.childNodes[startOffset].nodeName) > -1);
            if (isCursorAtStart) {
                return { start: isCursorAtStart, startNodeName: startContainer.childNodes[startOffset].nodeName };
            }
            else {
                return { start: false, startNodeName: '' };
            }
        };
        NodeSelection.prototype.isTableOrImageEnd = function (range) {
            var customHandlerElements = ['TABLE'];
            var startContainer = range.startContainer;
            var startOffset = range.startOffset;
            var isCursorAtEnd = range.collapsed && (startContainer.nodeType === 1) &&
                startContainer.isContentEditable && startContainer.childNodes[startOffset - 1] &&
                (customHandlerElements.indexOf(startContainer.childNodes[startOffset - 1].nodeName) > -1);
            if (isCursorAtEnd) {
                return { end: isCursorAtEnd, endNodeName: startContainer.childNodes[startOffset - 1].nodeName };
            }
            else {
                return { end: false, endNodeName: '' };
            }
        };
        NodeSelection.prototype.processedTableImageCursor = function (range) {
            var _a = this.isTableOrImageStart(range), start = _a.start, startNodeName = _a.startNodeName;
            var _b = this.isTableOrImageEnd(range), end = _b.end, endNodeName = _b.endNodeName;
            return { start: start, startName: startNodeName, end: end, endName: endNodeName };
        };
        return NodeSelection;
    }());
    exports.NodeSelection = NodeSelection;
});