Skip to content

Commit a261945

Browse files
committed
Combinator space: Change acceptable values
`combinator-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 combinator, and second one is spaces after combinator.
1 parent 7cbbe70 commit a261945

4 files changed

Lines changed: 78 additions & 143 deletions

File tree

.csscomb.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"colon-space": ["", " "],
99
"color-case": "lower",
1010
"color-shorthand": true,
11-
"combinator-space": true,
11+
"combinator-space": [" ", " "],
1212
"element-case": "lower",
1313
"eof-newline": true,
1414
"leading-zero": false,

README.md

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -429,42 +429,36 @@ b { color: #fc0 }
429429
430430
### combinator-space
431431
432-
Available values:
433-
* `{Boolean}`: `true` sets one space, `false` removes the spaces.
434-
* `{String}`: any combination of whitespaces.
435-
* `{Array}` with two `{String}` values: for setting left and right whitespace.
436-
437-
Example: `{ "combinator-space": true }`
438-
439-
```css
440-
/* before */
441-
a>b { color: red }
432+
Acceptable value is `{Array}` with 2 elements of following types:
433+
* `{Number}` of spaces;
434+
* `{String}` of whitespaces, tabs or new lines. If there is any other
435+
character in the string, the value will not be set.
442436
443-
/* after */
444-
a > b { color: red }
445-
```
437+
The first element of the array sets spaces before combinator, and the second
438+
one sets spaces after combinator.
446439
447-
Example: `{ "combinator-space": "" }`
440+
Example: `{ "combinator-space": [" ", "\n"] }`
448441
449442
```css
450443
/* before */
451-
a > b { color: red }
444+
a>b { color: red }
452445
453446
/* after */
454-
a>b { color: red }
447+
a >
448+
b { color: red }
455449
```
456450
457-
Example: `{ "combinator-space": [" ", "\n"] }`
451+
Example: `{ "combinator-space": [1, 1] }`
458452
459453
```css
460454
/* before */
461455
a>b { color: red }
462456
463457
/* after */
464-
a >
465-
b { color: red }
458+
a > b { color: red }
466459
```
467460
461+
468462
### element-case
469463
470464
Available values: `{String}` `lower` or `upper`

lib/options/combinator-space.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,33 @@ module.exports = {
33
/**
44
* Sets handler value.
55
*
6-
* @param {String|Boolean|Array} value Option value
7-
* @returns {Object}
6+
* @param {Array} value Option value
7+
* @returns {Object|undefined}
88
*/
99
setValue: function(value) {
10-
this._value = false;
11-
if (value === true) value = ' ';
12-
if (value === false) value = '';
13-
if (typeof value === 'string' && value.match(/^[ \t\n]*$/)) {
14-
this._value = [value, value];
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;
1520
}
16-
if (value.constructor === Array) this._value = value;
17-
if (!this._value) return;
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;
28+
}
29+
30+
this._value = value;
1831
return this;
32+
1933
},
2034

2135
/**

test/combinator-space.js

Lines changed: 41 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -3,105 +3,31 @@ var assert = require('assert');
33

44
describe('options/combinator-space', function() {
55
var comb;
6+
67
beforeEach(function() {
78
comb = new Comb();
89
});
9-
it('Invalid String should not change space around combinator', function() {
10+
11+
it('Number value should not change space around combinator', function() {
12+
var input = 'a >b { color: red }';
13+
comb.configure({ 'combinator-space': 2 });
14+
assert.equal(comb.processString(input), input);
15+
});
16+
17+
it('String value should not change space around combinator', function() {
18+
var input = 'a >b { color: red }';
1019
comb.configure({ 'combinator-space': 'foobar' });
11-
assert.equal(
12-
comb.processString(
13-
'a >b { color: red }' +
14-
'a ~b { color: red }' +
15-
'a +b { color: red }'
16-
),
17-
'a >b { color: red }' +
18-
'a ~b { color: red }' +
19-
'a +b { color: red }'
20-
);
20+
assert.equal(comb.processString(input), input);
2121
});
22-
it('True Boolean value should set space around combinator to one space', function() {
22+
23+
it('Boolean value should not change space around combinator', function() {
24+
var input = 'a >b { color: red }';
2325
comb.configure({ 'combinator-space': true });
24-
assert.equal(
25-
comb.processString(
26-
'a>b { color: red }' +
27-
'a> b { color: red }' +
28-
'a >b { color: red }' +
29-
'a+b { color: red }' +
30-
'a+ b { color: red }' +
31-
'a +b { color: red }' +
32-
'a~b { color: red }' +
33-
'a~ b { color: red }' +
34-
'a ~b { color: red }' +
35-
'a ~b+ c>d { color: red }'
36-
),
37-
'a > b { color: red }' +
38-
'a > b { color: red }' +
39-
'a > b { color: red }' +
40-
'a + b { color: red }' +
41-
'a + b { color: red }' +
42-
'a + b { color: red }' +
43-
'a ~ b { color: red }' +
44-
'a ~ b { color: red }' +
45-
'a ~ b { color: red }' +
46-
'a ~ b + c > d { color: red }'
47-
);
48-
});
49-
it('False Boolean value should remove spaces around combinator', function() {
50-
comb.configure({ 'combinator-space': false });
51-
assert.equal(
52-
comb.processString(
53-
'a>b { color: red }' +
54-
'a> b { color: red }' +
55-
'a >b { color: red }' +
56-
'a+b { color: red }' +
57-
'a+ b { color: red }' +
58-
'a +b { color: red }' +
59-
'a~b { color: red }' +
60-
'a~ b { color: red }' +
61-
'a ~b { color: red }' +
62-
'a ~b+ c>d { color: red }'
63-
),
64-
'a>b { color: red }' +
65-
'a>b { color: red }' +
66-
'a>b { color: red }' +
67-
'a+b { color: red }' +
68-
'a+b { color: red }' +
69-
'a+b { color: red }' +
70-
'a~b { color: red }' +
71-
'a~b { color: red }' +
72-
'a~b { color: red }' +
73-
'a~b+c>d { color: red }'
74-
);
26+
assert.equal(comb.processString(input), input);
7527
});
76-
it('String `` value should remove spaces around combinator', function() {
77-
comb.configure({ 'combinator-space': '' });
78-
assert.equal(
79-
comb.processString(
80-
'a>b { color: red }' +
81-
'a> b { color: red }' +
82-
'a >b { color: red }' +
83-
'a+b { color: red }' +
84-
'a+ b { color: red }' +
85-
'a +b { color: red }' +
86-
'a~b { color: red }' +
87-
'a~ b { color: red }' +
88-
'a ~b { color: red }' +
89-
'a ~b+ c>d { color: red }'
90-
),
91-
'a>b { color: red }' +
92-
'a>b { color: red }' +
93-
'a>b { color: red }' +
94-
'a+b { color: red }' +
95-
'a+b { color: red }' +
96-
'a+b { color: red }' +
97-
'a~b { color: red }' +
98-
'a~b { color: red }' +
99-
'a~b { color: red }' +
100-
'a~b+c>d { color: red }'
101-
);
102-
});
103-
it('String ` ` value should set two spaces around combinator', function() {
104-
comb.configure({ 'combinator-space': ' ' });
28+
29+
it('Array of strings should set proper spaces around combinator', function() {
30+
comb.configure({ 'combinator-space': [' ', '\n'] });
10531
assert.equal(
10632
comb.processString(
10733
'a>b { color: red }' +
@@ -115,20 +41,21 @@ describe('options/combinator-space', function() {
11541
'a ~b { color: red }' +
11642
'a ~b+ c>d { color: red }'
11743
),
118-
'a > b { color: red }' +
119-
'a > b { color: red }' +
120-
'a > b { color: red }' +
121-
'a + b { color: red }' +
122-
'a + b { color: red }' +
123-
'a + b { color: red }' +
124-
'a ~ b { color: red }' +
125-
'a ~ b { color: red }' +
126-
'a ~ b { color: red }' +
127-
'a ~ b + c > d { color: red }'
44+
'a >\nb { color: red }' +
45+
'a >\nb { color: red }' +
46+
'a >\nb { color: red }' +
47+
'a +\nb { color: red }' +
48+
'a +\nb { color: red }' +
49+
'a +\nb { color: red }' +
50+
'a ~\nb { color: red }' +
51+
'a ~\nb { color: red }' +
52+
'a ~\nb { color: red }' +
53+
'a ~\nb +\nc >\nd { color: red }'
12854
);
12955
});
130-
it('Array value should set different spaces around combinator', function() {
131-
comb.configure({ 'combinator-space': [' ', '\n'] });
56+
57+
it('Array of numbers should set proper spaces around combinator', function() {
58+
comb.configure({ 'combinator-space': [0, 1] });
13259
assert.equal(
13360
comb.processString(
13461
'a>b { color: red }' +
@@ -142,16 +69,16 @@ describe('options/combinator-space', function() {
14269
'a ~b { color: red }' +
14370
'a ~b+ c>d { color: red }'
14471
),
145-
'a >\nb { color: red }' +
146-
'a >\nb { color: red }' +
147-
'a >\nb { color: red }' +
148-
'a +\nb { color: red }' +
149-
'a +\nb { color: red }' +
150-
'a +\nb { color: red }' +
151-
'a ~\nb { color: red }' +
152-
'a ~\nb { color: red }' +
153-
'a ~\nb { color: red }' +
154-
'a ~\nb +\nc >\nd { color: red }'
72+
'a> b { color: red }' +
73+
'a> b { color: red }' +
74+
'a> b { color: red }' +
75+
'a+ b { color: red }' +
76+
'a+ b { color: red }' +
77+
'a+ b { color: red }' +
78+
'a~ b { color: red }' +
79+
'a~ b { color: red }' +
80+
'a~ b { color: red }' +
81+
'a~ b+ c> d { color: red }'
15582
);
15683
});
15784

0 commit comments

Comments
 (0)