js-beautify 1.14.10 → 1.15.0

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/README.md CHANGED
@@ -1,10 +1,10 @@
1
- <p align="center"><img src="https://raw.githubusercontent.com/beautify-web/js-beautify/7db71fc/web/wordmark-light.svg" height="200px" align="center" alt="JS Beautifier"/></p>
1
+ <p align="center"><img src="https://raw.githubusercontent.com/beautifier/js-beautify/7db71fc/web/wordmark-light.svg" height="200px" align="center" alt="JS Beautifier"/></p>
2
2
 
3
- <p align="center"><a href="https://github.com/beautify-web/js-beautify/actions/workflows/main.yml"><img alt="CI" src="https://github.com/beautify-web/js-beautify/workflows/CI/badge.svg"/></a>&#32;<a href="https://greenkeeper.io/" target="_blank"><img alt="Greenkeeper badge" src="https://badges.greenkeeper.io/beautify-web/js-beautify.svg"/></a></p>
3
+ <p align="center"><a href="https://github.com/beautifier/js-beautify/actions/workflows/main.yml"><img alt="CI" src="https://github.com/beautifier/js-beautify/workflows/CI/badge.svg"/></a>&#32;<a href="https://greenkeeper.io/" target="_blank"><img alt="Greenkeeper badge" src="https://badges.greenkeeper.io/beautifier/js-beautify.svg"/></a></p>
4
4
 
5
5
  <p align="center"><a href="https://pypi.python.org/pypi/jsbeautifier" target="_blank"><img alt="PyPI version" src="https://img.shields.io/pypi/v/jsbeautifier.svg"/></a>&#32;<a href="https://cdnjs.com/libraries/js-beautify" target="_blank"><img alt="CDNJS version" src="https://img.shields.io/cdnjs/v/js-beautify.svg"/></a>&#32;<a href="https://www.npmjs.com/package/js-beautify" target="_blank"><img alt="NPM @latest" src="https://img.shields.io/npm/v/js-beautify.svg"/></a>&#32;<a href="https://www.npmjs.com/package/js-beautify?activeTab=versions" target="_blank"><img alt="NPM @next" src="https://img.shields.io/npm/v/js-beautify/next.svg"/></a></p>
6
6
 
7
- <p align="center"><a href="https://gitter.im/beautify-web/js-beautify?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge" target="_blank"><img alt="Join the chat at https://gitter.im/beautify-web/js-beautify" src="https://badges.gitter.im/Join%20Chat.svg"></a>&#32;<a href="https://twitter.com/intent/user?screen_name=js_beautifier" target="_blank"><img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/js_beautifier.svg?style=social&label=Follow"/></a></p>
7
+ <p align="center"><a href="https://gitter.im/beautifier/js-beautify?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge" target="_blank"><img alt="Join the chat at https://gitter.im/beautifier/js-beautify" src="https://badges.gitter.im/Join%20Chat.svg"></a>&#32;<a href="https://twitter.com/intent/user?screen_name=js_beautifier" target="_blank"><img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/js_beautifier.svg?style=social&label=Follow"/></a></p>
8
8
 
9
9
  <p align="center"><a href="https://www.npmjs.org/package/js-beautify" target="_blank"><img alt="NPM stats" src=https://nodei.co/npm/js-beautify.svg?downloadRank=true&downloads=true"/></a></p>
10
10
 
@@ -21,7 +21,7 @@ I'm putting this front and center above because existing owners have very limite
21
21
  This is a popular project and widely used but it desperately needs contributors who have time to commit to fixing both
22
22
  customer facing bugs and underlying problems with the internal design and implementation.
23
23
 
