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,51 @@
|
|
|
1
|
+
/*global js_beautify: true */
|
|
2
|
+
/*jshint node:true */
|
|
3
|
+
/*jshint unused:false */
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
var fs = require('fs'),
|
|
7
|
+
SanityTest = require('./sanitytest'),
|
|
8
|
+
Benchmark = require('benchmark'),
|
|
9
|
+
Urlencoded = require('../lib/unpackers/urlencode_unpacker'),
|
|
10
|
+
js_beautify = require('../index').js_beautify,
|
|
11
|
+
css_beautify = require('../index').css_beautify,
|
|
12
|
+
html_beautify = require('../index').html_beautify;
|
|
13
|
+
|
|
14
|
+
function node_beautifier_html_tests() {
|
|
15
|
+
console.log('Testing performance...');
|
|
16
|
+
var index_html = fs.readFileSync(__dirname + '/../../index.html', 'utf8');
|
|
17
|
+
var data_attr = fs.readFileSync(__dirname + '/../../test/resources/html-with-base64image.html', 'utf8');
|
|
18
|
+
var options = {
|
|
19
|
+
wrap_line_length: 80
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
//warm-up
|
|
23
|
+
html_beautify(index_html, options);
|
|
24
|
+
html_beautify(data_attr, options);
|
|
25
|
+
|
|
26
|
+
var suite = new Benchmark.Suite();
|
|
27
|
+
|
|
28
|
+
suite.add("html-beautify (index.html)", function() {
|
|
29
|
+
html_beautify(index_html, options);
|
|
30
|
+
})
|
|
31
|
+
.add("html-beautify (base64 image)", function() {
|
|
32
|
+
html_beautify(data_attr, options);
|
|
33
|
+
})
|
|
34
|
+
// add listeners
|
|
35
|
+
.on('cycle', function(event) {
|
|
36
|
+
console.log(String(event.target));
|
|
37
|
+
})
|
|
38
|
+
.on('error', function(event) {
|
|
39
|
+
return 1;
|
|
40
|
+
})
|
|
41
|
+
.on('complete', function(event) {})
|
|
42
|
+
.run();
|
|
43
|
+
return 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
if (require.main === module) {
|
|
50
|
+
process.exit(node_beautifier_html_tests());
|
|
51
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/*global js_beautify: true */
|
|
2
|
+
/*jshint node:true */
|
|
3
|
+
/*jshint unused:false */
|
|
4
|
+
|
|
5
|
+
var fs = require('fs'),
|
|
6
|
+
SanityTest = require('./sanitytest'),
|
|
7
|
+
Benchmark = require('benchmark'),
|
|
8
|
+
Urlencoded = require('../lib/unpackers/urlencode_unpacker'),
|
|
9
|
+
js_beautify = require('../index').js_beautify,
|
|
10
|
+
css_beautify = require('../index').css_beautify,
|
|
11
|
+
html_beautify = require('../index').html_beautify;
|
|
12
|
+
|
|
13
|
+
function node_beautifier_tests() {
|
|
14
|
+
console.log('Testing performance...');
|
|
15
|
+
var data = fs.readFileSync(__dirname + '/../../test/resources/underscore.js', 'utf8');
|
|
16
|
+
var data_min = fs.readFileSync(__dirname + '/../../test/resources/underscore-min.js', 'utf8');
|
|
17
|
+
var options = {
|
|
18
|
+
wrap_line_length: 80
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
//warm-up
|
|
22
|
+
js_beautify(data, options);
|
|
23
|
+
js_beautify(data_min, options);
|
|
24
|
+
|
|
25
|
+
var suite = new Benchmark.Suite();
|
|
26
|
+
|
|
27
|
+
suite.add("js-beautify (underscore)", function() {
|
|
28
|
+
js_beautify(data, options);
|
|
29
|
+
})
|
|
30
|
+
.add("js-beautify (underscore-min)", function() {
|
|
31
|
+
js_beautify(data_min, options);
|
|
32
|
+
})
|
|
33
|
+
// add listeners
|
|
34
|
+
.on('cycle', function(event) {
|
|
35
|
+
console.log(String(event.target));
|
|
36
|
+
})
|
|
37
|
+
.on('error', function(event) {
|
|
38
|
+
return 1;
|
|
39
|
+
})
|
|
40
|
+
.on('complete', function(event) {})
|
|
41
|
+
.run();
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
if (require.main === module) {
|
|
49
|
+
process.exit(node_beautifier_tests());
|
|
50
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/*jshint node:true */
|
|
2
|
+
|
|
3
|
+
var SanityTest = require('./sanitytest'),
|
|
4
|
+
Urlencoded = require('../lib/unpackers/urlencode_unpacker'),
|
|
5
|
+
run_javascript_tests = require('./generated/beautify-javascript-tests').run_javascript_tests,
|
|
6
|
+
run_css_tests = require('./generated/beautify-css-tests').run_css_tests,
|
|
7
|
+
run_html_tests = require('./generated/beautify-html-tests').run_html_tests;
|
|
8
|
+
|
|
9
|
+
function test_legacy_names() {
|
|
10
|
+
var beautify = require('../index');
|
|
11
|
+
var results = new SanityTest();
|
|
12
|
+
|
|
13
|
+
console.log('First ensure that legacy import names equal the new ones');
|
|
14
|
+
results.expect(beautify.js, beautify.js_beautify);
|
|
15
|
+
results.expect(beautify.css, beautify.css_beautify);
|
|
16
|
+
results.expect(beautify.html, beautify.html_beautify);
|
|
17
|
+
|
|
18
|
+
console.log(results.results_raw());
|
|
19
|
+
return results;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function node_beautifier_tests(name, test_runner) {
|
|
23
|
+
console.log('Testing ' + name + ' with node.js CommonJS...');
|
|
24
|
+
var beautify = require('../index');
|
|
25
|
+
|
|
26
|
+
var results = new SanityTest();
|
|
27
|
+
test_runner(
|
|
28
|
+
results,
|
|
29
|
+
Urlencoded,
|
|
30
|
+
beautify.js,
|
|
31
|
+
beautify.html,
|
|
32
|
+
beautify.css);
|
|
33
|
+
|
|
34
|
+
console.log(results.results_raw());
|
|
35
|
+
return results;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (require.main === module) {
|
|
39
|
+
process.exit(
|
|
40
|
+
test_legacy_names() +
|
|
41
|
+
node_beautifier_tests('js-beautifier', run_javascript_tests).get_exitcode() +
|
|
42
|
+
node_beautifier_tests('css-beautifier', run_css_tests).get_exitcode() +
|
|
43
|
+
node_beautifier_tests('html-beautifier', run_html_tests).get_exitcode()
|
|
44
|
+
);
|
|
45
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en-US">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<title></title>
|
|
6
|
+
|
|
7
|
+
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.min.js"></script>
|
|
8
|
+
<script type="text/javascript">
|
|
9
|
+
|
|
10
|
+
require(["../lib/beautify-html"],function(html_beautify){
|
|
11
|
+
var input = document.getElementById("input").value;
|
|
12
|
+
var output = html_beautify.html_beautify(input);
|
|
13
|
+
document.getElementById("output").innerHTML = output;
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
</script>
|
|
17
|
+
|
|
18
|
+
<style type="text/css">
|
|
19
|
+
#output{
|
|
20
|
+
border: 1px solid black;
|
|
21
|
+
height: 300px;
|
|
22
|
+
width: 100%;
|
|
23
|
+
}
|
|
24
|
+
#input{
|
|
25
|
+
width: 100%;
|
|
26
|
+
height: 300px;
|
|
27
|
+
}
|
|
28
|
+
</style>
|
|
29
|
+
</head>
|
|
30
|
+
<body>
|
|
31
|
+
|
|
32
|
+
<h1>RequireJS test</h1>
|
|
33
|
+
|
|
34
|
+
<p>
|
|
35
|
+
This example loads the html-beautifier by using a relative path in the require call to the beautify-html.js file.
|
|
36
|
+
(also works works with absolute paths)
|
|
37
|
+
</p>
|
|
38
|
+
|
|
39
|
+
<pre>
|
|
40
|
+
require(["../lib/beautify-html"],function(html_beautify){
|
|
41
|
+
var input = document.getElementById("input").value;
|
|
42
|
+
var output = html_beautify.html_beautify(input);
|
|
43
|
+
document.getElementById("output").innerHTML = output;
|
|
44
|
+
});
|
|
45
|
+
</pre>
|
|
46
|
+
|
|
47
|
+
<h2>Input</h2>
|
|
48
|
+
<textarea name="" id="input">
|
|
49
|
+
<ul><li><a href="test"></a></li></ul>
|
|
50
|
+
</textarea>
|
|
51
|
+
|
|
52
|
+
<h2>Output</h2>
|
|
53
|
+
<textarea name="" id="output">
|
|
54
|
+
|
|
55
|
+
</textarea>
|
|
56
|
+
|
|
57
|
+
</body>
|
|
58
|
+
</html>
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Random stuff in here to cause parse error
|
|
File without changes
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#!/usr/bin/spidermonkey-1.7 -s
|
|
2
|
+
|
|
3
|
+
//#!/usr/bin/js
|
|
4
|
+
|
|
5
|
+
// a little helper for testing from command line
|
|
6
|
+
// just run it, it will output the test results
|
|
7
|
+
|
|
8
|
+
load('js/lib/beautify.js');
|
|
9
|
+
load('js/test/sanitytest.js')
|
|
10
|
+
load('js/test/beautify-tests.js')
|
|
11
|
+
load('js/lib/unpackers/urlencode_unpacker.js')
|
|
12
|
+
|
|
13
|
+
print(run_beautifier_tests(new SanityTest(), Urlencoded, js_beautify).results_raw())
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
// for nodejs use this from the command line from the main directory:
|
|
17
|
+
// node test/beautify-tests.js
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
//
|
|
2
|
+
// simple testing interface
|
|
3
|
+
// written by Einar Lielmanis, einar@jsbeautifier.org
|
|
4
|
+
//
|
|
5
|
+
// usage:
|
|
6
|
+
//
|
|
7
|
+
// var t = new SanityTest(function (x) { return x; }, 'my function');
|
|
8
|
+
// t.expect('input', 'output');
|
|
9
|
+
// t.expect('a', 'a');
|
|
10
|
+
// output_somewhere(t.results()); // good for <pre>, html safe-ish
|
|
11
|
+
// alert(t.results_raw()); // html unescaped
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
function SanityTest(func, name_of_test) {
|
|
15
|
+
|
|
16
|
+
var test_func = func || function(x) {
|
|
17
|
+
return x;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
var test_name = name_of_test || '';
|
|
21
|
+
|
|
22
|
+
var n_failed = 0;
|
|
23
|
+
var n_succeeded = 0;
|
|
24
|
+
|
|
25
|
+
var failures = [];
|
|
26
|
+
|
|
27
|
+
this.test_function = function(func, name) {
|
|
28
|
+
test_func = func;
|
|
29
|
+
test_name = name || '';
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
this.get_exitcode = function() {
|
|
33
|
+
return n_succeeded === 0 || n_failed !== 0 ? 1 : 0;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
this.expect = function(parameters, expected_value) {
|
|
37
|
+
// multi-parameter calls not supported (I don't need them now).
|
|
38
|
+
var result = test_func(parameters);
|
|
39
|
+
// proper array checking is a pain. i'll maybe do it later, compare strings representations instead
|
|
40
|
+
if ((result === expected_value) || (expected_value instanceof Array && result.join(', ') === expected_value.join(', '))) {
|
|
41
|
+
n_succeeded += 1;
|
|
42
|
+
} else {
|
|
43
|
+
n_failed += 1;
|
|
44
|
+
failures.push([test_name, parameters, expected_value, result]);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
this.results_raw = function() {
|
|
50
|
+
var results = '';
|
|
51
|
+
if (n_failed === 0) {
|
|
52
|
+
if (n_succeeded === 0) {
|
|
53
|
+
results = 'No tests run.';
|
|
54
|
+
} else {
|
|
55
|
+
results = 'All ' + n_succeeded + ' tests passed.';
|
|
56
|
+
}
|
|
57
|
+
} else {
|
|
58
|
+
for (var i = 0; i < failures.length; i++) {
|
|
59
|
+
var f = failures[i];
|
|
60
|
+
if (f[0]) {
|
|
61
|
+
f[0] = f[0] + ' ';
|
|
62
|
+
}
|
|
63
|
+
results += '==== ' + f[0] + '============================================================\n';
|
|
64
|
+
results += '---- input -------\n' + this.prettyprint(f[1]) + '\n';
|
|
65
|
+
results += '---- expected ----\n' + this.prettyprint(f[2]) + '\n';
|
|
66
|
+
results += '---- output ------\n' + this.prettyprint(f[3]) + '\n';
|
|
67
|
+
results += '---- expected-ws ------\n' + this.prettyprint_whitespace(f[2]) + '\n';
|
|
68
|
+
results += '---- output-ws ------\n' + this.prettyprint_whitespace(f[3]) + '\n';
|
|
69
|
+
results += '================================================================\n\n';
|
|
70
|
+
}
|
|
71
|
+
results += n_failed + ' tests failed.\n';
|
|
72
|
+
}
|
|
73
|
+
return results;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
this.results = function() {
|
|
78
|
+
return this.lazy_escape(this.results_raw());
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
this.prettyprint_whitespace = function(something, quote_strings) {
|
|
82
|
+
return (this.prettyprint(something, quote_strings)
|
|
83
|
+
.replace(/\r\n/g, '\\r\n')
|
|
84
|
+
.replace(/\n/g, '\\n\n')
|
|
85
|
+
.replace(/\r/g, '\\r\n')
|
|
86
|
+
.replace(/ /g, '_')
|
|
87
|
+
.replace(/\t/g, '===|'));
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
this.prettyprint = function(something, quote_strings) {
|
|
91
|
+
var type = typeof something;
|
|
92
|
+
switch (type.toLowerCase()) {
|
|
93
|
+
case 'string':
|
|
94
|
+
if (quote_strings) {
|
|
95
|
+
return "'" + something.replace("'", "\\'") + "'";
|
|
96
|
+
}
|
|
97
|
+
return something;
|
|
98
|
+
case 'number':
|
|
99
|
+
return '' + something;
|
|
100
|
+
case 'boolean':
|
|
101
|
+
return something ? 'true' : 'false';
|
|
102
|
+
case 'undefined':
|
|
103
|
+
return 'undefined';
|
|
104
|
+
case 'object':
|
|
105
|
+
if (something instanceof Array) {
|
|
106
|
+
var x = [];
|
|
107
|
+
var expected_index = 0;
|
|
108
|
+
for (var k in something) {
|
|
109
|
+
if (k === expected_index) {
|
|
110
|
+
x.push(this.prettyprint(something[k], true));
|
|
111
|
+
expected_index += 1;
|
|
112
|
+
} else {
|
|
113
|
+
x.push('\n' + k + ': ' + this.prettyprint(something[k], true));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return '[' + x.join(', ') + ']';
|
|
117
|
+
}
|
|
118
|
+
return 'object: ' + something;
|
|
119
|
+
default:
|
|
120
|
+
return type + ': ' + something;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
this.lazy_escape = function(str) {
|
|
126
|
+
return str.replace(/</g, '<').replace(/\>/g, '>').replace(/\n/g, '<br />');
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
this.log = function() {
|
|
131
|
+
if (window.console) {
|
|
132
|
+
if (console.firebug) {
|
|
133
|
+
console.log.apply(console, Array.prototype.slice.call(arguments));
|
|
134
|
+
} else {
|
|
135
|
+
console.log.call(console, Array.prototype.slice.call(arguments));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
143
|
+
module.exports = SanityTest;
|
|
144
|
+
}
|