js-beautify 1.7.0 → 1.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/CONTRIBUTING.md +3 -3
- package/README.md +15 -12
- package/js/bin/css-beautify.js +4 -0
- package/js/bin/html-beautify.js +4 -0
- package/js/bin/js-beautify.js +4 -0
- package/js/config/defaults.json +18 -0
- package/js/lib/beautify-css.js +1046 -0
- package/js/lib/beautify-html.js +1387 -0
- package/js/lib/beautify.js +2820 -0
- package/js/lib/cli.js +623 -0
- package/js/lib/unpackers/javascriptobfuscator_unpacker.js +103 -0
- package/js/lib/unpackers/myobfuscate_unpacker.js +90 -0
- package/js/lib/unpackers/p_a_c_k_e_r_unpacker.js +83 -0
- package/js/lib/unpackers/urlencode_unpacker.js +73 -0
- package/js/src/core/acorn.js +63 -0
- package/js/src/core/inputscanner.js +95 -0
- package/js/src/core/options.js +48 -0
- package/js/src/core/output.js +234 -0
- package/js/src/core/token.js +49 -0
- package/js/src/css/beautifier.js +477 -0
- package/js/src/css/index.js +36 -0
- package/js/src/html/beautifier.js +1035 -0
- package/js/src/html/index.js +36 -0
- package/js/src/index.js +27 -0
- package/js/src/javascript/beautifier.js +1449 -0
- package/js/src/javascript/index.js +36 -0
- package/js/src/javascript/tokenizer.js +620 -0
- package/js/test/amd-beautify-tests.js +62 -0
- package/js/test/generated/beautify-css-tests.js +1393 -0
- package/js/test/generated/beautify-html-tests.js +3212 -0
- package/js/test/generated/beautify-javascript-tests.js +5896 -0
- package/js/test/node-beautify-html-perf-tests.js +51 -0
- package/js/test/node-beautify-perf-tests.js +50 -0
- package/js/test/node-beautify-tests.js +45 -0
- package/js/test/requirejs-html-beautify.html +58 -0
- package/js/test/resources/configerror/.jsbeautifyrc +6 -0
- package/js/test/resources/configerror/subDir1/subDir2/empty.txt +0 -0
- package/js/test/resources/editorconfig/.editorconfig +6 -0
- package/js/test/resources/editorconfig/cr/.editorconfig +3 -0
- package/js/test/resources/editorconfig/crlf/.editorconfig +3 -0
- package/js/test/resources/editorconfig/error/.editorconfig +1 -0
- package/js/test/resources/editorconfig/example-base.js +3 -0
- package/js/test/resources/example1.js +3 -0
- package/js/test/resources/indent11chars/.jsbeautifyrc +6 -0
- package/js/test/resources/indent11chars/subDir1/subDir2/empty.txt +0 -0
- package/js/test/run-tests +17 -0
- package/js/test/sanitytest.js +144 -0
- package/js/test/shell-smoke-test.sh +383 -0
- package/package.json +1 -1
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */
|
|
2
|
+
/*
|
|
3
|
+
|
|
4
|
+
The MIT License (MIT)
|
|
5
|
+
|
|
6
|
+
Copyright (c) 2007-2017 Einar Lielmanis, Liam Newman, and contributors.
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person
|
|
9
|
+
obtaining a copy of this software and associated documentation files
|
|
10
|
+
(the "Software"), to deal in the Software without restriction,
|
|
11
|
+
including without limitation the rights to use, copy, modify, merge,
|
|
12
|
+
publish, distribute, sublicense, and/or sell copies of the Software,
|
|
13
|
+
and to permit persons to whom the Software is furnished to do so,
|
|
14
|
+
subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be
|
|
17
|
+
included in all copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
20
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
21
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
22
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
23
|
+
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
24
|
+
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
25
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
var Beautifier = require('./beautifier').Beautifier;
|
|
30
|
+
|
|
31
|
+
function js_beautify(js_source_text, options) {
|
|
32
|
+
var beautifier = new Beautifier(js_source_text, options);
|
|
33
|
+
return beautifier.beautify();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = js_beautify;
|
|
@@ -0,0 +1,620 @@
|
|
|
1
|
+
/*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */
|
|
2
|
+
/*
|
|
3
|
+
|
|
4
|
+
The MIT License (MIT)
|
|
5
|
+
|
|
6
|
+
Copyright (c) 2007-2017 Einar Lielmanis, Liam Newman, and contributors.
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person
|
|
9
|
+
obtaining a copy of this software and associated documentation files
|
|
10
|
+
(the "Software"), to deal in the Software without restriction,
|
|
11
|
+
including without limitation the rights to use, copy, modify, merge,
|
|
12
|
+
publish, distribute, sublicense, and/or sell copies of the Software,
|
|
13
|
+
and to permit persons to whom the Software is furnished to do so,
|
|
14
|
+
subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be
|
|
17
|
+
included in all copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
20
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
21
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
22
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
23
|
+
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
24
|
+
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
25
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
var InputScanner = require('core/inputscanner').InputScanner;
|
|
30
|
+
var Token = require('core/token').Token;
|
|
31
|
+
var acorn = require('core/acorn');
|
|
32
|
+
|
|
33
|
+
function trim(s) {
|
|
34
|
+
return s.replace(/^\s+|\s+$/g, '');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function in_array(what, arr) {
|
|
38
|
+
for (var i = 0; i < arr.length; i += 1) {
|
|
39
|
+
if (arr[i] === what) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function Tokenizer(input_string, opts) {
|
|
47
|
+
|
|
48
|
+
var whitespace = "\n\r\t ".split('');
|
|
49
|
+
var digit = /[0-9]/;
|
|
50
|
+
var digit_bin = /[01]/;
|
|
51
|
+
var digit_oct = /[01234567]/;
|
|
52
|
+
var digit_hex = /[0123456789abcdefABCDEF]/;
|
|
53
|
+
|
|
54
|
+
this.positionable_operators = '!= !== % & && * ** + - / : < << <= == === > >= >> >>> ? ^ | ||'.split(' ');
|
|
55
|
+
var punct = this.positionable_operators.concat(
|
|
56
|
+
// non-positionable operators - these do not follow operator position settings
|
|
57
|
+
'! %= &= *= **= ++ += , -- -= /= :: <<= = => >>= >>>= ^= |= ~ ...'.split(' '));
|
|
58
|
+
|
|
59
|
+
// words which should always start on new line.
|
|
60
|
+
this.line_starters = 'continue,try,throw,return,var,let,const,if,switch,case,default,for,while,break,function,import,export'.split(',');
|
|
61
|
+
var reserved_words = this.line_starters.concat(['do', 'in', 'of', 'else', 'get', 'set', 'new', 'catch', 'finally', 'typeof', 'yield', 'async', 'await', 'from', 'as']);
|
|
62
|
+
|
|
63
|
+
// /* ... */ comment ends with nearest */ or end of file
|
|
64
|
+
var block_comment_pattern = /([\s\S]*?)((?:\*\/)|$)/g;
|
|
65
|
+
|
|
66
|
+
// comment ends just before nearest linefeed or end of file
|
|
67
|
+
var comment_pattern = /([^\n\r\u2028\u2029]*)/g;
|
|
68
|
+
|
|
69
|
+
var directives_block_pattern = /\/\* beautify( \w+[:]\w+)+ \*\//g;
|
|
70
|
+
var directive_pattern = / (\w+)[:](\w+)/g;
|
|
71
|
+
var directives_end_ignore_pattern = /([\s\S]*?)((?:\/\*\sbeautify\signore:end\s\*\/)|$)/g;
|
|
72
|
+
|
|
73
|
+
var template_pattern = /((<\?php|<\?=)[\s\S]*?\?>)|(<%[\s\S]*?%>)/g;
|
|
74
|
+
|
|
75
|
+
var n_newlines, whitespace_before_token, in_html_comment, tokens;
|
|
76
|
+
var input;
|
|
77
|
+
|
|
78
|
+
this.tokenize = function() {
|
|
79
|
+
input = new InputScanner(input_string);
|
|
80
|
+
in_html_comment = false;
|
|
81
|
+
tokens = [];
|
|
82
|
+
|
|
83
|
+
var next, last;
|
|
84
|
+
var token_values;
|
|
85
|
+
var open = null;
|
|
86
|
+
var open_stack = [];
|
|
87
|
+
var comments = [];
|
|
88
|
+
|
|
89
|
+
while (!(last && last.type === 'TK_EOF')) {
|
|
90
|
+
token_values = tokenize_next();
|
|
91
|
+
next = new Token(token_values[1], token_values[0], n_newlines, whitespace_before_token);
|
|
92
|
+
while (next.type === 'TK_COMMENT' || next.type === 'TK_BLOCK_COMMENT' || next.type === 'TK_UNKNOWN') {
|
|
93
|
+
if (next.type === 'TK_BLOCK_COMMENT') {
|
|
94
|
+
next.directives = token_values[2];
|
|
95
|
+
}
|
|
96
|
+
comments.push(next);
|
|
97
|
+
token_values = tokenize_next();
|
|
98
|
+
next = new Token(token_values[1], token_values[0], n_newlines, whitespace_before_token);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (comments.length) {
|
|
102
|
+
next.comments_before = comments;
|
|
103
|
+
comments = [];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (next.type === 'TK_START_BLOCK' || next.type === 'TK_START_EXPR') {
|
|
107
|
+
next.parent = last;
|
|
108
|
+
open_stack.push(open);
|
|
109
|
+
open = next;
|
|
110
|
+
} else if ((next.type === 'TK_END_BLOCK' || next.type === 'TK_END_EXPR') &&
|
|
111
|
+
(open && (
|
|
112
|
+
(next.text === ']' && open.text === '[') ||
|
|
113
|
+
(next.text === ')' && open.text === '(') ||
|
|
114
|
+
(next.text === '}' && open.text === '{')))) {
|
|
115
|
+
next.parent = open.parent;
|
|
116
|
+
next.opened = open;
|
|
117
|
+
|
|
118
|
+
open = open_stack.pop();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
tokens.push(next);
|
|
122
|
+
last = next;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return tokens;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
function get_directives(text) {
|
|
129
|
+
if (!text.match(directives_block_pattern)) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
var directives = {};
|
|
134
|
+
directive_pattern.lastIndex = 0;
|
|
135
|
+
var directive_match = directive_pattern.exec(text);
|
|
136
|
+
|
|
137
|
+
while (directive_match) {
|
|
138
|
+
directives[directive_match[1]] = directive_match[2];
|
|
139
|
+
directive_match = directive_pattern.exec(text);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return directives;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function tokenize_next() {
|
|
146
|
+
var resulting_string;
|
|
147
|
+
var whitespace_on_this_line = [];
|
|
148
|
+
|
|
149
|
+
n_newlines = 0;
|
|
150
|
+
whitespace_before_token = '';
|
|
151
|
+
|
|
152
|
+
var c = input.next();
|
|
153
|
+
|
|
154
|
+
if (c === null) {
|
|
155
|
+
return ['', 'TK_EOF'];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
var last_token;
|
|
159
|
+
if (tokens.length) {
|
|
160
|
+
last_token = tokens[tokens.length - 1];
|
|
161
|
+
} else {
|
|
162
|
+
// For the sake of tokenizing we can pretend that there was on open brace to start
|
|
163
|
+
last_token = new Token('TK_START_BLOCK', '{');
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
while (in_array(c, whitespace)) {
|
|
167
|
+
|
|
168
|
+
if (acorn.newline.test(c)) {
|
|
169
|
+
if (!(c === '\n' && input.peek(-2) === '\r')) {
|
|
170
|
+
n_newlines += 1;
|
|
171
|
+
whitespace_on_this_line = [];
|
|
172
|
+
}
|
|
173
|
+
} else {
|
|
174
|
+
whitespace_on_this_line.push(c);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
c = input.next();
|
|
178
|
+
|
|
179
|
+
if (c === null) {
|
|
180
|
+
return ['', 'TK_EOF'];
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (whitespace_on_this_line.length) {
|
|
185
|
+
whitespace_before_token = whitespace_on_this_line.join('');
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (digit.test(c) || (c === '.' && input.testChar(digit))) {
|
|
189
|
+
var allow_decimal = true;
|
|
190
|
+
var allow_e = true;
|
|
191
|
+
var local_digit = digit;
|
|
192
|
+
|
|
193
|
+
if (c === '0' && input.testChar(/[XxOoBb]/)) {
|
|
194
|
+
// switch to hex/oct/bin number, no decimal or e, just hex/oct/bin digits
|
|
195
|
+
allow_decimal = false;
|
|
196
|
+
allow_e = false;
|
|
197
|
+
if (input.testChar(/[Bb]/)) {
|
|
198
|
+
local_digit = digit_bin;
|
|
199
|
+
} else if (input.testChar(/[Oo]/)) {
|
|
200
|
+
local_digit = digit_oct;
|
|
201
|
+
} else {
|
|
202
|
+
local_digit = digit_hex;
|
|
203
|
+
}
|
|
204
|
+
c += input.next();
|
|
205
|
+
} else if (c === '.') {
|
|
206
|
+
// Already have a decimal for this literal, don't allow another
|
|
207
|
+
allow_decimal = false;
|
|
208
|
+
} else {
|
|
209
|
+
// we know this first loop will run. It keeps the logic simpler.
|
|
210
|
+
c = '';
|
|
211
|
+
input.back();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Add the digits
|
|
215
|
+
while (input.testChar(local_digit)) {
|
|
216
|
+
c += input.next();
|
|
217
|
+
|
|
218
|
+
if (allow_decimal && input.peek() === '.') {
|
|
219
|
+
c += input.next();
|
|
220
|
+
allow_decimal = false;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// a = 1.e-7 is valid, so we test for . then e in one loop
|
|
224
|
+
if (allow_e && input.testChar(/[Ee]/)) {
|
|
225
|
+
c += input.next();
|
|
226
|
+
|
|
227
|
+
if (input.testChar(/[+-]/)) {
|
|
228
|
+
c += input.next();
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
allow_e = false;
|
|
232
|
+
allow_decimal = false;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return [c, 'TK_WORD'];
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (acorn.isIdentifierStart(input.peekCharCode(-1))) {
|
|
240
|
+
if (input.hasNext()) {
|
|
241
|
+
while (acorn.isIdentifierChar(input.peekCharCode())) {
|
|
242
|
+
c += input.next();
|
|
243
|
+
if (!input.hasNext()) {
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (!(last_token.type === 'TK_DOT' ||
|
|
250
|
+
(last_token.type === 'TK_RESERVED' && in_array(last_token.text, ['set', 'get']))) &&
|
|
251
|
+
in_array(c, reserved_words)) {
|
|
252
|
+
if (c === 'in' || c === 'of') { // hack for 'in' and 'of' operators
|
|
253
|
+
return [c, 'TK_OPERATOR'];
|
|
254
|
+
}
|
|
255
|
+
return [c, 'TK_RESERVED'];
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return [c, 'TK_WORD'];
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (c === '(' || c === '[') {
|
|
262
|
+
return [c, 'TK_START_EXPR'];
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (c === ')' || c === ']') {
|
|
266
|
+
return [c, 'TK_END_EXPR'];
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (c === '{') {
|
|
270
|
+
return [c, 'TK_START_BLOCK'];
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (c === '}') {
|
|
274
|
+
return [c, 'TK_END_BLOCK'];
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (c === ';') {
|
|
278
|
+
return [c, 'TK_SEMICOLON'];
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (c === '/') {
|
|
282
|
+
var comment = '';
|
|
283
|
+
var comment_match;
|
|
284
|
+
// peek for comment /* ... */
|
|
285
|
+
if (input.peek() === '*') {
|
|
286
|
+
input.next();
|
|
287
|
+
comment_match = input.match(block_comment_pattern);
|
|
288
|
+
comment = '/*' + comment_match[0];
|
|
289
|
+
var directives = get_directives(comment);
|
|
290
|
+
if (directives && directives.ignore === 'start') {
|
|
291
|
+
comment_match = input.match(directives_end_ignore_pattern);
|
|
292
|
+
comment += comment_match[0];
|
|
293
|
+
}
|
|
294
|
+
comment = comment.replace(acorn.allLineBreaks, '\n');
|
|
295
|
+
return [comment, 'TK_BLOCK_COMMENT', directives];
|
|
296
|
+
}
|
|
297
|
+
// peek for comment // ...
|
|
298
|
+
if (input.peek() === '/') {
|
|
299
|
+
input.next();
|
|
300
|
+
comment_match = input.match(comment_pattern);
|
|
301
|
+
comment = '//' + comment_match[0];
|
|
302
|
+
return [comment, 'TK_COMMENT'];
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
var startXmlRegExp = /<()([-a-zA-Z:0-9_.]+|{[\s\S]+?}|!\[CDATA\[[\s\S]*?\]\])(\s+{[\s\S]+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{[\s\S]+?}))*\s*(\/?)\s*>/g;
|
|
308
|
+
|
|
309
|
+
if (c === '`' || c === "'" || c === '"' || // string
|
|
310
|
+
(
|
|
311
|
+
(c === '/') || // regexp
|
|
312
|
+
(opts.e4x && c === "<" && input.test(startXmlRegExp, -1)) // xml
|
|
313
|
+
) && ( // regex and xml can only appear in specific locations during parsing
|
|
314
|
+
(last_token.type === 'TK_RESERVED' && in_array(last_token.text, ['return', 'case', 'throw', 'else', 'do', 'typeof', 'yield'])) ||
|
|
315
|
+
(last_token.type === 'TK_END_EXPR' && last_token.text === ')' &&
|
|
316
|
+
last_token.parent && last_token.parent.type === 'TK_RESERVED' && in_array(last_token.parent.text, ['if', 'while', 'for'])) ||
|
|
317
|
+
(in_array(last_token.type, ['TK_COMMENT', 'TK_START_EXPR', 'TK_START_BLOCK',
|
|
318
|
+
'TK_END_BLOCK', 'TK_OPERATOR', 'TK_EQUALS', 'TK_EOF', 'TK_SEMICOLON', 'TK_COMMA'
|
|
319
|
+
]))
|
|
320
|
+
)) {
|
|
321
|
+
|
|
322
|
+
var sep = c,
|
|
323
|
+
esc = false,
|
|
324
|
+
has_char_escapes = false;
|
|
325
|
+
|
|
326
|
+
resulting_string = c;
|
|
327
|
+
|
|
328
|
+
if (sep === '/') {
|
|
329
|
+
//
|
|
330
|
+
// handle regexp
|
|
331
|
+
//
|
|
332
|
+
var in_char_class = false;
|
|
333
|
+
while (input.hasNext() &&
|
|
334
|
+
((esc || in_char_class || input.peek() !== sep) &&
|
|
335
|
+
!input.testChar(acorn.newline))) {
|
|
336
|
+
resulting_string += input.peek();
|
|
337
|
+
if (!esc) {
|
|
338
|
+
esc = input.peek() === '\\';
|
|
339
|
+
if (input.peek() === '[') {
|
|
340
|
+
in_char_class = true;
|
|
341
|
+
} else if (input.peek() === ']') {
|
|
342
|
+
in_char_class = false;
|
|
343
|
+
}
|
|
344
|
+
} else {
|
|
345
|
+
esc = false;
|
|
346
|
+
}
|
|
347
|
+
input.next();
|
|
348
|
+
}
|
|
349
|
+
} else if (opts.e4x && sep === '<') {
|
|
350
|
+
//
|
|
351
|
+
// handle e4x xml literals
|
|
352
|
+
//
|
|
353
|
+
|
|
354
|
+
var xmlRegExp = /[\s\S]*?<(\/?)([-a-zA-Z:0-9_.]+|{[\s\S]+?}|!\[CDATA\[[\s\S]*?\]\])(\s+{[\s\S]+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{[\s\S]+?}))*\s*(\/?)\s*>/g;
|
|
355
|
+
input.back();
|
|
356
|
+
var xmlStr = '';
|
|
357
|
+
var match = input.match(startXmlRegExp);
|
|
358
|
+
if (match) {
|
|
359
|
+
// Trim root tag to attempt to
|
|
360
|
+
var rootTag = match[2].replace(/^{\s+/, '{').replace(/\s+}$/, '}');
|
|
361
|
+
var isCurlyRoot = rootTag.indexOf('{') === 0;
|
|
362
|
+
var depth = 0;
|
|
363
|
+
while (match) {
|
|
364
|
+
var isEndTag = !!match[1];
|
|
365
|
+
var tagName = match[2];
|
|
366
|
+
var isSingletonTag = (!!match[match.length - 1]) || (tagName.slice(0, 8) === "![CDATA[");
|
|
367
|
+
if (!isSingletonTag &&
|
|
368
|
+
(tagName === rootTag || (isCurlyRoot && tagName.replace(/^{\s+/, '{').replace(/\s+}$/, '}')))) {
|
|
369
|
+
if (isEndTag) {
|
|
370
|
+
--depth;
|
|
371
|
+
} else {
|
|
372
|
+
++depth;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
xmlStr += match[0];
|
|
376
|
+
if (depth <= 0) {
|
|
377
|
+
break;
|
|
378
|
+
}
|
|
379
|
+
match = input.match(xmlRegExp);
|
|
380
|
+
}
|
|
381
|
+
// if we didn't close correctly, keep unformatted.
|
|
382
|
+
if (!match) {
|
|
383
|
+
xmlStr += input.match(/[\s\S]*/g)[0];
|
|
384
|
+
}
|
|
385
|
+
xmlStr = xmlStr.replace(acorn.allLineBreaks, '\n');
|
|
386
|
+
return [xmlStr, "TK_STRING"];
|
|
387
|
+
}
|
|
388
|
+
} else {
|
|
389
|
+
//
|
|
390
|
+
// handle string
|
|
391
|
+
//
|
|
392
|
+
var parse_string = function(delimiter, allow_unescaped_newlines, start_sub) {
|
|
393
|
+
// Template strings can travers lines without escape characters.
|
|
394
|
+
// Other strings cannot
|
|
395
|
+
var current_char;
|
|
396
|
+
while (input.hasNext()) {
|
|
397
|
+
current_char = input.peek();
|
|
398
|
+
if (!(esc || (current_char !== delimiter &&
|
|
399
|
+
(allow_unescaped_newlines || !acorn.newline.test(current_char))))) {
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// Handle \r\n linebreaks after escapes or in template strings
|
|
404
|
+
if ((esc || allow_unescaped_newlines) && acorn.newline.test(current_char)) {
|
|
405
|
+
if (current_char === '\r' && input.peek(1) === '\n') {
|
|
406
|
+
input.next();
|
|
407
|
+
current_char = input.peek();
|
|
408
|
+
}
|
|
409
|
+
resulting_string += '\n';
|
|
410
|
+
} else {
|
|
411
|
+
resulting_string += current_char;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if (esc) {
|
|
415
|
+
if (current_char === 'x' || current_char === 'u') {
|
|
416
|
+
has_char_escapes = true;
|
|
417
|
+
}
|
|
418
|
+
esc = false;
|
|
419
|
+
} else {
|
|
420
|
+
esc = current_char === '\\';
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
input.next();
|
|
424
|
+
|
|
425
|
+
if (start_sub && resulting_string.indexOf(start_sub, resulting_string.length - start_sub.length) !== -1) {
|
|
426
|
+
if (delimiter === '`') {
|
|
427
|
+
parse_string('}', allow_unescaped_newlines, '`');
|
|
428
|
+
} else {
|
|
429
|
+
parse_string('`', allow_unescaped_newlines, '${');
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
if (input.hasNext()) {
|
|
433
|
+
resulting_string += input.next();
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
if (sep === '`') {
|
|
440
|
+
parse_string('`', true, '${');
|
|
441
|
+
} else {
|
|
442
|
+
parse_string(sep);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
if (has_char_escapes && opts.unescape_strings) {
|
|
447
|
+
resulting_string = unescape_string(resulting_string);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
if (input.peek() === sep) {
|
|
451
|
+
resulting_string += sep;
|
|
452
|
+
input.next();
|
|
453
|
+
|
|
454
|
+
if (sep === '/') {
|
|
455
|
+
// regexps may have modifiers /regexp/MOD , so fetch those, too
|
|
456
|
+
// Only [gim] are valid, but if the user puts in garbage, do what we can to take it.
|
|
457
|
+
while (input.hasNext() && acorn.isIdentifierStart(input.peekCharCode())) {
|
|
458
|
+
resulting_string += input.next();
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
return [resulting_string, 'TK_STRING'];
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
if (c === '#') {
|
|
466
|
+
|
|
467
|
+
if (tokens.length === 0 && input.peek() === '!') {
|
|
468
|
+
// shebang
|
|
469
|
+
resulting_string = c;
|
|
470
|
+
while (input.hasNext() && c !== '\n') {
|
|
471
|
+
c = input.next();
|
|
472
|
+
resulting_string += c;
|
|
473
|
+
}
|
|
474
|
+
return [trim(resulting_string) + '\n', 'TK_UNKNOWN'];
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
// Spidermonkey-specific sharp variables for circular references
|
|
480
|
+
// https://developer.mozilla.org/En/Sharp_variables_in_JavaScript
|
|
481
|
+
// http://mxr.mozilla.org/mozilla-central/source/js/src/jsscan.cpp around line 1935
|
|
482
|
+
var sharp = '#';
|
|
483
|
+
if (input.hasNext() && input.testChar(digit)) {
|
|
484
|
+
do {
|
|
485
|
+
c = input.next();
|
|
486
|
+
sharp += c;
|
|
487
|
+
} while (input.hasNext() && c !== '#' && c !== '=');
|
|
488
|
+
if (c === '#') {
|
|
489
|
+
//
|
|
490
|
+
} else if (input.peek() === '[' && input.peek(1) === ']') {
|
|
491
|
+
sharp += '[]';
|
|
492
|
+
input.next();
|
|
493
|
+
input.next();
|
|
494
|
+
} else if (input.peek() === '{' && input.peek(1) === '}') {
|
|
495
|
+
sharp += '{}';
|
|
496
|
+
input.next();
|
|
497
|
+
input.next();
|
|
498
|
+
}
|
|
499
|
+
return [sharp, 'TK_WORD'];
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
if (c === '<' && (input.peek() === '?' || input.peek() === '%')) {
|
|
504
|
+
input.back();
|
|
505
|
+
var template_match = input.match(template_pattern);
|
|
506
|
+
if (template_match) {
|
|
507
|
+
c = template_match[0];
|
|
508
|
+
c = c.replace(acorn.allLineBreaks, '\n');
|
|
509
|
+
return [c, 'TK_STRING'];
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
if (c === '<' && input.match(/\!--/g)) {
|
|
514
|
+
c = '<!--';
|
|
515
|
+
while (input.hasNext() && !input.testChar(acorn.newline)) {
|
|
516
|
+
c += input.next();
|
|
517
|
+
}
|
|
518
|
+
in_html_comment = true;
|
|
519
|
+
return [c, 'TK_COMMENT'];
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
if (c === '-' && in_html_comment && input.match(/->/g)) {
|
|
523
|
+
in_html_comment = false;
|
|
524
|
+
return ['-->', 'TK_COMMENT'];
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
if (c === '.') {
|
|
528
|
+
if (input.peek() === '.' && input.peek(1) === '.') {
|
|
529
|
+
c += input.next() + input.next();
|
|
530
|
+
return [c, 'TK_OPERATOR'];
|
|
531
|
+
}
|
|
532
|
+
return [c, 'TK_DOT'];
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
if (in_array(c, punct)) {
|
|
536
|
+
while (input.hasNext() && in_array(c + input.peek(), punct)) {
|
|
537
|
+
c += input.next();
|
|
538
|
+
if (!input.hasNext()) {
|
|
539
|
+
break;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
if (c === ',') {
|
|
544
|
+
return [c, 'TK_COMMA'];
|
|
545
|
+
} else if (c === '=') {
|
|
546
|
+
return [c, 'TK_EQUALS'];
|
|
547
|
+
} else {
|
|
548
|
+
return [c, 'TK_OPERATOR'];
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
return [c, 'TK_UNKNOWN'];
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
function unescape_string(s) {
|
|
557
|
+
// You think that a regex would work for this
|
|
558
|
+
// return s.replace(/\\x([0-9a-f]{2})/gi, function(match, val) {
|
|
559
|
+
// return String.fromCharCode(parseInt(val, 16));
|
|
560
|
+
// })
|
|
561
|
+
// However, dealing with '\xff', '\\xff', '\\\xff' makes this more fun.
|
|
562
|
+
var out = '',
|
|
563
|
+
escaped = 0;
|
|
564
|
+
|
|
565
|
+
var input_scan = new InputScanner(s);
|
|
566
|
+
var matched = null;
|
|
567
|
+
|
|
568
|
+
while (input_scan.hasNext()) {
|
|
569
|
+
// Keep any whitespace, non-slash characters
|
|
570
|
+
// also keep slash pairs.
|
|
571
|
+
matched = input_scan.match(/([\s]|[^\\]|\\\\)+/g);
|
|
572
|
+
|
|
573
|
+
if (matched) {
|
|
574
|
+
out += matched[0];
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
if (input_scan.peek() === '\\') {
|
|
578
|
+
input_scan.next();
|
|
579
|
+
if (input_scan.peek() === 'x') {
|
|
580
|
+
matched = input_scan.match(/x([0-9A-Fa-f]{2})/g);
|
|
581
|
+
} else if (input_scan.peek() === 'u') {
|
|
582
|
+
matched = input_scan.match(/u([0-9A-Fa-f]{4})/g);
|
|
583
|
+
} else {
|
|
584
|
+
out += '\\';
|
|
585
|
+
if (input_scan.hasNext()) {
|
|
586
|
+
out += input_scan.next();
|
|
587
|
+
}
|
|
588
|
+
continue;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// If there's some error decoding, return the original string
|
|
592
|
+
if (!matched) {
|
|
593
|
+
return s;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
escaped = parseInt(matched[1], 16);
|
|
597
|
+
|
|
598
|
+
if (escaped > 0x7e && escaped <= 0xff && matched[0].indexOf('x') === 0) {
|
|
599
|
+
// we bail out on \x7f..\xff,
|
|
600
|
+
// leaving whole string escaped,
|
|
601
|
+
// as it's probably completely binary
|
|
602
|
+
return s;
|
|
603
|
+
} else if (escaped >= 0x00 && escaped < 0x20) {
|
|
604
|
+
// leave 0x00...0x1f escaped
|
|
605
|
+
out += '\\' + matched[0];
|
|
606
|
+
continue;
|
|
607
|
+
} else if (escaped === 0x22 || escaped === 0x27 || escaped === 0x5c) {
|
|
608
|
+
// single-quote, apostrophe, backslash - escape these
|
|
609
|
+
out += '\\' + String.fromCharCode(escaped);
|
|
610
|
+
} else {
|
|
611
|
+
out += String.fromCharCode(escaped);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
return out;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
module.exports.Tokenizer = Tokenizer;
|