all files / block-manager/plugins/common/ node.js

94.74% Statements 54/57
84.38% Branches 27/32
80% Functions 4/5
96.36% Lines 53/55
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     157× 157× 157× 157× 203× 203× 203× 152× 152× 152× 131× 131× 131× 99×   131× 71× 71× 20×   20× 17×   20× 20×   71×   131×     21×     51× 51× 51× 51× 51× 35× 34× 34× 34×           35×     16×       16× 16× 17× 17×           157× 168× 168× 152×     16×     157×        
define(["require", "exports", "../../../common/utils/common", "../../../common/constant"], function (require, exports, common_1, constants) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var NodeCutter = (function () {
        function NodeCutter() {
        }
        NodeCutter.splitContent = function (contentElement, splitNode, splitOffset) {
            var beforeFragment = document.createDocumentFragment();
            var afterFragment = document.createDocumentFragment();
            var isSplitting = false;
            var processNode = function (node, container, parentChain, isAfter) {
                Iif (parentChain === void 0) { parentChain = []; }
                if (isAfter === void 0) { isAfter = false; }
                if (node.nodeType === Node.TEXT_NODE) {
                    var textNode = node;
                    var fullText = textNode.textContent;
                    if (!isAfter && node === splitNode) {
                        var beforeText = fullText.slice(0, splitOffset);
                        var afterText = fullText.slice(splitOffset);
                        if (beforeText) {
                            container.appendChild(document.createTextNode(beforeText));
                        }
                        if (afterText) {
                            var afterNode = document.createTextNode(afterText);
                            for (var i = parentChain.length - 1; i >= 0; i--) {
                                var cloned = parentChain[i]
                                    .cloneNode(false);
                                if (i === 0) {
                                    cloned.id = common_1.generateUniqueId(constants.CONTENT_ID_PREFIX);
                                }
                                cloned.appendChild(afterNode);
                                afterNode = cloned;
                            }
                            afterFragment.appendChild(afterNode);
                        }
                        isSplitting = true;
                    }
                    else {
                        container.appendChild(document.createTextNode(fullText));
                    }
                }
                else Eif (node.nodeType === Node.ELEMENT_NODE) {
                    var element = node;
                    var clone = element.cloneNode(false);
                    var newParentChain = parentChain.concat([element]);
                    if (!isAfter && (element.contains(splitNode) || element === splitNode)) {
                        for (var _i = 0, _a = Array.from(element.childNodes); _i < _a.length; _i++) {
                            var child = _a[_i];
                            Eif (!isSplitting && (child.contains(splitNode) || child === splitNode)) {
                                processNode(child, clone, newParentChain);
                            }
                            else {
                                processNode(child, clone, newParentChain, isSplitting);
                            }
                        }
                        container.appendChild(clone);
                    }
                    else {
                        Iif (isAfter) {
                            container.appendChild(element.cloneNode(true));
                        }
                        else {
                            container.appendChild(clone);
                            for (var _b = 0, _c = Array.from(element.childNodes); _b < _c.length; _b++) {
                                var child = _c[_b];
                                processNode(child, clone, newParentChain, isAfter);
                            }
                        }
                    }
                }
            };
            for (var _i = 0, _a = Array.from(contentElement.childNodes); _i < _a.length; _i++) {
                var node = _a[_i];
                if (!isSplitting) {
                    processNode(node, beforeFragment, []);
                }
                else {
                    afterFragment.appendChild(node.cloneNode(true));
                }
            }
            return { beforeFragment: beforeFragment, afterFragment: afterFragment, splitOffset: splitOffset };
        };
        return NodeCutter;
    }());
    exports.NodeCutter = NodeCutter;
});