all files / editor-manager/plugin/ insert-methods.js

100% Statements 48/48
100% Branches 22/22
100% Functions 7/7
100% Lines 48/48
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     142× 142× 142× 140×   142×   578× 578× 578×   510× 510× 510× 510× 510× 510× 510× 510× 509× 509× 509× 509× 509×   510× 510× 627×   510× 510× 491× 32× 32×   491× 32× 32×   491× 491× 491× 491×   510×   1226×            
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var InsertMethods = (function () {
        function InsertMethods() {
        }
        InsertMethods.WrapBefore = function (textNode, parentNode, isAfter) {
            parentNode.innerText = textNode.textContent;
            (!isAfter) ? this.AppendBefore(parentNode, textNode) : this.AppendBefore(parentNode, textNode, true);
            if (textNode.parentNode) {
                textNode.parentNode.removeChild(textNode);
            }
            return parentNode.childNodes[0];
        };
        InsertMethods.Wrap = function (childNode, parentNode) {
            this.AppendBefore(parentNode, childNode);
            parentNode.appendChild(childNode);
            return childNode;
        };
        InsertMethods.unwrap = function (node) {
            var parent = node.parentNode;
            var selection = node.ownerDocument.defaultView.getSelection();
            var start = null;
            var startOffset = 0;
            var end = null;
            var endOffset = 0;
            var range;
            if (selection && selection.rangeCount) {
                range = selection.getRangeAt(0);
                start = range.startContainer;
                startOffset = range.startOffset;
                end = range.endContainer;
                endOffset = range.endOffset;
            }
            var child = [];
            for (; node.firstChild; null) {
                child.push(parent.insertBefore(node.firstChild, node));
            }
            parent.removeChild(node);
            if (selection && start && end && child.length > 0) {
                if (start === node) {
                    start = child[Math.min(startOffset, child.length - 1)];
                    startOffset = 0;
                }
                if (end === node) {
                    end = child[Math.min(endOffset, child.length - 1)];
                    endOffset = 0;
                }
                selection.removeAllRanges();
                range.setStart(start, startOffset);
                range.setEnd(end, endOffset);
                selection.addRange(range);
            }
            return child;
        };
        InsertMethods.AppendBefore = function (textNode, parentNode, isAfter) {
            return (parentNode.parentNode) ? ((!isAfter) ? parentNode.parentNode.insertBefore(textNode, parentNode)
                : parentNode.parentNode.insertBefore(textNode, parentNode.nextSibling)) :
                parentNode;
        };
        return InsertMethods;
    }());
    exports.InsertMethods = InsertMethods;
});