@@ -17,7 +17,7 @@ exports.TYPES = {
1717 STRING : 7 ,
1818 ANGLE : 8 ,
1919 KEYWORD : 9 ,
20- NULL_OR_EMPTY_STR : 10 ,
20+ EMPTY : 10 ,
2121 CALC : 11 ,
2222} ;
2323
@@ -35,19 +35,46 @@ var calcRegEx = /^calc\(([^)]*)\)$/;
3535var 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 * ) ? \) / ;
3636var angleRegEx = / ^ ( [ - + ] ? [ 0 - 9 ] * \. ? [ 0 - 9 ] + ) ( d e g | g r a d | r a d ) $ / ;
3737
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+ }
43+ if ( val === undefined ) {
44+ return val ;
45+ }
46+ if ( typeof val === 'string' ) {
47+ return val ;
48+ }
49+ if ( typeof val === 'symbol' ) {
50+ throw Error ( 'Cannot convert symbol to string' ) ;
4251 }
43- if ( typeof val === 'number ' ) {
52+ if ( typeof val . toString === 'function ' ) {
4453 val = val . toString ( ) ;
54+ if ( val === null ) {
55+ return undefined ;
56+ }
57+ if ( typeof val !== 'string' ) {
58+ if ( typeof val === 'number' || typeof val === 'bigint' ) {
59+ val = val . toString ( ) ;
60+ } else {
61+ throw Error ( 'Cannot convert value to string' ) ;
62+ }
63+ }
4564 }
65+ return val . toString ( ) ;
66+ } ;
4667
47- if ( typeof val !== 'string' ) {
48- return undefined ;
49- }
68+ // This will return one of the above types based on the passed in string
69+ exports . valueType = function valueType ( val ) {
70+ val = exports . toDOMString ( val ) ;
5071
72+ if ( val === undefined ) {
73+ return val ;
74+ }
75+ if ( val === '' ) {
76+ return exports . TYPES . EMPTY ;
77+ }
5178 if ( integerRegEx . test ( val ) ) {
5279 return exports . TYPES . INTEGER ;
5380 }
@@ -157,7 +184,7 @@ exports.valueType = function valueType(val) {
157184
158185exports . parseInteger = function parseInteger ( val ) {
159186 var type = exports . valueType ( val ) ;
160- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
187+ if ( type === exports . TYPES . EMPTY ) {
161188 return val ;
162189 }
163190 if ( type !== exports . TYPES . INTEGER ) {
@@ -168,7 +195,7 @@ exports.parseInteger = function parseInteger(val) {
168195
169196exports . parseNumber = function parseNumber ( val ) {
170197 var type = exports . valueType ( val ) ;
171- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
198+ if ( type === exports . TYPES . EMPTY ) {
172199 return val ;
173200 }
174201 if ( type !== exports . TYPES . NUMBER && type !== exports . TYPES . INTEGER ) {
@@ -182,7 +209,7 @@ exports.parseLength = function parseLength(val) {
182209 return '0px' ;
183210 }
184211 var type = exports . valueType ( val ) ;
185- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
212+ if ( type === exports . TYPES . EMPTY ) {
186213 return val ;
187214 }
188215 if ( type !== exports . TYPES . LENGTH ) {
@@ -196,7 +223,7 @@ exports.parsePercent = function parsePercent(val) {
196223 return '0%' ;
197224 }
198225 var type = exports . valueType ( val ) ;
199- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
226+ if ( type === exports . TYPES . EMPTY ) {
200227 return val ;
201228 }
202229 if ( type !== exports . TYPES . PERCENT ) {
@@ -221,7 +248,7 @@ exports.parseMeasurement = function parseMeasurement(val) {
221248
222249exports . parseUrl = function parseUrl ( val ) {
223250 var type = exports . valueType ( val ) ;
224- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
251+ if ( type === exports . TYPES . EMPTY ) {
225252 return val ;
226253 }
227254 var res = urlRegEx . exec ( val ) ;
@@ -260,7 +287,7 @@ exports.parseUrl = function parseUrl(val) {
260287
261288exports . parseString = function parseString ( val ) {
262289 var type = exports . valueType ( val ) ;
263- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
290+ if ( type === exports . TYPES . EMPTY ) {
264291 return val ;
265292 }
266293 if ( type !== exports . TYPES . STRING ) {
@@ -287,7 +314,7 @@ exports.parseString = function parseString(val) {
287314
288315exports . parseColor = function parseColor ( val ) {
289316 var type = exports . valueType ( val ) ;
290- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
317+ if ( type === exports . TYPES . EMPTY ) {
291318 return val ;
292319 }
293320 var red ,
@@ -406,7 +433,7 @@ exports.parseColor = function parseColor(val) {
406433
407434exports . parseAngle = function parseAngle ( val ) {
408435 var type = exports . valueType ( val ) ;
409- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
436+ if ( type === exports . TYPES . EMPTY ) {
410437 return val ;
411438 }
412439 if ( type !== exports . TYPES . ANGLE ) {
@@ -431,7 +458,7 @@ exports.parseAngle = function parseAngle(val) {
431458
432459exports . parseKeyword = function parseKeyword ( val , valid_keywords ) {
433460 var type = exports . valueType ( val ) ;
434- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
461+ if ( type === exports . TYPES . EMPTY ) {
435462 return val ;
436463 }
437464 if ( type !== exports . TYPES . KEYWORD ) {
@@ -520,7 +547,7 @@ var getParts = function(str) {
520547exports . shorthandParser = function parse ( v , shorthand_for ) {
521548 var obj = { } ;
522549 var type = exports . valueType ( v ) ;
523- if ( type === exports . TYPES . NULL_OR_EMPTY_STR ) {
550+ if ( type === exports . TYPES . EMPTY ) {
524551 Object . keys ( shorthand_for ) . forEach ( function ( property ) {
525552 obj [ property ] = '' ;
526553 } ) ;
0 commit comments