Skip to content

Commit

Permalink
New option: space-before-combinator
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyganch committed Jun 9, 2014
1 parent 207075d commit f2a4521
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/csscomb.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"remove-empty-rulesets": true,
"space-after-colon": " ",
"space-before-colon": "",
"space-before-combinator": " ",
"strip-spaces": true,
"unitless-zero": true,
"vendor-prefix-align": true,
Expand Down
30 changes: 30 additions & 0 deletions doc/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,36 @@ a {
}
```

## space-before-combinator

Set space before combinator (for example, in selectors like `p > a`).

Acceptable values:

* `{Number}` — number of whitespaces;
* `{String}` — string with whitespaces, tabs or line breaks.

Example: `{ 'space-before-combinator': 1 }`

```scss
// Before:
p>a { color: panda; }

// After:
p >a { color: panda; }
```

Example: `{ 'space-before-combinator': '\n' }`

```scss
// Before:
p > a { color: panda; }

// After:
p
> a { color: panda; }
```

## strip-spaces

Whether to trim trailing spaces.
Expand Down
1 change: 1 addition & 0 deletions lib/csscomb.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var OPTIONS = [
'quotes',
'strip-spaces',
'eof-newline',
'space-before-combinator',
'space-before-colon',
'space-after-colon',
'sort-order',
Expand Down
60 changes: 60 additions & 0 deletions lib/options/space-before-combinator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module.exports = {
name: 'space-before-combinator',

accepts: {
number: true,
string: /^[ \t\n]*$/
},

/**
* Processes tree node.
*
* @param {String} nodeType
* @param {node} node
*/
process: function(nodeType, node) {
if (nodeType !== 'selector') return;

var value = this.getValue('space-before-combinator');

for (var i = node.length; i--;) {
var subSelector = node[i];
for (var j = subSelector.length; j--;) {
if (subSelector[j][0] !== 'combinator') continue;
if (subSelector[j - 1][0] === 's') {
subSelector[j - 1][1] = value;
} else {
subSelector.splice(j, 0, ['s', value]);
}
}
}
},

/**
* Detects the value of an option at the tree node.
*
* @param {String} nodeType
* @param {node} node
*/
detect: function(nodeType, node) {
if (nodeType !== 'selector') return;

var variants = [];

for (var i = node.length; i--;) {
var subSelector = node[i];
for (var j = subSelector.length; j--;) {
if (subSelector[j][0] !== 'combinator') continue;

if (subSelector[j - 1][0] === 's') {
variants.push(subSelector[j - 1][1]);
} else {
variants.push('');
}
}
}

return variants;
}
};

84 changes: 84 additions & 0 deletions test/options/space-before-combinator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
describe('options/space-before-combinator:', function() {
beforeEach(function() {
this.filename = __filename;
});

it('Array value => should not change anything', function() {
this.comb.configure({ 'space-before-combinator': ['', ' '] });
this.shouldBeEqual('test.css');
});

it('Invalid string value => should not change anything', function() {
this.comb.configure({ 'space-before-combinator': ' nani ' });
this.shouldBeEqual('test.css');
});

it('Float number value => should not change anything', function() {
this.comb.configure({ 'space-before-combinator': 3.5 });
this.shouldBeEqual('test.css');
});

it('Integer value => should set proper space before combinator', function() {
this.comb.configure({ 'space-before-combinator': 0 });
this.shouldBeEqual('test.css', 'test.expected.css');
});

it('Valid string value (spaces only) => should set proper space before combinator', function() {
this.comb.configure({ 'space-before-combinator': ' ' });
this.shouldBeEqual('test.css', 'test-2.expected.css');
});

it('Valid string value (spaces and newlines) => should set proper space before combinator', function() {
this.comb.configure({ 'space-before-combinator': '\n ' });
this.shouldBeEqual('test.css', 'test-3.expected.css');
});

it('Should detect no whitespaces before combinator', function() {
this.shouldDetect(
['space-before-combinator'],
'a+b { color:red }',
{ 'space-before-combinator': '' }
);
});

it('Should detect a space before combinator', function() {
this.shouldDetect(
['space-before-combinator'],
'a + b { color:red }',
{ 'space-before-combinator': ' ' }
);
});

it('Should detect a space before combinator in long selector', function() {
this.shouldDetect(
['space-before-combinator'],
'a + b ~ c>d { color:red }',
{ 'space-before-combinator': ' ' }
);
});

it('Should detect a space before combinator in long selector, test 2', function() {
this.shouldDetect(
['space-before-combinator'],
'a>b + c + d { color:red }',
{ 'space-before-combinator': ' ' }
);
});

it('Should detect no whitespaces before combinator in long selector', function() {
this.shouldDetect(
['space-before-combinator'],
'a+b ~ c+d { color:red }',
{ 'space-before-combinator': '' }
);
});

it('Shouldn’t detect whitespaces before combinator in selector without combinators', function() {
this.shouldDetect(
['space-before-combinator'],
'a { color:red }',
{}
);
});
});

10 changes: 10 additions & 0 deletions test/options/space-before-combinator/test-2.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
a >b { color: red }
a > b { color: red }
a >b { color: red }
a +b { color: red }
a + b { color: red }
a +b { color: red }
a ~b { color: red }
a ~ b { color: red }
a ~b { color: red }
a ~b + c >d { color: red }
22 changes: 22 additions & 0 deletions test/options/space-before-combinator/test-3.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
a
>b { color: red }
a
> b { color: red }
a
>b { color: red }
a
+b { color: red }
a
+ b { color: red }
a
+b { color: red }
a
~b { color: red }
a
~ b { color: red }
a
~b { color: red }
a
~b
+ c
>d { color: red }
10 changes: 10 additions & 0 deletions test/options/space-before-combinator/test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
a>b { color: red }
a> b { color: red }
a >b { color: red }
a+b { color: red }
a+ b { color: red }
a +b { color: red }
a~b { color: red }
a~ b { color: red }
a ~b { color: red }
a ~b+ c>d { color: red }
10 changes: 10 additions & 0 deletions test/options/space-before-combinator/test.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
a>b { color: red }
a> b { color: red }
a>b { color: red }
a+b { color: red }
a+ b { color: red }
a+b { color: red }
a~b { color: red }
a~ b { color: red }
a~b { color: red }
a~b+ c>d { color: red }

0 comments on commit f2a4521

Please sign in to comment.