Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored and updated the colon-space option #77

Merged
merged 1 commit into from
Sep 25, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ a { color: red
### colon-space

Available values:
* `{Boolean}` `true` (means `after`)
* `{Boolean}` `true` (means `after`) or `false` (no whitespace at all)
* `{String}`: `before`, `after`, `both` or any combination of whitespaces
and/or a colon (` `, `: `, `\t:\n\t` etc.)
* `{Array}` with two `{String}` values: for setting left and right whitespace around a colon

Example: `{ "colon-space": true }`

Expand All @@ -151,7 +152,7 @@ a {
}
```

Example: `{ "colon-space": ":" }`
Example: `{ "colon-space": "" }`

```css
/* before */
Expand All @@ -161,6 +162,16 @@ a { color: red }
a { color:red }
```

Example: `{ "colon-space": ["\t", "\t"] }`

```css
/* before */
a { color: red }

/* after */
a { color : red }
```

### color-case

Available values: `{String}` `lower` or `upper`
Expand Down
38 changes: 27 additions & 11 deletions lib/options/colon-space.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,33 @@ module.exports = {
*/
setValue: function(value) {
this._value = false;
if (value === true) this._value = 'after';
if (typeof value === 'string'&& value.match(/after|before|both|(?=.*(?:[ \t\n]|:))[ \t\n]*:?[ \t\n]*/)) {
if (value === true)
value = 'after';
if (value === false)
value = '';
if (value === 'both')
this._value = [' ', ' '];
if (value === 'before')
this._value = [' ', ''];
if (value === 'after')
this._value = ['', ' '];
if (value.constructor === Array && value[0].match(/^[ \t]*$/) && value[1].match(/^[ \t]*$/))
this._value = value;
if (typeof value === 'string') {
if (value.match(/^[ \t]*$/)) {
this._value = ['', value];
} else {
var detectSpaces = value.match(/^(([ \t]*):)?([ \t]*)$/);
if (detectSpaces) {
if (detectSpaces[1]) {
this._value = [detectSpaces[2], detectSpaces[3]];
} else {
this._value = ['', detectSpaces[3]];
}
}
}
}

if (!this._value) return;
return this;
},
Expand All @@ -22,20 +45,13 @@ module.exports = {
* @param {node} node
*/
process: function(nodeType, node) {
var detectSpaces = this._value.match(/(([ \t\n]*):)?([ \t\n]*)/);
if (nodeType === 'property') {
if (node[node.length - 1][0] === 's') node.pop();
if (this._value === 'both' || this._value === 'before')
node.push(['s', ' ']);
if (detectSpaces && detectSpaces[1])
node.push(['s', detectSpaces[2]]);
if (this._value[0] !== '') node.push(['s', this._value[0]]);
}
if (nodeType === 'value') {
if (node[0][0] === 's') node.shift();
if (this._value === 'both' || this._value === 'after')
node.unshift(['s', ' ']);
if (detectSpaces)
node.unshift(['s', detectSpaces[3]]);
if (this._value[1] !== '') node.unshift(['s', this._value[1]]);
}
}

Expand Down
39 changes: 28 additions & 11 deletions test/colon-space.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,24 @@ describe('options/colon-space', function() {
'a {color /* bar */: red }'
);
});
it('`after` value should set space after colon', function() {
it('False Boolean value should set no space around colon', function() {
comb.configure({ 'colon-space': false });
assert.equal(
comb.processString(
'a { color: red }' +
'a{color:red}' +
'a {color : red}' +
'a {color : /* foo */ red }' +
'a {color /* bar */ : red }'
),
'a { color:red }' +
'a{color:red}' +
'a {color:red}' +
'a {color:/* foo */ red }' +
'a {color /* bar */:red }'
);
});
it('String `after` value should set space after colon', function() {
comb.configure({ 'colon-space': 'after' });
assert.equal(
comb.processString(
Expand All @@ -47,7 +64,7 @@ describe('options/colon-space', function() {
'a {color /* bar */: red }'
);
});
it('`before` value should set space before colon', function() {
it('String `before` value should set space before colon', function() {
comb.configure({ 'colon-space': 'before' });
assert.equal(
comb.processString(
Expand All @@ -64,7 +81,7 @@ describe('options/colon-space', function() {
'a {color /* bar */ :red }'
);
});
it('`both` value should set spaces around colon', function() {
it('String `both` value should set spaces around colon', function() {
comb.configure({ 'colon-space': 'both' });
assert.equal(
comb.processString(
Expand All @@ -77,7 +94,7 @@ describe('options/colon-space', function() {
'a {color : red}'
);
});
it('` ` value should set two spaces after colon', function() {
it('String ` ` value should set two spaces after colon', function() {
comb.configure({ 'colon-space': ' ' });
assert.equal(
comb.processString(
Expand All @@ -90,7 +107,7 @@ describe('options/colon-space', function() {
'a {color: red}'
);
});
it('`:` value should set no space around colon', function() {
it('String `:` value should set no space around colon', function() {
comb.configure({ 'colon-space': ':' });
assert.equal(
comb.processString(
Expand All @@ -103,20 +120,20 @@ describe('options/colon-space', function() {
'a {color:red}'
);
});
it('`\\n:` value should set a newline before colon', function() {
comb.configure({ 'colon-space': '\n:' });
it('String `` value should set no space around colon', function() {
comb.configure({ 'colon-space': '' });
assert.equal(
comb.processString(
'a { color: red }' +
'a{color:red}' +
'a {color : red}'
),
'a { color\n:red }' +
'a{color\n:red}' +
'a {color\n:red}'
'a { color:red }' +
'a{color:red}' +
'a {color:red}'
);
});
it('`\\t:\\t` value should set tabs around colon', function() {
it('String `\\t:\\t` value should set tabs around colon', function() {
comb.configure({ 'colon-space': '\t:\t' });
assert.equal(
comb.processString(
Expand Down