<commit_before>/*
 * grunt-combine-media-queries
 * https://github.com/buildingblocks/grunt-combine-media-queries
 *
 * Copyright (c) 2013 <NAME>
 * Licensed under the MIT license.
 */

'use strict';

var processCssRule = function(rule) {
  var strCss = '';
  strCss += rule.selectors.join(',\n') + ' {\n';
  rule.declarations.forEach(function (declaration) {
    if(declaration.property != null && declaration.value != null){
      strCss += '\t' + declaration.property + ':' + declaration.value + ';\n';
    }
  });
  strCss += '}\n\n';
  return strCss;
};

module.exports = function(grunt) {

  grunt.registerMultiTask('cmq', 'Find duplicate media queries and combines them.', function() {
    
    // Require stuff
    var parseCss = require('css-parse');
    var path = require('path');
    var error = true;
    
    // Default options
    var options = this.options({
      log: false,
      ext: false 
    });
    
    // Function to log info only when 'options.log' is set to true
    var log = function(message){
      if (options.log){
        grunt.log.writeln(message);
      }
    };

    this.files.forEach(function(f) {
      
      f.src.forEach(function (filepath) {
      
        error = false;
      
        log('\nFile ' + filepath + ' found.');
              
        var filename = filepath.replace(/(.*)\//gi, '');
        var destpath = path.join(f.dest, filename);
        var source = grunt.file.read(filepath);
        var cssJson = parseCss(source);
        var strStyles = [];
        var processedCSS = {};
        processedCSS.base = {};
        processedCSS.base.rules = [];
        processedCSS.media = [];
        processedCSS.media.minWidth = [];
        processedCSS.media.maxWidth = [];
        processedCSS.media.minHeight = [];
        processedCSS.media.maxHeight = [];
        processedCSS.media.print = [];
        processedCSS.media.blank = [];
        processedCSS.keyframes = [];
        
        grunt.file.write(destpath, cssJson);
        
        // For every rule in the stylesheet...
        cssJson.stylesheet.rules.forEach( function (rule) {
          
          // if the rule is a media query...
          if (rule.type === 'media') {
            
            // Create 'id' based on the query (stripped from spaces and dashes etc.)
            var strMedia = rule.media.replace(/[^A-Za-z0-9]/ig,'');
            
            // Create an array with all the media queries with the same 'id'
            var item = processedCSS.media.filter(function (element) {
              return (element.val === strMedia);
            });
            
            // If there are no media queries in the array, define details
            if (item.length < 1) {
              var mediaObj = {};
              mediaObj.sortVal = parseFloat(rule.media.match( /\d+/g ));
              mediaObj.rule = rule.media;
              mediaObj.val = strMedia;
              mediaObj.rules = [];

              processedCSS.media.push(mediaObj);
            }
            
            // Compare the query to other queries
            var i = 0, matched = false;
            processedCSS.media.forEach(function (elm) {
              if (elm.val === strMedia) {
                matched = true;
              }
              if (!matched) {i++;}
            });
            
            // push every single of merged query
            rule.rules.forEach(function (mediaRule) {
              processedCSS.media[i].rules.push(mediaRule);
            });

          } else if (rule.type === 'keyframes') {
            processedCSS.keyframes.push(rule);
               
          } else if (rule.type === 'rule') {
            processedCSS.base.rules.push(rule);
          }<commit_msg>Fixed warning error when there are comments in media queries<commit_after>/*
 * grunt-combine-media-queries
 * https://github.com/buildingblocks/grunt-combine-media-queries
 *
 * Copyright (c) 2013 <NAME>
 * Licensed under the MIT license.
 */

'use strict';

var processCssRule = function(rule) {
  var strCss = '';
  strCss += rule.selectors.join(',\n') + ' {\n';
  rule.declarations.forEach(function (declaration) {
    if(declaration.property != null && declaration.value != null){
      strCss += '\t' + declaration.property + ':' + declaration.value + ';\n';
    }
  });
  strCss += '}\n\n';
  return strCss;
};

module.exports = function(grunt) {

  grunt.registerMultiTask('cmq', 'Find duplicate media queries and combines them.', function() {
    
    // Require stuff
    var parseCss = require('css-parse');
    var path = require('path');
    var error = true;
    
    // Default options
    var options = this.options({
      log: false,
      ext: false 
    });
    
    // Function to log info only when 'options.log' is set to true
    var log = function(message){
      if (options.log){
        grunt.log.writeln(message);
      }
    };

    this.files.forEach(function(f) {
      
      f.src.forEach(function (filepath) {
      
        error = false;
      
        log('\nFile ' + filepath + ' found.');
              
        var filename = filepath.replace(/(.*)\//gi, '');
        var destpath = path.join(f.dest, filename);
        var source = grunt.file.read(filepath);
        var cssJson = parseCss(source);
        var strStyles = [];
        var processedCSS = {};
        processedCSS.base = {};
        processedCSS.base.rules = [];
        processedCSS.media = [];
        processedCSS.media.minWidth = [];
        processedCSS.media.maxWidth = [];
        processedCSS.media.minHeight = [];
        processedCSS.media.maxHeight = [];
        processedCSS.media.print = [];
        processedCSS.media.blank = [];
        processedCSS.keyframes = [];
        
        grunt.file.write(destpath, cssJson);
        
        // For every rule in the stylesheet...
        cssJson.stylesheet.rules.forEach( function (rule) {
          
          // if the rule is a media query...
          if (rule.type === 'media') {
            
            // Create 'id' based on the query (stripped from spaces and dashes etc.)
            var strMedia = rule.media.replace(/[^A-Za-z0-9]/ig,'');
            
            // Create an array with all the media queries with the same 'id'
            var item = processedCSS.media.filter(function (element) {
              return (element.val === strMedia);
            });
            
            // If there are no media queries in the array, define details
            if (item.length < 1) {
              var mediaObj = {};
              mediaObj.sortVal = parseFloat(rule.media.match( /\d+/g ));
              mediaObj.rule = rule.media;
              mediaObj.val = strMedia;
              mediaObj.rules = [];

              processedCSS.media.push(mediaObj);
            }
            
            // Compare the query to other queries
            var i = 0, matched = false;
            processedCSS.media.forEach(function (elm) {
              if (elm.val === strMedia) {
                matched = true;
              }
              if (!matched) {i++;}
            });
            
            // push every single of merged query
            rule.rules.forEach(function (mediaRule) {
              if (mediaRule.type === 'rule') {
                processedCSS.media[i].rules.push(mediaRule);
              }
            });

          } else if (rule.type === 'keyframes') {
            processedCSS.keyframes.push(rule);
               
          } else if (rule.type === 'rule') {
            processedCSS.base.rules.push(rule);
          }
{'fraction_non_alphanumeric': 0.13421750663129972, 'fraction_numerical': 0.002122015915119363, 'mean_word_length': 1.6922527668689753, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '7564779', 'n_tokens_mistral': 2125, 'n_tokens_neox': 2084, 'n_words': 492}