1
- const inspect = require ( 'util' ) . inspect ;
2
- const css = require ( '../parse-css' ) ;
1
+ // var inspect = require('util').inspect;
2
+ var css = require ( '../parse-css' ) ;
3
+
4
+ module . exports . types = require ( './types' ) ;
3
5
4
6
module . exports . parse = function parse ( source ) {
5
7
return acceptStylesheet (
@@ -21,7 +23,7 @@ function acceptRules(nodes) {
21
23
function acceptRule ( node ) {
22
24
switch ( node . type ) {
23
25
case 'QUALIFIED-RULE' : return acceptStyleRule ( node ) ;
24
- case 'AT-RULE' : return acceptAtRule ( node ) ;
26
+ case 'AT-RULE' : return acceptAtRule ( node ) ;
25
27
default : throw new Error ( "Unknown rule type: " + node . type ) ;
26
28
}
27
29
}
@@ -53,7 +55,7 @@ function acceptDeclarations(tokens, acceptor) {
53
55
function acceptStyleDeclaration ( node ) {
54
56
switch ( node . type ) {
55
57
case 'DECLARATION' : return acceptStylePropertyDeclaration ( node ) ;
56
- case 'AT-RULE' : return acceptAtRule ( node ) ;
58
+ case 'AT-RULE' : return acceptAtRule ( node ) ;
57
59
default : throw new Error ( "Unknown style declaration type: " + node . type ) ;
58
60
}
59
61
}
@@ -63,39 +65,97 @@ function acceptStylePropertyDeclaration(node) {
63
65
type : 'StylePropertyDeclaration' ,
64
66
important : node . important ,
65
67
name : node . name ,
66
- value : acceptContextualValue ( node . name , node . value )
68
+ values : acceptComponentValues ( node . value )
67
69
} ;
68
70
}
69
71
70
- function acceptContextualValue ( name , parts ) {
71
- parts = trimWhitespaceTokens ( parts ) ;
72
+ function acceptComponentValues ( parts ) {
73
+ return trimWhitespaceTokens ( parts ) . map ( acceptComponentValue ) ;
74
+ }
72
75
73
- if ( name [ 0 ] === '-' && name [ 1 ] === '-' ) {
74
- return acceptCustomPropertyValue ( name , parts ) ;
75
- } else {
76
- return acceptStyleValue ( name , parts ) ;
76
+ function acceptComponentValue ( node ) {
77
+ switch ( node . tokenType ) {
78
+ case 'WHITESPACE' : return acceptToken ( node ) ;
79
+ case 'DELIM' : return acceptToken ( node ) ;
80
+ case ',' : return acceptToken ( node ) ;
81
+ case 'IDENT' : return acceptIdent ( node ) ;
82
+ case 'HASH' : return acceptHash ( node ) ;
83
+ case 'STRING' : return acceptString ( node ) ;
84
+ case 'NUMBER' : return acceptNumber ( node ) ;
85
+ case 'DIMENSION' : return acceptDimension ( node ) ;
86
+ case 'PERCENTAGE' : return acceptPercentage ( node ) ;
77
87
}
78
- }
79
88
80
- function acceptCustomPropertyValue ( name , parts ) {
81
- if ( parts [ 0 ] . type === 'BLOCK' ) {
82
- return acceptMixin ( parts ) ;
83
- } else {
84
- return acceptStyleValue ( name , parts ) ;
89
+ switch ( node . type ) {
90
+ // case 'integer': return acceptInteger(node);
91
+ case 'FUNCTION' : return acceptFunction ( node ) ;
85
92
}
93
+
94
+ throw new Error ( "Unknown component value: <" + node . tokenType + ", " + node . type + ">" ) ;
95
+ }
96
+
97
+ function acceptToken ( node ) {
98
+ return {
99
+ type : "Token" ,
100
+ source : node . toSource ( )
101
+ } ;
86
102
}
87
103
88
- function acceptMixin ( parts ) {
89
- var block = parts [ 0 ] ;
104
+ function acceptIdent ( node ) {
105
+ return {
106
+ type : "Ident" ,
107
+ name : node . value ,
108
+ source : node . toSource ( )
109
+ } ;
110
+ }
90
111
112
+ function acceptHash ( node ) {
91
113
return {
92
- type : 'Mixin' ,
93
- body : acceptDeclarations ( block . value , acceptMixinStyleDeclaration )
114
+ type : "Hash" ,
115
+ value : node . value ,
116
+ source : node . toSource ( )
94
117
} ;
95
118
}
96
119
97
- function acceptStyleValue ( name , parts ) {
98
- return printValue ( parts ) ;
120
+ function acceptString ( node ) {
121
+ return {
122
+ type : "String" ,
123
+ value : node . value ,
124
+ source : node . toSource ( )
125
+ } ;
126
+ }
127
+
128
+ function acceptNumber ( node ) {
129
+ return {
130
+ type : "Number" ,
131
+ value : node . value ,
132
+ source : node . toSource ( )
133
+ } ;
134
+ }
135
+
136
+ function acceptDimension ( node ) {
137
+ return {
138
+ type : "Dimension" ,
139
+ value : node . value ,
140
+ unit : node . unit ,
141
+ source : node . toSource ( )
142
+ } ;
143
+ }
144
+
145
+ function acceptPercentage ( node ) {
146
+ return {
147
+ type : "Percentage" ,
148
+ value : node . value ,
149
+ source : node . toSource ( )
150
+ } ;
151
+ }
152
+
153
+ function acceptFunction ( node ) {
154
+ return {
155
+ type : "Function" ,
156
+ name : node . name ,
157
+ args : acceptComponentValues ( node . value )
158
+ } ;
99
159
}
100
160
101
161
function printValue ( parts ) {
@@ -110,11 +170,11 @@ function printValue(parts) {
110
170
} ) . join ( '' ) ;
111
171
}
112
172
113
- function acceptStyleValueParts ( name , parts ) {
114
- return parts . map ( acceptStyleValuePart ) ;
173
+ function acceptComponentValueParts ( name , parts ) {
174
+ return parts . map ( acceptComponentValuePart ) ;
115
175
}
116
176
117
- function acceptStyleValuePart ( node ) {
177
+ function acceptComponentValuePart ( node ) {
118
178
if ( node . tokenType ) {
119
179
return node ;
120
180
}
@@ -125,10 +185,6 @@ function acceptStyleValuePart(node) {
125
185
}
126
186
}
127
187
128
- function acceptFunction ( node ) {
129
- throw new Error ( "Functions in property values (e.g. calc and color) are not yet supported." ) ;
130
- }
131
-
132
188
function acceptMixinStyleDeclaration ( node ) {
133
189
switch ( node . type ) {
134
190
case 'DECLARATION' : return acceptStylePropertyDeclaration ( node ) ;
0 commit comments