-
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-opening-brace
- Loading branch information
Showing
9 changed files
with
231 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
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,81 @@ | ||
module.exports = (function() { | ||
/** | ||
* Gets the last (the deepest) whitespace node. | ||
* | ||
* @param {node} node | ||
* @returns {node|undefined} If no whitespace node is found, returns | ||
* `undefined` | ||
*/ | ||
function getLastWhitespaceNode(node) { | ||
var lastNode = node[node.length - 1]; | ||
|
||
if (typeof lastNode !== 'object') return; | ||
if (lastNode[0] === 's') return lastNode; | ||
|
||
return getLastWhitespaceNode(lastNode); | ||
} | ||
|
||
return { | ||
name: 'space-before-opening-brace', | ||
|
||
accepts: { | ||
number: true, | ||
string: /^[ \t\n]*$/ | ||
}, | ||
|
||
/** | ||
* Processes tree node. | ||
* | ||
* @param {String} nodeType | ||
* @param {node} node | ||
*/ | ||
process: function(nodeType, node) { | ||
var value = this.getValue('space-before-opening-brace'); | ||
|
||
// Loop through node from the end to the beginning: | ||
for (var i = node.length; i--;) { | ||
// If found block node stop at the next one for space check: | ||
if (node[i][0] !== 'block' && node[i][0] !== 'atrulers') continue; | ||
|
||
// For the pre-block node, find its last (the deepest) child: | ||
var whitespaceNode = getLastWhitespaceNode(node[i - 1]); | ||
|
||
// If it's spaces, modify this node. | ||
// If it's something different from spaces, add a space node to | ||
// the end: | ||
if (whitespaceNode) { | ||
whitespaceNode[1] = value; | ||
} else if (value !== '') { | ||
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) { | ||
var variants = []; | ||
|
||
// Loop through node from the end to the beginning: | ||
for (var i = node.length; i--;) { | ||
// If found block node stop at the next one for space check: | ||
if (node[i][0] !== 'block' && node[i][0] !== 'atrulers') continue; | ||
|
||
// For the pre-block node, find its last (the deepest) child | ||
var whitespaceNode = getLastWhitespaceNode(node[i - 1]); | ||
|
||
if (whitespaceNode) { | ||
variants.push(whitespaceNode[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,84 @@ | ||
describe('options/space-before-opening-brace:', function() { | ||
beforeEach(function() { | ||
this.filename = __filename; | ||
}); | ||
|
||
it('Array value => should not change anything', function() { | ||
this.comb.configure({ 'space-before-opening-brace': ['', ' '] }); | ||
this.shouldBeEqual('test.css'); | ||
}); | ||
|
||
it('Invalid string value => should not change anything', function() { | ||
this.comb.configure({ 'space-before-opening-brace': ' nani ' }); | ||
this.shouldBeEqual('test.css'); | ||
}); | ||
|
||
it('Float number value => should not change anything', function() { | ||
this.comb.configure({ 'space-before-opening-brace': 3.5 }); | ||
this.shouldBeEqual('test.css'); | ||
}); | ||
|
||
it('Integer value => should set proper space before {', function() { | ||
this.comb.configure({ 'space-before-opening-brace': 0 }); | ||
this.shouldBeEqual('test.css', 'test.expected.css'); | ||
}); | ||
|
||
it('Valid string value (spaces only) => should set proper space before {', function() { | ||
this.comb.configure({ 'space-before-opening-brace': ' ' }); | ||
this.shouldBeEqual('test.css', 'test-2.expected.css'); | ||
}); | ||
|
||
it('Valid string value (spaces and newlines) => should set proper space before {', function() { | ||
this.comb.configure({ 'space-before-opening-brace': '\n ' }); | ||
this.shouldBeEqual('test.css', 'test-3.expected.css'); | ||
}); | ||
|
||
it('Should detect no whitespace', function() { | ||
this.shouldDetect( | ||
['space-before-opening-brace'], | ||
'a{top:0}', | ||
{ 'space-before-opening-brace': '' } | ||
); | ||
}); | ||
|
||
it('Should detect whitespace', function() { | ||
this.shouldDetect( | ||
['space-before-opening-brace'], | ||
'a \n {top:0}', | ||
{ 'space-before-opening-brace': ' \n ' } | ||
); | ||
}); | ||
|
||
it('Should detect no whitespace (2 blocks)', function() { | ||
this.shouldDetect( | ||
['space-before-opening-brace'], | ||
'a{top:0} b {left:0}', | ||
{ 'space-before-opening-brace': '' } | ||
); | ||
}); | ||
|
||
it('Should detect whitespace (2 blocks)', function() { | ||
this.shouldDetect( | ||
['space-before-opening-brace'], | ||
'a {top:0} b{left:0}', | ||
{ 'space-before-opening-brace': ' ' } | ||
); | ||
}); | ||
|
||
it('Should detect no whitespace (3 blocks)', function() { | ||
this.shouldDetect( | ||
['space-before-opening-brace'], | ||
'a {top:0} b{left:0} c{right:0}', | ||
{ 'space-before-opening-brace': '' } | ||
); | ||
}); | ||
|
||
it('Should detect whitespace (3 blocks)', function() { | ||
this.shouldDetect( | ||
['space-before-opening-brace'], | ||
'a{top:0} b {left:0} c {right:0}', | ||
{ 'space-before-opening-brace': ' ' } | ||
); | ||
}); | ||
}); | ||
|
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,5 @@ | ||
a {top: 0} | ||
a {top: 0} | ||
|
||
@media print {a {top: 0}} | ||
@media print { a { top: 0 } } |
11 changes: 11 additions & 0 deletions
11
test/options/space-before-opening-brace/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,11 @@ | ||
a | ||
{top: 0} | ||
a | ||
{top: 0} | ||
|
||
@media print | ||
{a | ||
{top: 0}} | ||
@media print | ||
{ a | ||
{ top: 0 } } |
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,5 @@ | ||
a{top: 0} | ||
a {top: 0} | ||
|
||
@media print{a{top: 0}} | ||
@media print { a { top: 0 } } |
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,5 @@ | ||
a{top: 0} | ||
a{top: 0} | ||
|
||
@media print{a{top: 0}} | ||
@media print{ a{ top: 0 } } |