<commit_before><link rel="import" href="../polymer/polymer.html">

<polymer-element name="symfony-collection" on-tap="{{onRemove}}" attributes="addText prototypeAttr itemAttr removeAttr maxLength">
    <template>
        <content></content>
        <button type="button" on-tap={{addChild}} class="symfony-collection-button">{{addText}}</button>
    </template>
    <script>
        Polymer('symfony-collection', {
            /**
             * Required
             */
            'data-prototype': null,
            addText: 'Add',
            prototypeAttr: 'data-prototype',
            itemAttr: 'symfony-collection-item',
            removeAttr: 'symfony-collection-remove',
            maxLength: null,

            addFirstItem: true,
            addButton: null,
            items: null,
            index: 0,

            /**
             *
             */
            ready: function() {
                this.fetchItems();
                this.index = this.items.length;

                if (this.addFirstItem && 0 === this.index) {
                    //add the markup and listeners for the new child
                    this.addChild();
                }
            },

            /**
             * Remove action
             */
            onRemove: function(event) {
                var selector = event.target.getAttribute(this.removeAttr);
                if (null !== selector) {
                    event.preventDefault();

                    var parent;
                    if (selector) {
                        parent = document.querySelector(selector);
                    } else {
                        parent = this.getParents(event.target, this.itemAttr);
                    }

                    if (parent) {
                        parent.parentNode.removeChild(parent);

                        //this.index -= 1;
                        this.fetchItems();
                    }
                }
            },

            /**
             * Add a child markup to the collection
             */
            addChild: function() {
                if (this.maxLength && this.index >= this.maxLength) return;

                //this.items = this.container.find(this.options.items);
                //get prototype markup, parse it
                //var item = $(prototype.replace(/__name__/g, this.index));
                var html = this.replace(/__name__/g, this.getAttribute(this.prototypeAttr), this.index);
                var item = this.createNode(html);

                //increment index
                this.index += 1;
                //add to html and add display it
                this.appendChild(item);
                //update Items
                this.fetchItems();
            },

            /**
             * Fetch items in the container
             */
            fetchItems: function() {
                this.items = this.querySelectorAll('['+this.itemAttr+']');
            },

            /**
             * Replace the reg just one time by property
             * eq: blabla___name___blabla___name___ > blabla_1_blabla___name___
             * todo: do it better...
             *
             * @param {RegExp} reg
             * @param {String} prototype
             * @param {Number} index
             * @returns {*}
             * @private
             */
            replace: function(reg, prototype, index) {
                var res = null,
                        founded = [],
                        key
                        ;

                do {
                    res = reg.exec(prototype);

                    if (null != res) {
                        var startIndex = this._findIndex(prototype, res.index, 1),
                                endIndex = this._findIndex(prototype, res.index, -1)
                                ;

                        founded[startIndex+'-'+endIndex] = prototype.substring(startIndex, endIndex);
                    }
                } while (null !== res && res.length > 0);

                for (key in founded) {
                    prototype = prototype.replace(founded[key], founded[key].replace(/__name__/, index));
                }

                return prototype;
            },

            /**
             * We try to find string like "blabla" or &quot;blabla&quot;
             *
             * @param {String} prototype
             * @param {Number} index
             * @param {Number} add
             * @returns {Number}
             * @private
             */
            _findIndex: function(prototype, index, add) {
                var char = null;
                var sequence = '';

                do {
                    char = prototype[index];
                    sequence += char;
                    if (sequence.length >= 7) {
                        sequence = sequence.substr(1, sequence.length)
                    }

                    if (char === '"' || sequence === ';touq&' || sequence === '&quot;') {
                        break;
                    }

                    index = index + add;
                } while (1);

                return index;
            },

            /**
             * Create node from string
             * @private
             */
            createNode: function(markup) {
                var container = document.createElement('div');
                container.innerHTML = markup;

                return container.firstChild;
            },

            /**
             * Get parent form
             *
             * @return {HTMLElement}
             * @private
             */
            getParents: function(element, attribute) {
                var p = element.parentNode, o;

                while (p !== null) {
                    o = p;
                    if (null !== o.getAttribute(attribute)) {
                        return o;
                    }

                    p = o.parentNode;
                }

                return null;
            }
        });
    </script>
</polymer-element><commit_msg>configurable prototype replace property<commit_after><link rel="import" href="../polymer/polymer.html">

