Skip to content

Commit 508c05e

Browse files
committed
option: rule-indent
1 parent f2db919 commit 508c05e

File tree

5 files changed

+125
-18
lines changed

5 files changed

+125
-18
lines changed

lib/csscomb.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ var Comb = function() {
1414
'strip-spaces',
1515
'stick-brace',
1616
'colon-space',
17-
'rule-indent',
1817
'always-semicolon',
18+
'rule-indent',
1919
'sort-order'
2020
];
2121
this._config = {};

lib/options/rule-indent.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module.exports = {
2+
3+
/**
4+
* Sets handler value.
5+
*
6+
* @param {String|Number|Boolean} value Option value
7+
* @returns {Object}
8+
*/
9+
setValue: function(value) {
10+
this._value = false;
11+
if (value === true) value = 4;
12+
if (typeof value === 'number' && value === Math.abs(Math.round(value))) value = new Array(value + 1).join(' ');
13+
if (typeof value === 'string' && value.match(/^[ \t]*$/)) this._value = value;
14+
if (!this._value) return;
15+
return this;
16+
},
17+
18+
/**
19+
* Processes tree node.
20+
* @param {String} nodeType
21+
* @param {node} node
22+
*/
23+
process: function(nodeType, node) {
24+
if (nodeType === 'block') {
25+
var value = '\n' + this._value;
26+
if (node[0][0] !== 's') {
27+
node.unshift(['s', '']);
28+
}
29+
node.forEach(function(nodeItem, i) {
30+
if (nodeItem[0] === 'declaration') {
31+
var space = node[i - 1];
32+
var tail;
33+
34+
if (space[0] !== 's') {
35+
space = ['s', ''];
36+
tail = node.splice(i);
37+
tail.unshift(space);
38+
Array.prototype.push.apply(node, tail);
39+
}
40+
41+
// replacing last line space by value:
42+
// '' => '\n\t'
43+
// '\n ' => '\n\t'
44+
// '\n \n ' => '\n \n\t'
45+
space[1] = space[1].replace(/(\n)?([\t ]+)?$/, value);
46+
}
47+
});
48+
}
49+
}
50+
51+
};

test/integral.expect.css

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/* Фигурные скобки. Вариант 1 */
22
a, b, i /* foobar */
33
{
4-
padding: 0;
5-
margin: 0;
4+
padding: 0;
5+
margin: 0;
66
}
77
div p
88
{
99
font-size: 1px;
10-
top: 0;
10+
top: 0;
1111
}
1212
div p em
1313
{
14-
font-style: italic;
15-
border-bottom: 1px solid red;
14+
font-style: italic;
15+
border-bottom: 1px solid red;
1616
}
1717

1818
@media all /* media */
@@ -44,41 +44,44 @@ a, b, i /* foobar */
4444
/* Фигурные скобки. Вариант 2 */
4545
div
4646
{
47-
padding: 0;
48-
margin: 0;
47+
padding: 0;
48+
margin: 0;
4949
}
5050
div p
5151
{
5252
font-size: 1px;
53-
top: 0;
53+
top: 0;
5454
}
5555
div p em
5656
{
57-
font-style: italic;/* inline comment*/
58-
border-bottom: 1px solid red;
57+
font-style: italic;/* inline comment*/
58+
border-bottom: 1px solid red;
5959
}
6060

6161
/* Фигурные скобки. Вариант 3 */
6262
div
6363
{
64-
padding: 0;
65-
margin: 0;
64+
padding: 0;
65+
margin: 0;
6666
}
6767
/* foo */ div p
6868
{
6969
font-size: 1px;
70-
top: 0;
70+
top: 0;
7171
}
7272
div p em
7373
{
7474
/* upline comment*/
75-
font-style: italic;
76-
border-bottom: 1px solid red; /* trololo */ /* trololo */
75+
font-style: italic;
76+
77+
border-bottom: 1px solid red; /* trololo */ /* trololo */
7778
}
7879

7980
a
8081
{
81-
top: 0;/* ololo */margin: 0;}
82+
top: 0;/* ololo */
83+
margin: 0;}
8284
b
8385
{
84-
top: 0/* trololo */;margin: 0;}
86+
top: 0/* trololo */;
87+
margin: 0;}

test/integral.origin.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ div {
6666
div p em {
6767
/* upline comment*/
6868
font-style:italic;
69+
6970
border-bottom:1px solid red /* trololo */ /* trololo */
7071
}
7172

test/rule-indent.js

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

0 commit comments

Comments
 (0)