<commit_before>(function($, window, document, undefined){
    
    var PufferfishImageChangeEvent = function(element, newImageSrc, oldImageSrc){
        this.element = element;
        this.newImageSrc = newImageSrc;
        this.oldImageSrc = oldImageSrc;
    };

    PufferfishImageChangeEvent.prototype.element = null;
    PufferfishImageChangeEvent.prototype.newImageSrc = null;
    PufferfishImageChangeEvent.prototype.oldImageSrc = null;
    PufferfishImageChangeEvent.prototype.isDefaultPrevented = false;

    PufferfishImageChangeEvent.prototype.preventDefault = function(){
        this.isDefaultPrevented = true;
    };

    var defaults = {
        'onChange': function(element, newImageSrc, oldImageSrc){},
        'afterChange': function(element, newImageSrc, oldImageSrc){}
    };

    $.pufferfish = (function(){

        var settings = $.extend({}, defaults);

        var sizeDefinitionRegex =       /\[([^,]+)(\s+)?,(\s+)?\(([^\)]+)\)(\s+)?\]/;
        var sizeDefinitionRegexGlobal = /\[([^,]+)(\s+)?,(\s+)?\(([^\)]+)\)(\s+)?\]/g;
        var constraintRegex =       /([^,\s:]+):(\s+)?([^,]+)/;
        var constraintRegexGlobal = /([^,\s:]+):(\s+)?([^,]+)/g;
        
        var watchedImages = [];

        function getDataAttrKey(name){
            return 'data-pufferfish' + (typeof name !== 'undefined' ? '-' + name : '');
        }
        
        function getDataAttrSelector(name){
            return '[' + getDataAttrKey(name) + ']';
        }
        
        function parseConstraints(constraintString){
            
            var defaults = {
                'max-width': null,
                'min-width': null
            };
            
            var constraints = {};
            
            var constraintStringParts = constraintString.match(constraintRegexGlobal);
            constraintStringParts.forEach(function(constraintStringPart){
                var constraintVars = constraintStringPart.match(constraintRegex);
                constraints[constraintVars[1]] = parseInt(constraintVars[3]);
            });
            
            return $.extend({}, defaults, constraints);
            
        }
        
        function parseSizeDefinitions(sizeDefString){
            
            var sizeDefStringParts = sizeDefString.match(sizeDefinitionRegexGlobal);
            var sizeDefs = [];
            
            sizeDefStringParts.forEach(function(sizeDefStringPart){
                var sizeDefVars = sizeDefStringPart.match(sizeDefinitionRegex);
                
                sizeDefs.push({
                    'src': sizeDefVars[1],
                    'constraints': parseConstraints(sizeDefVars[4])
                });
                
            });
            
            return sizeDefs;
            
        }
        
        function watchImage(img){
            
            var sizeDefString = img.attr( getDataAttrKey() );
            var sizeDefinitions = parseSizeDefinitions(sizeDefString);
            
            var srcAttr = img.attr( getDataAttrKey('src-attr') );
            if(!srcAttr) srcAttr = 'src';
            
            img.data( getDataAttrKey('watched'), true );
            
            watchedImages.push({
                'img': img,
                'sizeDefinitions': sizeDefinitions,
                'srcAttr': srcAttr,
                'currentSrc': null
            });
        
        }
        
        function reflow(){
            
            watchedImages.forEach(function(watchedImage){
                
                var img = watchedImage.img;

                var sizeDefinitions = watchedImage.sizeDefinitions;
                var container = img.parent();
                var availableWidth = container.innerWidth();
                
                var selectedSizeDefinition = null;
                
                $.each(sizeDefinitions, function(key, sizeDefinition){
                    
                    if(
                        sizeDefinition.constraints['min-width'] !== null &&
                        availableWidth < sizeDefinition.constraints['min-width']
                    ){
                        return;
                    }
                    
                    if(
                        sizeDefinition.constraints['max-width'] !== null &&
                        availableWidth > sizeDefinition.constraints['max-width']
                    ){
                        return;
                    }
                    
                    selectedSizeDefinition = sizeDefinition;
                    return false;
                    
                });
                
                if(selectedSizeDefinition !== null && watchedImage.currentSrc !== selectedSizeDefinition.src){

                    var oldImageSrc = watchedImage.currentSrc;
                    var newImageSrc = selectedSizeDefinition.src;

                    watchedImage.currentSrc = newImageSrc;

                    var event = new PufferfishImageChangeEvent(img, newImageSrc, oldImageSrc);
                    settings.onChange(event);

                    if(!event.isDefaultPrevented && elementIsVisible(img)){
                        img.attr(watchedImage.srcAttr, newImageSrc);
                    }

                    settings.afterChange(event);
                }
            
            });
            
        }

        function elementIsVisible(element){
            if(element[0] === window || element[0] === document) return true;

            if(element.css('display') === 'none'){
                return false;
            }
            else{
                var parent = element.parent();
                if(parent.length === 0) return false;
                return elementIsVisible(parent);
            }
        }
        
        function bindUnwatched(context){
            if(typeof context === 'undefined') context = document;
            $(context).find( getDataAttrSelector() ).each(function(){
                var img = $(this);
                if(img.data( getDataAttrKey('watched') )) return;
                watchImage(img);
            });
        }
        
        function init(context, _settings){
            settings = $.extend({}, defaults, _settings);
            if(typeof context === 'undefined') context = document;
            bindUnwatched(context);
            reflow();
        }
        
        // Public methods
        return {
            'init': init,
            'reflow': reflow
        };
        
    })();

    $.fn.pufferfish = function(settings){
        $.pufferfish.init(this, settings);
        $(window).on('resize', function(){
            $.pufferfish.reflow();
        });
    };
    
})( jQuery, window, window.document);
<commit_msg>Fix for edge case where hidden elements were not being updated on reflow() or init()<commit_after>(function($, window, document, undefined){
    
    var PufferfishImageChangeEvent = function(element, newImageSrc, oldImageSrc){
        this.element = element;
        this.newImageSrc = newImageSrc;
        this.oldImageSrc = oldImageSrc;
    };

    PufferfishImageChangeEvent.prototype.element = null;
    PufferfishImageChangeEvent.prototype.newImageSrc = null;
    PufferfishImageChangeEvent.prototype.oldImageSrc = null;
    PufferfishImageChangeEvent.prototype.isDefaultPrevented = false;

    PufferfishImageChangeEvent.prototype.preventDefault = function(){
        this.isDefaultPrevented = true;
    };

    var defaults = {
        'onChange': function(element, newImageSrc, oldImageSrc){},
        'afterChange': function(element, newImageSrc, oldImageSrc){}
    };

    $.pufferfish = (function(){

        var settings = $.extend({}, defaults);

        var sizeDefinitionRegex =       /\[([^,]+)(\s+)?,(\s+)?\(([^\)]+)\)(\s+)?\]/;
        var sizeDefinitionRegexGlobal = /\[([^,]+)(\s+)?,(\s+)?\(([^\)]+)\)(\s+)?\]/g;
        var constraintRegex =       /([^,\s:]+):(\s+)?([^,]+)/;
        var constraintRegexGlobal = /([^,\s:]+):(\s+)?([^,]+)/g;
        
        var watchedImages = [];

        function getDataAttrKey(name){
            return 'data-pufferfish' + (typeof name !== 'undefined' ? '-' + name : '');
        }
        
        function getDataAttrSelector(name){
            return '[' + getDataAttrKey(name) + ']';
        }
        
        function parseConstraints(constraintString){
            
            var defaults = {
                'max-width': null,
                'min-width': null
            };
            
            var constraints = {};
            
            var constraintStringParts = constraintString.match(constraintRegexGlobal);
            constraintStringParts.forEach(function(constraintStringPart){
                var constraintVars = constraintStringPart.match(constraintRegex);
                constraints[constraintVars[1]] = parseInt(constraintVars[3]);
            });
            
            return $.extend({}, defaults, constraints);
            
        }
        
        function parseSizeDefinitions(sizeDefString){
            
            var sizeDefStringParts = sizeDefString.match(sizeDefinitionRegexGlobal);
            var sizeDefs = [];
            
            sizeDefStringParts.forEach(function(sizeDefStringPart){
                var sizeDefVars = sizeDefStringPart.match(sizeDefinitionRegex);
                
                sizeDefs.push({
                    'src': sizeDefVars[1],
                    'constraints': parseConstraints(sizeDefVars[4])
                });
                
            });
            
            return sizeDefs;
            
        }
        
        function watchImage(img){
            
            var sizeDefString = img.attr( getDataAttrKey() );
            var sizeDefinitions = parseSizeDefinitions(sizeDefString);
            
            var srcAttr = img.attr( getDataAttrKey('src-attr') );
            if(!srcAttr) srcAttr = 'src';
            
            img.data( getDataAttrKey('watched'), true );
            
            watchedImages.push({
                'img': img,
                'sizeDefinitions': sizeDefinitions,
                'srcAttr': srcAttr,
                'currentSrc': null,
                'visible': null
            });
        
        }
        
        function reflow(){
            
            watchedImages.forEach(function(watchedImage){
                
                var img = watchedImage.img;

                var sizeDefinitions = watchedImage.sizeDefinitions;
                var container = img.parent();
                var availableWidth = container.innerWidth();
                
                var selectedSizeDefinition = null;
                
                $.each(sizeDefinitions, function(key, sizeDefinition){
                    
                    if(
                        sizeDefinition.constraints['min-width'] !== null &&
                        availableWidth < sizeDefinition.constraints['min-width']
                    ){
                        return;
                    }
                    
                    if(
                        sizeDefinition.constraints['max-width'] !== null &&
                        availableWidth > sizeDefinition.constraints['max-width']
                    ){
                        return;
                    }
                    
                    selectedSizeDefinition = sizeDefinition;
                    return false;
                    
                });

                var visible = elementIsVisible(img);
                
                if((selectedSizeDefinition !== null && watchedImage.currentSrc !== selectedSizeDefinition.src) || (visible && watchedImage.visible === null)){

                    var oldImageSrc = watchedImage.currentSrc;
                    var newImageSrc = selectedSizeDefinition.src;

                    watchedImage.currentSrc = newImageSrc;

                    var event = new PufferfishImageChangeEvent(img, newImageSrc, oldImageSrc);
                    settings.onChange(event);

                    if(!event.isDefaultPrevented && visible){
                        img.attr(watchedImage.srcAttr, newImageSrc);
                    }

                    watchedImage.visible = visible;

                    settings.afterChange(event);
                }
            
            });
            
        }

        function elementIsVisible(element){
            if(element[0] === window || element[0] === document) return true;

            if(element.css('display') === 'none'){
                return false;
            }
            else{
                var parent = element.parent();
                if(parent.length === 0) return false;
                return elementIsVisible(parent);
            }
        }
        
        function bindUnwatched(context){
            if(typeof context === 'undefined') context = document;
            $(context).find( getDataAttrSelector() ).each(function(){
                var img = $(this);
                if(img.data( getDataAttrKey('watched') )) return;
                watchImage(img);
            });
        }
        
        function init(context, _settings){
            settings = $.extend({}, defaults, _settings);
            if(typeof context === 'undefined') context = document;
            bindUnwatched(context);
            reflow();
        }
        
        // Public methods
        return {
            'init': init,
            'reflow': reflow
        };
        
    })();

    $.fn.pufferfish = function(settings){
        $.pufferfish.init(this, settings);
        $(window).on('resize', function(){
            $.pufferfish.reflow();
        });
    };
    
})( jQuery, window, window.document);

{'fraction_non_alphanumeric': 0.11452941605307283, 'fraction_numerical': 0.0010206313333819348, 'mean_word_length': 1.4296847325540205, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1819609', 'n_tokens_mistral': 3343, 'n_tokens_neox': 3190, 'n_words': 562}