@@ -17,7 +17,7 @@ exports.TYPES = {
17
17
STRING : 7 ,
18
18
ANGLE : 8 ,
19
19
KEYWORD : 9 ,
20
- NULL_OR_EMPTY_STR : 10 ,
20
+ EMPTY : 10 ,
21
21
CALC : 11 ,
22
22
} ;
23
23
@@ -35,19 +35,33 @@ var calcRegEx = /^calc\(([^)]*)\)$/;
35
35
var colorRegEx4 = / ^ h s l a ? \( \s * ( - ? \d + | - ? \d * .\d + ) \s * , \s * ( - ? \d + | - ? \d * .\d + ) % \s * , \s * ( - ? \d + | - ? \d * .\d + ) % \s * ( , \s * ( - ? \d + | - ? \d * .\d + ) \s * ) ? \) / ;
36
36
var angleRegEx = / ^ ( [ - + ] ? [ 0 - 9 ] * \. ? [ 0 - 9 ] + ) ( d e g | g r a d | r a d ) $ / ;
37
37
38
- // This will return one of the above types based on the passed in string
39
- exports . valueType = function valueType ( val ) {
40
- if ( val === '' || val === null ) {
41
- return exports . TYPES . NULL_OR_EMPTY_STR ;
38
+ // https://heycam.github.io/webidl/#es-DOMString
39
+ exports . toDOMString = function toDOMString ( val ) {
40
+ if ( val === null ) {
41
+ return '' ;
42
42
}
43
- if ( typeof val === 'number' ) {
44
- val = val . toString ( ) ;
43
+ if ( val === undefined ) {
44
+ return val ;
45
45
}
46
-
47
- if ( typeof val !== 'string' ) {
48
- return undefined ;
46
+ if ( typeof val === 'string' ) {
47
+ return val ;
48
+ }
49
+ if ( typeof val === 'symbol' ) {
50
+ throw Error ( 'Cannot convert symbol to string' ) ;
49
51
}
52
+ return String ( val ) ;
53
+ } ;
54
+
55
+ // This will return one of the above types based on the passed in string
56
+ exports . valueType = function valueType ( val ) {
57
+ val = exports . toDOMString ( val ) ;
50
58
59
+ if ( val === undefined ) {
60
+ return val ;
61
+ }
62
+ if ( val === '' ) {
63
+ return exports . TYPES . EMPTY ;
64
+ }
51
65
if ( integerRegEx . test ( val ) ) {
52
66
return exports . TYPES . INTEGER ;
53
67
}
@@ -157,7 +171,7 @@ exports.valueType = function valueType(val) {
157
171
158
172
exports . parseInteger = function parseInteger ( val ) {
159
173
var type = exports . valueType ( val ) ;
160
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
174
+ if ( type === exports . TYPES . EMPTY ) {
161
175
return val ;
162
176
}
163
177
if ( type !== exports . TYPES . INTEGER ) {
@@ -168,7 +182,7 @@ exports.parseInteger = function parseInteger(val) {
168
182
169
183
exports . parseNumber = function parseNumber ( val ) {
170
184
var type = exports . valueType ( val ) ;
171
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
185
+ if ( type === exports . TYPES . EMPTY ) {
172
186
return val ;
173
187
}
174
188
if ( type !== exports . TYPES . NUMBER && type !== exports . TYPES . INTEGER ) {
@@ -182,7 +196,7 @@ exports.parseLength = function parseLength(val) {
182
196
return '0px' ;
183
197
}
184
198
var type = exports . valueType ( val ) ;
185
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
199
+ if ( type === exports . TYPES . EMPTY ) {
186
200
return val ;
187
201
}
188
202
if ( type !== exports . TYPES . LENGTH ) {
@@ -196,7 +210,7 @@ exports.parsePercent = function parsePercent(val) {
196
210
return '0%' ;
197
211
}
198
212
var type = exports . valueType ( val ) ;
199
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
213
+ if ( type === exports . TYPES . EMPTY ) {
200
214
return val ;
201
215
}
202
216
if ( type !== exports . TYPES . PERCENT ) {
@@ -221,7 +235,7 @@ exports.parseMeasurement = function parseMeasurement(val) {
221
235
222
236
exports . parseUrl = function parseUrl ( val ) {
223
237
var type = exports . valueType ( val ) ;
224
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
238
+ if ( type === exports . TYPES . EMPTY ) {
225
239
return val ;
226
240
}
227
241
var res = urlRegEx . exec ( val ) ;
@@ -260,7 +274,7 @@ exports.parseUrl = function parseUrl(val) {
260
274
261
275
exports . parseString = function parseString ( val ) {
262
276
var type = exports . valueType ( val ) ;
263
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
277
+ if ( type === exports . TYPES . EMPTY ) {
264
278
return val ;
265
279
}
266
280
if ( type !== exports . TYPES . STRING ) {
@@ -287,7 +301,7 @@ exports.parseString = function parseString(val) {
287
301
288
302
exports . parseColor = function parseColor ( val ) {
289
303
var type = exports . valueType ( val ) ;
290
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
304
+ if ( type === exports . TYPES . EMPTY ) {
291
305
return val ;
292
306
}
293
307
var red ,
@@ -406,7 +420,7 @@ exports.parseColor = function parseColor(val) {
406
420
407
421
exports . parseAngle = function parseAngle ( val ) {
408
422
var type = exports . valueType ( val ) ;
409
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
423
+ if ( type === exports . TYPES . EMPTY ) {
410
424
return val ;
411
425
}
412
426
if ( type !== exports . TYPES . ANGLE ) {
@@ -431,7 +445,7 @@ exports.parseAngle = function parseAngle(val) {
431
445
432
446
exports . parseKeyword = function parseKeyword ( val , valid_keywords ) {
433
447
var type = exports . valueType ( val ) ;
434
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
448
+ if ( type === exports . TYPES . EMPTY ) {
435
449
return val ;
436
450
}
437
451
if ( type !== exports . TYPES . KEYWORD ) {
@@ -520,7 +534,7 @@ var getParts = function(str) {
520
534
exports . shorthandParser = function parse ( v , shorthand_for ) {
521
535
var obj = { } ;
522
536
var type = exports . valueType ( v ) ;
523
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
537
+ if ( type === exports . TYPES . EMPTY ) {
524
538
Object . keys ( shorthand_for ) . forEach ( function ( property ) {
525
539
obj [ property ] = '' ;
526
540
} ) ;
0 commit comments