1 | var minus = "-".charCodeAt(0);
|
---|
2 | var plus = "+".charCodeAt(0);
|
---|
3 | var dot = ".".charCodeAt(0);
|
---|
4 | var exp = "e".charCodeAt(0);
|
---|
5 | var EXP = "E".charCodeAt(0);
|
---|
6 |
|
---|
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
|
---|
48 | module.exports = function(value) {
|
---|
49 | var pos = 0;
|
---|
50 | var length = value.length;
|
---|
51 | 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 | }
|
---|
64 |
|
---|
65 | while (pos < length) {
|
---|
66 | code = value.charCodeAt(pos);
|
---|
67 |
|
---|
68 | if (code < 48 || code > 57) {
|
---|
69 | break;
|
---|
70 | }
|
---|
71 |
|
---|
72 | pos += 1;
|
---|
73 | }
|
---|
74 |
|
---|
75 | code = value.charCodeAt(pos);
|
---|
76 | nextCode = value.charCodeAt(pos + 1);
|
---|
77 |
|
---|
78 | if (code === dot && nextCode >= 48 && nextCode <= 57) {
|
---|
79 | pos += 2;
|
---|
80 |
|
---|
81 | while (pos < length) {
|
---|
82 | code = value.charCodeAt(pos);
|
---|
83 |
|
---|
84 | if (code < 48 || code > 57) {
|
---|
85 | break;
|
---|
86 | }
|
---|
87 |
|
---|
88 | pos += 1;
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
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);
|
---|
107 |
|
---|
108 | if (code < 48 || code > 57) {
|
---|
109 | break;
|
---|
110 | }
|
---|
111 |
|
---|
112 | pos += 1;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | return {
|
---|
117 | number: value.slice(0, pos),
|
---|
118 | unit: value.slice(pos)
|
---|
119 | };
|
---|
120 | };
|
---|