all files / selection/ selection.js

99% Statements 199/201
95.07% Branches 135/142
100% Functions 35/35
99% Lines 199/201
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   20522× 20522× 20522×   9787× 9787× 9787× 9787× 9787× 9787× 9787× 9787× 9787×   9787×   21985× 21985× 21985×         31591×   9787× 9787×   182707× 182707× 182707× 182707× 86966× 320049× 320049× 320049× 320049×     182707×   10789× 14588× 157×     10632×   27884× 220×   27664×     2037×   25627× 12×   25615× 9895×   15720× 6317×   9403× 19×     9384×     10801×   10801×   10801× 10801× 86× 86× 86×     10715×   8427×   2288×     2288× 2288× 27884× 19966×   27884×   2288×   3779×   3779× 3779× 9015×       4599× 4599×   4416× 2085×     3779×   173×   1029×   211× 211× 211× 521×   312× 312×     211×   1029× 1029× 1029× 4858×     2835× 2835×     1029×   354×   408× 408× 1802×       792× 792×     408×   21246× 21246× 21246× 180477×   180473× 180473× 180473×   21246×   6476× 6476× 6476× 6476× 55486×   6476× 6396×     6396×   6476×   3238× 3238× 3238× 3238× 3238×   3239× 3239×   5372× 5372× 5372×   1619× 1619× 1619× 1619×   228× 228× 228×   101× 101× 101×   706×   447×     443× 443× 443× 443× 443× 443×   10964× 10964× 10964× 10964× 10964×     10964× 45×     10919×     10964× 10964× 10964× 10964× 10964×     10964× 51×     10913×     10964× 10964× 10964×        
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') {
                    var tableNode = tableCursor.start ? tableCursor.startNode : tableCursor.endNode;
                    return [tableNode];
                }
            }
            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 startNode = startContainer.childNodes[startOffset];
            var isCursorAtStart = range.collapsed && (startContainer.nodeType === 1) &&
                startContainer.isContentEditable && startNode &&
                (customHandlerElements.indexOf(startNode.nodeName) > -1);
            if (isCursorAtStart) {
                return { start: isCursorAtStart, startNodeName: startNode.nodeName, startNode: startNode };
            }
            else {
                return { start: false, startNodeName: '', startNode: undefined };
            }
        };
        NodeSelection.prototype.isTableOrImageEnd = function (range) {
            var customHandlerElements = ['TABLE'];
            var startContainer = range.startContainer;
            var startOffset = range.startOffset;
            var endNode = startContainer.childNodes[startOffset - 1];
            var isCursorAtEnd = range.collapsed && (startContainer.nodeType === 1) &&
                startContainer.isContentEditable && endNode &&
                (customHandlerElements.indexOf(endNode.nodeName) > -1);
            if (isCursorAtEnd) {
                return { end: isCursorAtEnd, endNodeName: endNode.nodeName, endNode: endNode };
            }
            else {
                return { end: false, endNodeName: '', endNode: undefined };
            }
        };
        NodeSelection.prototype.processedTableImageCursor = function (range) {
            var _a = this.isTableOrImageStart(range), start = _a.start, startNodeName = _a.startNodeName, startNode = _a.startNode;
            var _b = this.isTableOrImageEnd(range), end = _b.end, endNodeName = _b.endNodeName, endNode = _b.endNode;
            return { start: start, startName: startNodeName, end: end, endName: endNodeName, startNode: startNode, endNode: endNode };
        };
        return NodeSelection;
    }());
    exports.NodeSelection = NodeSelection;
});