@@ -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,25 @@ 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 ( typeof val === 'string ' ) {
44
+ return val ;
45
45
}
46
-
47
- if ( typeof val !== 'string' ) {
48
- return undefined ;
46
+ if ( typeof val === 'symbol' ) {
47
+ throw Error ( 'Cannot convert symbol to string' ) ;
49
48
}
49
+ return String ( val ) ;
50
+ } ;
50
51
52
+ // This will return one of the above types based on the passed in string
53
+ exports . valueType = function valueType ( val ) {
54
+ if ( val === '' ) {
55
+ return exports . TYPES . EMPTY ;
56
+ }
51
57
if ( integerRegEx . test ( val ) ) {
52
58
return exports . TYPES . INTEGER ;
53
59
}
@@ -157,7 +163,7 @@ exports.valueType = function valueType(val) {
157
163
158
164
exports . parseInteger = function parseInteger ( val ) {
159
165
var type = exports . valueType ( val ) ;
160
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
166
+ if ( type === exports . TYPES . EMPTY ) {
161
167
return val ;
162
168
}
163
169
if ( type !== exports . TYPES . INTEGER ) {
@@ -168,7 +174,7 @@ exports.parseInteger = function parseInteger(val) {
168
174
169
175
exports . parseNumber = function parseNumber ( val ) {
170
176
var type = exports . valueType ( val ) ;
171
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
177
+ if ( type === exports . TYPES . EMPTY ) {
172
178
return val ;
173
179
}
174
180
if ( type !== exports . TYPES . NUMBER && type !== exports . TYPES . INTEGER ) {
@@ -178,11 +184,11 @@ exports.parseNumber = function parseNumber(val) {
178
184
} ;
179
185
180
186
exports . parseLength = function parseLength ( val ) {
181
- if ( val === 0 || val === '0' ) {
187
+ if ( val === '0' ) {
182
188
return '0px' ;
183
189
}
184
190
var type = exports . valueType ( val ) ;
185
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
191
+ if ( type === exports . TYPES . EMPTY ) {
186
192
return val ;
187
193
}
188
194
if ( type !== exports . TYPES . LENGTH ) {
@@ -192,11 +198,11 @@ exports.parseLength = function parseLength(val) {
192
198
} ;
193
199
194
200
exports . parsePercent = function parsePercent ( val ) {
195
- if ( val === 0 || val === '0' ) {
201
+ if ( val === '0' ) {
196
202
return '0%' ;
197
203
}
198
204
var type = exports . valueType ( val ) ;
199
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
205
+ if ( type === exports . TYPES . EMPTY ) {
200
206
return val ;
201
207
}
202
208
if ( type !== exports . TYPES . PERCENT ) {
@@ -221,7 +227,7 @@ exports.parseMeasurement = function parseMeasurement(val) {
221
227
222
228
exports . parseUrl = function parseUrl ( val ) {
223
229
var type = exports . valueType ( val ) ;
224
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
230
+ if ( type === exports . TYPES . EMPTY ) {
225
231
return val ;
226
232
}
227
233
var res = urlRegEx . exec ( val ) ;
@@ -260,7 +266,7 @@ exports.parseUrl = function parseUrl(val) {
260
266
261
267
exports . parseString = function parseString ( val ) {
262
268
var type = exports . valueType ( val ) ;
263
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
269
+ if ( type === exports . TYPES . EMPTY ) {
264
270
return val ;
265
271
}
266
272
if ( type !== exports . TYPES . STRING ) {
@@ -287,7 +293,7 @@ exports.parseString = function parseString(val) {
287
293
288
294
exports . parseColor = function parseColor ( val ) {
289
295
var type = exports . valueType ( val ) ;
290
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
296
+ if ( type === exports . TYPES . EMPTY ) {
291
297
return val ;
292
298
}
293
299
var red ,
@@ -406,7 +412,7 @@ exports.parseColor = function parseColor(val) {
406
412
407
413
exports . parseAngle = function parseAngle ( val ) {
408
414
var type = exports . valueType ( val ) ;
409
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
415
+ if ( type === exports . TYPES . EMPTY ) {
410
416
return val ;
411
417
}
412
418
if ( type !== exports . TYPES . ANGLE ) {
@@ -431,7 +437,7 @@ exports.parseAngle = function parseAngle(val) {
431
437
432
438
exports . parseKeyword = function parseKeyword ( val , valid_keywords ) {
433
439
var type = exports . valueType ( val ) ;
434
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
440
+ if ( type === exports . TYPES . EMPTY ) {
435
441
return val ;
436
442
}
437
443
if ( type !== exports . TYPES . KEYWORD ) {
@@ -520,21 +526,12 @@ var getParts = function(str) {
520
526
exports . shorthandParser = function parse ( v , shorthand_for ) {
521
527
var obj = { } ;
522
528
var type = exports . valueType ( v ) ;
523
- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
529
+ if ( type === exports . TYPES . EMPTY ) {
524
530
Object . keys ( shorthand_for ) . forEach ( function ( property ) {
525
531
obj [ property ] = '' ;
526
532
} ) ;
527
533
return obj ;
528
534
}
529
-
530
- if ( typeof v === 'number' ) {
531
- v = v . toString ( ) ;
532
- }
533
-
534
- if ( typeof v !== 'string' ) {
535
- return undefined ;
536
- }
537
-
538
535
if ( v . toLowerCase ( ) === 'inherit' ) {
539
536
return { } ;
540
537
}
@@ -623,12 +620,6 @@ exports.implicitSetter = function(property_before, property_after, isValid, pars
623
620
var part_names = [ 'top' , 'right' , 'bottom' , 'left' ] ;
624
621
625
622
return function ( v ) {
626
- if ( typeof v === 'number' ) {
627
- v = v . toString ( ) ;
628
- }
629
- if ( typeof v !== 'string' ) {
630
- return undefined ;
631
- }
632
623
var parts ;
633
624
if ( v . toLowerCase ( ) === 'inherit' || v === '' ) {
634
625
parts = [ v ] ;
@@ -679,12 +670,6 @@ exports.subImplicitSetter = function(prefix, part, isValid, parser) {
679
670
var subparts = [ prefix + '-top' , prefix + '-right' , prefix + '-bottom' , prefix + '-left' ] ;
680
671
681
672
return function ( v ) {
682
- if ( typeof v === 'number' ) {
683
- v = v . toString ( ) ;
684
- }
685
- if ( typeof v !== 'string' ) {
686
- return undefined ;
687
- }
688
673
if ( ! isValid ( v ) ) {
689
674
return undefined ;
690
675
}
0 commit comments