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,42 @@
1
+ #!/bin/sh
2
+
3
+ DESTINATION=beautifier.js
4
+
5
+ echo '// ==UserScript==
6
+ // @name Scripts beautifier for Opera
7
+ // @author Rafal Chlodnicki
8
+ // @author Einar Lielmanis
9
+ // @version 1.1
10
+ // @include *
11
+ // ==/UserScript==
12
+
13
+ (function(){
14
+
15
+ // Enabling this setting will beautify all scripts on page
16
+ var enabled = /*@Beautify all scripts@bool@*/false/*@*/;
17
+
18
+ /* Specially formated comment above is for use with Opera Unite UJS Manager
19
+ https://unite.opera.com/application/401/ */
20
+
21
+ ' > $DESTINATION
22
+ cat ../beautify.js >> $DESTINATION
23
+
24
+ echo '
25
+ var toString = String.prototype.toString;
26
+
27
+ // Set up tidy method on strings and functions
28
+ // @returns beautified string representation
29
+ var tidy =
30
+ Function.prototype.tidy =
31
+ String.prototype.tidy =
32
+ function(){ return js_beautify( toString.call(this) ) };
33
+
34
+ if (enabled)
35
+ {
36
+ opera.addEventListener("BeforeScript", function(ev) {
37
+ ev.element.text = tidy.call(ev.element.text);
38
+ }, false);
39
+ }
40
+
41
+ })();
42
+ ' >> $DESTINATION
@@ -0,0 +1,121 @@
1
+ /*
2
+ Copyright (c) 2010 Ariya Hidayat <ariya.hidayat@gmail.com>
3
+ Copyright (c) 2009 Einar Lielmanis
4
+ Copyright (c) 2010 Nicolas Ferrero <ferrero.nicolas@gmail.com>
5
+
6
+ Permission is hereby granted, free of charge, to any person
7
+ obtaining a copy of this software and associated documentation
8
+ files (the "Software"), to deal in the Software without
9
+ restriction, including without limitation the rights to use,
10
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the
12
+ Software is furnished to do so, subject to the following
13
+ conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
+ OTHER DEALINGS IN THE SOFTWARE.
26
+ */
27
+
28
+ #include <QtScript>
29
+
30
+ #include <iostream>
31
+
32
+ static QString readFile(const QString &fileName)
33
+ {
34
+ QFile file;
35
+ file.setFileName(fileName);
36
+ if (!file.open(QFile::ReadOnly)) {
37
+ return QString();
38
+ }
39
+ QString content = file.readAll();
40
+ file.close();
41
+ return content;
42
+ }
43
+
44
+ static void usage(char *progname)
45
+ {
46
+ std::cerr << "Usage: " << progname << " [options] source-file\n";
47
+ std::cerr << "[options]\n";
48
+ std::cerr << " --indent-size or -s: Indentation size. (default 4)\n";
49
+ std::cerr << " --indent-char or -c: Character to indent with. (default space)\n";
50
+ std::cerr << " --disable-preserve-newlines or -d: Do not preserve existing line breaks.\n";
51
+ std::cerr << " --indent-level or -l: Initial indentation level, you probably won't need this ever. (default 0)\n";
52
+ std::cerr << " --space-after-anon-function or -f: Space is added between \"function ()\", otherwise the common \"function()\" output is used.\n";
53
+ std::cerr << " --braces-on-own-line or -b: ANSI / Allman brace style, each opening/closing brace gets its own line.\n";
54
+ std::cerr << " --keep-array-indentation or -k: Keep array indentation.\n";
55
+ std::cerr << " --help or -h: Prints this help statement.\n";
56
+ }
57
+
58
+ int main(int argc, char **argv)
59
+ {
60
+ QCoreApplication app(argc, argv);
61
+ QScriptEngine engine;
62
+ QScriptValue options = engine.newObject();
63
+ QString source;
64
+
65
+ for (int argpos = 1; argpos < app.argc(); ++argpos) {
66
+ if (app.argv()[argpos][0] != '-') {
67
+ source = readFile(QString::fromLocal8Bit(app.argv()[argpos]));
68
+
69
+ } else if (strcmp(app.argv()[argpos], "--indent-size") == 0 ||
70
+ strcmp(app.argv()[argpos], "-s") == 0) {
71
+ options.setProperty("indent_size", app.argv()[argpos+1]);
72
+
73
+ } else if (strcmp(app.argv()[argpos], "--indent-char") == 0 ||
74
+ strcmp(app.argv()[argpos], "-c") == 0) {
75
+ options.setProperty("indent_char", app.argv()[argpos+1]);
76
+
77
+ } else if (strcmp(app.argv()[argpos], "--disable-preserve-newlines") == 0 ||
78
+ strcmp(app.argv()[argpos], "-d") == 0) {
79
+ options.setProperty("preserve_newlines", false);
80
+
81
+ } else if (strcmp(app.argv()[argpos], "--indent-level") == 0 ||
82
+ strcmp(app.argv()[argpos], "-l") == 0) {
83
+ options.setProperty("indent_level", app.argv()[argpos+1]);
84
+
85
+ } else if (strcmp(app.argv()[argpos], "--space-after-anon-function") == 0 ||
86
+ strcmp(app.argv()[argpos], "-f") == 0) {
87
+ options.setProperty("space_after_anon_function", true);
88
+
89
+ } else if (strcmp(app.argv()[argpos], "--braces-on-own-line") == 0 ||
90
+ strcmp(app.argv()[argpos], "-b") == 0) {
91
+ options.setProperty("braces_on_own_line", true);
92
+
93
+ } else if (strcmp(app.argv()[argpos], "--keep-array-indentation") == 0 ||
94
+ strcmp(app.argv()[argpos], "-k") == 0) {
95
+ options.setProperty("keep_array_indentation", true);
96
+
97
+ } else if (strcmp(app.argv()[argpos], "--help") == 0 ||
98
+ strcmp(app.argv()[argpos], "-h") == 0) {
99
+ usage(app.argv()[0]);
100
+ return -1;
101
+ }
102
+ }
103
+
104
+ if (source.isEmpty()) {
105
+ usage(app.argv()[0]);
106
+ return -1;
107
+ }
108
+
109
+ QString script = readFile(":/beautify.js");
110
+
111
+ if (!script.isEmpty()) {
112
+ engine.evaluate(script);
113
+ engine.globalObject().setProperty("source", QScriptValue(source));
114
+ engine.globalObject().setProperty("options", options);
115
+ QScriptValue result = engine.evaluate("js_beautify(source, options);");
116
+ std::cout << qPrintable(result.toString()) << std::endl;
117
+ }
118
+
119
+ return 0;
120
+ }
121
+
@@ -0,0 +1,5 @@
1
+ TARGET = jsbeautify
2
+ TEMPLATE = app
3
+ SOURCES = jsbeautify.cpp
4
+ RESOURCES = jsbeautify.qrc
5
+ QT += script
@@ -0,0 +1,6 @@
1
+ <RCC>
2
+ <qresource prefix="/">
3
+ <file>../beautify.js</file>
4
+ <alias>beautify.js</alias>
5
+ </qresource>
6
+ </RCC>
@@ -0,0 +1,28 @@
1
+ A small qtscript project to invoke jsbeautify.js from the command line.
2
+
3
+ An alternative to the rhino version.
4
+
5
+ Requirement
6
+ -----------
7
+
8
+ Qt 4.6 is later is needed to build.
9
+
10
+ For OpenSUSE, install it using 'sudo zypper in libqt4-devel'.
11
+ For Ubuntu/Debian, install it using 'sudo apt-get install libqt4-dev'.
12
+
13
+ For other distributions and/or operating systems, download the binary for free
14
+ from http://qt.nokia.com/downloads.
15
+
16
+ Build
17
+ -----
18
+
19
+ qmake && make
20
+
21
+ (On some Ubuntu/Debian, change qmake to qmake-qt4).
22
+
23
+ Use
24
+ ---
25
+
26
+ jsbeautify source-file
27
+
28
+ The content of source-file will be read, beautified, and printed to the console.
@@ -0,0 +1,2 @@
1
+ Here lay the scripts that were useful at a time, now they are kind of
2
+ obsoleted by the python script, but you may still find them useful.
@@ -0,0 +1,522 @@
1
+ /* 20090220, as
2
+
3
+ the javascript functions are from:
4
+
5
+ JS Beautifier
6
+ ---------------
7
+
8
+
9
+ Written by Einar Lielmanis, <einars@gmail.com>
10
+ http://jsbeautifier.org/
11
+
12
+ Originally converted to javascript by Vital, <vital76@gmail.com>
13
+
14
+ You are free to use this in any way you want, in case you find this useful or working for you.
15
+
16
+ Usage:
17
+ js_beautify(js_source_text);
18
+ js_beautify(js_source_text, options);
19
+
20
+ The options are:
21
+ indent_size (default 4) — indentation size,
22
+ indent_char (default space) — character to indent with,
23
+ preserve_newlines (default true) — whether existing line breaks should be preserved,
24
+ indent_level (default 0) — initial indentation level, you probably won't need this ever,
25
+
26
+ e.g
27
+
28
+ js_beautify(js_source_text, {indent_size: 1, indent_char: '\t'});
29
+
30
+
31
+ */
32
+
33
+
34
+ var sourceWindow, sourceText, beautifiedText, SelectionFlag
35
+ BBEdit = MacOS.appBySignature("R*ch");
36
+ BBEdit._strict = false;
37
+
38
+ try {
39
+ sourceWindow = BBEdit.window[1];
40
+ sourceText = sourceWindow.selection.contents;
41
+ SelectionFlag = true;
42
+ if (sourceText == "") {
43
+ sourceText = sourceWindow.text;
44
+ SelectionFlag = false;
45
+ }
46
+ if (sourceText != "") {
47
+ beautifiedText = js_beautify(sourceText, {indent_size: 4, indent_char: ' ', indent_level: 0});
48
+ if (SelectionFlag) {
49
+ sourceWindow.selection.contents = beautifiedText;
50
+ } else {
51
+ sourceWindow.text = beautifiedText;
52
+ }
53
+ }
54
+ } catch(what) {
55
+ Core.message(what.toString())
56
+ }
57
+
58
+ function js_beautify(js_source_text, options) {
59
+ var input, output, token_text, last_type, last_text, last_word, current_mode, modes, indent_string;
60
+ var whitespace, wordchar, punct, parser_pos, line_starters, in_case;
61
+ var prefix, token_type, do_block_just_closed, var_line, var_line_tainted, if_line_flag;
62
+ var indent_level;
63
+ var options = options || {};
64
+ var opt_indent_size = options['indent_size'] || 4;
65
+ var opt_indent_char = options['indent_char'] || ' ';
66
+ var opt_preserve_newlines = typeof options['preserve_newlines'] === 'undefined' ? true: options['preserve_newlines'];
67
+ var opt_indent_level = options['indent_level'] || 0; // starting indentation
68
+ function trim_output() {
69
+ while (output.length && (output[output.length - 1] === ' ' || output[output.length - 1] === indent_string)) {
70
+ output.pop();
71
+ }
72
+ }
73
+ function print_newline(ignore_repeated) {
74
+ ignore_repeated = typeof ignore_repeated === 'undefined' ? true: ignore_repeated;
75
+ if_line_flag = false;
76
+ trim_output();
77
+ if (!output.length) {
78
+ return; // no newline on start of file
79
+ }
80
+ if (output[output.length - 1] !== "\n" || !ignore_repeated) {
81
+ output.push("\n");
82
+ }
83
+ for (var i = 0; i < indent_level; i++) {
84
+ output.push(indent_string);
85
+ }
86
+ }
87
+ function print_space() {
88
+ var last_output = output.length ? output[output.length - 1] : ' ';
89
+ if (last_output !== ' ' && last_output !== '\n' && last_output !== indent_string) { // prevent occassional duplicate space
90
+ output.push(' ');
91
+ }
92
+ }
93
+ function print_token() {
94
+ output.push(token_text);
95
+ }
96
+ function indent() {
97
+ indent_level++;
98
+ }
99
+ function unindent() {
100
+ if (indent_level) {
101
+ indent_level--;
102
+ }
103
+ }
104
+ function remove_indent() {
105
+ if (output.length && output[output.length - 1] === indent_string) {
106
+ output.pop();
107
+ }
108
+ }
109
+ function set_mode(mode) {
110
+ modes.push(current_mode);
111
+ current_mode = mode;
112
+ }
113
+ function restore_mode() {
114
+ do_block_just_closed = current_mode === 'DO_BLOCK';
115
+ current_mode = modes.pop();
116
+ }
117
+ function in_array(what, arr) {
118
+ for (var i = 0; i < arr.length; i++) {
119
+ if (arr[i] === what) {
120
+ return true;
121
+ }
122
+ }
123
+ return false;
124
+ }
125
+ function get_next_token() {
126
+ var n_newlines = 0;
127
+ var c = '';
128
+ do {
129
+ if (parser_pos >= input.length) {
130
+ return ['', 'TK_EOF'];
131
+ }
132
+ c = input.charAt(parser_pos);
133
+ parser_pos += 1;
134
+ if (c === "\n") {
135
+ n_newlines += 1;
136
+ }
137
+ } while ( in_array ( c , whitespace ));
138
+ var wanted_newline = false;
139
+ if (opt_preserve_newlines) {
140
+ if (n_newlines > 1) {
141
+ for (var i = 0; i < 2; i++) {
142
+ print_newline(i === 0);
143
+ }
144
+ }
145
+ wanted_newline = (n_newlines === 1);
146
+ }
147
+ if (in_array(c, wordchar)) {
148
+ if (parser_pos < input.length) {
149
+ while (in_array(input.charAt(parser_pos), wordchar)) {
150
+ c += input.charAt(parser_pos);
151
+ parser_pos += 1;
152
+ if (parser_pos === input.length) {
153
+ break;
154
+ }
155
+ }
156
+ } // small and surprisingly unugly hack for 1E-10 representation
157
+ if (parser_pos !== input.length && c.match(/^[0-9]+[Ee]$/) && input.charAt(parser_pos) === '-') {
158
+ parser_pos += 1;
159
+ var t = get_next_token(parser_pos);
160
+ c += '-' + t[0];
161
+ return [c, 'TK_WORD'];
162
+ }
163
+ if (c === 'in') { // hack for 'in' operator
164
+ return [c, 'TK_OPERATOR'];
165
+ }
166
+ if (wanted_newline && last_type !== 'TK_OPERATOR' && !if_line_flag) {
167
+ print_newline();
168
+ }
169
+ return [c, 'TK_WORD'];
170
+ }
171
+ if (c === '(' || c === '[') {
172
+ return [c, 'TK_START_EXPR'];
173
+ }
174
+ if (c === ')' || c === ']') {
175
+ return [c, 'TK_END_EXPR'];
176
+ }
177
+ if (c === '{') {
178
+ return [c, 'TK_START_BLOCK'];
179
+ }
180
+ if (c === '}') {
181
+ return [c, 'TK_END_BLOCK'];
182
+ }
183
+ if (c === ';') {
184
+ return [c, 'TK_SEMICOLON'];
185
+ }
186
+ if (c === '/') {
187
+ var comment = ''; // peek for comment /* ... */
188
+ if (input.charAt(parser_pos) === '*') {
189
+ parser_pos += 1;
190
+ if (parser_pos < input.length) {
191
+ while (! (input.charAt(parser_pos) === '*' && input.charAt(parser_pos + 1) && input.charAt(parser_pos + 1) === '/') && parser_pos < input.length) {
192
+ comment += input.charAt(parser_pos);
193
+ parser_pos += 1;
194
+ if (parser_pos >= input.length) {
195
+ break;
196
+ }
197
+ }
198
+ }
199
+ parser_pos += 2;
200
+ return ['/*' + comment + '*/', 'TK_BLOCK_COMMENT'];
201
+ } // peek for comment // ...
202
+ if (input.charAt(parser_pos) === '/') {
203
+ comment = c;
204
+ while (input.charAt(parser_pos) !== "\x0d" && input.charAt(parser_pos) !== "\x0a") {
205
+ comment += input.charAt(parser_pos);
206
+ parser_pos += 1;
207
+ if (parser_pos >= input.length) {
208
+ break;
209
+ }
210
+ }
211
+ parser_pos += 1;
212
+ if (wanted_newline) {
213
+ print_newline();
214
+ }
215
+ return [comment, 'TK_COMMENT'];
216
+ }
217
+ }
218
+ if (c === "'" || // string
219
+ c === '"' || // string
220
+ (c === '/' && ((last_type === 'TK_WORD' && last_text === 'return') || (last_type === 'TK_START_EXPR' || last_type === 'TK_END_BLOCK' || last_type === 'TK_OPERATOR' || last_type === 'TK_EOF' || last_type === 'TK_SEMICOLON')))) { // regexp
221
+ var sep = c;
222
+ var esc = false;
223
+ var resulting_string = '';
224
+ if (parser_pos < input.length) {
225
+ while (esc || input.charAt(parser_pos) !== sep) {
226
+ resulting_string += input.charAt(parser_pos);
227
+ if (!esc) {
228
+ esc = input.charAt(parser_pos) === '\\';
229
+ } else {
230
+ esc = false;
231
+ }
232
+ parser_pos += 1;
233
+ if (parser_pos >= input.length) {
234
+ break;
235
+ }
236
+ }
237
+ }
238
+ parser_pos += 1;
239
+ resulting_string = sep + resulting_string + sep;
240
+ if (sep == '/') { // regexps may have modifiers /regexp/MOD , so fetch those, too
241
+ while (parser_pos < input.length && in_array(input.charAt(parser_pos), wordchar)) {
242
+ resulting_string += input.charAt(parser_pos);
243
+ parser_pos += 1;
244
+ }
245
+ }
246
+ return [resulting_string, 'TK_STRING'];
247
+ }
248
+ if (in_array(c, punct)) {
249
+ while (parser_pos < input.length && in_array(c + input.charAt(parser_pos), punct)) {
250
+ c += input.charAt(parser_pos);
251
+ parser_pos += 1;
252
+ if (parser_pos >= input.length) {
253
+ break;
254
+ }
255
+ }
256
+ return [c, 'TK_OPERATOR'];
257
+ }
258
+ return [c, 'TK_UNKNOWN'];
259
+ } //----------------------------------
260
+ indent_string = '';
261
+ while (opt_indent_size--) {
262
+ indent_string += opt_indent_char;
263
+ }
264
+ indent_level = opt_indent_level;
265
+ input = js_source_text;
266
+ last_word = ''; // last 'TK_WORD' passed
267
+ last_type = 'TK_START_EXPR'; // last token type
268
+ last_text = ''; // last token text
269
+ output = [];
270
+ do_block_just_closed = false;
271
+ var_line = false; // currently drawing var .... ;
272
+ var_line_tainted = false; // false: var a = 5; true: var a = 5, b = 6
273
+ whitespace = "\n\r\t ".split('');
274
+ wordchar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$'.split('');
275
+ punct = '+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::'.split(' '); // words which should always start on new line.
276
+ line_starters = 'continue,try,throw,return,var,if,switch,case,default,for,while,break,function'.split(','); // states showing if we are currently in expression (i.e. "if" case) - 'EXPRESSION', or in usual block (like, procedure), 'BLOCK'.
277
+ // some formatting depends on that.
278
+ current_mode = 'BLOCK';
279
+ modes = [current_mode];
280
+ parser_pos = 0;
281
+ in_case = false; // flag for parser that case/default has been processed, and next colon needs special attention
282
+ while (true) {
283
+ var t = get_next_token(parser_pos);
284
+ token_text = t[0];
285
+ token_type = t[1];
286
+ if (token_type === 'TK_EOF') {
287
+ break;
288
+ }
289
+ switch (token_type) {
290
+ case 'TK_START_EXPR':
291
+ var_line = false;
292
+ set_mode('EXPRESSION');
293
+ if (last_text === ';') {
294
+ print_newline();
295
+ } else if (last_type === 'TK_END_EXPR' || last_type === 'TK_START_EXPR') { // do nothing on (( and )( and ][ and ]( ..
296
+ } else if (last_type !== 'TK_WORD' && last_type !== 'TK_OPERATOR') {
297
+ print_space();
298
+ } else if (in_array(last_word, line_starters) && last_word !== 'function') {
299
+ print_space();
300
+ }
301
+ print_token();
302
+ break;
303
+ case 'TK_END_EXPR':
304
+ print_token();
305
+ restore_mode();
306
+ break;
307
+ case 'TK_START_BLOCK':
308
+ if (last_word === 'do') {
309
+ set_mode('DO_BLOCK');
310
+ } else {
311
+ set_mode('BLOCK');
312
+ }
313
+ if (last_type !== 'TK_OPERATOR' && last_type !== 'TK_START_EXPR') {
314
+ if (last_type === 'TK_START_BLOCK') {
315
+ print_newline();
316
+ } else {
317
+ print_space();
318
+ }
319
+ }
320
+ print_token();
321
+ indent();
322
+ break;
323
+ case 'TK_END_BLOCK':
324
+ if (last_type === 'TK_START_BLOCK') { // nothing
325
+ trim_output();
326
+ unindent();
327
+ } else {
328
+ unindent();
329
+ print_newline();
330
+ }
331
+ print_token();
332
+ restore_mode();
333
+ break;
334
+ case 'TK_WORD':
335
+ if (do_block_just_closed) {
336
+ print_space();
337
+ print_token();
338
+ print_space();
339
+ break;
340
+ }
341
+ if (token_text === 'case' || token_text === 'default') {
342
+ if (last_text === ':') { // switch cases following one another
343
+ remove_indent();
344
+ } else { // case statement starts in the same line where switch
345
+ unindent();
346
+ print_newline();
347
+ indent();
348
+ }
349
+ print_token();
350
+ in_case = true;
351
+ break;
352
+ }
353
+ prefix = 'NONE';
354
+ if (last_type === 'TK_END_BLOCK') {
355
+ if (!in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
356
+ prefix = 'NEWLINE';
357
+ } else {
358
+ prefix = 'SPACE';
359
+ print_space();
360
+ }
361
+ } else if (last_type === 'TK_SEMICOLON' && (current_mode === 'BLOCK' || current_mode === 'DO_BLOCK')) {
362
+ prefix = 'NEWLINE';
363
+ } else if (last_type === 'TK_SEMICOLON' && current_mode === 'EXPRESSION') {
364
+ prefix = 'SPACE';
365
+ } else if (last_type === 'TK_STRING') {
366
+ prefix = 'NEWLINE';
367
+ } else if (last_type === 'TK_WORD') {
368
+ prefix = 'SPACE';
369
+ } else if (last_type === 'TK_START_BLOCK') {
370
+ prefix = 'NEWLINE';
371
+ } else if (last_type === 'TK_END_EXPR') {
372
+ print_space();
373
+ prefix = 'NEWLINE';
374
+ }
375
+ if (last_type !== 'TK_END_BLOCK' && in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
376
+ print_newline();
377
+ } else if (in_array(token_text, line_starters) || prefix === 'NEWLINE') {
378
+ if (last_text === 'else') { // no need to force newline on else break
379
+ print_space();
380
+ } else if ((last_type === 'TK_START_EXPR' || last_text === '=') && token_text === 'function') { // no need to force newline on 'function': (function
381
+ // DONOTHING
382
+ } else if (last_type === 'TK_WORD' && (last_text === 'return' || last_text === 'throw')) { // no newline between 'return nnn'
383
+ print_space();
384
+ } else if (last_type !== 'TK_END_EXPR') {
385
+ if ((last_type !== 'TK_START_EXPR' || token_text !== 'var') && last_text !== ':') { // no need to force newline on 'var': for (var x = 0...)
386
+ if (token_text === 'if' && last_type === 'TK_WORD' && last_word === 'else') { // no newline for } else if {
387
+ print_space();
388
+ } else {
389
+ print_newline();
390
+ }
391
+ }
392
+ } else {
393
+ if (in_array(token_text, line_starters) && last_text !== ')') {
394
+ print_newline();
395
+ }
396
+ }
397
+ } else if (prefix === 'SPACE') {
398
+ print_space();
399
+ }
400
+ print_token();
401
+ last_word = token_text;
402
+ if (token_text === 'var') {
403
+ var_line = true;
404
+ var_line_tainted = false;
405
+ }
406
+ if (token_text === 'if' || token_text === 'else') {
407
+ if_line_flag = true;
408
+ }
409
+ break;
410
+ case 'TK_SEMICOLON':
411
+ print_token();
412
+ var_line = false;
413
+ break;
414
+ case 'TK_STRING':
415
+ if (last_type === 'TK_START_BLOCK' || last_type === 'TK_END_BLOCK' || last_type == 'TK_SEMICOLON') {
416
+ print_newline();
417
+ } else if (last_type === 'TK_WORD') {
418
+ print_space();
419
+ }
420
+ print_token();
421
+ break;
422
+ case 'TK_OPERATOR':
423
+ var start_delim = true;
424
+ var end_delim = true;
425
+ if (var_line && token_text !== ',') {
426
+ var_line_tainted = true;
427
+ if (token_text === ':') {
428
+ var_line = false;
429
+ }
430
+ }
431
+ if (var_line && token_text === ',' && current_mode === 'EXPRESSION') { // do not break on comma, for(var a = 1, b = 2)
432
+ var_line_tainted = false;
433
+ }
434
+ if (token_text === ':' && in_case) {
435
+ print_token(); // colon really asks for separate treatment
436
+ print_newline();
437
+ break;
438
+ }
439
+ if (token_text === '::') { // no spaces around exotic namespacing syntax operator
440
+ print_token();
441
+ break;
442
+ }
443
+ in_case = false;
444
+ if (token_text === ',') {
445
+ if (var_line) {
446
+ if (var_line_tainted) {
447
+ print_token();
448
+ print_newline();
449
+ var_line_tainted = false;
450
+ } else {
451
+ print_token();
452
+ print_space();
453
+ }
454
+ } else if (last_type === 'TK_END_BLOCK') {
455
+ print_token();
456
+ print_newline();
457
+ } else {
458
+ if (current_mode === 'BLOCK') {
459
+ print_token();
460
+ print_newline();
461
+ } else { // EXPR od DO_BLOCK
462
+ print_token();
463
+ print_space();
464
+ }
465
+ }
466
+ break;
467
+ } else if (token_text === '--' || token_text === '++') { // unary operators special case
468
+ if (last_text === ';') { // space for (;; ++i)
469
+ start_delim = true;
470
+ end_delim = false;
471
+ } else {
472
+ start_delim = false;
473
+ end_delim = false;
474
+ }
475
+ } else if (token_text === '!' && last_type === 'TK_START_EXPR') { // special case handling: if (!a)
476
+ start_delim = false;
477
+ end_delim = false;
478
+ } else if (last_type === 'TK_OPERATOR') {
479
+ start_delim = false;
480
+ end_delim = false;
481
+ } else if (last_type === 'TK_END_EXPR') {
482
+ start_delim = true;
483
+ end_delim = true;
484
+ } else if (token_text === '.') { // decimal digits or object.property
485
+ start_delim = false;
486
+ end_delim = false;
487
+ } else if (token_text === ':') { // zz: xx
488
+ // can't differentiate ternary op, so for now it's a ? b: c; without space before colon
489
+ if (last_text.match(/^\d+$/)) { // a little help for ternary a ? 1 : 0;
490
+ start_delim = true;
491
+ } else {
492
+ start_delim = false;
493
+ }
494
+ }
495
+ if (start_delim) {
496
+ print_space();
497
+ }
498
+ print_token();
499
+ if (end_delim) {
500
+ print_space();
501
+ }
502
+ break;
503
+ case 'TK_BLOCK_COMMENT':
504
+ print_newline();
505
+ print_token();
506
+ print_newline();
507
+ break;
508
+ case 'TK_COMMENT':
509
+ // print_newline();
510
+ print_space();
511
+ print_token();
512
+ print_newline();
513
+ break;
514
+ case 'TK_UNKNOWN':
515
+ print_token();
516
+ break;
517
+ }
518
+ last_type = token_type;
519
+ last_text = token_text;
520
+ }
521
+ return output.join('');
522
+ }MARK1.00ANYAJscr ��ޭ