js-beautify 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/.document +5 -0
  2. data/.gitmodules +4 -0
  3. data/Gemfile +13 -0
  4. data/Gemfile.lock +27 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.rdoc +13 -0
  7. data/Rakefile +29 -0
  8. data/VERSION +1 -0
  9. data/bin/js-beautify +5 -0
  10. data/init.sh +3 -0
  11. data/js-beautify-copy/.gitmodules +3 -0
  12. data/js-beautify-copy/Makefile +71 -0
  13. data/js-beautify-copy/README.md +39 -0
  14. data/js-beautify-copy/attic/beautify-cl/beautify-cl.js +142 -0
  15. data/js-beautify-copy/attic/bin/beautify_js +80 -0
  16. data/js-beautify-copy/attic/opera-userscript/beautifier.js +1087 -0
  17. data/js-beautify-copy/attic/opera-userscript/make_opera_userscript.sh +42 -0
  18. data/js-beautify-copy/attic/qtscript/jsbeautify.cpp +121 -0
  19. data/js-beautify-copy/attic/qtscript/jsbeautify.pro +5 -0
  20. data/js-beautify-copy/attic/qtscript/jsbeautify.qrc +6 -0
  21. data/js-beautify-copy/attic/qtscript/readme.txt +28 -0
  22. data/js-beautify-copy/attic/readme.txt +2 -0
  23. data/js-beautify-copy/attic/unmaintained/bbedit/jsBeautify_BBED.scpt +522 -0
  24. data/js-beautify-copy/attic/unmaintained/c-sharp/JSBeautify.cs +801 -0
  25. data/js-beautify-copy/attic/v8/README.txt +40 -0
  26. data/js-beautify-copy/attic/v8/beautify.h +2390 -0
  27. data/js-beautify-copy/attic/v8/jsbeautify.cpp +215 -0
  28. data/js-beautify-copy/beautify-css.js +198 -0
  29. data/js-beautify-copy/beautify-html.js +514 -0
  30. data/js-beautify-copy/beautify.js +1293 -0
  31. data/js-beautify-copy/favicon.png +0 -0
  32. data/js-beautify-copy/index.html +401 -0
  33. data/js-beautify-copy/jquery/jquery.cookie.js +96 -0
  34. data/js-beautify-copy/jquery/jquery.js +167 -0
  35. data/js-beautify-copy/license.txt +24 -0
  36. data/js-beautify-copy/php/jsbeautifier.php +1599 -0
  37. data/js-beautify-copy/php/test.php +476 -0
  38. data/js-beautify-copy/python/MANIFEST.in +2 -0
  39. data/js-beautify-copy/python/js-beautify +7 -0
  40. data/js-beautify-copy/python/js-beautify-profile +16 -0
  41. data/js-beautify-copy/python/js-beautify-test +10 -0
  42. data/js-beautify-copy/python/jsbeautifier/__init__.py +1166 -0
  43. data/js-beautify-copy/python/jsbeautifier/tests/__init__.py +1 -0
  44. data/js-beautify-copy/python/jsbeautifier/tests/testindentation.py +43 -0
  45. data/js-beautify-copy/python/jsbeautifier/tests/testjsbeautifier.py +464 -0
  46. data/js-beautify-copy/python/jsbeautifier/unpackers/README.specs.mkd +25 -0
  47. data/js-beautify-copy/python/jsbeautifier/unpackers/__init__.py +67 -0
  48. data/js-beautify-copy/python/jsbeautifier/unpackers/evalbased.py +39 -0
  49. data/js-beautify-copy/python/jsbeautifier/unpackers/javascriptobfuscator.py +58 -0
  50. data/js-beautify-copy/python/jsbeautifier/unpackers/myobfuscate.py +86 -0
  51. data/js-beautify-copy/python/jsbeautifier/unpackers/packer.py +104 -0
  52. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/__init__.py +2 -0
  53. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/test-myobfuscate-input.js +1 -0
  54. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/test-myobfuscate-output.js +65 -0
  55. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/test-packer-62-input.js +1 -0
  56. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/test-packer-non62-input.js +1 -0
  57. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/testjavascriptobfuscator.py +46 -0
  58. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/testmyobfuscate.py +40 -0
  59. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/testpacker.py +34 -0
  60. data/js-beautify-copy/python/jsbeautifier/unpackers/tests/testurlencode.py +36 -0
  61. data/js-beautify-copy/python/jsbeautifier/unpackers/urlencode.py +34 -0
  62. data/js-beautify-copy/python/setup.py +17 -0
  63. data/js-beautify-copy/tests/beautify-tests.js +489 -0
  64. data/js-beautify-copy/tests/run-tests +17 -0
  65. data/js-beautify-copy/tests/sanitytest.js +128 -0
  66. data/js-beautify-copy/unpackers/javascriptobfuscator_unpacker.js +103 -0
  67. data/js-beautify-copy/unpackers/myobfuscate_unpacker.js +81 -0
  68. data/js-beautify-copy/unpackers/p_a_c_k_e_r_unpacker.js +61 -0
  69. data/js-beautify-copy/unpackers/urlencode_unpacker.js +51 -0
  70. data/lib/js-beautify.rb +0 -0
  71. data/test/helper.rb +18 -0
  72. data/test/test_js-beautify.rb +7 -0
  73. data/update.sh +23 -0
  74. metadata +173 -0
