Skip to content

Commit

Permalink
Use exclude config from CSSComb
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Dec 26, 2015
1 parent 95b0d1d commit 79a1a06
Show file tree
Hide file tree
Showing 11 changed files with 520 additions and 12 deletions.
12 changes: 11 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ module.exports = function (grunt) {
src: ['*.css', '!*.resorted.css'],
dest: 'test/fixtures/dest/',
ext: '.resorted.css'
},
excludes: {
options: {
config: 'test/fixtures/excludes/excludes.json'
},
expand: true,
cwd: 'test/fixtures/excludes/',
src: ['*.css', '!*.resorted.css'],
dest: 'test/fixtures/excludes/',
ext: '.resorted.css'
}
},

Expand All @@ -79,4 +89,4 @@ module.exports = function (grunt) {
// By default, lint and run all tests.
grunt.registerTask('default', ['jshint', 'test']);

};
};
65 changes: 55 additions & 10 deletions tasks/csscomb.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,65 @@ module.exports = function (grunt) {
}
}).forEach(function (src) {

// Get CSS from a source file:
var css = grunt.file.read(src);
// Placeholder for our css content
var css;
var combed;

// Comb it:
grunt.log.ok('Sorting file "' + src + '"...');
var syntax = src.split('.').pop();
try {
combed = comb.processString(css, { syntax: syntax });
grunt.file.write(f.dest, combed);
} catch(e) {
grunt.log.error(e);
if (shouldProcess(src, config)) {

// Get CSS from a source file:
css = grunt.file.read(src);

// Comb it:
grunt.log.ok('Sorting file "' + src + '"...');
var syntax = src.split('.').pop();

try {
combed = comb.processString(css, { syntax: syntax });
grunt.file.write(f.dest, combed);
} catch(e) {
grunt.log.error(e);
}
} else {
grunt.log.ok('Skipping file "' + src + '" because of exclude.');
grunt.file.copy(src, f.dest);
}
});
});
});

function shouldProcess(src, config) {
var excludes = zip(
(config.exclude || []).map(function(pattern) {
return grunt.file.expand(pattern);
})
);
var ok = true;

if (excludes) {
var found = false;
src = src.replace(/^\.\//, '');
for (var i = 0, excludeLength = excludes.length; i < excludeLength && !found; i++) {
if (excludes[i].match(src)) {
found = true;
}
}

ok = ok && !found;
}

return ok;
}

function zip(arrays) {
var returnArray = [];

arrays.forEach(function(value) {
value.forEach(function(item) {
returnArray.push(item);
});
});

return returnArray;
}
};
19 changes: 18 additions & 1 deletion test/csscomb_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ exports.csscomb = {
var expected2 = grunt.file.read('test/expected/multi2.css');
test.equal(actual2, expected2, 'sholud be sorted.');

test.done();
},
excludes: function (test) {
test.expect(3);

var actual = grunt.file.read('test/fixtures/excludes/exclude1.resorted.css');
var expected = grunt.file.read('test/expected/exclude1.css');
test.equal(actual, expected, 'should be sorted.');

var actual2 = grunt.file.read('test/fixtures/excludes/exclude2.resorted.css');
var expected2 = grunt.file.read('test/expected/exclude2.css');
test.equal(actual2, expected2, 'should be sorted.');

var actual3 = grunt.file.read('test/fixtures/excludes/exclude2.resorted.css');
var expected3 = grunt.file.read('test/expected/exclude2-notignored.css');
test.notEqual(actual3, expected3, 'should not be sorted.');

test.done();
}
};
};
19 changes: 19 additions & 0 deletions test/expected/exclude1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.exclude1
{
font-weight: bold;

z-index: 100;

display: block;
visibility: hidden;

width: 100px;
height: 100px;
max-height: 44px;

text-align: center;
vertical-align: 5px;

border-color: 1px #000 solid;
background-color: red;
}
17 changes: 17 additions & 0 deletions test/expected/exclude2-notignored.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.expected2
{
position: absolute;
z-index: 10;
top: 10px;
left: 10px;

display: table;
float: left;

width: 10px;
height: 10px;
margin: 10px;
padding: 10px;

border: 1px #fff solid;
}
13 changes: 13 additions & 0 deletions test/expected/exclude2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.exclude2 {
position: absolute;
top: 10px;
left: 10px;
z-index: 10;
display: table;
float: left;
margin: 10px;
padding: 10px;
width: 10px;
height: 10px;
border: 1px #fff solid;
}
13 changes: 13 additions & 0 deletions test/fixtures/excludes/exclude1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.exclude1 {
z-index: 100;
display: block;
visibility: hidden;
max-height: 44px;
width: 100px;
height: 100px;
border-color: 1px #000 solid;
background-color: red;
vertical-align: 5px;
text-align: center;
font-weight: bold;
}
19 changes: 19 additions & 0 deletions test/fixtures/excludes/exclude1.resorted.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.exclude1
{
font-weight: bold;

z-index: 100;

display: block;
visibility: hidden;

width: 100px;
height: 100px;
max-height: 44px;

text-align: center;
vertical-align: 5px;

border-color: 1px #000 solid;
background-color: red;
}
13 changes: 13 additions & 0 deletions test/fixtures/excludes/exclude2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.exclude2 {
position: absolute;
top: 10px;
left: 10px;
z-index: 10;
display: table;
float: left;
margin: 10px;
padding: 10px;
width: 10px;
height: 10px;
border: 1px #fff solid;
}
13 changes: 13 additions & 0 deletions test/fixtures/excludes/exclude2.resorted.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.exclude2 {
position: absolute;
top: 10px;
left: 10px;
z-index: 10;
display: table;
float: left;
margin: 10px;
padding: 10px;
width: 10px;
height: 10px;
border: 1px #fff solid;
}
Loading

0 comments on commit 79a1a06

Please sign in to comment.