Skip to content

Commit

Permalink
New option: space-before-selector-delimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
vecmezoni authored and tonyganch committed Jun 9, 2014
1 parent dc55303 commit 8d78ac9
Show file tree
Hide file tree
Showing 8 changed files with 169 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 @@ -19,6 +19,7 @@
"space-before-colon": "",
"space-before-combinator": " ",
"space-before-opening-brace": "\n",
"space-before-selector-delimiter": "",
"strip-spaces": true,
"unitless-zero": true,
"vendor-prefix-align": true,
Expand Down
1 change: 1 addition & 0 deletions lib/csscomb.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var OPTIONS = [
'space-after-colon',
'space-before-opening-brace',
'space-after-opening-brace',
'space-before-selector-delimiter',
'block-indent',
'sort-order',
'space-before-closing-brace',
Expand Down
54 changes: 54 additions & 0 deletions lib/options/space-before-selector-delimiter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module.exports = {
name: 'space-before-selector-delimiter',

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-selector-delimiter');

for (var i = node.length; i--;) {
if (node[i][0] === 'delim') {
if (node[i - 1][node[i - 1].length - 1][0] === 's') {
node[i - 1][node[i - 1].length - 1][1] = value;
} else {
node[i - 1].push(['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--;) {
if (node[i][0] !== 'delim') continue;

if (node[i - 1][node[i - 1].length - 1][0] === 's') {
variants.push(node[i - 1][node[i - 1].length - 1][1]);
} else {
variants.push('');
}
}

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

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

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

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

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

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

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

it('Should detect no whitespace', function() {
this.shouldDetect(
['space-before-selector-delimiter'],
'a,b{top:0}',
{ 'space-before-selector-delimiter': '' }
);
});

it('Should detect whitespace', function() {
this.shouldDetect(
['space-before-selector-delimiter'],
'a \n ,b {top:0}',
{ 'space-before-selector-delimiter': ' \n ' }
);
});

it('Should detect no whitespace (2 blocks)', function() {
this.shouldDetect(
['space-before-selector-delimiter'],
'a,b{top:0} a ,b{left:0}',
{ 'space-before-selector-delimiter': '' }
);
});

it('Should detect whitespace (2 blocks)', function() {
this.shouldDetect(
['space-before-selector-delimiter'],
'a ,b {top:0} b,a{left:0}',
{ 'space-before-selector-delimiter': ' ' }
);
});

it('Should detect no whitespace (3 blocks)', function() {
this.shouldDetect(
['space-before-selector-delimiter'],
'a ,b{top:0} b,c{left:0} c,d{right:0}',
{ 'space-before-selector-delimiter': '' }
);
});

it('Should detect whitespace (3 blocks)', function() {
this.shouldDetect(
['space-before-selector-delimiter'],
'a,b{top:0} b ,c{left:0} c ,d{right:0}',
{ 'space-before-selector-delimiter': ' ' }
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
a ,b { color: red }
a , b { color: red }
a ,b { color: red }
a ,
b { color: red }
a+b ,c>d ,e{ color: red }
12 changes: 12 additions & 0 deletions test/options/space-before-selector-delimiter/test-3.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
a
,b { color: red }
a
, b { color: red }
a
,b { color: red }
a
,
b { color: red }
a+b
,c>d
,e{ color: red }
6 changes: 6 additions & 0 deletions test/options/space-before-selector-delimiter/test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
a,b { color: red }
a, b { color: red }
a ,b { color: red }
a,
b { color: red }
a+b,c>d,e{ color: red }
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
a,b { color: red }
a, b { color: red }
a,b { color: red }
a,
b { color: red }
a+b,c>d,e{ color: red }

0 comments on commit 8d78ac9

Please sign in to comment.