1+ var gonzales = require ( 'gonzales-pe' ) ;
2+
13module . exports = ( function ( ) {
24 function getLastWhitespaceNode ( node ) {
3- var lastNode = node [ node . length - 1 ] ;
5+ var lastNode = node . last ( ) ;
6+
7+ if ( ! lastNode || ! lastNode . content ) return null ;
48
5- if ( typeof lastNode !== 'object' || lastNode [ 0 ] === ' block') return null ;
6- if ( lastNode [ 0 ] === 's' ) return lastNode ;
9+ if ( lastNode . is ( ' block') ) return null ;
10+ if ( lastNode . is ( 's' ) ) return lastNode ;
711
812 return getLastWhitespaceNode ( lastNode ) ;
913 }
@@ -22,33 +26,50 @@ module.exports = (function() {
2226
2327 /**
2428 * Processes tree node.
25- * @param {String } nodeType
2629 * @param {node } node
27- * @param {Number } level
2830 */
29- process : function ( nodeType , node , level ) {
30- if ( nodeType !== 'block' && nodeType !== 'atrulers' ) return ;
31-
31+ process : function ( node ) {
3232 var value = this . getValue ( 'space-before-closing-brace' ) ;
33+ var blockIndent = this . getValue ( 'block-indent' ) ;
3334
34- // If found block node stop at the next one for space check
35- // For the pre-block node, find its last (the deepest) child
36- var whitespaceNode = getLastWhitespaceNode ( node ) ;
35+ if ( ! node . is ( 'stylesheet' ) ) return ;
3736
38- if ( value . indexOf ( '\n' ) > - 1 ) {
39- var blockIndent = this . getValue ( 'block-indent' ) ;
40- // TODO: Check that it works for '' block indent value <tg>
41- if ( blockIndent ) value += new Array ( level + 1 ) . join ( blockIndent ) ;
42- }
37+ function processBlock ( x , level ) {
38+ level = level || 0 ;
39+
40+ for ( var i = 0 ; i < x . content . length ; i ++ ) {
41+ node = x . get ( i ) ;
42+ if ( ! node ) continue ;
4343
44- // If it's spaces, modify this node
45- // If it's something different from spaces, add a space node to the end
44+ if ( node . is ( 'block' ) || node . is ( 'atrulers' ) ) {
45+ // If found block node stop at the next one for space check
46+ // For the pre-block node, find its last (the deepest) child
47+ var whitespaceNode = getLastWhitespaceNode ( node ) ;
4648
47- if ( whitespaceNode ) {
48- whitespaceNode [ 1 ] = value ;
49- } else if ( value !== '' ) {
50- node . push ( [ 's' , value ] ) ;
49+ if ( value . indexOf ( '\n' ) > - 1 ) {
50+ // TODO: Check that it works for '' block indent value <tg>
51+ if ( blockIndent ) value += new Array ( level + 1 ) . join ( blockIndent ) ;
52+ }
53+
54+ // If it's spaces, modify this node
55+ // If it's something different from spaces, add a space node to the end
56+
57+ if ( whitespaceNode ) {
58+ whitespaceNode . content = value ;
59+ } else if ( value !== '' ) {
60+ var space = gonzales . createNode ( { type : 's' , content : value } ) ;
61+ if ( Array . isArray ( node . content ) )
62+ node . content . push ( space ) ;
63+ }
64+
65+ level ++ ;
66+ }
67+
68+ processBlock ( node , level ) ;
69+ }
5170 }
71+
72+ processBlock ( node ) ;
5273 } ,
5374
5475 /**
0 commit comments