Skip to content

Commit 79a1a06

Browse files
committed
Use exclude config from CSSComb
1 parent 95b0d1d commit 79a1a06

11 files changed

+520
-12
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

+55-10
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,65 @@ module.exports = function (grunt) {
7272
}
7373
}).forEach(function (src) {
7474

75-
// Get CSS from a source file:
76-
var css = grunt.file.read(src);
75+
// Placeholder for our css content
76+
var css;
7777
var combed;
7878

79-
// Comb it:
80-
grunt.log.ok('Sorting file "' + src + '"...');
81-
var syntax = src.split('.').pop();
82-
try {
83-
combed = comb.processString(css, { syntax: syntax });
84-
grunt.file.write(f.dest, combed);
85-
} catch(e) {
86-
grunt.log.error(e);
79+
if (shouldProcess(src, config)) {
80+
81+
// Get CSS from a source file:
82+
css = grunt.file.read(src);
83+
84+
// Comb it:
85+
grunt.log.ok('Sorting file "' + src + '"...');
86+
var syntax = src.split('.').pop();
87+
88+
try {
89+
combed = comb.processString(css, { syntax: syntax });
90+
grunt.file.write(f.dest, combed);
91+
} catch(e) {
92+
grunt.log.error(e);
93+
}
94+
} else {
95+
grunt.log.ok('Skipping file "' + src + '" because of exclude.');
96+
grunt.file.copy(src, f.dest);
8797
}
8898
});
8999
});
90100
});
101+
102+
function shouldProcess(src, config) {
103+
var excludes = zip(
104+
(config.exclude || []).map(function(pattern) {
105+
return grunt.file.expand(pattern);
106+
})
107+
);
108+
var ok = true;
109+
110+
if (excludes) {
111+
var found = false;
112+
src = src.replace(/^\.\//, '');
113+
for (var i = 0, excludeLength = excludes.length; i < excludeLength && !found; i++) {
114+
if (excludes[i].match(src)) {
115+
found = true;
116+
}
117+
}
118+
119+
ok = ok && !found;
120+
}
121+
122+
return ok;
123+
}
124+
125+
function zip(arrays) {
126+
var returnArray = [];
127+
128+
arrays.forEach(function(value) {
129+
value.forEach(function(item) {
130+
returnArray.push(item);
131+
});
132+
});
133+
134+
return returnArray;
135+
}
91136
};

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)