11module . exports = ( function ( ) {
2- function processStylesheetContent ( nodeContent ) {
3- removeEmptyRulesets ( nodeContent ) ;
4- mergeAdjacentWhitespace ( nodeContent ) ;
2+ function processStylesheetContent ( node ) {
3+ removeEmptyRulesets ( node ) ;
4+ mergeAdjacentWhitespace ( node ) ;
55 }
66
7- function removeEmptyRulesets ( nodeContent ) {
8- var i = nodeContent . length ;
7+ function removeEmptyRulesets ( stylesheet ) {
8+ var i = stylesheet . content . length ;
99 // Loop through node and try to find a ruleset:
1010 while ( i -- ) {
11- var node = nodeContent [ i ] ;
11+ var node = stylesheet . content [ i ] ;
1212 if ( ! isRuleset ( node ) ) continue ;
1313 // If a ruleset is found, try to find its nested rulesets and remove
1414 // all empty ones:
15- var j = node . length ;
15+ var j = node . content . length ;
1616 while ( j -- ) {
1717 // Nested rulesets are located inside blocks, that's why look
1818 // for blocks only:
19- var blockNode = node [ j ] ;
20- if ( blockNode [ 0 ] !== 'block' ) continue ;
21- blockNode . shift ( ) ;
19+ var blockNode = node . content [ j ] ;
20+ if ( blockNode . type !== 'block' ) continue ;
2221 processStylesheetContent ( blockNode ) ;
23- blockNode . unshift ( 'block' ) ;
24- node [ j ] = blockNode ;
22+ node . content [ j ] = blockNode ;
2523 }
2624 // If after removing all empty nested rulesets the parent has also
2725 // become empty, remove it too:
2826 if ( isEmptyRuleset ( node ) ) {
29- nodeContent . splice ( i , 1 ) ;
27+ stylesheet . content . splice ( i , 1 ) ;
3028 }
3129 }
3230 }
@@ -37,42 +35,41 @@ module.exports = (function() {
3735 * To ensure correctness of further processing we should merge such nodes into one.
3836 * [space, space] => [space]
3937 */
40- function mergeAdjacentWhitespace ( nodeContent ) {
41- var i = nodeContent . length - 1 ;
38+ function mergeAdjacentWhitespace ( node ) {
39+ var i = node . content . length - 1 ;
4240 while ( i -- > 0 ) {
43- if ( isWhitespace ( nodeContent [ i ] ) && isWhitespace ( nodeContent [ i + 1 ] ) ) {
44- nodeContent [ i ] [ 1 ] += nodeContent [ i + 1 ] [ 1 ] ;
45- nodeContent . splice ( i + 1 , 1 ) ;
41+ if ( isWhitespace ( node . content [ i ] ) && isWhitespace ( node . content [ i + 1 ] ) ) {
42+ node . content [ i ] . content += node . content [ i + 1 ] . content ;
43+ node . content . splice ( i + 1 , 1 ) ;
4644 }
4745 }
4846 }
4947
5048 function isEmptyRuleset ( ruleset ) {
51- return ruleset . filter ( isBlock ) . every ( isEmptyBlock , this ) ;
49+ return isEmptyBlock ( ruleset . first ( 'block' ) ) ;
5250 }
5351
5452 /**
5553 * Block is considered empty when it has nothing but spaces.
5654 */
5755 function isEmptyBlock ( node ) {
58- return node . length === 1 || ! node . some ( isNotWhitespace ) ;
59- }
56+ if ( ! node . content . length ) return true ;
6057
61- function isRuleset ( node ) {
62- return node [ 0 ] === 'ruleset' ;
58+ var isEmpty = true ;
59+ node . forEach ( function ( childNode ) {
60+ if ( childNode . type !== 's' ) isEmpty = false ;
61+ } ) ;
62+ return isEmpty ;
6363 }
6464
65- function isBlock ( node ) {
66- return node [ 0 ] === 'block ' ;
65+ function isRuleset ( node ) {
66+ return node . type === 'ruleset ' ;
6767 }
6868
6969 function isWhitespace ( node ) {
70- return node [ 0 ] === 's' ;
70+ return node . type === 's' ;
7171 }
7272
73- function isNotWhitespace ( node ) {
74- return typeof node === 'object' && node [ 0 ] !== 's' ;
75- }
7673
7774 return {
7875 name : 'remove-empty-rulesets' ,
@@ -86,12 +83,11 @@ module.exports = (function() {
8683 /**
8784 * Remove rulesets with no declarations.
8885 *
89- * @param {String } nodeType
90- * @param {Array } nodeContent
86+ * @param {String } node
9187 */
92- process : function ( nodeType , nodeContent ) {
93- if ( nodeType === 'stylesheet' ) {
94- processStylesheetContent ( nodeContent ) ;
88+ process : function ( node ) {
89+ if ( node . type === 'stylesheet' ) {
90+ processStylesheetContent ( node ) ;
9591 }
9692 } ,
9793
0 commit comments