-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmyFormat_v1.0.js
441 lines (404 loc) · 17.4 KB
/
myFormat_v1.0.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* javascript format
* ------------------
* writen by hongru.chen [hongru.chenhr[at]gmail.com]
*
* Options:
* {
* indentSize: (default 4), --> indentation size
* indentChar: (default space), --> character to indentation with
* isBlankLinesRetain: (default true), --> whether to preserve blank new lines
* maxBlankLines: (default unlimited), --> max blank lines to preserve
* isSpaceAfterAnonFunc: (default true), --> 'function()' or 'function ()'
* braceStyle: (default 'collapse'), --> 'collapse' || 'expand' || 'end-expand'
* isKeepArrayIndent: (default false) --> [[], []] or [
[],
[]
]
* }
*
* e.g
* new jsFormat(sourceText, options)
*
*/
(function () {
var extend = function (target, source, overwrite) {
if (overwrite === undefined) overwrite = true;
for (var p in source) {
if (overwrite || !(p in target))
target[p] = source[p];
}
return target;
},
log = function (s) {
!!window.console && window.console.log(s);
};
var jsFormat = function (source, options) {
this.setOptions(options);
this.setVars(source);
//this.setMode();
this.initialize();
};
extend(jsFormat.prototype, {
setOptions: function (o) {
this.opt = {
indentSize : 4,
indentChar : ' ',
isBlankLinesRetain : true,
maxBlankLines : false,
isSpaceAfterAnonFunc: true,
isKeepArrayIndent : false,
braceStyle : 'collapse',
isParseHtml : false
};
extend(this.opt, o || {});
},
setVars: function (s) {
this.indentStr = '';
this.source = s;
this.lastWord = '';
this.lastType = 'TK_START_EXPR'; // the default value of lastType
this.lastText = '';
this.preLastText = '';
this.output = [];
this.whitespace = ' \n\r\t'.split('');
this.wordchar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$'.split('');
this.numbers = '0123456789'.split('');
this.operator = '+ - * % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::'.split(' ');
this.lineKeywords = 'continue,try,catch,finaly,throw,return,var,if,else,switch,case,default,for,while,do,break,function,this,new'.split(',');
this.jsKeywords = 'getElementById,getElementsByTagName,getElementsByName,length,offsetLeft,offsetRight,offsetWidth,offsetHeight,scrollLeft,scrollTop,setInterval,clearInterval,setTimeout,clearTimeout,clientWidth,clientHeight,document,window,Date,Number,String,Object,Array,Boolean,Function,null,true,false'.split(',');
this.tokenStore = [];
this.parsePos = 0;
this.sourceLength = s.length;
this.token = {};
this.initIndentLv = 0;
this.toHtml = '';
},
encodeHtml: function (str) {
if (this.opt.isParseHtml) {
var el = document.createElement('pre'),
t = document.createTextNode(str);
el.appendChild(t);
return el.innerHTML;
}
return str;
},
isInArray: function (c, arr) {
for (var i = 0; i < arr.length; i ++) {
if (c === arr[i]) return true;
}
return false;
},
isArrayMode: function (mode) {
return (mode === '[EXPRESSION]' || mode === '[INDENTED_EXPRESSION]');
},
// trim whitespace at the end of output
// if isEatLines == true, also trim the newline space such as '\n' '\r'
trimOutput: function (isEatLines) {
var o = this.output,
l = o.length;
while(!!l && (o[l-1] === ' ' || o[l-1] === this.indentStr || (!!isEatLines && (o[l-1] === '\n' || o[l-1] === '\r')))) {
this.output.pop();
}
},
getToken: function () {
if (this.parsePos > this.sourceLength) {
return ['', 'TK_EOF'];
}
var c = this.source.charAt(this.parsePos);
this.parsePos ++;
this.blanklinesN = 0;
// word
if (this.isInArray(c, this.wordchar)) {
return this._wordToken(c);
}
// expression '(' or '['
if (c === '(' || c === '[') {
return [c, 'TK_START_EXPR'];
}
// expression ')' or ']'
if (c === ')' || c === ']') {
return [c, 'TK_END_EXPR'];
}
// brace '{'
if (c === '{') {
return [c, 'TK_START_BRACE'];
}
// brace '}'
if (c === '}') {
return [c, 'TK_END_BRACE'];
}
// semicolon ';'
if (c === ';') {
return [c, 'TK_SEMICOLON'];
}
/* !! modify at 2010 04 12 */
if (c === '/') {
if (this.source.charAt(this.parsePos) === '/' || this.source.charAt(this.parsePos) === '*') {
return this._commentToken(c);
}
else if (this.lastType === 'TK_REGEXP'
|| this.lastType === 'TK_STRING'
//|| this.lastType === 'TK_END_BRACE'
|| this.lastType === 'TK_END_EXPR'
|| (this.lastType === 'TK_BLOCK_COMMENT' && this.preLastType === 'TK_WORD' && this.preLastText !== 'return' && this.preLastText !== 'do')
|| (this.lastType === 'TK_WORD' && this.lastText !== 'return' && this.lastText !== 'do')) {
return [c, 'TK_OPERATOR']
}
else {
return this._regexpToken(c);
}
}
// operator without '/'
if (this.isInArray(c, this.operator)) {
return this._operatorToken(c);
}
// string such as '"' or "'"
if (c === '"' || c === "'") {
return this._stringToken(c);
}
// whitespace to fit the situation of keep the array-indent of source code
if (this.opt.isKeepArrayIndent && this.isArrayMode(this.tkConfig.mode) && this.isInArray(c, this.whitespace)) {
var ret = this._keepindentWhitespace(c);
if (ret !== undefined && ret instanceof Array) {return ret};
} else if (this.isInArray(c, this.whitespace)) {
// normal whitespace
var ret = this._normalWhitespace(c);
if (ret !== undefined && ret instanceof Array) {return ret};
}
return [c, 'TK_UNKNOWN']
},
newLine: function (isIgonreRepeat) {
this.output.push('\n');
this.addLine = true;
for (var i=0; i < this.tkConfig.indentLv; i++) {
this.output.push(this.indentStr)
}
},
_operatorToken: function (c) {
while(this.parsePos < this.sourceLength && this.isInArray(c + this.source.charAt(this.parsePos), this.operator)) {
c += this.source.charAt(this.parsePos);
this.parsePos ++;
}
return [c, 'TK_OPERATOR'];
},
_normalWhitespace: function (c) {
while(this.isInArray(this.source.charAt(this.parsePos), this.whitespace)) {
/*if (c === '\n') {
this.blanklinesN += (this.opt.maxBlankLines ? (this.blanklinesN <= this.opt.maxBlankLines ? 1 : 0) : 1);
}*/
c += this.source.charAt(this.parsePos);
if (this.parsePos > this.sourceLength) {
return ['', 'TK_EOF'];
}
this.parsePos ++;
}
return [c, 'TK_WHITESPACE'];
/*if (this.opt.isBlankLinesRetain) {
if (this.blanklinesN > 1) {
for (var i=0; i< this.blanklinesN; i++) {
this.newLine(i === 0);
this.addLine = true;
}
}
}
this.wantNewline = (this.blanklinesN > 0);*/
},
_keepindentWhitespace: function (c) {
var spaceCnt = 0;
while(this.isInArray(c, this.whitespace)) {
if (c === '\n') {
this.trimOutput(); // before push '\n'
this.output.push('\n');
this.addLine = true;
spaceCnt = 0;
}
else if (c === '\t') {
spaceCnt += 4; // plus spaceCnt to keep indent the source has
}
else if (c === '\r') {
// todo
}
else {
spaceCnt ++;
}
if (this.parsePos > this.sourceLength) {
return ['', 'TK_EOF']
}
// get next char
c = this.source.charAt(this.parsePos);
this.parsePos ++;
}
// set every newline's indentation
if (this.tkConfig.baseIndentation === undefined) this.tkConfig.baseIndentation = spaceCnt;
if (this.addline === true) {
var i;
for (i = 0; i < this.tkConfig.indentLv + 1; i++) {
this.output.push(this.indentStr)
}
if (this.tkConfig.baseIndentation !== undefined) {
for (i = 0; i < spaceCnt - this.tiConfig.baseIndentation; i++) {
this.output.push(' ');
}
}
}
},
_wordToken: function (c) {
if (this.parsePos < this.sourceLength) {
while(this.isInArray(this.source.charAt(this.parsePos), this.wordchar)) {
c += this.source.charAt(this.parsePos);
this.parsePos ++;
if (this.parsePos === this.sourceLength) break;
}
}
return [c, 'TK_WORD'];
},
_commentToken: function (c) {
var comment = '/*';
// match inline comments /* ... */
var isInline = true,
curChar = this.source.charAt(this.parsePos);
if (curChar === '*') {
this.parsePos ++;
if (this.parsePos < this.sourceLength) {
while(!(this.source.charAt(this.parsePos) === '*' && !!this.source.charAt(this.parsePos + 1) && this.source.charAt(this.parsePos + 1) === '/') && this.parsePos < this.sourceLength) { // exclude /**/, get content from /*, end as */
//comment content start after /*
c = this.source.charAt(this.parsePos);
if (c === '\x0d' || c === '\x0a') {
isInline = false;
}
comment += c;
this.parsePos ++;
if (this.parsePos > this.sourceLength) {
break;
}
}
}
this.parsePos += 2;
comment += '*/';
if (isInline) {
return [comment, 'TK_INLINE_COMMENT'];
} else {
return [comment, 'TK_BLOCK_COMMENT'];
}
}
// match comment like //...
if (curChar === '/') {
comment = c;
while(this.source.charAt(this.parsePos) !== '\r' && this.source.charAt(this.parsePos) !== '\n') {
comment += this.source.charAt(this.parsePos);
this.parsePos ++;
if (this.parsePos > this.sourceLength) {
break;
}
}
this.parsePos ++;
return [comment+'\n', 'TK_COMMENT']
}
},
_regexpToken: function (c) {
var escape = false,
ret = c;
if (this.parsePos < this.sourceLength) {
var retainExp = false;
while((escape || retainExp || this.source.charAt(this.parsePos) !== c) && this.source.charAt(this.parsePos) !== '\n') {
var curChar = this.source.charAt(this.parsePos);
ret += curChar;
if (!escape) {
if (curChar === '\\') escape = true; // '/\\/'.charAt(1) === '\\'
if (curChar === '[') retainExp = true; // '/[...' exp continue
else if (curChar === ']') retainExp = false; // '/]....' exp search break
}
else {escape = false}
this.parsePos ++;
// if regexp continue to the end of file, return
if (this.parsePos >= this.sourceLength) {
return [ret, 'TK_REGEXP']
}
}
}
this.parsePos ++;
ret += c;
// regexp may have modifier like /regexp/ig
// here we relax the rules of the modifiers, more than i,g,m...
while(this.parsePos < this.sourceLength && this.isInArray(this.source.charAt(this.parsePos), this.wordchar)) {
ret += this.source.charAt(this.parsePos);
this.parsePos ++;
}
return [ret, 'TK_REGEXP'];
},
_stringToken: function (c) {
var escape = false,
ret = c;
if (this.parsePos < this.sourceLength) {
while(escape || this.source.charAt(this.parsePos) !== c) {
var curChar = this.source.charAt(this.parsePos);
ret += curChar;
if (!escape) {
if (curChar === '\\') escape = true;
} else escape = false;
this.parsePos ++;
if (this.parsePos >= this.sourceLength) {
return [ret, 'TK_STRING']
}
}
}
this.parsePos ++;
ret += c;
ret = this.encodeHtml(ret);
return [ret, 'TK_STRING'];
},
setMode: function (mode) {
// push previous token config
if (!this.tkConfig) {
this.tokenStore.push(this.tkConfig);
}
// define current token config
this.tkConfig = {
mode: mode,
preMode: !!this.tkConfig ? this.tkConfig.mode : 'BLOCK',
eatSpace: false,
varLine: false,
ifLine: false,
varLineReindent: false,
indentLv: (!!this.tkConfig ? this.tkConfig.indentLv + ((this.tkConfig.varLine && this.tkConfig.varLineReindent) ? 1 : 0) : 0)
}
},
initialize: function () {
//console.log(this.source);
this.setMode('BLOCK');
while(true) {
var tk = this.getToken();
if (tk[1] !== 'TK_WHITESPACE') {
// filter the whitespace
this.token.text = tk[0];
this.token.type = tk[1];
//log(tk)
if (tk[1] === 'TK_EOF') {
//log('eof');
break;
}
this.preLastText = this.lastText;
this.preLastType = this.lastType;
this.lastText = this.token.text;
this.lastType = this.token.type;
}
this.highLight(tk);
}
return this.toHtml;
},
highLight: function (tk) {
var cn = tk[1].toLowerCase();
if (tk[1] === 'TK_WORD' && this.isInArray(tk[0], this.lineKeywords)) {
this.toHtml += '<span class="tk_keywords">'+tk[0]+'</span>'
}
else if (tk[1] === 'TK_WORD' && this.isInArray(tk[0], this.jsKeywords)){
this.toHtml += '<span class="tk_jskeywords">'+tk[0]+'</span>'
}
else {
this.toHtml += '<span class="'+cn+'">'+tk[0]+'</span>'
}
}
})
this.jsFormat = jsFormat;
})();