forked from lojjic/PIE
/
Tokenizer.js
268 lines (238 loc) · 8.9 KB
/
Tokenizer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* A tokenizer for CSS value strings.
* @constructor
* @param {string} css The CSS value string
*/
PIE.Tokenizer = (function() {
function Tokenizer( css ) {
this.css = css;
this.ch = 0;
this.tokens = [];
this.tokenIndex = 0;
}
/**
* Enumeration of token type constants.
* @enum {number}
*/
var Type = Tokenizer.Type = {
ANGLE: 1,
CHARACTER: 2,
COLOR: 4,
DIMEN: 8,
FUNCTION: 16,
IDENT: 32,
LENGTH: 64,
NUMBER: 128,
OPERATOR: 256,
PERCENT: 512,
STRING: 1024,
URL: 2048
};
/**
* A single token
* @constructor
* @param {number} type The type of the token - see PIE.Tokenizer.Type
* @param {string} value The value of the token
*/
Tokenizer.Token = function( type, value ) {
this.tokenType = type;
this.tokenValue = value;
};
Tokenizer.Token.prototype = {
isLength: function() {
return this.tokenType & Type.LENGTH || ( this.tokenType & Type.NUMBER && this.tokenValue === '0' );
},
isLengthOrPercent: function() {
return this.isLength() || this.tokenType & Type.PERCENT;
}
};
Tokenizer.prototype = {
whitespace: /\s/,
number: /^[\+\-]?(\d*\.)?\d+/,
url: /^url\(\s*("([^"]*)"|'([^']*)'|([!#$%&*-~]*))\s*\)/i,
ident: /^\-?[_a-z][\w-]*/i,
string: /^("([^"]*)"|'([^']*)')/,
operator: /^[\/,]/,
hash: /^#[\w]+/,
hashColor: /^#([\da-f]{6}|[\da-f]{3})/i,
unitTypes: {
'px': Type.LENGTH, 'em': Type.LENGTH, 'ex': Type.LENGTH,
'mm': Type.LENGTH, 'cm': Type.LENGTH, 'in': Type.LENGTH,
'pt': Type.LENGTH, 'pc': Type.LENGTH,
'deg': Type.ANGLE, 'rad': Type.ANGLE, 'grad': Type.ANGLE
},
colorFunctions: {
'rgb': 1, 'rgba': 1, 'hsl': 1, 'hsla': 1
},
/**
* Advance to and return the next token in the CSS string. If the end of the CSS string has
* been reached, null will be returned.
* @param {boolean} forget - if true, the token will not be stored for the purposes of backtracking with prev().
* @return {PIE.Tokenizer.Token}
*/
next: function( forget ) {
var css, ch, firstChar, match, val,
me = this;
function newToken( type, value ) {
var tok = new Tokenizer.Token( type, value );
if( !forget ) {
me.tokens.push( tok );
me.tokenIndex++;
}
return tok;
}
function failure() {
me.tokenIndex++;
return null;
}
// In case we previously backed up, return the stored token in the next slot
if( this.tokenIndex < this.tokens.length ) {
return this.tokens[ this.tokenIndex++ ];
}
// Move past leading whitespace characters
while( this.whitespace.test( this.css.charAt( this.ch ) ) ) {
this.ch++;
}
if( this.ch >= this.css.length ) {
return failure();
}
ch = this.ch;
css = this.css.substring( this.ch );
firstChar = css.charAt( 0 );
switch( firstChar ) {
case '#':
if( match = css.match( this.hashColor ) ) {
this.ch += match[0].length;
return newToken( Type.COLOR, match[0] );
}
break;
case '"':
case "'":
if( match = css.match( this.string ) ) {
this.ch += match[0].length;
return newToken( Type.STRING, match[2] || match[3] || '' );
}
break;
case "/":
case ",":
this.ch++;
return newToken( Type.OPERATOR, firstChar );
case 'u':
if( match = css.match( this.url ) ) {
this.ch += match[0].length;
return newToken( Type.URL, match[2] || match[3] || match[4] || '' );
}
}
// Numbers and values starting with numbers
if( match = css.match( this.number ) ) {
val = match[0];
this.ch += val.length;
// Check if it is followed by a unit
if( css.charAt( val.length ) === '%' ) {
this.ch++;
return newToken( Type.PERCENT, val + '%' );
}
if( match = css.substring( val.length ).match( this.ident ) ) {
val += match[0];
this.ch += match[0].length;
return newToken( this.unitTypes[ match[0].toLowerCase() ] || Type.DIMEN, val );
}
// Plain ol' number
return newToken( Type.NUMBER, val );
}
// Identifiers
if( match = css.match( this.ident ) ) {
val = match[0];
this.ch += val.length;
// Named colors
if( val.toLowerCase() in PIE.Color.names || val === 'currentColor' || val === 'transparent' ) {
return newToken( Type.COLOR, val );
}
// Functions
if( css.charAt( val.length ) === '(' ) {
this.ch++;
// Color values in function format: rgb, rgba, hsl, hsla
if( val.toLowerCase() in this.colorFunctions ) {
function isNum( tok ) {
return tok && tok.tokenType & Type.NUMBER;
}
function isNumOrPct( tok ) {
return tok && ( tok.tokenType & ( Type.NUMBER | Type.PERCENT ) );
}
function isValue( tok, val ) {
return tok && tok.tokenValue === val;
}
function next() {
return me.next( 1 );
}
if( ( val.charAt(0) === 'r' ? isNumOrPct( next() ) : isNum( next() ) ) &&
isValue( next(), ',' ) &&
isNumOrPct( next() ) &&
isValue( next(), ',' ) &&
isNumOrPct( next() ) &&
( val === 'rgb' || val === 'hsa' || (
isValue( next(), ',' ) &&
isNum( next() )
) ) &&
isValue( next(), ')' ) ) {
return newToken( Type.COLOR, this.css.substring( ch, this.ch ) );
}
return failure();
}
return newToken( Type.FUNCTION, val );
}
// Other identifier
return newToken( Type.IDENT, val );
}
// Standalone character
this.ch++;
return newToken( Type.CHARACTER, firstChar );
},
/**
* Determine whether there is another token
* @return {boolean}
*/
hasNext: function() {
var next = this.next();
this.prev();
return !!next;
},
/**
* Back up and return the previous token
* @return {PIE.Tokenizer.Token}
*/
prev: function() {
return this.tokens[ this.tokenIndex-- - 2 ];
},
/**
* Retrieve all the tokens in the CSS string
* @return {Array.<PIE.Tokenizer.Token>}
*/
all: function() {
while( this.next() ) {}
return this.tokens;
},
/**
* Return a list of tokens from the current position until the given function returns
* true. The final token will not be included in the list.
* @param {function():boolean} func - test function
* @param {boolean} require - if true, then if the end of the CSS string is reached
* before the test function returns true, null will be returned instead of the
* tokens that have been found so far.
* @return {Array.<PIE.Tokenizer.Token>}
*/
until: function( func, require ) {
var list = [], t, hit;
while( t = this.next() ) {
if( func( t ) ) {
hit = true;
this.prev();
break;
}
list.push( t );
}
return require && !hit ? null : list;
}
};
return Tokenizer;
})();