js-beautify 1.6.14 → 1.7.3

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.
Files changed (48) hide show
  1. package/CHANGELOG.md +14 -179
  2. package/README.md +12 -10
  3. package/js/lib/beautify-css.js +768 -293
  4. package/js/lib/beautify-html.js +1016 -757
  5. package/js/lib/beautify.js +2385 -2046
  6. package/js/lib/cli.js +3 -0
  7. package/js/src/core/acorn.js +63 -0
  8. package/js/src/core/inputscanner.js +95 -0
  9. package/js/src/core/options.js +48 -0
  10. package/js/src/core/output.js +234 -0
  11. package/js/src/core/token.js +49 -0
  12. package/js/src/css/beautifier.js +477 -0
  13. package/js/src/css/index.js +36 -0
  14. package/js/src/html/beautifier.js +1035 -0
  15. package/js/src/html/index.js +36 -0
  16. package/js/src/index.js +27 -0
  17. package/js/src/javascript/beautifier.js +1449 -0
  18. package/js/src/javascript/index.js +36 -0
  19. package/js/src/javascript/tokenizer.js +620 -0
  20. package/js/test/generated/beautify-javascript-tests.js +33 -0
  21. package/package.json +5 -4
  22. package/.codeclimate.yml +0 -13
  23. package/.jshintignore +0 -9
  24. package/.jshintrc +0 -10
  25. package/.npmignore +0 -5
  26. package/.travis.yml +0 -14
  27. package/ISSUE_TEMPLATE.md +0 -55
  28. package/appveyor.yml +0 -35
  29. package/bower.json +0 -11
  30. package/build +0 -5
  31. package/jsbeautifyrc +0 -18
  32. package/test/data/css/node.mustache +0 -269
  33. package/test/data/css/python.mustache +0 -270
  34. package/test/data/css/tests.js +0 -445
  35. package/test/data/html/node.mustache +0 -378
  36. package/test/data/html/tests.js +0 -1105
  37. package/test/data/javascript/inputlib.js +0 -84
  38. package/test/data/javascript/node.mustache +0 -1114
  39. package/test/data/javascript/python.mustache +0 -1322
  40. package/test/data/javascript/tests.js +0 -3083
  41. package/test/generate-tests.js +0 -216
  42. package/test/resources/html-with-base64image.html +0 -1
  43. package/test/resources/underscore-min.js +0 -6
  44. package/test/resources/underscore.js +0 -1439
  45. package/tools/build.sh +0 -140
  46. package/tools/generate-changelog.sh +0 -40
  47. package/tools/git-status-clear.sh +0 -18
  48. package/tools/release-all.sh +0 -86
