Skip to content

Commit 7cbbe70

Browse files
committed
Colon space: Change acceptable values
`colon-space` option now accepts only `{Array}` of these kinds of values: - `{Number}` of spaces; - `{String}` of whitespaces and tabs. If there is any other character in the string, the value will not be set. The first element of array is spaces before colon, and second one is spaces after colon.
1 parent 91486e0 commit 7cbbe70

4 files changed

Lines changed: 51 additions & 172 deletions

File tree

.csscomb.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
],
66
"always-semicolon": true,
77
"block-indent": " ",
8-
"colon-space": true,
8+
"colon-space": ["", " "],
99
"color-case": "lower",
1010
"color-shorthand": true,
1111
"combinator-space": true,

README.md

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ Example configuration:
150150

151151
"always-semicolon": true,
152152
"block-indent": " ",
153-
"colon-space": true,
153+
"colon-space": ["", " "],
154154
"color-case": "lower",
155155
"color-shorthand": true,
156156
"element-case": "lower",
@@ -370,57 +370,35 @@ a { color: red
370370
371371
### colon-space
372372
373-
Available values:
374-
* `{Boolean}` `true` (means `after`) or `false` (no whitespace at all)
375-
* `{String}`: `before`, `after`, `both` or any combination of whitespaces
376-
and/or a colon (` `, `: `, `\t:\n\t` etc.)
377-
* `{Array}` with two `{String}` values: for setting left and right whitespace around a colon
378-
379-
Example: `{ "colon-space": true }`
380-
381-
```css
382-
/* before */
383-
a { color:red }
384-
385-
/* after */
386-
a { color: red }
387-
```
388-
389-
Example: `{ "colon-space": ":\n\t\t" }`
390-
391-
```css
392-
/* before */
393-
a {
394-
color: red;
395-
}
373+
Acceptable value is `{Array}` with 2 elements of following types:
374+
* `{Number}` of spaces;
375+
* `{String}` of whitespaces or tabs. If there is any other character in the
376+
string, the value will not be set.
396377
397-
/* after */
398-
a {
399-
color:
400-
red;
401-
}
402-
```
378+
The first element of the array sets spaces before colon, and the second one sets
379+
spaces after colon.
403380
404-
Example: `{ "colon-space": "" }`
381+
Example: `{ "colon-space": ["\t", "\t"] }`
405382
406383
```css
407384
/* before */
408385
a { color: red }
409386
410387
/* after */
411-
a { color:red }
388+
a { color : red }
412389
```
413390
414-
Example: `{ "colon-space": ["\t", "\t"] }`
391+
Example: `{ "colon-space": [0, 1] }`
415392
416393
```css
417394
/* before */
418-
a { color: red }
395+
a { color:red }
419396
420397
/* after */
421-
a { color : red }
398+
a { color: red }
422399
```
423400
401+
424402
### color-case
425403
426404
Available values: `{String}` `lower` or `upper`

lib/options/colon-space.js

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,31 @@ module.exports = {
33
/**
44
* Sets handler value.
55
*
6-
* @param {String|Boolean} value Option value
6+
* @param {Array} value Option value
77
* @returns {Object|undefined}
88
*/
99
setValue: function(value) {
10-
this._value = false;
11-
if (value === true)
12-
value = 'after';
13-
if (value === false)
14-
value = '';
15-
if (value === 'both')
16-
this._value = [' ', ' '];
17-
if (value === 'before')
18-
this._value = [' ', ''];
19-
if (value === 'after')
20-
this._value = ['', ' '];
21-
if (value.constructor === Array && value[0].match(/^[ \t]*$/) && value[1].match(/^[ \t]*$/))
22-
this._value = value;
23-
if (typeof value === 'string') {
24-
if (value.match(/^[ \t]*$/)) {
25-
this._value = ['', value];
26-
} else {
27-
var detectSpaces = value.match(/^(([ \t]*):)?([ \t]*)$/);
28-
if (detectSpaces) {
29-
if (detectSpaces[1]) {
30-
this._value = [detectSpaces[2], detectSpaces[3]];
31-
} else {
32-
this._value = ['', detectSpaces[3]];
33-
}
34-
}
35-
}
10+
delete this._value;
11+
12+
if (value.constructor !== Array) return;
13+
14+
if (typeof value[0] === 'number' &&
15+
value[0] === Math.abs(Math.round(value[0]))) {
16+
value[0] = new Array(value[0] + 1).join(' ');
17+
} else if (typeof value[0] !== 'string' ||
18+
!value[0].match(/^[ \t\n]*$/)) {
19+
return;
20+
}
21+
22+
if (typeof value[1] === 'number' &&
23+
value[1] === Math.abs(Math.round(value[1]))) {
24+
value[1] = new Array(value[1] + 1).join(' ');
25+
} else if (typeof value[1] !== 'string' ||
26+
!value[1].match(/^[ \t\n]*$/)) {
27+
return;
3628
}
3729

38-
if (!this._value) return;
30+
this._value = value;
3931
return this;
4032
},
4133

test/colon-space.js

Lines changed: 17 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,25 @@ var assert = require('assert');
33

44
describe('options/colon-space', function() {
55
var comb;
6+
67
beforeEach(function() {
78
comb = new Comb();
89
});
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-
);
10+
11+
it('String value should not change space around colon', function() {
12+
var input = 'a { color : red }';
13+
comb.configure({ 'colon-space': ' ' });
14+
assert.equal(comb.processString(input), input);
1515
});
16-
it('True Boolean value should set space after colon', function() {
16+
17+
it('Boolean value should not change space around colon', function() {
18+
var input = 'a { color : red }';
1719
comb.configure({ 'colon-space': true });
20+
assert.equal(comb.processString(input), input);
21+
});
22+
23+
it('Array of strings should set proper space around colon', function() {
24+
comb.configure({ 'colon-space': ['', ' '] });
1825
assert.equal(
1926
comb.processString(
2027
'a { color: red }' +
@@ -30,25 +37,9 @@ describe('options/colon-space', function() {
3037
'a {color /* bar */: red }'
3138
);
3239
});
33-
it('False Boolean value should set no space around colon', function() {
34-
comb.configure({ 'colon-space': false });
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('String `after` value should set space after colon', function() {
51-
comb.configure({ 'colon-space': 'after' });
40+
41+
it('Array of numbers should set proper space around colon', function() {
42+
comb.configure({ 'colon-space': [0, 1] });
5243
assert.equal(
5344
comb.processString(
5445
'a { color: red }' +
@@ -64,88 +55,6 @@ describe('options/colon-space', function() {
6455
'a {color /* bar */: red }'
6556
);
6657
});
67-
it('String `before` value should set space before colon', function() {
68-
comb.configure({ 'colon-space': 'before' });
69-
assert.equal(
70-
comb.processString(
71-
'a { color: red }' +
72-
'a{color:red}' +
73-
'a {color : red}' +
74-
'a {color : /* foo */ red }' +
75-
'a {color /* bar */ : red }'
76-
),
77-
'a { color :red }' +
78-
'a{color :red}' +
79-
'a {color :red}' +
80-
'a {color :/* foo */ red }' +
81-
'a {color /* bar */ :red }'
82-
);
83-
});
84-
it('String `both` value should set spaces around colon', function() {
85-
comb.configure({ 'colon-space': 'both' });
86-
assert.equal(
87-
comb.processString(
88-
'a { color: red }' +
89-
'a{color:red}' +
90-
'a {color : red}'
91-
),
92-
'a { color : red }' +
93-
'a{color : red}' +
94-
'a {color : red}'
95-
);
96-
});
97-
it('String ` ` value should set two spaces after colon', function() {
98-
comb.configure({ 'colon-space': ' ' });
99-
assert.equal(
100-
comb.processString(
101-
'a { color: red }' +
102-
'a{color:red}' +
103-
'a {color : red}'
104-
),
105-
'a { color: red }' +
106-
'a{color: red}' +
107-
'a {color: red}'
108-
);
109-
});
110-
it('String `:` value should set no space around colon', function() {
111-
comb.configure({ 'colon-space': ':' });
112-
assert.equal(
113-
comb.processString(
114-
'a { color: red }' +
115-
'a{color:red}' +
116-
'a {color : red}'
117-
),
118-
'a { color:red }' +
119-
'a{color:red}' +
120-
'a {color:red}'
121-
);
122-
});
123-
it('String `` value should set no space around colon', function() {
124-
comb.configure({ 'colon-space': '' });
125-
assert.equal(
126-
comb.processString(
127-
'a { color: red }' +
128-
'a{color:red}' +
129-
'a {color : red}'
130-
),
131-
'a { color:red }' +
132-
'a{color:red}' +
133-
'a {color:red}'
134-
);
135-
});
136-
it('String `\\t:\\t` value should set tabs around colon', function() {
137-
comb.configure({ 'colon-space': '\t:\t' });
138-
assert.equal(
139-
comb.processString(
140-
'a { color: red }' +
141-
'a{color:red}' +
142-
'a {color : red}'
143-
),
144-
'a { color\t:\tred }' +
145-
'a{color\t:\tred}' +
146-
'a {color\t:\tred}'
147-
);
148-
});
14958

15059
// Helper to check the detection
15160
function should_detect(options, a, b) {

0 commit comments

Comments
 (0)