@@ -466,58 +466,47 @@ function tokenize(str) {
466
466
} ;
467
467
468
468
var consumeANumber = function ( ) {
469
- var repr = [ ] ;
470
- var type = "integer" ;
469
+ let isInteger = true ;
470
+ let sign ;
471
+ let numberPart = "" ;
472
+ let exponentPart = "" ;
471
473
if ( next ( ) == 0x2b || next ( ) == 0x2d ) {
472
474
consume ( ) ;
473
- repr += String . fromCodePoint ( code ) ;
475
+ sign = String . fromCodePoint ( code ) ;
476
+ numberPart += sign ;
474
477
}
475
478
while ( digit ( next ( ) ) ) {
476
479
consume ( ) ;
477
- repr += String . fromCodePoint ( code ) ;
480
+ numberPart += String . fromCodePoint ( code ) ;
478
481
}
479
482
if ( next ( 1 ) == 0x2e && digit ( next ( 2 ) ) ) {
480
483
consume ( ) ;
481
- repr += String . fromCodePoint ( code ) ;
482
- consume ( ) ;
483
- repr += String . fromCodePoint ( code ) ;
484
- type = "number" ;
484
+ numberPart += "." ;
485
485
while ( digit ( next ( ) ) ) {
486
486
consume ( ) ;
487
- repr += String . fromCodePoint ( code ) ;
487
+ numberPart += String . fromCodePoint ( code ) ;
488
488
}
489
+ isInteger = false ;
489
490
}
490
491
var c1 = next ( 1 ) , c2 = next ( 2 ) , c3 = next ( 3 ) ;
491
- if ( ( c1 == 0x45 || c1 == 0x65 ) && digit ( c2 ) ) {
492
- consume ( ) ;
493
- repr += String . fromCodePoint ( code ) ;
492
+ const eDigit = ( c1 == 0x45 || c1 == 0x65 ) && digit ( c2 ) ;
493
+ const eSignDigit = ( c1 == 0x45 || c1 == 0x65 ) && ( c2 == 0x2b || c2 == 0x2d ) && digit ( c3 ) ;
494
+ if ( eDigit || eSignDigit ) {
494
495
consume ( ) ;
495
- repr += String . fromCodePoint ( code ) ;
496
- type = "number" ;
497
- while ( digit ( next ( ) ) ) {
496
+ if ( eSignDigit ) {
498
497
consume ( ) ;
499
- repr += String . fromCodePoint ( code ) ;
498
+ exponentPart += String . fromCodePoint ( code ) ;
500
499
}
501
- } else if ( ( c1 == 0x45 || c1 == 0x65 ) && ( c2 == 0x2b || c2 == 0x2d ) && digit ( c3 ) ) {
502
- consume ( ) ;
503
- repr += String . fromCodePoint ( code ) ;
504
- consume ( ) ;
505
- repr += String . fromCodePoint ( code ) ;
506
- consume ( ) ;
507
- repr += String . fromCodePoint ( code ) ;
508
- type = "number" ;
509
500
while ( digit ( next ( ) ) ) {
510
501
consume ( ) ;
511
- repr += String . fromCodePoint ( code ) ;
502
+ exponentPart += String . fromCodePoint ( code ) ;
512
503
}
504
+ isInteger = false ;
513
505
}
514
- var value = convertAStringToANumber ( repr ) ;
515
- return { value :value , isInteger : type == "integer" } ;
516
- } ;
506
+ let value = + numberPart ;
507
+ if ( exponentPart ) value = value * Math . pow ( 10 , + exponentPart ) ;
517
508
518
- var convertAStringToANumber = function ( string ) {
519
- // CSS's number rules are identical to JS, afaik.
520
- return + string ;
509
+ return { value, isInteger, sign} ;
521
510
} ;
522
511
523
512
var consumeTheRemnantsOfABadURL = function ( ) {
@@ -756,17 +745,19 @@ class URLToken extends CSSParserToken {
756
745
}
757
746
758
747
class NumberToken extends CSSParserToken {
759
- constructor ( val , isInteger ) {
748
+ constructor ( val , isInteger , sign = undefined ) {
760
749
super ( "NUMBER" ) ;
761
750
this . value = val ;
762
751
this . isInteger = isInteger ;
752
+ this . sign = sign ;
763
753
}
764
754
toString ( ) {
765
- if ( this . isInteger ) return `INT(${ this . value } )` ;
766
- return `NUMBER(${ this . value } )` ;
755
+ const name = this . isInteger ? "INT" : "NUMBER" ;
756
+ const sign = this . sign == "+" : "+ " : " "
757
+ return `${ name } (${ this . sign } ${ this . value } )` ;
767
758
}
768
- toJSON ( ) { return { type :this . type , value :this . value , isInteger :this . isInteger } ; }
769
- toSource ( ) { return formatNumber ( this . value ) ; }
759
+ toJSON ( ) { return { type :this . type , value :this . value , isInteger :this . isInteger , sign : this . sign } ; }
760
+ toSource ( ) { return formatNumber ( this . value , this . sign ) ; }
770
761
}
771
762
772
763
class PercentageToken extends CSSParserToken {
@@ -841,9 +832,9 @@ function escapeString(string) {
841
832
} ) . join ( "" ) ;
842
833
}
843
834
844
- function formatNumber ( num ) {
835
+ function formatNumber ( num , sign = undefined ) {
845
836
// TODO: Fix this to match CSS stringification behavior.
846
- return String ( num ) ;
837
+ return ( sign == "+" ? "+" : "" ) + String ( num ) ;
847
838
}
848
839
849
840
// Exportation.
0 commit comments