@@ -0,0 +1,1087 @@
1
+ // ==UserScript==
2
+ // @name Scripts beautifier for Opera
3
+ // @author Rafal Chlodnicki
4
+ // @author Einar Lielmanis
5
+ // @version 1.1
6
+ // @include *
7
+ // ==/UserScript==
8
+
9
+ (function(){
10
+
11
+ // Enabling this setting will beautify all scripts on page
12
+ var enabled = /*@Beautify all scripts@bool@*/false/*@*/;
13
+
14
+ /* Specially formated comment above is for use with Opera Unite UJS Manager
15
+ https://unite.opera.com/application/401/ */
16
+
17
+
18
+ /*jslint onevar: false, plusplus: false */
19
+ /*
20
+
21
+ JS Beautifier
22
+ ---------------
23
+
24
+
25
+ Written by Einar Lielmanis, <einar@jsbeautifier.org>
26
+ http://jsbeautifier.org/
27
+
28
+ Originally converted to javascript by Vital, <vital76@gmail.com>
29
+
30
+ You are free to use this in any way you want, in case you find this useful or working for you.
31
+
32
+ Usage:
33
+ js_beautify(js_source_text);
34
+ js_beautify(js_source_text, options);
35
+
36
+ The options are:
37
+ indent_size (default 4) — indentation size,
38
+ indent_char (default space) — character to indent with,
39
+ preserve_newlines (default true) — whether existing line breaks should be preserved,
40
+ indent_level (default 0) — initial indentation level, you probably won't need this ever,
41
+
42
+ space_after_anon_function (default false) — if true, then space is added between "function ()"
43
+ (jslint is happy about this); if false, then the common "function()" output is used.
44
+ braces_on_own_line (default false) - ANSI / Allman brace style, each opening/closing brace gets its own line.
45
+
46
+ e.g
47
+
48
+ js_beautify(js_source_text, {indent_size: 1, indent_char: '\t'});
49
+
50
+
51
+ */
52
+
53
+
54
+
55
+ function js_beautify(js_source_text, options) {
56
+
57
+ var input, output, token_text, last_type, last_text, last_last_text, last_word, flags, flag_store, indent_string;
58
+ var whitespace, wordchar, punct, parser_pos, line_starters, digits;
59
+ var prefix, token_type, do_block_just_closed;
60
+ var wanted_newline, just_added_newline, n_newlines;
61
+
62
+
63
+ // Some interpreters have unexpected results with foo = baz || bar;
64
+ options = options ? options : {};
65
+ var opt_braces_on_own_line = options.braces_on_own_line ? options.braces_on_own_line : false;
66
+ var opt_indent_size = options.indent_size ? options.indent_size : 4;
67
+ var opt_indent_char = options.indent_char ? options.indent_char : ' ';
68
+ var opt_preserve_newlines = typeof options.preserve_newlines === 'undefined' ? true : options.preserve_newlines;
69
+ var opt_indent_level = options.indent_level ? options.indent_level : 0; // starting indentation
70
+ var opt_space_after_anon_function = options.space_after_anon_function === 'undefined' ? false : options.space_after_anon_function;
71
+ var opt_keep_array_indentation = typeof options.keep_array_indentation === 'undefined' ? true : options.keep_array_indentation;
72
+
73
+ just_added_newline = false;
74
+
75
+ // cache the source's length.
76
+ var input_length = js_source_text.length;
77
+
78
+ function trim_output() {
79
+ while (output.length && (output[output.length - 1] === ' ' || output[output.length - 1] === indent_string)) {
80
+ output.pop();
81
+ }
82
+ }
83
+
84
+ function is_array(mode) {
85
+ return mode === '[EXPRESSION]' || mode === '[INDENTED-EXPRESSION]';
86
+ }
87
+
88
+
89
+ function print_newline(ignore_repeated) {
90
+
91
+ flags.eat_next_space = false;
92
+ if (opt_keep_array_indentation && is_array(flags.mode)) {
93
+ return;
94
+ }
95
+
96
+ ignore_repeated = typeof ignore_repeated === 'undefined' ? true : ignore_repeated;
97
+
98
+ flags.if_line = false;
99
+ trim_output();
100
+
101
+ if (!output.length) {
102
+ return; // no newline on start of file
103
+ }
104
+
105
+ if (output[output.length - 1] !== "\n" || !ignore_repeated) {
106
+ just_added_newline = true;
107
+ output.push("\n");
108
+ }
109
+ for (var i = 0; i < flags.indentation_level + (flags.var_line && flags.var_line_reindented ? 1 : 0); i += 1) {
110
+ output.push(indent_string);
111
+ }
112
+ }
113
+
114
+
115
+
116
+ function print_single_space() {
117
+ if (flags.eat_next_space) {
118
+ flags.eat_next_space = false;
119
+ return;
120
+ }
121
+ var last_output = ' ';
122
+ if (output.length) {
123
+ last_output = output[output.length - 1];
124
+ }
125
+ if (last_output !== ' ' && last_output !== '\n' && last_output !== indent_string) { // prevent occassional duplicate space
126
+ output.push(' ');
127
+ }
128
+ }
129
+
130
+
131
+ function print_token() {
132
+ just_added_newline = false;
133
+ flags.eat_next_space = false;
134
+ output.push(token_text);
135
+ }
136
+
137
+ function indent() {
138
+ flags.indentation_level += 1;
139
+ }
140
+
141
+
142
+ function remove_indent() {
143
+ if (output.length && output[output.length - 1] === indent_string) {
144
+ output.pop();
145
+ }
146
+ }
147
+
148
+ function set_mode(mode) {
149
+ if (flags) {
150
+ flag_store.push(flags);
151
+ }
152
+ flags = {
153
+ mode: mode,
154
+ var_line: false,
155
+ var_line_tainted: false,
156
+ var_line_reindented: false,
157
+ in_html_comment: false,
158
+ if_line: false,
159
+ in_case: false,
160
+ eat_next_space: false,
161
+ indentation_baseline: -1,
162
+ indentation_level: (flags ? flags.indentation_level + ((flags.var_line && flags.var_line_reindented) ? 1 : 0) : opt_indent_level)
163
+ };
164
+ }
165
+
166
+ function is_expression(mode) {
167
+ return mode === '[EXPRESSION]' || mode === '[INDENTED-EXPRESSION]' || mode === '(EXPRESSION)';
168
+ }
169
+
170
+ function restore_mode() {
171
+ do_block_just_closed = flags.mode === 'DO_BLOCK';
172
+ if (flag_store.length > 0) {
173
+ flags = flag_store.pop();
174
+ }
175
+ }
176
+
177
+
178
+ function in_array(what, arr) {
179
+ for (var i = 0; i < arr.length; i += 1) {
180
+ if (arr[i] === what) {
181
+ return true;
182
+ }
183
+ }
184
+ return false;
185
+ }
186
+
187
+ // Walk backwards from the colon to find a '?' (colon is part of a ternary op)
188
+ // or a '{' (colon is part of a class literal). Along the way, keep track of
189
+ // the blocks and expressions we pass so we only trigger on those chars in our
190
+ // own level, and keep track of the colons so we only trigger on the matching '?'.
191
+
192
+
193
+ function is_ternary_op() {
194
+ var level = 0,
195
+ colon_count = 0;
196
+ for (var i = output.length - 1; i >= 0; i--) {
197
+ switch (output[i]) {
198
+ case ':':
199
+ if (level === 0) {
200
+ colon_count++;
201
+ }
202
+ break;
203
+ case '?':
204
+ if (level === 0) {
205
+ if (colon_count === 0) {
206
+ return true;
207
+ } else {
208
+ colon_count--;
209
+ }
210
+ }
211
+ break;
212
+ case '{':
213
+ if (level === 0) {
214
+ return false;
215
+ }
216
+ level--;
217
+ break;
218
+ case '(':
219
+ case '[':
220
+ level--;
221
+ break;
222
+ case ')':
223
+ case ']':
224
+ case '}':
225
+ level++;
226
+ break;
227
+ }
228
+ }
229
+ }
230
+
231
+ function get_next_token() {
232
+ n_newlines = 0;
233
+
234
+ if (parser_pos >= input_length) {
235
+ return ['', 'TK_EOF'];
236
+ }
237
+
238
+ wanted_newline = false;
239
+
240
+ var c = input.charAt(parser_pos);
241
+ parser_pos += 1;
242
+
243
+
244
+ var keep_whitespace = opt_keep_array_indentation && is_array(flags.mode);
245
+
246
+ if (keep_whitespace) {
247
+
248
+ //
249
+ // slight mess to allow nice preservation of array indentation and reindent that correctly
250
+ // first time when we get to the arrays:
251
+ // var a = [
252
+ // ....'something'
253
+ // we make note of whitespace_count = 4 into flags.indentation_baseline
254
+ // so we know that 4 whitespaces in original source match indent_level of reindented source
255
+ //
256
+ // and afterwards, when we get to
257
+ // 'something,
258
+ // .......'something else'
259
+ // we know that this should be indented to indent_level + (7 - indentation_baseline) spaces
260
+ //
261
+ var whitespace_count = 0;
262
+
263
+ while (in_array(c, whitespace)) {
264
+
265
+ if (c === "\n") {
266
+ trim_output();
267
+ output.push("\n");
268
+ just_added_newline = true;
269
+ whitespace_count = 0;
270
+ } else {
271
+ if (c === '\t') {
272
+ whitespace_count += 4;
273
+ } else {
274
+ whitespace_count += 1;
275
+ }
276
+ }
277
+
278
+ if (parser_pos >= input_length) {
279
+ return ['', 'TK_EOF'];
280
+ }
281
+
282
+ c = input.charAt(parser_pos);
283
+ parser_pos += 1;
284
+
285
+ }
286
+ if (flags.indentation_baseline === -1) {
287
+ flags.indentation_baseline = whitespace_count;
288
+ }
289
+
290
+ if (just_added_newline) {
291
+ var i;
292
+ for (i = 0; i < flags.indentation_level + 1; i += 1) {
293
+ output.push(indent_string);
294
+ }
295
+ if (flags.indentation_baseline !== -1) {
296
+ for (i = 0; i < whitespace_count - flags.indentation_baseline; i++) {
297
+ output.push(' ');
298
+ }
299
+ }
300
+ }
301
+
302
+ } else {
303
+ while (in_array(c, whitespace)) {
304
+
305
+ if (c === "\n") {
306
+ n_newlines += 1;
307
+ }
308
+
309
+
310
+ if (parser_pos >= input_length) {
311
+ return ['', 'TK_EOF'];
312
+ }
313
+
314
+ c = input.charAt(parser_pos);
315
+ parser_pos += 1;
316
+
317
+ }
318
+
319
+ if (opt_preserve_newlines) {
320
+ if (n_newlines > 1) {
321
+ for (i = 0; i < n_newlines; i += 1) {
322
+ print_newline(i === 0);
323
+ just_added_newline = true;
324
+ }
325
+ }
326
+ }
327
+ wanted_newline = n_newlines > 0;
328
+ }
329
+
330
+
331
+ if (in_array(c, wordchar)) {
332
+ if (parser_pos < input_length) {
333
+ while (in_array(input.charAt(parser_pos), wordchar)) {
334
+ c += input.charAt(parser_pos);
335
+ parser_pos += 1;
336
+ if (parser_pos === input_length) {
337
+ break;
338
+ }
339
+ }
340
+ }
341
+
342
+ // small and surprisingly unugly hack for 1E-10 representation
343
+ if (parser_pos !== input_length && c.match(/^[0-9]+[Ee]$/) && (input.charAt(parser_pos) === '-' || input.charAt(parser_pos) === '+')) {
344
+
345
+ var sign = input.charAt(parser_pos);
346
+ parser_pos += 1;
347
+
348
+ var t = get_next_token(parser_pos);
349
+ c += sign + t[0];
350
+ return [c, 'TK_WORD'];
351
+ }
352
+
353
+ if (c === 'in') { // hack for 'in' operator
354
+ return [c, 'TK_OPERATOR'];
355
+ }
356
+ if (wanted_newline && last_type !== 'TK_OPERATOR' && !flags.if_line && (opt_preserve_newlines || last_text !== 'var')) {
357
+ print_newline();
358
+ }
359
+ return [c, 'TK_WORD'];
360
+ }
361
+
362
+ if (c === '(' || c === '[') {
363
+ return [c, 'TK_START_EXPR'];
364
+ }
365
+
366
+ if (c === ')' || c === ']') {
367
+ return [c, 'TK_END_EXPR'];
368
+ }
369
+
370
+ if (c === '{') {
371
+ return [c, 'TK_START_BLOCK'];
372
+ }
373
+
374
+ if (c === '}') {
375
+ return [c, 'TK_END_BLOCK'];
376
+ }
377
+
378
+ if (c === ';') {
379
+ return [c, 'TK_SEMICOLON'];
380
+ }
381
+
382
+ if (c === '/') {
383
+ var comment = '';
384
+ // peek for comment /* ... */
385
+ var inline_comment = true;
386
+ if (input.charAt(parser_pos) === '*') {
387
+ parser_pos += 1;
388
+ if (parser_pos < input_length) {
389
+ while (! (input.charAt(parser_pos) === '*' && input.charAt(parser_pos + 1) && input.charAt(parser_pos + 1) === '/') && parser_pos < input_length) {
390
+ c = input.charAt(parser_pos);
391
+ comment += c;
392
+ if (c === '\x0d' || c === '\x0a') {
393
+ inline_comment = false;
394
+ }
395
+ parser_pos += 1;
396
+ if (parser_pos >= input_length) {
397
+ break;
398
+ }
399
+ }
400
+ }
401
+ parser_pos += 2;
402
+ if (inline_comment) {
403
+ return ['/*' + comment + '*/', 'TK_INLINE_COMMENT'];
404
+ } else {
405
+ return ['/*' + comment + '*/', 'TK_BLOCK_COMMENT'];
406
+ }
407
+ }
408
+ // peek for comment // ...
409
+ if (input.charAt(parser_pos) === '/') {
410
+ comment = c;
411
+ while (input.charAt(parser_pos) !== "\x0d" && input.charAt(parser_pos) !== "\x0a") {
412
+ comment += input.charAt(parser_pos);
413
+ parser_pos += 1;
414
+ if (parser_pos >= input_length) {
415
+ break;
416
+ }
417
+ }
418
+ parser_pos += 1;
419
+ if (wanted_newline) {
420
+ print_newline();
421
+ }
422
+ return [comment, 'TK_COMMENT'];
423
+ }
424
+
425
+ }
426
+
427
+ if (c === "'" || // string
428
+ c === '"' || // string
429
+ (c === '/' && ((last_type === 'TK_WORD' && in_array(last_text, ['return', 'do'])) || (last_type === 'TK_START_EXPR' || last_type === 'TK_START_BLOCK' || last_type === 'TK_END_BLOCK' || last_type === 'TK_OPERATOR' || last_type === 'TK_EQUALS' || last_type === 'TK_EOF' || last_type === 'TK_SEMICOLON')))) { // regexp
430
+ var sep = c;
431
+ var esc = false;
432
+ var resulting_string = c;
433
+
434
+ if (parser_pos < input_length) {
435
+ if (sep === '/') {
436
+ //
437
+ // handle regexp separately...
438
+ //
439
+ var in_char_class = false;
440
+ while (esc || in_char_class || input.charAt(parser_pos) !== sep) {
441
+ resulting_string += input.charAt(parser_pos);
442
+ if (!esc) {
443
+ esc = input.charAt(parser_pos) === '\\';
444
+ if (input.charAt(parser_pos) === '[') {
445
+ in_char_class = true;
446
+ } else if (input.charAt(parser_pos) === ']') {
447
+ in_char_class = false;
448
+ }
449
+ } else {
450
+ esc = false;
451
+ }
452
+ parser_pos += 1;
453
+ if (parser_pos >= input_length) {
454
+ // incomplete string/rexp when end-of-file reached.
455
+ // bail out with what had been received so far.
456
+ return [resulting_string, 'TK_STRING'];
457
+ }
458
+ }
459
+
460
+ } else {
461
+ //
462
+ // and handle string also separately
463
+ //
464
+ while (esc || input.charAt(parser_pos) !== sep) {
465
+ resulting_string += input.charAt(parser_pos);
466
+ if (!esc) {
467
+ esc = input.charAt(parser_pos) === '\\';
468
+ } else {
469
+ esc = false;
470
+ }
471
+ parser_pos += 1;
472
+ if (parser_pos >= input_length) {
473
+ // incomplete string/rexp when end-of-file reached.
474
+ // bail out with what had been received so far.
475
+ return [resulting_string, 'TK_STRING'];
476
+ }
477
+ }
478
+ }
479
+
480
+
481
+
482
+ }
483
+
484
+ parser_pos += 1;
485
+
486
+ resulting_string += sep;
487
+
488
+ if (sep === '/') {
489
+ // regexps may have modifiers /regexp/MOD , so fetch those, too
490
+ while (parser_pos < input_length && in_array(input.charAt(parser_pos), wordchar)) {
491
+ resulting_string += input.charAt(parser_pos);
492
+ parser_pos += 1;
493
+ }
494
+ }
495
+ return [resulting_string, 'TK_STRING'];
496
+ }
497
+
498
+ if (c === '#') {
499
+ // Spidermonkey-specific sharp variables for circular references
500
+ // https://developer.mozilla.org/En/Sharp_variables_in_JavaScript
501
+ // http://mxr.mozilla.org/mozilla-central/source/js/src/jsscan.cpp around line 1935
502
+ var sharp = '#';
503
+ if (parser_pos < input_length && in_array(input.charAt(parser_pos), digits)) {
504
+ do {
505
+ c = input.charAt(parser_pos);
506
+ sharp += c;
507
+ parser_pos += 1;
508
+ } while (parser_pos < input_length && c !== '#' && c !== '=');
509
+ if (c === '#') {
510
+ //
511
+ } else if (input.charAt(parser_pos) === '[' && input.charAt(parser_pos + 1) === ']') {
512
+ sharp += '[]';
513
+ parser_pos += 2;
514
+ } else if (input.charAt(parser_pos) === '{' && input.charAt(parser_pos + 1) === '}') {
515
+ sharp += '{}';
516
+ parser_pos += 2;
517
+ }
518
+ return [sharp, 'TK_WORD'];
519
+ }
520
+ }
521
+
522
+ if (c === '<' && input.substring(parser_pos - 1, parser_pos + 3) === '<!--') {
523
+ parser_pos += 3;
524
+ flags.in_html_comment = true;
525
+ return ['<!--', 'TK_COMMENT'];
526
+ }
527
+
528
+ if (c === '-' && flags.in_html_comment && input.substring(parser_pos - 1, parser_pos + 2) === '-->') {
529
+ flags.in_html_comment = false;
530
+ parser_pos += 2;
531
+ if (wanted_newline) {
532
+ print_newline();
533
+ }
534
+ return ['-->', 'TK_COMMENT'];
535
+ }
536
+
537
+ if (in_array(c, punct)) {
538
+ while (parser_pos < input_length && in_array(c + input.charAt(parser_pos), punct)) {
539
+ c += input.charAt(parser_pos);
540
+ parser_pos += 1;
541
+ if (parser_pos >= input_length) {
542
+ break;
543
+ }
544
+ }
545
+
546
+ if (c === '=') {
547
+ return [c, 'TK_EQUALS'];
548
+ } else {
549
+ return [c, 'TK_OPERATOR'];
550
+ }
551
+ }
552
+
553
+ return [c, 'TK_UNKNOWN'];
554
+ }
555
+
556
+ //----------------------------------
557
+ indent_string = '';
558
+ while (opt_indent_size > 0) {
559
+ indent_string += opt_indent_char;
560
+ opt_indent_size -= 1;
561
+ }
562
+
563
+ input = js_source_text;
564
+
565
+ last_word = ''; // last 'TK_WORD' passed
566
+ last_type = 'TK_START_EXPR'; // last token type
567
+ last_text = ''; // last token text
568
+ last_last_text = ''; // pre-last token text
569
+ output = [];
570
+
571
+ do_block_just_closed = false;
572
+
573
+ whitespace = "\n\r\t ".split('');
574
+ wordchar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$'.split('');
575
+ digits = '0123456789'.split('');
576
+
577
+ punct = '+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::'.split(' ');
578
+
579
+ // words which should always start on new line.
580
+ line_starters = 'continue,try,throw,return,var,if,switch,case,default,for,while,break,function'.split(',');
581
+
582
+ // states showing if we are currently in expression (i.e. "if" case) - 'EXPRESSION', or in usual block (like, procedure), 'BLOCK'.
583
+ // some formatting depends on that.
584
+ flag_store = [];
585
+ set_mode('BLOCK');
586
+
587
+ parser_pos = 0;
588
+ while (true) {
589
+ var t = get_next_token(parser_pos);
590
+ token_text = t[0];
591
+ token_type = t[1];
592
+ if (token_type === 'TK_EOF') {
593
+ break;
594
+ }
595
+
596
+ switch (token_type) {
597
+
598
+ case 'TK_START_EXPR':
599
+
600
+ if (token_text === '[') {
601
+
602
+ if (last_type === 'TK_WORD' || last_text === ')') {
603
+ // this is array index specifier, break immediately
604
+ // a[x], fn()[x]
605
+ if (in_array(last_text, line_starters)) {
606
+ print_single_space();
607
+ }
608
+ set_mode('(EXPRESSION)');
609
+ print_token();
610
+ break;
611
+ }
612
+
613
+ if (flags.mode === '[EXPRESSION]' || flags.mode === '[INDENTED-EXPRESSION]') {
614
+ if (last_last_text === ']' && last_text === ',') {
615
+ // ], [ goes to new line
616
+ if (flags.mode === '[EXPRESSION]') {
617
+ flags.mode = '[INDENTED-EXPRESSION]';
618
+ if (!opt_keep_array_indentation) {
619
+ indent();
620
+ }
621
+ }
622
+ set_mode('[EXPRESSION]');
623
+ if (!opt_keep_array_indentation) {
624
+ print_newline();
625
+ }
626
+ } else if (last_text === '[') {
627
+ if (flags.mode === '[EXPRESSION]') {
628
+ flags.mode = '[INDENTED-EXPRESSION]';
629
+ if (!opt_keep_array_indentation) {
630
+ indent();
631
+ }
632
+ }
633
+ set_mode('[EXPRESSION]');
634
+
635
+ if (!opt_keep_array_indentation) {
636
+ print_newline();
637
+ }
638
+ } else {
639
+ set_mode('[EXPRESSION]');
640
+ }
641
+ } else {
642
+ set_mode('[EXPRESSION]');
643
+ }
644
+
645
+
646
+
647
+ } else {
648
+ set_mode('(EXPRESSION)');
649
+ }
650
+
651
+ if (last_text === ';' || last_type === 'TK_START_BLOCK') {
652
+ print_newline();
653
+ } else if (last_type === 'TK_END_EXPR' || last_type === 'TK_START_EXPR' || last_type === 'TK_END_BLOCK' || last_text === '.') {
654
+ // do nothing on (( and )( and ][ and ]( and .(
655
+ } else if (last_type !== 'TK_WORD' && last_type !== 'TK_OPERATOR') {
656
+ print_single_space();
657
+ } else if (last_word === 'function') {
658
+ // function() vs function ()
659
+ if (opt_space_after_anon_function) {
660
+ print_single_space();
661
+ }
662
+ } else if (in_array(last_text, line_starters) || last_text === 'catch') {
663
+ print_single_space();
664
+ }
665
+ print_token();
666
+
667
+ break;
668
+
669
+ case 'TK_END_EXPR':
670
+ if (token_text === ']') {
671
+ if (opt_keep_array_indentation) {
672
+ if (last_text === '}') {
673
+ // trim_output();
674
+ // print_newline(true);
675
+ remove_indent();
676
+ print_token();
677
+ restore_mode();
678
+ break;
679
+ }
680
+ } else {
681
+ if (flags.mode === '[INDENTED-EXPRESSION]') {
682
+ if (last_text === ']') {
683
+ restore_mode();
684
+ print_newline();
685
+ print_token();
686
+ break;
687
+ }
688
+ }
689
+ }
690
+ }
691
+ restore_mode();
692
+ print_token();
693
+ break;
694
+
695
+ case 'TK_START_BLOCK':
696
+
697
+ if (last_word === 'do') {
698
+ set_mode('DO_BLOCK');
699
+ } else {
700
+ set_mode('BLOCK');
701
+ }
702
+ if (opt_braces_on_own_line) {
703
+ if (last_type !== 'TK_OPERATOR') {
704
+ print_newline(true);
705
+ }
706
+ print_token();
707
+ indent();
708
+ } else {
709
+ if (last_type !== 'TK_OPERATOR' && last_type !== 'TK_START_EXPR') {
710
+ if (last_type === 'TK_START_BLOCK') {
711
+ print_newline();
712
+ } else {
713
+ print_single_space();
714
+ }
715
+ }
716
+ indent();
717
+ print_token();
718
+ }
719
+
720
+ break;
721
+
722
+ case 'TK_END_BLOCK':
723
+ restore_mode();
724
+ if (opt_braces_on_own_line) {
725
+ print_newline();
726
+ print_token();
727
+ } else {
728
+ if (last_type === 'TK_START_BLOCK') {
729
+ // nothing
730
+ if (just_added_newline) {
731
+ remove_indent();
732
+ } else {
733
+ // {}
734
+ trim_output();
735
+ }
736
+ } else {
737
+ print_newline();
738
+ }
739
+ print_token();
740
+ }
741
+ break;
742
+
743
+ case 'TK_WORD':
744
+
745
+ // no, it's not you. even I have problems understanding how this works
746
+ // and what does what.
747
+ if (do_block_just_closed) {
748
+ // do {} ## while ()
749
+ print_single_space();
750
+ print_token();
751
+ print_single_space();
752
+ do_block_just_closed = false;
753
+ break;
754
+ }
755
+
756
+ if (token_text === 'function') {
757
+ if ((just_added_newline || last_text === ';') && last_text !== '{') {
758
+ // make sure there is a nice clean space of at least one blank line
759
+ // before a new function definition
760
+ n_newlines = just_added_newline ? n_newlines : 0;
761
+
762
+ for (var i = 0; i < 2 - n_newlines; i++) {
763
+ print_newline(false);
764
+ }
765
+
766
+ }
767
+ }
768
+ if (token_text === 'case' || token_text === 'default') {
769
+ if (last_text === ':') {
770
+ // switch cases following one another
771
+ remove_indent();
772
+ } else {
773
+ // case statement starts in the same line where switch
774
+ flags.indentation_level--;
775
+ print_newline();
776
+ flags.indentation_level++;
777
+ }
778
+ print_token();
779
+ flags.in_case = true;
780
+ break;
781
+ }
782
+
783
+ prefix = 'NONE';
784
+
785
+ if (last_type === 'TK_END_BLOCK') {
786
+ if (!in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
787
+ prefix = 'NEWLINE';
788
+ } else {
789
+ if (opt_braces_on_own_line) {
790
+ prefix = 'NEWLINE';
791
+ } else {
792
+ prefix = 'SPACE';
793
+ print_single_space();
794
+ }
795
+ }
796
+ } else if (last_type === 'TK_SEMICOLON' && (flags.mode === 'BLOCK' || flags.mode === 'DO_BLOCK')) {
797
+ prefix = 'NEWLINE';
798
+ } else if (last_type === 'TK_SEMICOLON' && is_expression(flags.mode)) {
799
+ prefix = 'SPACE';
800
+ } else if (last_type === 'TK_STRING') {
801
+ prefix = 'NEWLINE';
802
+ } else if (last_type === 'TK_WORD') {
803
+ prefix = 'SPACE';
804
+ } else if (last_type === 'TK_START_BLOCK') {
805
+ prefix = 'NEWLINE';
806
+ } else if (last_type === 'TK_END_EXPR') {
807
+ print_single_space();
808
+ prefix = 'NEWLINE';
809
+ }
810
+
811
+ if (last_type !== 'TK_END_BLOCK' && in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
812
+ print_newline();
813
+ } else if (in_array(token_text, line_starters) || prefix === 'NEWLINE') {
814
+ if (last_text === 'else') {
815
+ // no need to force newline on else break
816
+ print_single_space();
817
+ } else if ((last_type === 'TK_START_EXPR' || last_text === '=' || last_text === ',') && token_text === 'function') {
818
+ // no need to force newline on 'function': (function
819
+ // DONOTHING
820
+ } else if (last_text === 'return' || last_text === 'throw') {
821
+ // no newline between 'return nnn'
822
+ print_single_space();
823
+ } else if (last_type !== 'TK_END_EXPR') {
824
+ if ((last_type !== 'TK_START_EXPR' || token_text !== 'var') && last_text !== ':') {
825
+ // no need to force newline on 'var': for (var x = 0...)
826
+ if (token_text === 'if' && last_word === 'else' && last_text !== '{') {
827
+ // no newline for } else if {
828
+ print_single_space();
829
+ } else {
830
+ print_newline();
831
+ }
832
+ }
833
+ } else {
834
+ if (in_array(token_text, line_starters) && last_text !== ')') {
835
+ print_newline();
836
+ }
837
+ }
838
+ } else if (prefix === 'SPACE') {
839
+ print_single_space();
840
+ }
841
+ print_token();
842
+ last_word = token_text;
843
+
844
+ if (token_text === 'var') {
845
+ flags.var_line = true;
846
+ flags.var_line_reindented = false;
847
+ flags.var_line_tainted = false;
848
+ }
849
+
850
+ if (token_text === 'if' || token_text === 'else') {
851
+ flags.if_line = true;
852
+ }
853
+
854
+ break;
855
+
856
+ case 'TK_SEMICOLON':
857
+
858
+ print_token();
859
+ flags.var_line = false;
860
+ flags.var_line_reindented = false;
861
+ break;
862
+
863
+ case 'TK_STRING':
864
+
865
+ if (last_type === 'TK_START_BLOCK' || last_type === 'TK_END_BLOCK' || last_type === 'TK_SEMICOLON') {
866
+ print_newline();
867
+ } else if (last_type === 'TK_WORD') {
868
+ print_single_space();
869
+ }
870
+ print_token();
871
+ break;
872
+
873
+ case 'TK_EQUALS':
874
+ if (flags.var_line) {
875
+ // just got an '=' in a var-line, different formatting/line-breaking, etc will now be done
876
+ flags.var_line_tainted = true;
877
+ }
878
+ print_single_space();
879
+ print_token();
880
+ print_single_space();
881
+ break;
882
+
883
+ case 'TK_OPERATOR':
884
+
885
+ var space_before = true;
886
+ var space_after = true;
887
+
888
+ if (flags.var_line && token_text === ',' && (is_expression(flags.mode))) {
889
+ // do not break on comma, for(var a = 1, b = 2)
890
+ flags.var_line_tainted = false;
891
+ }
892
+
893
+ if (flags.var_line) {
894
+ if (token_text === ',') {
895
+ if (flags.var_line_tainted) {
896
+ print_token();
897
+ flags.var_line_reindented = true;
898
+ flags.var_line_tainted = false;
899
+ print_newline();
900
+ break;
901
+ } else {
902
+ flags.var_line_tainted = false;
903
+ }
904
+ // } else if (token_text === ':') {
905
+ // hmm, when does this happen? tests don't catch this
906
+ // flags.var_line = false;
907
+ }
908
+ }
909
+
910
+ if (last_text === 'return' || last_text === 'throw') {
911
+ // "return" had a special handling in TK_WORD. Now we need to return the favor
912
+ print_single_space();
913
+ print_token();
914
+ break;
915
+ }
916
+
917
+ if (token_text === ':' && flags.in_case) {
918
+ print_token(); // colon really asks for separate treatment
919
+ print_newline();
920
+ flags.in_case = false;
921
+ break;
922
+ }
923
+
924
+ if (token_text === '::') {
925
+ // no spaces around exotic namespacing syntax operator
926
+ print_token();
927
+ break;
928
+ }
929
+
930
+ if (token_text === ',') {
931
+ if (flags.var_line) {
932
+ if (flags.var_line_tainted) {
933
+ print_token();
934
+ print_newline();
935
+ flags.var_line_tainted = false;
936
+ } else {
937
+ print_token();
938
+ print_single_space();
939
+ }
940
+ } else if (last_type === 'TK_END_BLOCK' && flags.mode !== "(EXPRESSION)") {
941
+ print_token();
942
+ print_newline();
943
+ } else {
944
+ if (flags.mode === 'BLOCK') {
945
+ print_token();
946
+ print_newline();
947
+ } else {
948
+ // EXPR or DO_BLOCK
949
+ print_token();
950
+ print_single_space();
951
+ }
952
+ }
953
+ break;
954
+ // } else if (in_array(token_text, ['--', '++', '!']) || (in_array(token_text, ['-', '+']) && (in_array(last_type, ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS']) || in_array(last_text, line_starters) || in_array(last_text, ['==', '!=', '+=', '-=', '*=', '/=', '+', '-'])))) {
955
+ } else if (in_array(token_text, ['--', '++', '!']) || (in_array(token_text, ['-', '+']) && (in_array(last_type, ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR']) || in_array(last_text, line_starters)))) {
956
+ // unary operators (and binary +/- pretending to be unary) special cases
957
+
958
+ space_before = false;
959
+ space_after = false;
960
+
961
+ if (last_text === ';' && is_expression(flags.mode)) {
962
+ // for (;; ++i)
963
+ // ^^^
964
+ space_before = true;
965
+ }
966
+ if (last_type === 'TK_WORD' && in_array(last_text, line_starters)) {
967
+ space_before = true;
968
+ }
969
+
970
+ if (flags.mode === 'BLOCK' && (last_text === '{' || last_text === ';')) {
971
+ // { foo; --i }
972
+ // foo(); --bar;
973
+ print_newline();
974
+ }
975
+ } else if (token_text === '.') {
976
+ // decimal digits or object.property
977
+ space_before = false;
978
+
979
+ } else if (token_text === ':') {
980
+ if (!is_ternary_op()) {
981
+ space_before = false;
982
+ }
983
+ }
984
+ if (space_before) {
985
+ print_single_space();
986
+ }
987
+
988
+ print_token();
989
+
990
+ if (space_after) {
991
+ print_single_space();
992
+ }
993
+
994
+ if (token_text === '!') {
995
+ // flags.eat_next_space = true;
996
+ }
997
+
998
+ break;
999
+
1000
+ case 'TK_BLOCK_COMMENT':
1001
+
1002
+ var lines = token_text.split(/\x0a|\x0d\x0a/);
1003
+
1004
+ if (/^\/\*\*/.test(token_text)) {
1005
+ // javadoc: reformat and reindent
1006
+ print_newline();
1007
+ output.push(lines[0]);
1008
+ for (i = 1; i < lines.length; i++) {
1009
+ print_newline();
1010
+ output.push(' ');
1011
+ output.push(lines[i].replace(/^\s\s*|\s\s*$/, ''));
1012
+ }
1013
+
1014
+ } else {
1015
+ // simple block comment: leave intact
1016
+ if (lines.length > 1) {
1017
+ // multiline comment block starts with a new line
1018
+ print_newline();
1019
+ trim_output();
1020
+ } else {
1021
+ // single-line /* comment */ stays where it is
1022
+ print_single_space();
1023
+ }
1024
+ for (i = 0; i < lines.length; i++) {
1025
+ output.push(lines[i]);
1026
+ output.push('\n');
1027
+ }
1028
+
1029
+ }
1030
+ print_newline();
1031
+ break;
1032
+
1033
+ case 'TK_INLINE_COMMENT':
1034
+
1035
+ print_single_space();
1036
+ print_token();
1037
+ if (is_expression(flags.mode)) {
1038
+ print_single_space();
1039
+ } else {
1040
+ print_newline();
1041
+ }
1042
+ break;
1043
+
1044
+ case 'TK_COMMENT':
1045
+
1046
+ // print_newline();
1047
+ if (wanted_newline) {
1048
+ print_newline();
1049
+ } else {
1050
+ print_single_space();
1051
+ }
1052
+ print_token();
1053
+ print_newline();
1054
+ break;
1055
+
1056
+ case 'TK_UNKNOWN':
1057
+ print_token();
1058
+ break;
1059
+ }
1060
+
1061
+ last_last_text = last_text;
1062
+ last_type = token_type;
1063
+ last_text = token_text;
1064
+ }
1065
+
1066
+ return output.join('').replace(/[\n ]+$/, '');
1067
+
1068
+ }
1069
+
1070
+ var toString = String.prototype.toString;
1071
+
1072
+ // Set up tidy method on strings and functions
1073
+ // @returns beautified string representation
1074
+ var tidy =
1075
+ Function.prototype.tidy =
1076
+ String.prototype.tidy =
1077
+ function(){ return js_beautify( toString.call(this) ) };
1078
+
1079
+ if (enabled)
1080
+ {
1081
+ opera.addEventListener("BeforeScript", function(ev) {
1082
+ ev.element.text = tidy.call(ev.element.text);
1083
+ }, false);
1084
+ }
1085
+
1086
+ })();
1087
+