@@ -0,0 +1,477 @@
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 mergeOpts = require('core/options').mergeOpts;
30
+ var acorn = require('core/acorn');
31
+ var Output = require('core/output').Output;
32
+
33
+
34
+ var lineBreak = acorn.lineBreak;
35
+ var allLineBreaks = acorn.allLineBreaks;
36
+
37
+ function Beautifier(source_text, options) {
38
+ options = options || {};
39
+
40
+ // Allow the setting of language/file-type specific options
41
+ // with inheritance of overall settings
42
+ options = mergeOpts(options, 'css');
43
+
44
+ source_text = source_text || '';
45
+
46
+ var newlinesFromLastWSEat = 0;
47
+ var indentSize = options.indent_size ? parseInt(options.indent_size, 10) : 4;
48
+ var indentCharacter = options.indent_char || ' ';
49
+ var preserve_newlines = (options.preserve_newlines === undefined) ? false : options.preserve_newlines;
50
+ var selectorSeparatorNewline = (options.selector_separator_newline === undefined) ? true : options.selector_separator_newline;
51
+ var end_with_newline = (options.end_with_newline === undefined) ? false : options.end_with_newline;
52
+ var newline_between_rules = (options.newline_between_rules === undefined) ? true : options.newline_between_rules;
53
+ var space_around_combinator = (options.space_around_combinator === undefined) ? false : options.space_around_combinator;
54
+ space_around_combinator = space_around_combinator || ((options.space_around_selector_separator === undefined) ? false : options.space_around_selector_separator);
55
+ var eol = options.eol ? options.eol : 'auto';
56
+
57
+ if (options.indent_with_tabs) {
58
+ indentCharacter = '\t';
59
+ indentSize = 1;
60
+ }
61
+
62
+ if (eol === 'auto') {
63
+ eol = '\n';
64
+ if (source_text && lineBreak.test(source_text || '')) {
65
+ eol = source_text.match(lineBreak)[0];
66
+ }
67
+ }
68
+
69
+ eol = eol.replace(/\\r/, '\r').replace(/\\n/, '\n');
70
+
71
+ // HACK: newline parsing inconsistent. This brute force normalizes the input.
72
+ source_text = source_text.replace(allLineBreaks, '\n');
73
+
74
+ // tokenizer
75
+ var whiteRe = /^\s+$/;
76
+
77
+ var pos = -1,
78
+ ch;
79
+ var parenLevel = 0;
80
+
81
+ function next() {
82
+ ch = source_text.charAt(++pos);
83
+ return ch || '';
84
+ }
85
+
86
+ function peek(skipWhitespace) {
87
+ var result = '';
88
+ var prev_pos = pos;
89
+ if (skipWhitespace) {
90
+ eatWhitespace();
91
+ }
92
+ result = source_text.charAt(pos + 1) || '';
93
+ pos = prev_pos - 1;
94
+ next();
95
+ return result;
96
+ }
97
+
98
+ function eatString(endChars) {
99
+ var start = pos;
100
+ while (next()) {
101
+ if (ch === "\\") {
102
+ next();
103
+ } else if (endChars.indexOf(ch) !== -1) {
104
+ break;
105
+ } else if (ch === "\n") {
106
+ break;
107
+ }
108
+ }
109
+ return source_text.substring(start, pos + 1);
110
+ }
111
+
112
+ function peekString(endChar) {
113
+ var prev_pos = pos;
114
+ var str = eatString(endChar);
115
+ pos = prev_pos - 1;
116
+ next();
117
+ return str;
118
+ }
119
+
120
+ function eatWhitespace(preserve_newlines_local) {
121
+ var result = 0;
122
+ while (whiteRe.test(peek())) {
123
+ next();
124
+ if (ch === '\n' && preserve_newlines_local && preserve_newlines) {
125
+ output.add_new_line(true);
126
+ result++;
127
+ }
128
+ }
129
+ newlinesFromLastWSEat = result;
130
+ return result;
131
+ }
132
+
133
+ function skipWhitespace() {
134
+ var result = '';
135
+ if (ch && whiteRe.test(ch)) {
136
+ result = ch;
137
+ }
138
+ while (whiteRe.test(next())) {
139
+ result += ch;
140
+ }
141
+ return result;
142
+ }
143
+
144
+ function eatComment() {
145
+ var start = pos;
146
+ var singleLine = peek() === "/";
147
+ next();
148
+ while (next()) {
149
+ if (!singleLine && ch === "*" && peek() === "/") {
150
+ next();
151
+ break;
152
+ } else if (singleLine && ch === "\n") {
153
+ return source_text.substring(start, pos);
154
+ }
155
+ }
156
+
157
+ return source_text.substring(start, pos) + ch;
158
+ }
159
+
160
+
161
+ function lookBack(str) {
162
+ return source_text.substring(pos - str.length, pos).toLowerCase() ===
163
+ str;
164
+ }
165
+
166
+ // Nested pseudo-class if we are insideRule
167
+ // and the next special character found opens
168
+ // a new block
169
+ function foundNestedPseudoClass() {
170
+ var openParen = 0;
171
+ for (var i = pos + 1; i < source_text.length; i++) {
172
+ var ch = source_text.charAt(i);
173
+ if (ch === "{") {
174
+ return true;
175
+ } else if (ch === '(') {
176
+ // pseudoclasses can contain ()
177
+ openParen += 1;
178
+ } else if (ch === ')') {
179
+ if (openParen === 0) {
180
+ return false;
181
+ }
182
+ openParen -= 1;
183
+ } else if (ch === ";" || ch === "}") {
184
+ return false;
185
+ }
186
+ }
187
+ return false;
188
+ }
189
+
190
+ // printer
191
+ var baseIndentString = '';
192
+ var preindent_index = 0;
193
+ if (source_text && source_text.length) {
194
+ while ((source_text.charAt(preindent_index) === ' ' ||
195
+ source_text.charAt(preindent_index) === '\t')) {
196
+ preindent_index += 1;
197
+ }
198
+ baseIndentString = source_text.substring(0, preindent_index);
199
+ js_source_text = source_text.substring(preindent_index);
200
+ }
201
+
202
+
203
+ var singleIndent = new Array(indentSize + 1).join(indentCharacter);
204
+ var indentLevel;
205
+ var nestedLevel;
206
+ var output;
207
+
208
+ function print_string(output_string) {
209
+ if (output.just_added_newline()) {
210
+ output.set_indent(indentLevel);
211
+ }
212
+ output.add_token(output_string);
213
+ }
214
+
215
+ function preserveSingleSpace(isAfterSpace) {
216
+ if (isAfterSpace) {
217
+ output.space_before_token = true;
218
+ }
219
+ }
220
+
221
+ function indent() {
222
+ indentLevel++;
223
+ }
224
+
225
+ function outdent() {
226
+ if (indentLevel > 0) {
227
+ indentLevel--;
228
+ }
229
+ }
230
+
231
+ /*_____________________--------------------_____________________*/
232
+
233
+ this.beautify = function() {
234
+ // reset
235
+ output = new Output(singleIndent, baseIndentString);
236
+ indentLevel = 0;
237
+ nestedLevel = 0;
238
+
239
+ pos = -1;
240
+ ch = null;
241
+ parenLevel = 0;
242
+
243
+ var insideRule = false;
244
+ var insidePropertyValue = false;
245
+ var enteringConditionalGroup = false;
246
+ var top_ch = '';
247
+ var last_top_ch = '';
248
+
249
+ while (true) {
250
+ var whitespace = skipWhitespace();
251
+ var isAfterSpace = whitespace !== '';
252
+ var isAfterNewline = whitespace.indexOf('\n') !== -1;
253
+ last_top_ch = top_ch;
254
+ top_ch = ch;
255
+
256
+ if (!ch) {
257
+ break;
258
+ } else if (ch === '/' && peek() === '*') { /* css comment */
259
+ var header = indentLevel === 0;
260
+
261
+ if (isAfterNewline || header) {
262
+ output.add_new_line();
263
+ }
264
+
265
+ print_string(eatComment());
266
+ output.add_new_line();
267
+ if (header) {
268
+ output.add_new_line(true);
269
+ }
270
+ } else if (ch === '/' && peek() === '/') { // single line comment
271
+ if (!isAfterNewline && last_top_ch !== '{') {
272
+ output.trim(true);
273
+ }
274
+ output.space_before_token = true;
275
+ print_string(eatComment());
276
+ output.add_new_line();
277
+ } else if (ch === '@') {
278
+ preserveSingleSpace(isAfterSpace);
279
+
280
+ // deal with less propery mixins @{...}
281
+ if (peek() === '{') {
282
+ print_string(eatString('}'));
283
+ } else {
284
+ print_string(ch);
285
+
286
+ // strip trailing space, if present, for hash property checks
287
+ var variableOrRule = peekString(": ,;{}()[]/='\"");
288
+
289
+ if (variableOrRule.match(/[ :]$/)) {
290
+ // we have a variable or pseudo-class, add it and insert one space before continuing
291
+ next();
292
+ variableOrRule = eatString(": ").replace(/\s$/, '');
293
+ print_string(variableOrRule);
294
+ output.space_before_token = true;
295
+ }
296
+
297
+ variableOrRule = variableOrRule.replace(/\s$/, '');
298
+
299
+ // might be a nesting at-rule
300
+ if (variableOrRule in this.NESTED_AT_RULE) {
301
+ nestedLevel += 1;
302
+ if (variableOrRule in this.CONDITIONAL_GROUP_RULE) {
303
+ enteringConditionalGroup = true;
304
+ }
305
+ }
306
+ }
307
+ } else if (ch === '#' && peek() === '{') {
308
+ preserveSingleSpace(isAfterSpace);
309
+ print_string(eatString('}'));
310
+ } else if (ch === '{') {
311
+ if (peek(true) === '}') {
312
+ eatWhitespace();
313
+ next();
314
+ output.space_before_token = true;
315
+ print_string("{}");
316
+ if (!eatWhitespace(true)) {
317
+ output.add_new_line();
318
+ }
319
+
320
+ if (newlinesFromLastWSEat < 2 && newline_between_rules && indentLevel === 0) {
321
+ output.add_new_line(true);
322
+ }
323
+ } else {
324
+ indent();
325
+ output.space_before_token = true;
326
+ print_string(ch);
327
+ if (!eatWhitespace(true)) {
328
+ output.add_new_line();
329
+ }
330
+
331
+ // when entering conditional groups, only rulesets are allowed
332
+ if (enteringConditionalGroup) {
333
+ enteringConditionalGroup = false;
334
+ insideRule = (indentLevel > nestedLevel);
335
+ } else {
336
+ // otherwise, declarations are also allowed
337
+ insideRule = (indentLevel >= nestedLevel);
338
+ }
339
+ }
340
+ } else if (ch === '}') {
341
+ outdent();
342
+ output.add_new_line();
343
+ print_string(ch);
344
+ insideRule = false;
345
+ insidePropertyValue = false;
346
+ if (nestedLevel) {
347
+ nestedLevel--;
348
+ }
349
+
350
+ if (!eatWhitespace(true)) {
351
+ output.add_new_line();
352
+ }
353
+
354
+ if (newlinesFromLastWSEat < 2 && newline_between_rules && indentLevel === 0) {
355
+ output.add_new_line(true);
356
+ }
357
+ } else if (ch === ":") {
358
+ eatWhitespace();
359
+ if ((insideRule || enteringConditionalGroup) &&
360
+ !(lookBack("&") || foundNestedPseudoClass()) &&
361
+ !lookBack("(")) {
362
+ // 'property: value' delimiter
363
+ // which could be in a conditional group query
364
+ print_string(':');
365
+ if (!insidePropertyValue) {
366
+ insidePropertyValue = true;
367
+ output.space_before_token = true;
368
+ }
369
+ } else {
370
+ // sass/less parent reference don't use a space
371
+ // sass nested pseudo-class don't use a space
372
+
373
+ // preserve space before pseudoclasses/pseudoelements, as it means "in any child"
374
+ if (lookBack(" ")) {
375
+ output.space_before_token = true;
376
+ }
377
+ if (peek() === ":") {
378
+ // pseudo-element
379
+ next();
380
+ print_string("::");
381
+ } else {
382
+ // pseudo-class
383
+ print_string(':');
384
+ }
385
+ }
386
+ } else if (ch === '"' || ch === '\'') {
387
+ preserveSingleSpace(isAfterSpace);
388
+ print_string(eatString(ch));
389
+ } else if (ch === ';') {
390
+ insidePropertyValue = false;
391
+ print_string(ch);
392
+ if (!eatWhitespace(true)) {
393
+ output.add_new_line();
394
+ }
395
+ } else if (ch === '(') { // may be a url
396
+ if (lookBack("url")) {
397
+ print_string(ch);
398
+ eatWhitespace();
399
+ if (next()) {
400
+ if (ch !== ')' && ch !== '"' && ch !== '\'') {
401
+ print_string(eatString(')'));
402
+ } else {
403
+ pos--;
404
+ }
405
+ }
406
+ } else {
407
+ parenLevel++;
408
+ preserveSingleSpace(isAfterSpace);
409
+ print_string(ch);
410
+ eatWhitespace();
411
+ }
412
+ } else if (ch === ')') {
413
+ print_string(ch);
414
+ parenLevel--;
415
+ } else if (ch === ',') {
416
+ print_string(ch);
417
+ if (!eatWhitespace(true) && selectorSeparatorNewline && !insidePropertyValue && parenLevel < 1) {
418
+ output.add_new_line();
419
+ } else {
420
+ output.space_before_token = true;
421
+ }
422
+ } else if ((ch === '>' || ch === '+' || ch === '~') &&
423
+ !insidePropertyValue && parenLevel < 1) {
424
+ //handle combinator spacing
425
+ if (space_around_combinator) {
426
+ output.space_before_token = true;
427
+ print_string(ch);
428
+ output.space_before_token = true;
429
+ } else {
430
+ print_string(ch);
431
+ eatWhitespace();
432
+ // squash extra whitespace
433
+ if (ch && whiteRe.test(ch)) {
434
+ ch = '';
435
+ }
436
+ }
437
+ } else if (ch === ']') {
438
+ print_string(ch);
439
+ } else if (ch === '[') {
440
+ preserveSingleSpace(isAfterSpace);
441
+ print_string(ch);
442
+ } else if (ch === '=') { // no whitespace before or after
443
+ eatWhitespace();
444
+ print_string('=');
445
+ if (whiteRe.test(ch)) {
446
+ ch = '';
447
+ }
448
+
449
+ } else {
450
+ preserveSingleSpace(isAfterSpace);
451
+ print_string(ch);
452
+ }
453
+ }
454
+
455
+ var sweetCode = output.get_code(end_with_newline, eol);
456
+
457
+ return sweetCode;
458
+ };
459
+
460
+ // https://developer.mozilla.org/en-US/docs/Web/CSS/At-rule
461
+ this.NESTED_AT_RULE = {
462
+ "@page": true,
463
+ "@font-face": true,
464
+ "@keyframes": true,
465
+ // also in CONDITIONAL_GROUP_RULE below
466
+ "@media": true,
467
+ "@supports": true,
468
+ "@document": true
469
+ };
470
+ this.CONDITIONAL_GROUP_RULE = {
471
+ "@media": true,
472
+ "@supports": true,
473
+ "@document": true
474
+ };
475
+ }
476
+
477
+ module.exports.Beautifier = Beautifier;
@@ -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 css_beautify(source_text, options) {
32
+ var beautifier = new Beautifier(source_text, options);
33
+ return beautifier.beautify();
34
+ }
35
+
36
+ module.exports = css_beautify;