forked from csscomb/csscomb.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
321 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,77 @@ | ||
module.exports = { | ||
name: 'block-indent', | ||
|
||
accepts: { | ||
number: true, | ||
string: /^[ \t]*$/ | ||
}, | ||
|
||
/** | ||
* Processes tree node. | ||
* | ||
* @param {String} nodeType | ||
* @param {node} node | ||
* @param {Number} level | ||
*/ | ||
process: function process(nodeType, node, level) { | ||
var spaces; | ||
|
||
if (nodeType === 'stylesheet') { | ||
for (var i = node.length; i--;) { | ||
var whitespaceNode = node[i]; | ||
|
||
if (whitespaceNode[0] !== 's') continue; | ||
|
||
spaces = whitespaceNode[1].replace(/\n[ \t]+/gm, '\n'); | ||
if (spaces === '') { | ||
node.splice(i, 1); | ||
} else { | ||
whitespaceNode[1] = spaces; | ||
} | ||
} | ||
return; | ||
} | ||
|
||
// Continue only with space nodes inside {...}: | ||
if (level === 0 || nodeType !== 's') return; | ||
|
||
// Remove all whitespaces and tabs, leave only new lines: | ||
spaces = node[0].replace(/[ \t]/gm, ''); | ||
|
||
if (!spaces) return; | ||
|
||
spaces += new Array(level + 1).join(this.getValue('block-indent')); | ||
node[0] = spaces; | ||
}, | ||
|
||
/** | ||
* Detects the value of an option at the tree node. | ||
* | ||
* @param {String} nodeType | ||
* @param {node} node | ||
* @param {Number} level | ||
*/ | ||
detect: function(nodeType, node, level) { | ||
var result = []; | ||
|
||
// Continue only with non-empty {...} blocks: | ||
if (nodeType !== 'atrulers' && nodeType !== 'block' || !node.length) return; | ||
|
||
for (var i = node.length; i--;) { | ||
var whitespaceNode = node[i]; | ||
if (whitespaceNode[0] !== 's') continue; | ||
|
||
var spaces = whitespaceNode[1]; | ||
var lastIndex = spaces.lastIndexOf('\n'); | ||
|
||
// Do not continue if there is no line break: | ||
if (lastIndex < 0) continue; | ||
|
||
// Number of spaces from beginning of line: | ||
var spacesLength = spaces.slice(lastIndex + 1).length; | ||
result.push(new Array(spacesLength / (level + 1) + 1).join(' ')); | ||
} | ||
|
||
return result; | ||
} | ||
}; |
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,62 @@ | ||
describe('options/block-indent:', function() { | ||
beforeEach(function() { | ||
this.filename = __filename; | ||
}); | ||
|
||
it('Array value => should not change anything', function() { | ||
this.comb.configure({ 'block-indent': ['', ' '] }); | ||
this.shouldBeEqual('test.css'); | ||
}); | ||
|
||
it('Invalid string value => should not change anything', function() { | ||
this.comb.configure({ 'block-indent': ' nani ' }); | ||
this.shouldBeEqual('test.css'); | ||
}); | ||
|
||
it('Float number value => should not change anything', function() { | ||
this.comb.configure({ 'block-indent': 3.5 }); | ||
this.shouldBeEqual('test.css'); | ||
}); | ||
|
||
it('Integer value => should set proper number of spaces', function() { | ||
this.comb.configure({ 'block-indent': 0 }); | ||
this.shouldBeEqual('test.css', 'test.expected.css'); | ||
}); | ||
|
||
it('Valid string value => should set proper number of spaces', function() { | ||
this.comb.configure({ 'block-indent': ' ' }); | ||
this.shouldBeEqual('test.css', 'test-2.expected.css'); | ||
}); | ||
|
||
it('Should detect nothing with an empty block, test 1', function() { | ||
this.shouldDetect( | ||
['block-indent'], | ||
'a{ }', | ||
{} | ||
); | ||
}); | ||
|
||
it('Should detect nothing with an empty block, test 2', function() { | ||
this.shouldDetect( | ||
['block-indent'], | ||
'a{}', | ||
{} | ||
); | ||
}); | ||
|
||
it('Should detect correct number of spaces', function() { | ||
this.shouldDetect( | ||
['block-indent'], | ||
'a{\n top: 0;\n color: tomato;\n}', | ||
{ 'block-indent': ' ' } | ||
); | ||
}); | ||
|
||
it('Should detect no indent for one-line code', function() { | ||
this.shouldDetect( | ||
['block-indent'], | ||
'a{ top: 0; color: tomato; }', | ||
{} | ||
); | ||
}); | ||
}); |
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,30 @@ | ||
a {color: tomato; top: 0;} | ||
|
||
a { color: tomato; | ||
top: 0; } | ||
|
||
a { color: tomato; | ||
top: 0; } | ||
|
||
a { | ||
color: tomato; | ||
top: 0; } | ||
|
||
a { | ||
color: tomato; | ||
top: 0; | ||
} | ||
|
||
a { | ||
color: tomato; | ||
top: 0; | ||
} | ||
|
||
@media print { a {color: tomato; top: 0; } } | ||
|
||
@media print { | ||
a { | ||
color: tomato; | ||
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,36 @@ | ||
a {color: tomato; top: 0; | ||
} | ||
|
||
a { color: tomato; | ||
top: 0; | ||
} | ||
|
||
a { color: tomato; | ||
top: 0; | ||
} | ||
|
||
a { | ||
color: tomato; | ||
top: 0; | ||
} | ||
|
||
a { | ||
color: tomato; | ||
top: 0; | ||
} | ||
|
||
a { | ||
color: tomato; | ||
top: 0; | ||
} | ||
|
||
@media print { a {color: tomato; top: 0; | ||
} | ||
} | ||
|
||
@media print { | ||
a { | ||
color: tomato; | ||
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,30 @@ | ||
a {color: tomato; top: 0;} | ||
|
||
a { color: tomato; | ||
top: 0; } | ||
|
||
a { color: tomato; | ||
top: 0; } | ||
|
||
a { | ||
color: tomato; | ||
top: 0; } | ||
|
||
a { | ||
color: tomato; | ||
top: 0; | ||
} | ||
|
||
a { | ||
color: tomato; | ||
top: 0; | ||
} | ||
|
||
@media print { a {color: tomato; top: 0; } } | ||
|
||
@media print { | ||
a { | ||
color: tomato; | ||
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,30 @@ | ||
a {color: tomato; top: 0;} | ||
|
||
a { color: tomato; | ||
top: 0; } | ||
|
||
a { color: tomato; | ||
top: 0; } | ||
|
||
a { | ||
color: tomato; | ||
top: 0; } | ||
|
||
a { | ||
color: tomato; | ||
top: 0; | ||
} | ||
|
||
a { | ||
color: tomato; | ||
top: 0; | ||
} | ||
|
||
@media print { a {color: tomato; top: 0; } } | ||
|
||
@media print { | ||
a { | ||
color: tomato; | ||
top: 0; | ||
} | ||
} |