Skip to content

Commit f2ec0cd

Browse files
committed
Use exclude config from CSSComb
1 parent d4318c9 commit f2ec0cd

11 files changed

+517
-10
lines changed

Gruntfile.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ module.exports = function (grunt) {
5454
src: ['*.css', '!*.resorted.css'],
5555
dest: 'test/fixtures/dest/',
5656
ext: '.resorted.css'
57+
},
58+
excludes: {
59+
options: {
60+
config: 'test/fixtures/excludes/excludes.json'
61+
},
62+
expand: true,
63+
cwd: 'test/fixtures/excludes/',
64+
src: ['*.css', '!*.resorted.css'],
65+
dest: 'test/fixtures/excludes/',
66+
ext: '.resorted.css'
5767
}
5868
},
5969

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

82-
};
92+
};

tasks/csscomb.js

+52-8
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,59 @@ module.exports = function (grunt) {
7272
}
7373
}).forEach(function (src) {
7474

75-
// Get CSS from a source file:
76-
var css = grunt.file.read(src);
77-
78-
// Comb it:
79-
grunt.log.ok('Sorting file "' + src + '"...');
80-
var syntax = src.split('.').pop();
81-
var combed = comb.processString(css, { syntax: syntax });
82-
grunt.file.write(f.dest, combed);
75+
// Placeholder for our css content
76+
var css;
77+
78+
if (shouldProcess(src, config)) {
79+
80+
// Get CSS from a source file:
81+
css = grunt.file.read(src);
82+
83+
// Comb it:
84+
grunt.log.ok('Sorting file "' + src + '"...');
85+
var syntax = src.split('.').pop();
86+
var combed = comb.processString(css, { syntax: syntax });
87+
grunt.file.write(f.dest, combed);
88+
} else {
89+
grunt.log.ok('Skipping file "' + src + '" because of exclude.');
90+
grunt.file.copy(src, f.dest);
91+
}
8392
});
8493
});
8594
});
95+
96+
function shouldProcess(src, config) {
97+
var excludes = zip(
98+
(config.exclude || []).map(function(pattern) {
99+
return grunt.file.expand(pattern);
100+
})
101+
);
102+
var ok = true;
103+
104+
if (excludes) {
105+
var found = false;
106+
src = src.replace(/^\.\//, '');
107+
for (var i = 0, excludeLength = excludes.length; i < excludeLength && !found; i++) {
108+
if (excludes[i].match(src)) {
109+
found = true;
110+
}
111+
}
112+
113+
ok = ok && !found;
114+
}
115+
116+
return ok;
117+
}
118+
119+
function zip(arrays) {
120+
var returnArray = [];
121+
122+
arrays.forEach(function(value) {
123+
value.forEach(function(item) {
124+
returnArray.push(item);
125+
});
126+
});
127+
128+
return returnArray;
129+
}
86130
};

test/csscomb_test.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ exports.csscomb = {
4545
var expected2 = grunt.file.read('test/expected/multi2.css');
4646
test.equal(actual2, expected2, 'sholud be sorted.');
4747

48+
test.done();
49+
},
50+
excludes: function (test) {
51+
test.expect(3);
52+
53+
var actual = grunt.file.read('test/fixtures/excludes/exclude1.resorted.css');
54+
var expected = grunt.file.read('test/expected/exclude1.css');
55+
test.equal(actual, expected, 'should be sorted.');
56+
57+
var actual2 = grunt.file.read('test/fixtures/excludes/exclude2.resorted.css');
58+
var expected2 = grunt.file.read('test/expected/exclude2.css');
59+
test.equal(actual2, expected2, 'should be sorted.');
60+
61+
var actual3 = grunt.file.read('test/fixtures/excludes/exclude2.resorted.css');
62+
var expected3 = grunt.file.read('test/expected/exclude2-notignored.css');
63+
test.notEqual(actual3, expected3, 'should not be sorted.');
64+
4865
test.done();
4966
}
50-
};
67+
};

test/expected/exclude1.css

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.exclude1
2+
{
3+
font-weight: bold;
4+
5+
z-index: 100;
6+
7+
display: block;
8+
visibility: hidden;
9+
10+
width: 100px;
11+
height: 100px;
12+
max-height: 44px;
13+
14+
text-align: center;
15+
vertical-align: 5px;
16+
17+
border-color: 1px #000 solid;
18+
background-color: red;
19+
}

test/expected/exclude2-notignored.css

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.expected2
2+
{
3+
position: absolute;
4+
z-index: 10;
5+
top: 10px;
6+
left: 10px;
7+
8+
display: table;
9+
float: left;
10+
11+
width: 10px;
12+
height: 10px;
13+
margin: 10px;
14+
padding: 10px;
15+
16+
border: 1px #fff solid;
17+
}

test/expected/exclude2.css

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.exclude2 {
2+
position: absolute;
3+
top: 10px;
4+
left: 10px;
5+
z-index: 10;
6+
display: table;
7+
float: left;
8+
margin: 10px;
9+
padding: 10px;
10+
width: 10px;
11+
height: 10px;
12+
border: 1px #fff solid;
13+
}

test/fixtures/excludes/exclude1.css

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.exclude1 {
2+
z-index: 100;
3+
display: block;
4+
visibility: hidden;
5+
max-height: 44px;
6+
width: 100px;
7+
height: 100px;
8+
border-color: 1px #000 solid;
9+
background-color: red;
10+
vertical-align: 5px;
11+
text-align: center;
12+
font-weight: bold;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.exclude1
2+
{
3+
font-weight: bold;
4+
5+
z-index: 100;
6+
7+
display: block;
8+
visibility: hidden;
9+
10+
width: 100px;
11+
height: 100px;
12+
max-height: 44px;
13+
14+
text-align: center;
15+
vertical-align: 5px;
16+
17+
border-color: 1px #000 solid;
18+
background-color: red;
19+
}

test/fixtures/excludes/exclude2.css

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.exclude2 {
2+
position: absolute;
3+
top: 10px;
4+
left: 10px;
5+
z-index: 10;
6+
display: table;
7+
float: left;
8+
margin: 10px;
9+
padding: 10px;
10+
width: 10px;
11+
height: 10px;
12+
border: 1px #fff solid;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.exclude2 {
2+
position: absolute;
3+
top: 10px;
4+
left: 10px;
5+
z-index: 10;
6+
display: table;
7+
float: left;
8+
margin: 10px;
9+
padding: 10px;
10+
width: 10px;
11+
height: 10px;
12+
border: 1px #fff solid;
13+
}

0 commit comments

Comments
 (0)