Skip to content

Commit dea995f

Browse files
committed
Merge pull request #77 from kizu/colon-space-patch
Refactored and updated the colon-space option
2 parents 8d9da0f + c026cb5 commit dea995f

3 files changed

Lines changed: 68 additions & 24 deletions

File tree

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,10 @@ a { color: red
139139
### colon-space
140140
141141
Available values:
142-
* `{Boolean}` `true` (means `after`)
142+
* `{Boolean}` `true` (means `after`) or `false` (no whitespace at all)
143143
* `{String}`: `before`, `after`, `both` or any combination of whitespaces
144144
and/or a colon (` `, `: `, `\t:\n\t` etc.)
145+
* `{Array}` with two `{String}` values: for setting left and right whitespace around a colon
145146
146147
Example: `{ "colon-space": true }`
147148
@@ -168,7 +169,7 @@ a {
168169
}
169170
```
170171
171-
Example: `{ "colon-space": ":" }`
172+
Example: `{ "colon-space": "" }`
172173
173174
```css
174175
/* before */
@@ -178,6 +179,16 @@ a { color: red }
178179
a { color:red }
179180
```
180181
182+
Example: `{ "colon-space": ["\t", "\t"] }`
183+
184+
```css
185+
/* before */
186+
a { color: red }
187+
188+
/* after */
189+
a { color : red }
190+
```
191+
181192
### color-case
182193
183194
Available values: `{String}` `lower` or `upper`

lib/options/colon-space.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,33 @@ module.exports = {
88
*/
99
setValue: function(value) {
1010
this._value = false;
11-
if (value === true) this._value = 'after';
12-
if (typeof value === 'string'&& value.match(/after|before|both|(?=.*(?:[ \t\n]|:))[ \t\n]*:?[ \t\n]*/)) {
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]*$/))
1322
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+
}
1436
}
37+
1538
if (!this._value) return;
1639
return this;
1740
},
@@ -22,20 +45,13 @@ module.exports = {
2245
* @param {node} node
2346
*/
2447
process: function(nodeType, node) {
25-
var detectSpaces = this._value.match(/(([ \t\n]*):)?([ \t\n]*)/);
2648
if (nodeType === 'property') {
2749
if (node[node.length - 1][0] === 's') node.pop();
28-
if (this._value === 'both' || this._value === 'before')
29-
node.push(['s', ' ']);
30-
if (detectSpaces && detectSpaces[1])
31-
node.push(['s', detectSpaces[2]]);
50+
if (this._value[0] !== '') node.push(['s', this._value[0]]);
3251
}
3352
if (nodeType === 'value') {
3453
if (node[0][0] === 's') node.shift();
35-
if (this._value === 'both' || this._value === 'after')
36-
node.unshift(['s', ' ']);
37-
if (detectSpaces)
38-
node.unshift(['s', detectSpaces[3]]);
54+
if (this._value[1] !== '') node.unshift(['s', this._value[1]]);
3955
}
4056
}
4157

test/colon-space.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,24 @@ describe('options/colon-space', function() {
3030
'a {color /* bar */: red }'
3131
);
3232
});
33-
it('`after` value should set space after colon', function() {
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() {
3451
comb.configure({ 'colon-space': 'after' });
3552
assert.equal(
3653
comb.processString(
@@ -47,7 +64,7 @@ describe('options/colon-space', function() {
4764
'a {color /* bar */: red }'
4865
);
4966
});
50-
it('`before` value should set space before colon', function() {
67+
it('String `before` value should set space before colon', function() {
5168
comb.configure({ 'colon-space': 'before' });
5269
assert.equal(
5370
comb.processString(
@@ -64,7 +81,7 @@ describe('options/colon-space', function() {
6481
'a {color /* bar */ :red }'
6582
);
6683
});
67-
it('`both` value should set spaces around colon', function() {
84+
it('String `both` value should set spaces around colon', function() {
6885
comb.configure({ 'colon-space': 'both' });
6986
assert.equal(
7087
comb.processString(
@@ -77,7 +94,7 @@ describe('options/colon-space', function() {
7794
'a {color : red}'
7895
);
7996
});
80-
it('` ` value should set two spaces after colon', function() {
97+
it('String ` ` value should set two spaces after colon', function() {
8198
comb.configure({ 'colon-space': ' ' });
8299
assert.equal(
83100
comb.processString(
@@ -90,7 +107,7 @@ describe('options/colon-space', function() {
90107
'a {color: red}'
91108
);
92109
});
93-
it('`:` value should set no space around colon', function() {
110+
it('String `:` value should set no space around colon', function() {
94111
comb.configure({ 'colon-space': ':' });
95112
assert.equal(
96113
comb.processString(
@@ -103,20 +120,20 @@ describe('options/colon-space', function() {
103120
'a {color:red}'
104121
);
105122
});
106-
it('`\\n:` value should set a newline before colon', function() {
107-
comb.configure({ 'colon-space': '\n:' });
123+
it('String `` value should set no space around colon', function() {
124+
comb.configure({ 'colon-space': '' });
108125
assert.equal(
109126
comb.processString(
110127
'a { color: red }' +
111128
'a{color:red}' +
112129
'a {color : red}'
113130
),
114-
'a { color\n:red }' +
115-
'a{color\n:red}' +
116-
'a {color\n:red}'
131+
'a { color:red }' +
132+
'a{color:red}' +
133+
'a {color:red}'
117134
);
118135
});
119-
it('`\\t:\\t` value should set tabs around colon', function() {
136+
it('String `\\t:\\t` value should set tabs around colon', function() {
120137
comb.configure({ 'colon-space': '\t:\t' });
121138
assert.equal(
122139
comb.processString(

0 commit comments

Comments
 (0)