-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New option: space-before-selector-delimiter
- Loading branch information
Showing
8 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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': ' ' } | ||
); | ||
}); | ||
}); |
6 changes: 6 additions & 0 deletions
6
test/options/space-before-selector-delimiter/test-2.expected.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
test/options/space-before-selector-delimiter/test-3.expected.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
6 changes: 6 additions & 0 deletions
6
test/options/space-before-selector-delimiter/test.expected.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |