<commit_before>/**
 * Detect BEM blocks in DOM: proof of concept
 */

(function(global) {
    var Makeup = global.Makeup || {fn: {}}; // for tests
    var $ = Makeup.$;
    var _ = Makeup._;


    // for simplicity, we accept two types of BEM delimiters:
    // 1. blockName__elementName, block-name__element-name, etc
    // 2. blockName--elementName, blockName--modifierName, etc
    var delimitersRe = /--|__/;

    function isBlockClassName(className) {
        return !delimitersRe.test(className);
    }

    function getBlockName(elem) {
        if (typeof elem.className != 'string') return; // svg etc

        var classes = _.compact(elem.className.split(' ')),
            blockyClasses = _.filter(classes, function(className) {
                return isBlockClassName(className);
            });

        if (!blockyClasses.length) return;

        var block = $(elem);
        var approvedBlock;

        _.find(blockyClasses, function(blockName) {
            var selector = '';
            selector += '[class^="' + blockName + '__"],';
            selector += '[class^="' + blockName + '--"],';
            selector += '[class*=" ' + blockName + '__"],';
            selector += '[class*=" ' + blockName + '--"]';

            var seemsLegit = block.find(selector).length;
            if (seemsLegit) {
                approvedBlock = blockName;
            }
            return approvedBlock;
        });

        return approvedBlock;
    }

    Makeup.fn.detectBlocks = function(root) {
        root = root || document.body;

        var blocks = [];

        $(root).find('*').each(function() {
            var block = getBlockName(this);
            if (block) {
                blocks.push(block);
            }
        });

        blocks = _.uniq(blocks);

        if (blocks.length) {
            return blocks;
        }

        // if there are no BEM blocks, search for common block names + body children
        var commonBlockNames = [
            'head',
            'header',
            'nav',
            'navigation',
            'menu',
            'item',
            'list',
            'footer',
            'main',
            'content',
            'box',
            'popup',
            'post',
            'btn',
            'button',
            'auth',
            'user',
            'home',
            'player',
            'logo',
            'section',
            'article',
            'login',
            'form',
            'table',
            'profile'
        ];

        $(root).find('.' + commonBlockNames.join(',.')).each(function() {
            var classes = _.compact(this.className.split(' '));
            var blockName = _.intersection(classes, commonBlockNames)[0];

            if (blockName) {
                blocks.push(blockName);
            }
        });

        $(root).children().each(function() {
            var firstClass = _.compact(this.className.split(' '))[0];
            if (firstClass) {
                blocks.push(firstClass);
            }
        });

        return _.uniq(blocks);
    };

})(this);
<commit_msg>remove script, iframe, etc from detector<commit_after>/**
 * Detect BEM blocks in DOM: proof of concept
 */

(function(global) {
    var Makeup = global.Makeup || {fn: {}}; // for tests
    var $ = Makeup.$;
    var _ = Makeup._;


    // for simplicity, we accept two types of BEM delimiters:
    // 1. blockName__elementName, block-name__element-name, etc
    // 2. blockName--elementName, blockName--modifierName, etc
    var delimitersRe = /--|__/;

    function isBlockClassName(className) {
        return !delimitersRe.test(className);
    }

    function getBlockName(elem) {
        if (typeof elem.className != 'string') return; // svg etc

        var classes = _.compact(elem.className.split(' ')),
            blockyClasses = _.filter(classes, function(className) {
                return isBlockClassName(className);
            });

        if (!blockyClasses.length) return;

        var block = $(elem);
        var approvedBlock;

        _.find(blockyClasses, function(blockName) {
            var selector = '';
            selector += '[class^="' + blockName + '__"],';
            selector += '[class^="' + blockName + '--"],';
            selector += '[class*=" ' + blockName + '__"],';
            selector += '[class*=" ' + blockName + '--"]';

            var seemsLegit = block.find(selector).length;
            if (seemsLegit) {
                approvedBlock = blockName;
            }
            return approvedBlock;
        });

        return approvedBlock;
    }

    Makeup.fn.detectBlocks = function(root) {
        root = root || document.body;

        var blocks = [];

        $(root).find('*').each(function() {
            var block = getBlockName(this);
            if (block) {
                blocks.push(block);
            }
        });

        blocks = _.uniq(blocks);

        if (blocks.length) {
            return blocks;
        }

        // if there are no BEM blocks, search for common block names + body children
        var commonBlockNames = [
            'head',
            'header',
            'nav',
            'navigation',
            'menu',
            'item',
            'list',
            'footer',
            'main',
            'content',
            'box',
            'popup',
            'post',
            'btn',
            'button',
            'auth',
            'user',
            'home',
            'player',
            'logo',
            'section',
            'article',
            'login',
            'form',
            'table',
            'profile',
            'gadget',
            'dashboard'
        ];

        $(root).find('.' + commonBlockNames.join(',.')).not('script, link, iframe, object, svg').each(function() {
            var classes = _.compact(this.className.split(' '));
            var blockName = _.intersection(classes, commonBlockNames)[0];

            if (blockName) {
                blocks.push(blockName);
            }
        });

        $(root).children().each(function() {
            var firstClass = _.compact(this.className.split(' '))[0];
            if (firstClass) {
                blocks.push(firstClass);
            }
        });

        return _.uniq(blocks);
    };

})(this);

{'fraction_non_alphanumeric': 0.1452505136715663, 'fraction_numerical': 0.001264422317053896, 'mean_word_length': 1.4786525656090874, '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': '1641092', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1677, 'n_words': 369}