<commit_before>define(['constants/CssConstant'], function (CssConstant) {

    function updateEditable(domElem, textObservable, htmlObservable, homeCss, window, document) {
        if (domElem.textContent == textObservable())
            return;

        var selectedNode, anchorOffset, focusOffset, updateDom = false;

        var textNodes = [];
        for (var i = 0; i < domElem.children.length; i++)
            textNodes.push({css: domElem.children[i].classList[1], text: domElem.children[i].textContent});

        // if it's the 1st character help with init
        if (textNodes.length == 0 && domElem.textContent.length > 0) {
            textNodes.push({css: homeCss, text: domElem.textContent});
            selectedNode = 0;
            anchorOffset = domElem.textContent.length;
            focusOffset = domElem.textContent.length;
            updateDom = true;

        } else if (domElem.textContent.length >= (textObservable() != null ? textObservable() : "").length) {

            var oldDomNodes = document.createElement("div");
            oldDomNodes.innerHTML = htmlObservable();
            var oldNodes = [];
            for (i = 0; i < oldDomNodes.children.length; i++)
                oldNodes.push({css: oldDomNodes.children[i].classList[1], text: oldDomNodes.children[i].textContent});

            for (i = 0; i < textNodes.length; i++) {
                var currTxtNode = textNodes[i];
                if (currTxtNode.css != homeCss && currTxtNode.text.length >= oldNodes[i].text.length) {
                    for (var u = 0; u < currTxtNode.text.length; u++) {

                        if (currTxtNode.text[u] != oldNodes[i].text[u]) {

                            var newTxtNode = {css: homeCss, text: currTxtNode.text[u]};
                            var partOneOldTextNode = {css: currTxtNode.css, text: currTxtNode.text.substring(0, u)};
                            var partTwoOldTextNode = {css: currTxtNode.css, text: currTxtNode.text.substring(u + 1)};

                            if (partOneOldTextNode.text.length < 1 && partTwoOldTextNode.text.length > 0) {
                                textNodes.splice(i, 1, newTxtNode, partTwoOldTextNode);
                                selectedNode = i;
                            } else if (partTwoOldTextNode.text.length < 1 && partOneOldTextNode.text.length > 0) {
                                textNodes.splice(i, 1, partOneOldTextNode, newTxtNode);
                                selectedNode = i + 1;
                            } else {
                                textNodes.splice(i, 1, partOneOldTextNode, newTxtNode, partTwoOldTextNode);
                                selectedNode = i + 1;
                            }
                            anchorOffset = newTxtNode.text.length;
                            focusOffset = newTxtNode.text.length;

                            updateDom = true;
                            break;
                        }
                    }
                    if (updateDom)
                        break;
                }
            }
        }

        // remove empty nodes and concatenate nodes with same css class
        var lastCss = "initial nothing";

        for (var a = 0; a < textNodes.length; a++) {
            if (textNodes[a].text.length < 1) {
                deleteNode(textNodes[a - 1].text.length);
            } else {
                if (lastCss == textNodes[a].css) {
                    var possibleFocus = textNodes[a - 1].text.length;
                    textNodes[a - 1].text += textNodes[a].text;
                    deleteNode(possibleFocus);
                }

                lastCss = textNodes[a].css;
            }

            function deleteNode(possibleFocus) {
                textNodes.splice(a, 1);
                selectedNode = a - 1;
                anchorOffset = possibleFocus;
                focusOffset = possibleFocus;
                a--;
                updateDom = true;
            }
        }

        if (updateDom) {
            var newInnerHtml = "";
            textNodes.forEach(function (txtN) {
                newInnerHtml += '<span class="label ' + txtN.css + '">' + txtN.text + '</span>';
            });

            domElem.innerHTML = newInnerHtml;

            if (domElem.children[selectedNode]) {
                window.getSelection().removeAllRanges();
                var range = document.createRange();

                range.setStart(domElem.children[selectedNode].firstChild, anchorOffset);
                range.setEnd(domElem.children[selectedNode].firstChild, focusOffset);
                window.getSelection().addRange(range);
            }
        }
        textObservable(domElem.textContent);
        htmlObservable(domElem.innerHTML);
    }

    return {
        init: function (element, valueAccessor) {

            var isUserUpdate = false;
            var isRemoteUpdate = false;

            element.addEventListener('keyup', function (event) {
                var textObservable = valueAccessor().text;
                var htmlObservable = valueAccessor().html;
                isUserUpdate = true;
                updateEditable(event.target, textObservable, htmlObservable, CssConstant.LOCAL_CSS, window, document);
                isUserUpdate = false;
            });

            valueAccessor().text.subscribe(function () {

                if (!isUserUpdate && !isRemoteUpdate) {
                    isRemoteUpdate = true;

                    var textObservable = valueAccessor().text;
                    var htmlObservable = valueAccessor().html;
                    var domElem = element;
                    var homeCss = CssConstant.REMOTE_CSS;

                    var textNodes = [];
                    for (var i = 0; i < domElem.children.length; i++)
                        textNodes.push({css: domElem.children[i].classList[1], text: domElem.children[i].textContent});

                    // var simplePushChange = false;
                    var pureText = valueAccessor().text();

                    if (textNodes.length == 0 && pureText.length > 0) {
                        textNodes.push({css: homeCss, text: pureText});

                    } else {

                        var lowerIndex = 0;
                        for (i = 0; i < textNodes.length; i++) {
                            var currElem = textNodes[i];
                            var stringToCompare = pureText.substring(0, currElem.text.length);
                            if (stringToCompare == currElem.text) {
                                pureText = pureText.substring(currElem.text.length);
                            } else {
                                lowerIndex = i;
                                var charsToRemove = 0;
                                for (var u = textNodes.length - 1; u > lowerIndex; u--) {<commit_msg>fix empty inputs bug<commit_after>define(['constants/CssConstant'], function (CssConstant) {

    function updateEditable(domElem, textObservable, htmlObservable, homeCss, window, document) {
        if (domElem.textContent == textObservable())
            return;

        if (textObservable() != "" && domElem.textContent == "") {
            domElem.innerHTML = "";
            htmlObservable("");
            textObservable("");
            return;
        }

        var selectedNode, anchorOffset, focusOffset, updateDom = false;

        var textNodes = [];
        for (var i = 0; i < domElem.children.length; i++)
            textNodes.push({css: domElem.children[i].classList[1], text: domElem.children[i].textContent});

        // if it's the 1st character help with init
        if (textNodes.length == 0 && domElem.textContent.length > 0) {
            textNodes.push({css: homeCss, text: domElem.textContent});
            selectedNode = 0;
            anchorOffset = domElem.textContent.length;
            focusOffset = domElem.textContent.length;
            updateDom = true;

        } else if (domElem.textContent.length >= (textObservable() != null ? textObservable() : "").length) {

            var oldDomNodes = document.createElement("div");
            oldDomNodes.innerHTML = htmlObservable();
            var oldNodes = [];
            for (i = 0; i < oldDomNodes.children.length; i++)
                oldNodes.push({css: oldDomNodes.children[i].classList[1], text: oldDomNodes.children[i].textContent});

            if (oldNodes.length == 0 && textNodes.length == 1) {
                textNodes[0].css = CssConstant.LOCAL_CSS;
                selectedNode = 0;
                anchorOffset = domElem.textContent.length;
                focusOffset = domElem.textContent.length;
                updateDom = true;
            } else {

                for (i = 0; i < textNodes.length; i++) {
                    var currTxtNode = textNodes[i];
                    if (currTxtNode.css != homeCss && currTxtNode.text.length >= oldNodes[i].text.length) {
                        for (var u = 0; u < currTxtNode.text.length; u++) {

                            if (currTxtNode.text[u] != oldNodes[i].text[u]) {

                                var newTxtNode = {css: homeCss, text: currTxtNode.text[u]};
                                var partOneOldTextNode = {css: currTxtNode.css, text: currTxtNode.text.substring(0, u)};
                                var partTwoOldTextNode = {css: currTxtNode.css, text: currTxtNode.text.substring(u + 1)};

                                if (partOneOldTextNode.text.length < 1 && partTwoOldTextNode.text.length > 0) {
                                    textNodes.splice(i, 1, newTxtNode, partTwoOldTextNode);
                                    selectedNode = i;
                                } else if (partTwoOldTextNode.text.length < 1 && partOneOldTextNode.text.length > 0) {
                                    textNodes.splice(i, 1, partOneOldTextNode, newTxtNode);
                                    selectedNode = i + 1;
                                } else {
                                    textNodes.splice(i, 1, partOneOldTextNode, newTxtNode, partTwoOldTextNode);
                                    selectedNode = i + 1;
                                }
                                anchorOffset = newTxtNode.text.length;
                                focusOffset = newTxtNode.text.length;

                                updateDom = true;
                                break;
                            }
                        }
                        if (updateDom)
                            break;
                    }
                }
            }
        }

        // remove empty nodes and concatenate nodes with same css class
        var lastCss = "initial nothing";

        for (var a = 0; a < textNodes.length; a++) {
            if (textNodes[a].text.length < 1) {
                deleteNode(textNodes[a - 1].text.length);
            } else {
                if (lastCss == textNodes[a].css) {
                    var possibleFocus = textNodes[a - 1].text.length;
                    textNodes[a - 1].text += textNodes[a].text;
                    deleteNode(possibleFocus);
                }

                lastCss = textNodes[a].css;
            }

            function deleteNode(possibleFocus) {
                textNodes.splice(a, 1);
                selectedNode = a - 1;
                anchorOffset = possibleFocus;
                focusOffset = possibleFocus;
                a--;
                updateDom = true;
            }
        }

        if (updateDom) {
            var newInnerHtml = "";
            textNodes.forEach(function (txtN) {
                newInnerHtml += '<span class="label ' + txtN.css + '">' + txtN.text + '</span>';
            });

            domElem.innerHTML = newInnerHtml;

            if (domElem.children[selectedNode]) {
                window.getSelection().removeAllRanges();
                var range = document.createRange();

                range.setStart(domElem.children[selectedNode].firstChild, anchorOffset);
                range.setEnd(domElem.children[selectedNode].firstChild, focusOffset);
                window.getSelection().addRange(range);
            }
        }
        textObservable(domElem.textContent);
        htmlObservable(domElem.innerHTML);
    }

    return {
        init: function (element, valueAccessor) {

            var isUserUpdate = false;
            var isRemoteUpdate = false;

            element.addEventListener('keyup', function (event) {
                var textObservable = valueAccessor().text;
                var htmlObservable = valueAccessor().html;
                isUserUpdate = true;
                updateEditable(event.target, textObservable, htmlObservable, CssConstant.LOCAL_CSS, window, document);
                isUserUpdate = false;
            });

            valueAccessor().text.subscribe(function () {

                if (!isUserUpdate && !isRemoteUpdate) {
                    isRemoteUpdate = true;

                    var textObservable = valueAccessor().text;
                    var htmlObservable = valueAccessor().html;
                    var domElem = element;
                    var homeCss = CssConstant.REMOTE_CSS;

                    if (textObservable() == "" && htmlObservable() != "") {
                        domElem.innerHTML = "";
                        htmlObservable("");
                        isRemoteUpdate = false;
                        return;
                    }

                    var textNodes = [];
                    for (var i = 0; i < domElem.children.length; i++)
                        textNodes.push({css: domElem.children[i].classList[1], text: domElem.children[i].textContent});

                    // var simplePushChange = false;
                    var pureText = valueAccessor().text();

                    if (textNodes.length == 0 && pureText.length > 0) {
                        textNodes.push({css: homeCss, text: pureText});

                    } else {

                        var lowerIndex = 0;
                        for (i = 0; i < textNodes.length; i++) {
                            var currElem = textNodes[i];
                            var stringToCompare = pureText.substring(0, currElem.text.length);
                            if (stringToCompare == currElem.text) {
                                pureText = pureText.substring(currElem.text.length);
                            } else {
                                lowerIndex = i;
                                var charsToRemove = 0;
                                for (var u = textNodes.length - 1; u > lowerIndex; u--) {
{'fraction_non_alphanumeric': 0.10405883955325525, 'fraction_numerical': 0.005311904113320621, 'mean_word_length': 1.3624517374517375, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7307113', 'n_tokens_mistral': 3863, 'n_tokens_neox': 3616, 'n_words': 758}