Skip to content

Commit 9d34b32

Browse files
committed
Merge pull request #24 from csscomb/m.indent
Option: rule-indent
2 parents f2db919 + 524ed6a commit 9d34b32

5 files changed

Lines changed: 142 additions & 32 deletions

File tree

lib/csscomb.js

Lines changed: 5 additions & 3 deletions
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 = {};
@@ -54,21 +54,23 @@ Comb.prototype = {
5454
* @returns {Array}
5555
*/
5656
processTree: function(tree) {
57-
this.processNode(['tree', tree]);
57+
this.processNode(['tree', tree], 0);
5858
return tree;
5959
},
6060

6161
/**
6262
* Processes tree node.
6363
* @param {Array} node Tree node
64+
* @param {Number} level Indent level
6465
*/
6566
processNode: function(node, level) {
6667
var _this = this;
6768
node.forEach(function(node) {
6869
if (!Array.isArray(node)) return;
6970
var nodeType = node.shift();
71+
if (nodeType === 'atrulers') level++;
7072
_this._handlers.forEach(function(handler) {
71-
handler.process(nodeType, node);
73+
handler.process(nodeType, node, level);
7274
});
7375
node.unshift(nodeType);
7476
_this.processNode(node, level);

lib/options/rule-indent.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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, level) {
24+
// increasing indent level
25+
if (nodeType === 'block') {
26+
var value = '\n' + new Array(level + 2).join(this._value);
27+
if (node[0][0] !== 's') {
28+
node.unshift(['s', '']);
29+
}
30+
node.forEach(function(nodeItem, i) {
31+
if (nodeItem[0] === 'declaration') {
32+
var space = node[i - 1];
33+
var tail;
34+
35+
if (space[0] !== 's') {
36+
space = ['s', ''];
37+
tail = node.splice(i);
38+
tail.unshift(space);
39+
Array.prototype.push.apply(node, tail);
40+
}
41+
42+
// replacing last line space by value:
43+
// '' => '\n\t'
44+
// '\n ' => '\n\t'
45+
// '\n \n ' => '\n \n\t'
46+
space[1] = space[1].replace(/(\n)?([\t ]+)?$/, value);
47+
}
48+
});
49+
}
50+
}
51+
52+
};

test/integral.expect.css

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,87 @@
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 */
1919
{ /* foobar */
2020
.input__control
2121
{
22-
color: #000;
23-
font-size: 100%;
24-
margin: 0;
25-
position: relative;
26-
width: 100%;
27-
z-index: 3;
22+
color: #000;
23+
font-size: 100%;
24+
margin: 0;
25+
position: relative;
26+
width: 100%;
27+
z-index: 3;
2828
}}
2929

3030
@media screen and (min-width: 35em) /* screen */,
3131
print and (min-width: 40em) /* print */
3232
{ /* foobar */
3333
.input__control
3434
{
35-
-moz-box-sizing: border-box;
36-
background: none;
37-
border: 0;
38-
box-sizing: border-box;
39-
outline: 0;
40-
padding: 0.4em 0;
35+
-moz-box-sizing: border-box;
36+
background: none;
37+
border: 0;
38+
box-sizing: border-box;
39+
outline: 0;
40+
padding: 0.4em 0;
4141
}
4242
}
4343

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)