Skip to content

Commit 66381e3

Browse files
committed
v0.0.4
2 parents d88d572 + 6c8eb90 commit 66381e3

File tree

8 files changed

+196
-44
lines changed

8 files changed

+196
-44
lines changed

lib/csscomb.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ var vfs = require('vow-fs');
99
* @name Comb
1010
*/
1111
var Comb = function() {
12-
this._options = {
13-
'colon-space': {},
14-
'rule-indent': {},
15-
'sort-order': {},
16-
'stick-brace': {},
17-
'strip-spaces': {}
18-
},
12+
this._handlers;
13+
this._options = [
14+
'strip-spaces',
15+
'stick-brace',
16+
'colon-space',
17+
'rule-indent',
18+
'sort-order'
19+
];
1920
this._config = {};
2021
this._exclude = null;
2122
};
@@ -29,13 +30,18 @@ Comb.prototype = {
2930
* @param {Object} config
3031
*/
3132
configure: function(config) {
32-
for (var option in config) {
33-
if (config.hasOwnProperty(option) && config[option] && this._options[option]) {
33+
var _this = this;
34+
this._handlers = [];
35+
this._options.forEach(function(option) {
36+
if (config[option]) {
3437
try {
35-
this._config[option] = require('./options/' + option).setValue(config[option]);
36-
} catch (e) {}
38+
var handler = require('./options/' + option).setValue(config[option]);
39+
handler && _this._handlers.push(handler);
40+
} catch (e) {
41+
throw new Error('Handler ' + option + ' was not found.');
42+
}
3743
}
38-
}
44+
});
3945
this._exclude = (config.exclude || []).map(function(pattern) {
4046
return new minimatch.Minimatch(pattern);
4147
});
@@ -62,11 +68,9 @@ Comb.prototype = {
6268
node.forEach(function(node) {
6369
if (!Array.isArray(node)) return;
6470
var nodeType = node.shift();
65-
for (var option in config) {
66-
if (config.hasOwnProperty(option)) {
67-
config[option].process(nodeType, node);
68-
}
69-
}
71+
_this._handlers.forEach(function(handler) {
72+
handler.process(nodeType, node);
73+
});
7074
node.unshift(nodeType);
7175
_this.processNode(node, level);
7276
});
@@ -84,7 +88,7 @@ Comb.prototype = {
8488
} catch (e) {
8589
throw new Error('Parsing error at ' + filename + ': ' + e.message);
8690
}
87-
return cssp.translate(cssp.transform(tree));
91+
return cssp.translate(tree);
8892
},
8993

9094
/**

lib/options/colon-space.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
module.exports = {
22

3-
_value: false,
4-
53
/**
64
* Sets handler value.
75
*
86
* @param {String|Boolean} value Option value
97
* @returns {Object}
108
*/
119
setValue: function(value) {
12-
if (!value) return this;
13-
if (typeof value === 'string') {
14-
this._value = value;
15-
} else {
16-
this._value = 'after'; // default
17-
}
10+
if (value === true) this._value = 'after';
11+
if (typeof value === 'string' && value.match(/after|before|both/)) this._value = value;
12+
if (!this._value) return;
1813
return this;
1914
},
2015

lib/options/stick-brace.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
module.exports = {
22

3-
_value: false,
4-
53
/**
64
* Sets handler value.
75
*
86
* @param {String|Number|Boolean} value Option value
97
* @returns {Object}
108
*/
119
setValue: function(value) {
12-
if (!value) return this;
13-
if (value === true) value = ' '; // 1 space by default
14-
if (typeof value === 'number') this._value = new Array(Math.round(value) + 1).join(' ');
10+
if (value === true) value = ' ';
1511
if (typeof value === 'string' && value.match(/^[ \t\n]*$/)) this._value = value;
12+
if (!this._value) return;
1613
return this;
1714
},
1815

lib/options/strip-spaces.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
module.exports = {
22

3-
value: false,
4-
53
/**
64
* Sets handler value.
75
*
86
* @param {String|Boolean} value Option value
97
* @returns {Object}
108
*/
119
setValue: function(value) {
12-
this._value = Boolean(value);
10+
this._value = value === true;
11+
if (!this._value) return;
1312
return this;
1413
},
1514

package.json

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
{
22
"name": "csscomb",
33
"description": "Tool for beautify CSS",
4-
"version": "0.0.3",
4+
"version": "0.0.4",
55
"homepage": "http://csscomb.com/",
66
"author": "Mikhail Troshev <mishanga@yandex-team.ru>",
77
"repository": "https://github.com/csscomb/csscomb.js",
8-
"contributors": [
9-
{
10-
"name": "Slava Oliyanchuk",
11-
"email": "miripiruni@gmail.com",
12-
"url": "http://miripiruni.org/"
13-
},
8+
"maintainers": [
149
{
1510
"name": "Mikhail Troshev",
1611
"email": "mishanga@yandex-team.ru",
17-
"url": "http://mishanga.pro/"
12+
"web": "http://mishanga.pro/"
13+
},
14+
{
15+
"name": "Slava Oliyanchuk",
16+
"email": "miripiruni@gmail.com",
17+
"web": "http://miripiruni.org/"
1818
}
1919
],
2020
"engines": {
@@ -30,12 +30,14 @@
3030
"devDependencies": {
3131
"jshint-groups": "0.5.1",
3232
"jshint": "2.1.7",
33-
"jscs": "1.0.2"
33+
"jscs": "1.0.2",
34+
"mocha": "1.11.0"
3435
},
36+
"main": "./lib/csscomb.js",
3537
"bin": {
3638
"csscomb": "./bin/csscomb"
3739
},
3840
"scripts": {
39-
"test": "./node_modules/.bin/jshint-groups; ./node_modules/.bin/jscs ."
41+
"test": "./node_modules/.bin/jshint-groups . && ./node_modules/.bin/jscs . && ./node_modules/.bin/mocha -u bdd -R spec"
4042
}
4143
}

test/colon-space.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
var Comb = require('../lib/csscomb');
2+
var assert = require('assert');
3+
4+
describe('options/colon-space', function() {
5+
var comb;
6+
beforeEach(function() {
7+
comb = new Comb();
8+
});
9+
it('Invalid String should not change space around colon', function() {
10+
comb.configure({ 'colon-space': 'foobar' });
11+
assert.equal(
12+
comb.processString('a { color : red }'),
13+
'a { color : red }'
14+
);
15+
});
16+
it('True Boolean value should set space after colon', function() {
17+
comb.configure({ 'colon-space': true });
18+
assert.equal(
19+
comb.processString(
20+
'a { color: red }' +
21+
'a{color:red}' +
22+
'a {color : red}' +
23+
'a {color : /* foo */ red }' +
24+
'a {color /* bar */ : red }'
25+
),
26+
'a { color: red }' +
27+
'a{color: red}' +
28+
'a {color: red}' +
29+
'a {color: /* foo */ red }' +
30+
'a {color /* bar */: red }'
31+
);
32+
});
33+
it('`after` value should set space after colon', function() {
34+
comb.configure({ 'colon-space': 'after' });
35+
assert.equal(
36+
comb.processString(
37+
'a { color: red }' +
38+
'a{color:red}' +
39+
'a {color : red}' +
40+
'a {color : /* foo */ red }' +
41+
'a {color /* bar */ : red }'
42+
),
43+
'a { color: red }' +
44+
'a{color: red}' +
45+
'a {color: red}' +
46+
'a {color: /* foo */ red }' +
47+
'a {color /* bar */: red }'
48+
);
49+
});
50+
it('`before` value should set space before colon', function() {
51+
comb.configure({ 'colon-space': 'before' });
52+
assert.equal(
53+
comb.processString(
54+
'a { color: red }' +
55+
'a{color:red}' +
56+
'a {color : red}' +
57+
'a {color : /* foo */ red }' +
58+
'a {color /* bar */ : red }'
59+
),
60+
'a { color :red }' +
61+
'a{color :red}' +
62+
'a {color :red}' +
63+
'a {color :/* foo */ red }' +
64+
'a {color /* bar */ :red }'
65+
);
66+
});
67+
it('`both` value should set spaces around colon', function() {
68+
comb.configure({ 'colon-space': 'both' });
69+
assert.equal(
70+
comb.processString(
71+
'a { color: red }' +
72+
'a{color:red}' +
73+
'a {color : red}'
74+
),
75+
'a { color : red }' +
76+
'a{color : red}' +
77+
'a {color : red}'
78+
);
79+
});
80+
});

test/stick-brace.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var Comb = require('../lib/csscomb');
2+
var assert = require('assert');
3+
4+
describe('options/stick-brace', function() {
5+
var comb;
6+
beforeEach(function() {
7+
comb = new Comb();
8+
});
9+
it('Invalid String should not change space after brace', function() {
10+
comb.configure({ 'stick-brace': 'foobar' });
11+
assert.equal(
12+
comb.processString('a { color: red }'),
13+
'a { color: red }'
14+
);
15+
});
16+
it('True Boolean value should set 1 space after brace', function() {
17+
comb.configure({ 'stick-brace': true });
18+
assert.equal(
19+
comb.processString(
20+
'a { color: red }' +
21+
'a{color:red}' +
22+
'a \t\t \n{color:red}' +
23+
'a /* foo */ {color:red}'
24+
),
25+
'a { color: red }' +
26+
'a {color:red}' +
27+
'a {color:red}' +
28+
'a /* foo */ {color:red}'
29+
);
30+
});
31+
it('Valid String value should set equal space after brace', function() {
32+
comb.configure({ 'stick-brace': '\n' });
33+
assert.equal(
34+
comb.processString(
35+
'a { color: red }' +
36+
'a{color:red}' +
37+
'a \t\t \n{color:red}' +
38+
'a /* foo */ {color:red}'
39+
),
40+
'a\n{ color: red }' +
41+
'a\n{color:red}' +
42+
'a\n{color:red}' +
43+
'a /* foo */\n{color:red}'
44+
);
45+
});
46+
});

test/strip-space.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var Comb = require('../lib/csscomb');
2+
var assert = require('assert');
3+
4+
describe('options/strip-space', function() {
5+
var comb;
6+
beforeEach(function() {
7+
comb = new Comb();
8+
});
9+
it('Invalid value should not change trim trailing brace', function() {
10+
comb.configure({ 'strip-spaces': 'foobar' });
11+
assert.equal(
12+
comb.processString('a { color: red }'),
13+
'a { color: red }'
14+
);
15+
});
16+
it('True Boolean value should trim all trailing spaces', function() {
17+
comb.configure({ 'strip-spaces': true });
18+
assert.equal(
19+
comb.processString(
20+
'a { color: red } \n' +
21+
'a{color:red}\t /* foobar */ \n' +
22+
'a {color:red}'
23+
),
24+
'a { color: red }\n' +
25+
'a{color:red}\t /* foobar */\n' +
26+
'a {color:red}\n'
27+
);
28+
});
29+
});

0 commit comments

Comments
 (0)