<commit_before>import { Make } from '../af/util/make.js';
import { parseExpression } from './Parser.js';
import Binding from './Binding.js';
import BindingRegistry from './BindingRegistry.js';

let IfBinding = Make(/** @lends module:DataBinding.IfBinding# **/{

    /** @type {string} */
    name: 'bind-if',

    /**
     * @type {Node}
     */
    parentNode: null,

    /**
     * @type {Node}
     */
    node: null,

    /**
     * @type {Node}
     */
    nextSibling: null,

    /**
     *
     * @constructs
     * @extends {Binding}
     * @param {Node} parentNode - this node
     * @param {string} text - the attribute value
     * @param {ScopeInfo} scopeInfo - bindings container
     *
     * @return {void}
     */
    _make: function({ parentNode, text, scopeInfo }) {
        this.node = parentNode;
        this.parentNode = this.node.parentNode;
        this.text = text;
        this.nextSibling = parentNode.nextSibling;

        scopeInfo.bindings.push(this);
    },

    update: function(scope) {
        let isTrue = parseExpression(this.text, scope);

        if (isTrue) {
            if (this.node.parentNode !== this.parentNode) {
                if (this.nextSibling) {
                    this.parentNode.insertBefore(this.node, this.nextSibling);
                } else {
                    this.parentNode.appendChild(this.node);
                }
            }
        } else {
            if (this.node.parentNode === this.parentNode) {
                this.parentNode.removeChild(this.node);
            }
        }
    },

}, Binding).get();

BindingRegistry.register(IfBinding);

export default IfBinding;<commit_msg>refactor IfBinding to work reliably<commit_after>import { Make } from '../af/util/make.js';
import { parseExpression } from './Parser.js';
import Binding from './Binding.js';
import BindingRegistry from './BindingRegistry.js';

let IfBinding = Make(/** @lends module:DataBinding.IfBinding# **/{

    /** @type {string} */
    name: 'bind-if',

    /**
     * @type {Node}
     */
    parentNode: null,

    /**
     * @type {Node}
     */
    node: null,

    /**
     * @type {Node}
     */
    comment: null,

    /**
     *
     * @constructs
     * @extends {Binding}
     * @param {Node} parentNode - this node
     * @param {string} text - the attribute value
     * @param {ScopeInfo} scopeInfo - bindings container
     *
     * @return {void}
     */
    _make: function({ parentNode, text, scopeInfo }) {
        this.node = parentNode;
        this.text = text;
        this.comment = document.createComment(`   If: ${text}   `);

        scopeInfo.bindings.push(this);
    },

    update: function(scope) {
        if (this.comment.parentNode) {
            this.parentNode = this.comment.parentNode;
        }

        if (this.node.parentNode) {
            this.parentNode = this.node.parentNode;
        }

        if (this.parentNode.nodeType === 11) {
            return scope.update();
        }

        let isTrue = parseExpression(this.text, scope);

        if (isTrue) {
            if (!this.parentNode.contains(this.node)) {
                this.parentNode.replaceChild(this.node, this.comment);
            }
        } else {
            if (this.parentNode.contains(this.node)) {
                this.parentNode.replaceChild(this.comment, this.node);
            }
        }
    },

}, Binding).get();

BindingRegistry.register(IfBinding);

export default IfBinding;
{'fraction_non_alphanumeric': 0.14207016526529428, 'fraction_numerical': 0.0005798782255726297, 'mean_word_length': 1.904040404040404, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7611917', 'n_tokens_mistral': 1029, 'n_tokens_neox': 1014, 'n_words': 209}