@@ -4,68 +4,117 @@ var dot = ".".charCodeAt(0);
44var exp = "e" . charCodeAt ( 0 ) ;
55var EXP = "E" . charCodeAt ( 0 ) ;
66
7+ // Check if three code points would start a number
8+ // https://www.w3.org/TR/css-syntax-3/#starts-with-a-number
9+ function likeNumber ( value ) {
10+ var code = value . charCodeAt ( 0 ) ;
11+ var nextCode ;
12+
13+ if ( code === plus || code === minus ) {
14+ nextCode = value . charCodeAt ( 1 ) ;
15+
16+ if ( nextCode >= 48 && nextCode <= 57 ) {
17+ return true ;
18+ }
19+
20+ var nextNextCode = value . charCodeAt ( 2 ) ;
21+
22+ if ( nextCode === dot && nextNextCode >= 48 && nextNextCode <= 57 ) {
23+ return true ;
24+ }
25+
26+ return false ;
27+ }
28+
29+ if ( code === dot ) {
30+ nextCode = value . charCodeAt ( 1 ) ;
31+
32+ if ( nextCode >= 48 && nextCode <= 57 ) {
33+ return true ;
34+ }
35+
36+ return false ;
37+ }
38+
39+ if ( code >= 48 && code <= 57 ) {
40+ return true ;
41+ }
42+
43+ return false ;
44+ }
45+
46+ // Consume a number
47+ // https://www.w3.org/TR/css-syntax-3/#consume-number
748module . exports = function ( value ) {
849 var pos = 0 ;
950 var length = value . length ;
10- var dotted = false ;
11- var sciPos = - 1 ;
12- var containsNumber = false ;
1351 var code ;
52+ var nextCode ;
53+ var nextNextCode ;
54+
55+ if ( length === 0 || ! likeNumber ( value ) ) {
56+ return false ;
57+ }
58+
59+ code = value . charCodeAt ( pos ) ;
60+
61+ if ( code === plus || code === minus ) {
62+ pos ++ ;
63+ }
1464
1565 while ( pos < length ) {
1666 code = value . charCodeAt ( pos ) ;
1767
18- if ( code >= 48 && code <= 57 ) {
19- containsNumber = true ;
20- } else if ( code === exp || code === EXP ) {
21- if ( sciPos > - 1 || pos === 0 ) {
22- break ;
23- }
24- sciPos = pos ;
68+ if ( code < 48 || code > 57 ) {
69+ break ;
70+ }
2571
26- var nextCode = value . charCodeAt ( pos + 1 ) ;
72+ pos += 1 ;
73+ }
2774
28- if (
29- nextCode === plus ||
30- nextCode === minus ||
31- ( nextCode >= 48 && nextCode <= 57 )
32- ) {
33- if ( nextCode === plus || nextCode === minus ) {
34- var nextNextCode = value . charCodeAt ( pos + 2 ) ;
75+ code = value . charCodeAt ( pos ) ;
76+ nextCode = value . charCodeAt ( pos + 1 ) ;
3577
36- if ( nextNextCode < 48 || nextNextCode > 57 ) {
37- break ;
38- }
78+ if ( code === dot && nextCode >= 48 && nextCode <= 57 ) {
79+ pos += 2 ;
3980
40- pos += 1 ;
41- }
81+ while ( pos < length ) {
82+ code = value . charCodeAt ( pos ) ;
4283
43- pos += 1 ;
44- } else {
84+ if ( code < 48 || code > 57 ) {
4585 break ;
4686 }
47- } else if ( code === dot ) {
48- if ( dotted ) {
49- break ;
50- }
51- dotted = true ;
52- } else if ( code === plus || code === minus ) {
53- if ( pos !== 0 ) {
54- break ;
55- }
56- } else {
57- break ;
58- }
5987
60- pos += 1 ;
88+ pos += 1 ;
89+ }
6190 }
6291
63- if ( sciPos + 1 === pos ) pos -- ;
92+ code = value . charCodeAt ( pos ) ;
93+ nextCode = value . charCodeAt ( pos + 1 ) ;
94+ nextNextCode = value . charCodeAt ( pos + 2 ) ;
95+
96+ if (
97+ ( code === exp || code === EXP ) &&
98+ ( ( nextCode >= 48 && nextCode <= 57 ) ||
99+ ( ( nextCode === plus || nextCode === minus ) &&
100+ nextNextCode >= 48 &&
101+ nextNextCode <= 57 ) )
102+ ) {
103+ pos += nextCode === plus || nextCode === minus ? 3 : 2 ;
104+
105+ while ( pos < length ) {
106+ code = value . charCodeAt ( pos ) ;
64107
65- return containsNumber
66- ? {
67- number : value . slice ( 0 , pos ) ,
68- unit : value . slice ( pos )
108+ if ( code < 48 || code > 57 ) {
109+ break ;
69110 }
70- : false ;
111+
112+ pos += 1 ;
113+ }
114+ }
115+
116+ return {
117+ number : value . slice ( 0 , pos ) ,
118+ unit : value . slice ( pos )
119+ } ;
71120} ;
0 commit comments