1
1
2
+ /**
3
+ * Module dependencies.
4
+ */
5
+
6
+ var Base = require ( './compiler' ) ;
7
+
2
8
/**
3
9
* Expose compiler.
4
10
*/
@@ -10,9 +16,15 @@ module.exports = Compiler;
10
16
*/
11
17
12
18
function Compiler ( options ) {
13
- options = options || { } ;
19
+ Base . call ( this , options ) ;
14
20
}
15
21
22
+ /**
23
+ * Inherit from `Base.prototype`.
24
+ */
25
+
26
+ Compiler . prototype . __proto__ = Base . prototype ;
27
+
16
28
/**
17
29
* Compile `node`.
18
30
*/
@@ -23,40 +35,31 @@ Compiler.prototype.compile = function(node){
23
35
. join ( '' ) ;
24
36
} ;
25
37
26
- /**
27
- * Visit `node`.
28
- */
29
-
30
- Compiler . prototype . visit = function ( node ) {
31
- return this [ node . type ] ( node ) ;
32
- } ;
33
-
34
38
/**
35
39
* Visit comment node.
36
40
*/
37
41
38
42
Compiler . prototype . comment = function ( node ) {
39
- return '' ;
43
+ return this . emit ( '' , node . position ) ;
40
44
} ;
41
45
42
46
/**
43
47
* Visit import node.
44
48
*/
45
49
46
50
Compiler . prototype . import = function ( node ) {
47
- return '@import ' + node . import + ';' ;
51
+ return this . emit ( '@import ' + node . import + ';' , node . position ) ;
48
52
} ;
49
53
50
54
/**
51
55
* Visit media node.
52
56
*/
53
57
54
58
Compiler . prototype . media = function ( node ) {
55
- return '@media '
56
- + node . media
57
- + '{'
58
- + node . rules . map ( this . visit , this ) . join ( '' )
59
- + '}' ;
59
+ return this . emit ( '@media ' + node . media , node . position , true )
60
+ + this . emit ( '{' )
61
+ + this . mapVisit ( node . rules )
62
+ + this . emit ( '}' ) ;
60
63
} ;
61
64
62
65
/**
@@ -66,52 +69,51 @@ Compiler.prototype.media = function(node){
66
69
Compiler . prototype . document = function ( node ) {
67
70
var doc = '@' + ( node . vendor || '' ) + 'document ' + node . document ;
68
71
69
- return doc
70
- + '{'
71
- + node . rules . map ( this . visit , this ) . join ( '' )
72
- + '}' ;
72
+ return this . emit ( doc , node . position , true )
73
+ + this . emit ( '{' )
74
+ + this . mapVisit ( node . rules )
75
+ + this . emit ( '}' ) ;
73
76
} ;
74
77
75
78
/**
76
79
* Visit charset node.
77
80
*/
78
81
79
82
Compiler . prototype . charset = function ( node ) {
80
- return '@charset ' + node . charset + ';' ;
83
+ return this . emit ( '@charset ' + node . charset + ';' , node . position ) ;
81
84
} ;
82
85
83
86
/**
84
87
* Visit namespace node.
85
88
*/
86
89
87
90
Compiler . prototype . namespace = function ( node ) {
88
- return '@namespace ' + node . namespace + ';' ;
91
+ return this . emit ( '@namespace ' + node . namespace + ';' , node . position ) ;
89
92
} ;
90
93
91
94
/**
92
95
* Visit supports node.
93
96
*/
94
97
95
98
Compiler . prototype . supports = function ( node ) {
96
- return '@supports '
97
- + node . supports
98
- + '{'
99
- + node . rules . map ( this . visit , this ) . join ( '' )
100
- + '}' ;
99
+ return this . emit ( '@supports ' + node . supports , node . position , true )
100
+ + this . emit ( '{' )
101
+ + this . mapVisit ( node . rules )
102
+ + this . emit ( '}' ) ;
101
103
} ;
102
104
103
105
/**
104
106
* Visit keyframes node.
105
107
*/
106
108
107
109
Compiler . prototype . keyframes = function ( node ) {
108
- return '@'
110
+ return this . emit ( '@'
109
111
+ ( node . vendor || '' )
110
112
+ 'keyframes '
111
- + node . name
112
- + '{'
113
- + node . keyframes . map ( this . visit , this ) . join ( '' )
114
- + '}' ;
113
+ + node . name , node . position , true )
114
+ + this . emit ( '{' )
115
+ + this . mapVisit ( node . keyframes )
116
+ + this . emit ( '}' ) ;
115
117
} ;
116
118
117
119
/**
@@ -121,10 +123,10 @@ Compiler.prototype.keyframes = function(node){
121
123
Compiler . prototype . keyframe = function ( node ) {
122
124
var decls = node . declarations ;
123
125
124
- return node . values . join ( ',' )
125
- + '{'
126
- + decls . map ( this . visit , this ) . join ( '' )
127
- + '}' ;
126
+ return this . emit ( node . values . join ( ',' ) , node . position , true )
127
+ + this . emit ( '{' )
128
+ + this . mapVisit ( decls )
129
+ + this . emit ( '}' ) ;
128
130
} ;
129
131
130
132
/**
@@ -136,10 +138,10 @@ Compiler.prototype.page = function(node){
136
138
? node . selectors . join ( ', ' )
137
139
: '' ;
138
140
139
- return '@page ' + sel
140
- + '{'
141
- + node . declarations . map ( this . visit , this ) . join ( '' )
142
- + '}' ;
141
+ return this . emit ( '@page ' + sel , node . position , true )
142
+ + this . emit ( '{' )
143
+ + this . mapVisit ( node . declarations )
144
+ + this . emit ( '}' ) ;
143
145
} ;
144
146
145
147
/**
@@ -150,17 +152,17 @@ Compiler.prototype.rule = function(node){
150
152
var decls = node . declarations ;
151
153
if ( ! decls . length ) return '' ;
152
154
153
- return node . selectors . join ( ',' )
154
- + '{'
155
- + decls . map ( this . visit , this ) . join ( '' )
156
- + '}' ;
155
+ return this . emit ( node . selectors . join ( ',' ) , node . position , true )
156
+ + this . emit ( '{' )
157
+ + this . mapVisit ( decls )
158
+ + this . emit ( '}' ) ;
157
159
} ;
158
160
159
161
/**
160
162
* Visit declaration node.
161
163
*/
162
164
163
165
Compiler . prototype . declaration = function ( node ) {
164
- return node . property + ':' + node . value + ';' ;
166
+ return this . emit ( node . property + ':' + node . value , node . position ) + this . emit ( ';' ) ;
165
167
} ;
166
168
0 commit comments