24
- If you are interested, please take a look at the [CONTRIBUTING.md](https://github.com/beautify-web/js-beautify/blob/main/CONTRIBUTING.md) then fix an issue marked with the ["Good first issue"](https://github.com/beautify-web/js-beautify/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) label and submit a PR. Repeat as often as possible. Thanks!
24
+ If you are interested, please take a look at the [CONTRIBUTING.md](https://github.com/beautifier/js-beautify/blob/main/CONTRIBUTING.md) then fix an issue marked with the ["Good first issue"](https://github.com/beautifier/js-beautify/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) label and submit a PR. Repeat as often as possible. Thanks!
25
25
 
26
26
 
27
27
  # Installation
@@ -58,13 +58,13 @@ JS Beautifier is hosted on two CDN services: [cdnjs](https://cdnjs.com/libraries
58
58
 
59
59
  To pull the latest version from one of these services include one set of the script tags below in your document:
60
60
  ```html
61
- <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.10/beautify.js"></script>
62
- <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.10/beautify-css.js"></script>
63
- <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.10/beautify-html.js"></script>
61
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.15.0/beautify.js"></script>
62
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.15.0/beautify-css.js"></script>
63
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.15.0/beautify-html.js"></script>
64
64
 
65
- <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.10/beautify.min.js"></script>
66
- <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.10/beautify-css.min.js"></script>
67
- <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.10/beautify-html.min.js"></script>
65
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.15.0/beautify.min.js"></script>
66
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.15.0/beautify-css.min.js"></script>
67
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.15.0/beautify-html.min.js"></script>
68
68
  ```
69
69
 
70
70
  Example usage of a JS tag in html:
@@ -76,7 +76,7 @@ Example usage of a JS tag in html:
76
76
 
77
77
  . . .
78
78
 
79
- <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.10/beautify.min.js"></script>
79
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.15.0/beautify.min.js"></script>
80
80
  <script src="script.js"></script>
81
81
  </body>
82
82
  </html>
@@ -223,7 +223,7 @@ Beautifier Options:
223
223
  -C, --comma-first Put commas at the beginning of new line instead of end
224
224
  -O, --operator-position Set operator position (before-newline|after-newline|preserve-newline) [before-newline]
225
225
  --indent-empty-lines Keep indentation on empty lines
226
- --templating List of templating languages (auto,django,erb,handlebars,php,smarty) ["auto"] auto = none in JavaScript, all in HTML
226
+ --templating List of templating languages (auto,django,erb,handlebars,php,smarty,angular) ["auto"] auto = none in JavaScript, all in HTML
227
227
  ```
228
228
 
229
229
  Which correspond to the underscored option keys for both library interfaces
@@ -379,7 +379,7 @@ HTML Beautifier Options:
379
379
  --indent_scripts Sets indent level inside script tags ("normal", "keep", "separate")
380
380
  --unformatted_content_delimiter Keep text content together between this string [""]
381
381
  --indent-empty-lines Keep indentation on empty lines
382
- --templating List of templating languages (auto,none,django,erb,handlebars,php,smarty) ["auto"] auto = none in JavaScript, all in html
382
+ --templating List of templating languages (auto,none,django,erb,handlebars,php,smarty,angular) ["auto"] auto = none in JavaScript, all in html
383
383
  ```
384
384
 
385
385
  ## Directives
@@ -434,4 +434,4 @@ Thanks also to Jason Diamond, Patrick Hof, Nochum Sossonko, Andreas Schneider, D
434
434
  Vasilevsky, Vital Batmanov, Ron Baldwin, Gabriel Harrison, Chris J. Shull,
435
435
  Mathias Bynens, Vittorio Gambaletta and others.
436
436
 
437
- (README.md: js-beautify@1.14.10)
437
+ (README.md: js-beautify@1.15.0)
@@ -2315,10 +2315,10 @@ function Options(options, merge_child_field) {
2315
2315
 
2316
2316
  this.indent_empty_lines = this._get_boolean('indent_empty_lines');
2317
2317
 
2318
- // valid templating languages ['django', 'erb', 'handlebars', 'php', 'smarty']
2318
+ // valid templating languages ['django', 'erb', 'handlebars', 'php', 'smarty', 'angular']
2319
2319
  // For now, 'auto' = all off for javascript, all on for html (and inline javascript).
2320
2320
  // other values ignored
2321
- this.templating = this._get_selection_list('templating', ['auto', 'none', 'django', 'erb', 'handlebars', 'php', 'smarty'], ['auto']);
2321
+ this.templating = this._get_selection_list('templating', ['auto', 'none', 'django', 'erb', 'handlebars', 'php', 'smarty', 'angular'], ['auto']);
2322
2322
  }
2323
2323
 
2324
2324
  Options.prototype._get_array = function(name, default_value) {
@@ -3782,7 +3782,8 @@ var template_names = {
3782
3782
  erb: false,
3783
3783
  handlebars: false,
3784
3784
  php: false,
3785
- smarty: false
3785
+ smarty: false,
3786
+ angular: false
3786
3787
  };
3787
3788
 
3788
3789
  // This lets templates appear anywhere we would do a readUntil
@@ -4785,6 +4786,13 @@ Printer.prototype.indent = function() {
4785
4786
  this.indent_level++;
4786
4787
  };
4787
4788
 
4789
+ Printer.prototype.deindent = function() {
4790
+ if (this.indent_level > 0) {
4791
+ this.indent_level--;
4792
+ this._output.set_indent(this.indent_level, this.alignment_size);
4793
+ }
4794
+ };
4795
+
4788
4796
  Printer.prototype.get_full_indent = function(level) {
4789
4797
  level = this.indent_level + (level || 0);
4790
4798
  if (level < 1) {
@@ -4979,6 +4987,10 @@ Beautifier.prototype.beautify = function() {
4979
4987
  parser_token = this._handle_tag_close(printer, raw_token, last_tag_token);
4980
4988
  } else if (raw_token.type === TOKEN.TEXT) {
4981
4989
  parser_token = this._handle_text(printer, raw_token, last_tag_token);
4990
+ } else if (raw_token.type === TOKEN.CONTROL_FLOW_OPEN) {
4991
+ parser_token = this._handle_control_flow_open(printer, raw_token);
4992
+ } else if (raw_token.type === TOKEN.CONTROL_FLOW_CLOSE) {
4993
+ parser_token = this._handle_control_flow_close(printer, raw_token);
4982
4994
  } else {
4983
4995
  // This should never happen, but if it does. Print the raw token
4984
4996
  printer.add_raw_token(raw_token);
@@ -4993,6 +5005,38 @@ Beautifier.prototype.beautify = function() {
4993
5005
  return sweet_code;
4994
5006
  };
4995
5007
 
5008
+ Beautifier.prototype._handle_control_flow_open = function(printer, raw_token) {
5009
+ var parser_token = {
5010
+ text: raw_token.text,
5011
+ type: raw_token.type
5012
+ };
5013
+ printer.set_space_before_token(raw_token.newlines || raw_token.whitespace_before !== '', true);
5014
+ if (raw_token.newlines) {
5015
+ printer.print_preserved_newlines(raw_token);
5016
+ } else {
5017
+ printer.set_space_before_token(raw_token.newlines || raw_token.whitespace_before !== '', true);
5018
+ }
5019
+ printer.print_token(raw_token);
5020
+ printer.indent();
5021
+ return parser_token;
5022
+ };
5023
+
5024
+ Beautifier.prototype._handle_control_flow_close = function(printer, raw_token) {
5025
+ var parser_token = {
5026
+ text: raw_token.text,
5027
+ type: raw_token.type
5028
+ };
5029
+
5030
+ printer.deindent();
5031
+ if (raw_token.newlines) {
5032
+ printer.print_preserved_newlines(raw_token);
5033
+ } else {
5034
+ printer.set_space_before_token(raw_token.newlines || raw_token.whitespace_before !== '', true);
5035
+ }
5036
+ printer.print_token(raw_token);
5037
+ return parser_token;
5038
+ };
5039
+
4996
5040
  Beautifier.prototype._handle_tag_close = function(printer, raw_token, last_tag_token) {
4997
5041
  var parser_token = {
4998
5042
  text: raw_token.text,
@@ -5589,7 +5633,7 @@ var BaseOptions = (__webpack_require__(7).Options);
5589
5633
  function Options(options) {
5590
5634
  BaseOptions.call(this, options, 'html');
5591
5635
  if (this.templating.length === 1 && this.templating[0] === 'auto') {
5592
- this.templating = ['django', 'erb', 'handlebars', 'php'];
5636
+ this.templating = ['django', 'erb', 'handlebars', 'php', 'angular'];
5593
5637
  }
5594
5638
 
5595
5639
  this.indent_inner_html = this._get_boolean('indent_inner_html');
@@ -5692,6 +5736,8 @@ var Pattern = (__webpack_require__(13).Pattern);
5692
5736
  var TOKEN = {
5693
5737
  TAG_OPEN: 'TK_TAG_OPEN',
5694
5738
  TAG_CLOSE: 'TK_TAG_CLOSE',
5739
+ CONTROL_FLOW_OPEN: 'TK_CONTROL_FLOW_OPEN',
5740
+ CONTROL_FLOW_CLOSE: 'TK_CONTROL_FLOW_CLOSE',
5695
5741
  ATTRIBUTE: 'TK_ATTRIBUTE',
5696
5742
  EQUALS: 'TK_EQUALS',
5697
5743
  VALUE: 'TK_VALUE',
@@ -5716,11 +5762,13 @@ var Tokenizer = function(input_string, options) {
5716
5762
 
5717
5763
  this.__patterns = {
5718
5764
  word: templatable_reader.until(/[\n\r\t <]/),
5765
+ word_control_flow_close_excluded: templatable_reader.until(/[\n\r\t <}]/),
5719
5766
  single_quote: templatable_reader.until_after(/'/),
5720
5767
  double_quote: templatable_reader.until_after(/"/),
5721
5768
  attribute: templatable_reader.until(/[\n\r\t =>]|\/>/),
5722
5769
  element_name: templatable_reader.until(/[\n\r\t >\/]/),
5723
5770
 
5771
+ angular_control_flow_start: pattern_reader.matching(/\@[a-zA-Z]+[^({]*[({]/),
5724
5772
  handlebars_comment: pattern_reader.starting_with(/{{!--/).until_after(/--}}/),
5725
5773
  handlebars: pattern_reader.starting_with(/{{/).until_after(/}}/),
5726
5774
  handlebars_open: pattern_reader.until(/[\n\r\t }]/),
@@ -5734,6 +5782,7 @@ var Tokenizer = function(input_string, options) {
5734
5782
 
5735
5783
  if (this._options.indent_handlebars) {
5736
5784
  this.__patterns.word = this.__patterns.word.exclude('handlebars');
5785
+ this.__patterns.word_control_flow_close_excluded = this.__patterns.word_control_flow_close_excluded.exclude('handlebars');
5737
5786
  }
5738
5787
 
5739
5788
  this._unformatted_content_delimiter = null;
@@ -5752,14 +5801,16 @@ Tokenizer.prototype._is_comment = function(current_token) { // jshint unused:fal
5752
5801
  };
5753
5802
 
5754
5803
  Tokenizer.prototype._is_opening = function(current_token) {
5755
- return current_token.type === TOKEN.TAG_OPEN;
5804
+ return current_token.type === TOKEN.TAG_OPEN || current_token.type === TOKEN.CONTROL_FLOW_OPEN;
5756
5805
  };
5757
5806
 
5758
5807
  Tokenizer.prototype._is_closing = function(current_token, open_token) {
5759
- return current_token.type === TOKEN.TAG_CLOSE &&
5808
+ return (current_token.type === TOKEN.TAG_CLOSE &&
5760
5809
  (open_token && (
5761
5810
  ((current_token.text === '>' || current_token.text === '/>') && open_token.text[0] === '<') ||
5762
- (current_token.text === '}}' && open_token.text[0] === '{' && open_token.text[1] === '{')));
5811
+ (current_token.text === '}}' && open_token.text[0] === '{' && open_token.text[1] === '{')))
5812
+ ) || (current_token.type === TOKEN.CONTROL_FLOW_CLOSE &&
5813
+ (current_token.text === '}' && open_token.text.endsWith('{')));
5763
5814
  };
5764
5815
 
5765
5816
  Tokenizer.prototype._reset = function() {
@@ -5778,8 +5829,9 @@ Tokenizer.prototype._get_next_token = function(previous_token, open_token) { //
5778
5829
  token = token || this._read_open_handlebars(c, open_token);
5779
5830
  token = token || this._read_attribute(c, previous_token, open_token);
5780
5831
  token = token || this._read_close(c, open_token);
5832
+ token = token || this._read_control_flows(c, open_token);
5781
5833
  token = token || this._read_raw_content(c, previous_token, open_token);
5782
- token = token || this._read_content_word(c);
5834
+ token = token || this._read_content_word(c, open_token);
5783
5835
  token = token || this._read_comment_or_cdata(c);
5784
5836
  token = token || this._read_processing(c);
5785
5837
  token = token || this._read_open(c, open_token);
@@ -5844,7 +5896,7 @@ Tokenizer.prototype._read_processing = function(c) { // jshint unused:false
5844
5896
  Tokenizer.prototype._read_open = function(c, open_token) {
5845
5897
  var resulting_string = null;
5846
5898
  var token = null;
5847
- if (!open_token) {
5899
+ if (!open_token || open_token.type === TOKEN.CONTROL_FLOW_OPEN) {
5848
5900
  if (c === '<') {
5849
5901
 
5850
5902
  resulting_string = this._input.next();
@@ -5861,7 +5913,7 @@ Tokenizer.prototype._read_open = function(c, open_token) {
5861
5913
  Tokenizer.prototype._read_open_handlebars = function(c, open_token) {
5862
5914
  var resulting_string = null;
5863
5915
  var token = null;
5864
- if (!open_token) {
5916
+ if (!open_token || open_token.type === TOKEN.CONTROL_FLOW_OPEN) {
5865
5917
  if (this._options.indent_handlebars && c === '{' && this._input.peek(1) === '{') {
5866
5918
  if (this._input.peek(2) === '!') {
5867
5919
  resulting_string = this.__patterns.handlebars_comment.read();
@@ -5876,11 +5928,48 @@ Tokenizer.prototype._read_open_handlebars = function(c, open_token) {
5876
5928
  return token;
5877
5929
  };
5878
5930
 
5931
+ Tokenizer.prototype._read_control_flows = function(c, open_token) {
5932
+ var resulting_string = '';
5933
+ var token = null;
5934
+ // Only check for control flows if angular templating is set AND indenting is set
5935
+ if (!this._options.templating.includes('angular') || !this._options.indent_handlebars) {
5936
+ return token;
5937
+ }
5938
+
5939
+ if (c === '@') {
5940
+ resulting_string = this.__patterns.angular_control_flow_start.read();
5941
+ if (resulting_string === '') {
5942
+ return token;
5943
+ }
5944
+
5945
+ var opening_parentheses_count = resulting_string.endsWith('(') ? 1 : 0;
5946
+ var closing_parentheses_count = 0;
5947
+ // The opening brace of the control flow is where the number of opening and closing parentheses equal
5948
+ // e.g. @if({value: true} !== null) {
5949
+ while (!(resulting_string.endsWith('{') && opening_parentheses_count === closing_parentheses_count)) {
5950
+ var next_char = this._input.next();
5951
+ if (next_char === null) {
5952
+ break;
5953
+ } else if (next_char === '(') {
5954
+ opening_parentheses_count++;
5955
+ } else if (next_char === ')') {
5956
+ closing_parentheses_count++;
5957
+ }
5958
+ resulting_string += next_char;
5959
+ }
5960
+ token = this._create_token(TOKEN.CONTROL_FLOW_OPEN, resulting_string);
5961
+ } else if (c === '}' && open_token && open_token.type === TOKEN.CONTROL_FLOW_OPEN) {
5962
+ resulting_string = this._input.next();
5963
+ token = this._create_token(TOKEN.CONTROL_FLOW_CLOSE, resulting_string);
5964
+ }
5965
+ return token;
5966
+ };
5967
+
5879
5968
 
5880
5969
  Tokenizer.prototype._read_close = function(c, open_token) {
5881
5970
  var resulting_string = null;
5882
5971
  var token = null;
5883
- if (open_token) {
5972
+ if (open_token && open_token.type === TOKEN.TAG_OPEN) {
5884
5973
  if (open_token.text[0] === '<' && (c === '>' || (c === '/' && this._input.peek(1) === '>'))) {
5885
5974
  resulting_string = this._input.next();
5886
5975
  if (c === '/') { // for close tag "/>"
@@ -5967,7 +6056,7 @@ Tokenizer.prototype._read_raw_content = function(c, previous_token, open_token)
5967
6056
  return null;
5968
6057
  };
5969
6058
 
5970
- Tokenizer.prototype._read_content_word = function(c) {
6059
+ Tokenizer.prototype._read_content_word = function(c, open_token) {
5971
6060
  var resulting_string = '';
5972
6061
  if (this._options.unformatted_content_delimiter) {
5973
6062
  if (c === this._options.unformatted_content_delimiter[0]) {
@@ -5976,7 +6065,7 @@ Tokenizer.prototype._read_content_word = function(c) {
5976
6065
  }
5977
6066
 
5978
6067
  if (!resulting_string) {
5979
- resulting_string = this.__patterns.word.read();
6068
+ resulting_string = (open_token && open_token.type === TOKEN.CONTROL_FLOW_OPEN) ? this.__patterns.word_control_flow_close_excluded.read() : this.__patterns.word.read();
5980
6069
  }
5981
6070
  if (resulting_string) {
5982
6071
  return this._create_token(TOKEN.TEXT, resulting_string);