<polymer-element name="symfony-collection" on-tap="{{onRemove}}" attributes="addText prototypeAttr prototypeProp itemAttr removeAttr maxLength">
    <template>
        <content></content>
        <button type="button" on-tap={{addChild}} class="symfony-collection-button">{{addText}}</button>
    </template>
    <script>
        Polymer('symfony-collection', {
            /**
             * Required
             */
            'data-prototype': null,
            addText: 'Add',
            prototypeAttr: 'data-prototype',
            prototypeProp: '__name__',
            itemAttr: 'symfony-collection-item',
            removeAttr: 'symfony-collection-remove',
            maxLength: null,

            addFirstItem: true,
            addButton: null,
            items: null,
            index: 0,

            /**
             *
             */
            ready: function() {
                this.fetchItems();
                this.index = this.items.length;

                if (this.addFirstItem && 0 === this.index) {
                    //add the markup and listeners for the new child
                    this.addChild();
                }
            },

            /**
             * Remove action
             */
            onRemove: function(event) {
                var selector = event.target.getAttribute(this.removeAttr);
                if (null !== selector) {
                    event.preventDefault();

                    var parent;
                    if (selector) {
                        parent = document.querySelector(selector);
                    } else {
                        parent = this.getParents(event.target, this.itemAttr);
                    }

                    if (parent) {
                        parent.parentNode.removeChild(parent);

                        //this.index -= 1;
                        this.fetchItems();
                    }
                }
            },

            /**
             * Add a child markup to the collection
             */
            addChild: function() {
                if (this.maxLength && this.index >= this.maxLength) return;

                //this.items = this.container.find(this.options.items);
                //get prototype markup, parse it
                //var item = $(prototype.replace(/__name__/g, this.index));
                var re = new RegExp(this.prototypeProp, 'g');
                var html = this.replace(re, this.getAttribute(this.prototypeAttr), this.index);
                var item = this.createNode(html);

                //increment index
                this.index += 1;
                //add to html and add display it
                this.appendChild(item);
                //update Items
                this.fetchItems();
            },

            /**
             * Fetch items in the container
             */
            fetchItems: function() {
                this.items = this.querySelectorAll('['+this.itemAttr+']');
            },

            /**
             * Replace the reg just one time by property
             * eq: blabla___name___blabla___name___ > blabla_1_blabla___name___
             * todo: do it better...
             *
             * @param {RegExp} reg
             * @param {String} prototype
             * @param {Number} index
             * @returns {*}
             * @private
             */
            replace: function(reg, prototype, index) {
                var res = null,
                        founded = [],
                        key
                        ;

                do {
                    res = reg.exec(prototype);

                    if (null != res) {
                        var startIndex = this._findIndex(prototype, res.index, 1),
                                endIndex = this._findIndex(prototype, res.index, -1)
                                ;

                        founded[startIndex+'-'+endIndex] = prototype.substring(startIndex, endIndex);
                    }
                } while (null !== res && res.length > 0);

                for (key in founded) {
                    prototype = prototype.replace(founded[key], founded[key].replace(/__name__/, index));
                }

                return prototype;
            },

            /**
             * We try to find string like "blabla" or &quot;blabla&quot;
             *
             * @param {String} prototype
             * @param {Number} index
             * @param {Number} add
             * @returns {Number}
             * @private
             */
            _findIndex: function(prototype, index, add) {
                var char = null;
                var sequence = '';

                do {
                    char = prototype[index];
                    sequence += char;
                    if (sequence.length >= 7) {
                        sequence = sequence.substr(1, sequence.length)
                    }

                    if (char === '"' || sequence === ';touq&' || sequence === '&quot;') {
                        break;
                    }

                    index = index + add;
                } while (1);

                return index;
            },

            /**
             * Create node from string
             * @private
             */
            createNode: function(markup) {
                var container = document.createElement('div');
                container.innerHTML = markup;

                return container.firstChild;
            },

            /**
             * Get parent form
             *
             * @return {HTMLElement}
             * @private
             */
            getParents: function(element, attribute) {
                var p = element.parentNode, o;

                while (p !== null) {
                    o = p;
                    if (null !== o.getAttribute(attribute)) {
                        return o;
                    }

                    p = o.parentNode;
                }

                return null;
            }
        });
    </script>
</polymer-element>
{'fraction_non_alphanumeric': 0.10692030173827484, 'fraction_numerical': 0.0018038701213512628, 'mean_word_length': 1.0133707494222515, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 33, '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': '2451967', 'n_tokens_mistral': 2940, 'n_tokens_neox': 2696, 'n_words': 630}