<commit_before>/**
Copyright (c) 2013, <NAME>, http://cjwainwright.co.uk

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

// DOM selector, returns as array so can forEach etc.
function $(selector) {
    return Array.apply(null, document.querySelectorAll(selector));
}

// Utility DOM functions
var DOM = {
    toggleClass: function (el, className, on) {
        var currentClassName = el.className;
        var newClassName = currentClassName.replace(new RegExp('\\b' + className + '\\b', 'g'), '').replace(/^\s+/g, '').replace(/\s+$/g, '');
        if(on) {
            if(newClassName) {
                newClassName += ' ';
            }
            newClassName += className;
        }
        
        if(newClassName != currentClassName) {
            el.className = newClassName;
        }
    }
}

// Function to process the stylesheet
// Assumes pseudo :where() classes have already been re-written as standard classes in the form .where-style-comparator-value, e.g. .where-width-lt-500px
// For each condition, build a lookup (conditions) from this class name to the function which evaluates the condition on an element
// Store a lookup (rules) to these conditions from all CSS selectors which select the elements that need this condition applying
function parseStylesheet() {
    
    // create object containing data about parsed stylesheet to be returned from function
    var conditions = {};
    
    var noMatch = function(el) { return false; };
    var comparers = {
        eq: function(v1, v2){ return parseInt(v1) == parseInt(v2); },
        lt: function(v1, v2){ return parseInt(v1) < parseInt(v2); },
        gt: function(v1, v2){ return parseInt(v1) > parseInt(v2); },
        lteq: function(v1, v2){ return parseInt(v1) <= parseInt(v2); },
        gteq: function(v1, v2){ return parseInt(v1) >= parseInt(v2); }
    };

    // parse condition into a function that can be run on an element to evaluate the condition
    function createMatchMethod(condition) {
        var items = condition.split('-'); 
        if(items.length == 3) { 
            // assume of form 'style-comparator-value'
            var style = items[0], 
                comparer = comparers[items[1]],
                value = items[2];

            if(comparer == null) {
                return noMatch;
            }
            
            return function(el) { return comparer(getComputedStyle(el)[items[0]], items[2]); };
        }
        return noMatch;
    }
    
    // process selector, building up conditions and rules and adding to returned data
    function processSelector(selector) {
        do
        {
            var matched = false;
            selector = selector.replace(/\.where-([^\s]*)/, function(match, condition, index, str){ //TODO - need to handle other pseudo classes/elements after this one, currently assuming this is the last thing before the space
                matched = true;
                var selector = str.substr(0, index); // everything up to the where class. 
                var conditionClass = 'where-' + condition;
                var data = conditions[conditionClass];
                if(data == null) {
                    conditions[conditionClass] = data = {match: createMatchMethod(condition), selectors: []};
                }
                data.selectors.push(selector);
                
                return ''; //remove the where class from the selector before looking for further ones
            });
        }
        while(matched);
    }
    <commit_msg>Handling condition class not appearing at end.<commit_after>/**
Copyright (c) 2013, <NAME>, http://cjwainwright.co.uk

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

// DOM selector, returns as array so can forEach etc.
function $(selector) {
    return Array.apply(null, document.querySelectorAll(selector));
}

// Utility DOM functions
var DOM = {
    toggleClass: function (el, className, on) {
        var currentClassName = el.className;
        var newClassName = currentClassName.replace(new RegExp('\\b' + className + '\\b', 'g'), '').replace(/^\s+/g, '').replace(/\s+$/g, '');
        if(on) {
            if(newClassName) {
                newClassName += ' ';
            }
            newClassName += className;
        }
        
        if(newClassName != currentClassName) {
            el.className = newClassName;
        }
    }
}

// Function to process the stylesheet
// Assumes pseudo :where() classes have already been re-written as standard classes in the form .where-style-comparator-value, e.g. .where-width-lt-500px
// For each condition, build a lookup (conditions) from this class name to the function which evaluates the condition on an element
// Store a lookup (rules) to these conditions from all CSS selectors which select the elements that need this condition applying
function parseStylesheet() {
    
    // create object containing data about parsed stylesheet to be returned from function
    var conditions = {};
    
    var noMatch = function(el) { return false; };
    var comparers = {
        eq: function(v1, v2){ return parseInt(v1) == parseInt(v2); },
        lt: function(v1, v2){ return parseInt(v1) < parseInt(v2); },
        gt: function(v1, v2){ return parseInt(v1) > parseInt(v2); },
        lteq: function(v1, v2){ return parseInt(v1) <= parseInt(v2); },
        gteq: function(v1, v2){ return parseInt(v1) >= parseInt(v2); }
    };

    // parse condition into a function that can be run on an element to evaluate the condition
    function createMatchMethod(condition) {
        var items = condition.split('-'); 
        if(items.length == 3) { 
            // assume of form 'style-comparator-value'
            var style = items[0], 
                comparer = comparers[items[1]],
                value = items[2];

            if(comparer == null) {
                return noMatch;
            }
            
            return function(el) { return comparer(getComputedStyle(el)[items[0]], items[2]); };
        }
        return noMatch;
    }
    
    // process selector, building up conditions and rules and adding to returned data
    function processSelector(selector) {
        do
        {
            var matched = false;
            selector = selector.replace(/\.where-([^\s\.\:\#]*)/, function(match, condition, index, str){
                matched = true;
                var conditionClass = 'where-' + condition;
                
                var data = conditions[conditionClass];
                if(data == null) {
                    conditions[conditionClass] = data = {match: createMatchMethod(condition), selectors: []};
                }

                var start = str.substr(0, index); // everything up to the where class. 
                var remaining = str.substr(index + conditionClass.length + 1); // everything after the where class
                if((remaining.length != 0) &&
                   (remaining[0] != ' '))
                {
                    // we haven't finished at this level
                    // TODO - handle the possibility of further where classes at this level
                    start += remaining.split(' ')[0];
                }

                data.selectors.push(start);
                
                return ''; //remove the where class from the selector before looking for further ones
            });
        }
        while(matched);
    }
    
{'fraction_non_alphanumeric': 0.09697423286645995, 'fraction_numerical': 0.007698064792045333, 'mean_word_length': 2.413868613138686, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '6168265', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2340, 'n_words': 937}