Skip to content

Commit 6014e7a

Browse files
author
GitHub Action
committed
Release: 1.15.2
1 parent 99b86e4 commit 6014e7a

File tree

8 files changed

+129
-58
lines changed

8 files changed

+129
-58
lines changed

js/lib/beautifier.js

Lines changed: 35 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/lib/beautifier.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/lib/beautifier.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/lib/beautifier.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/lib/beautify-html.js

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,10 @@ TemplatablePattern.prototype.__set_templated_pattern = function() {
16151615
if (!this._disabled.handlebars) {
16161616
items.push(this.__patterns.handlebars._starting_pattern.source);
16171617
}
1618+
if (!this._disabled.angular) {
1619+
// Handlebars ('{{' and '}}') are also special tokens in Angular)
1620+
items.push(this.__patterns.handlebars._starting_pattern.source);
1621+
}
16181622
if (!this._disabled.erb) {
16191623
items.push(this.__patterns.erb._starting_pattern.source);
16201624
}
@@ -2040,7 +2044,7 @@ Beautifier.prototype.beautify = function() {
20402044
type: ''
20412045
};
20422046

2043-
var last_tag_token = new TagOpenParserToken();
2047+
var last_tag_token = new TagOpenParserToken(this._options);
20442048

20452049
var printer = new Printer(this._options, baseIndentString);
20462050
var tokens = new Tokenizer(source_text, this._options).tokenize();
@@ -2363,7 +2367,7 @@ Beautifier.prototype._handle_tag_open = function(printer, raw_token, last_tag_to
23632367
return parser_token;
23642368
};
23652369

2366-
var TagOpenParserToken = function(parent, raw_token) {
2370+
var TagOpenParserToken = function(options, parent, raw_token) {
23672371
this.parent = parent || null;
23682372
this.text = '';
23692373
this.type = 'TK_TAG_OPEN';
@@ -2430,13 +2434,14 @@ var TagOpenParserToken = function(parent, raw_token) {
24302434
}
24312435

24322436
// handlebars tags that don't start with # or ^ are single_tags, and so also start and end.
2437+
// if they start with # or ^, they are still considered single tags if indenting of handlebars is set to false
24332438
this.is_end_tag = this.is_end_tag ||
2434-
(this.tag_start_char === '{' && (this.text.length < 3 || (/[^#\^]/.test(this.text.charAt(handlebar_starts)))));
2439+
(this.tag_start_char === '{' && (!options.indent_handlebars || this.text.length < 3 || (/[^#\^]/.test(this.text.charAt(handlebar_starts)))));
24352440
}
24362441
};
24372442

24382443
Beautifier.prototype._get_tag_open_token = function(raw_token) { //function to get a full tag and parse its type
2439-
var parser_token = new TagOpenParserToken(this._tag_stack.get_parser_token(), raw_token);
2444+
var parser_token = new TagOpenParserToken(this._options, this._tag_stack.get_parser_token(), raw_token);
24402445

24412446
parser_token.alignment_size = this._options.wrap_attributes_indent_size;
24422447

@@ -2903,6 +2908,7 @@ Tokenizer.prototype._get_next_token = function(previous_token, open_token) { //
29032908
token = token || this._read_open_handlebars(c, open_token);
29042909
token = token || this._read_attribute(c, previous_token, open_token);
29052910
token = token || this._read_close(c, open_token);
2911+
token = token || this._read_script_and_style(c, previous_token);
29062912
token = token || this._read_control_flows(c, open_token);
29072913
token = token || this._read_raw_content(c, previous_token, open_token);
29082914
token = token || this._read_content_word(c, open_token);
@@ -2988,8 +2994,8 @@ Tokenizer.prototype._read_open_handlebars = function(c, open_token) {
29882994
var resulting_string = null;
29892995
var token = null;
29902996
if (!open_token || open_token.type === TOKEN.CONTROL_FLOW_OPEN) {
2991-
if (this._options.indent_handlebars && c === '{' && this._input.peek(1) === '{') {
2992-
if (this._input.peek(2) === '!') {
2997+
if ((this._options.templating.includes('angular') || this._options.indent_handlebars) && c === '{' && this._input.peek(1) === '{') {
2998+
if (this._options.indent_handlebars && this._input.peek(2) === '!') {
29932999
resulting_string = this.__patterns.handlebars_comment.read();
29943000
resulting_string = resulting_string || this.__patterns.handlebars.read();
29953001
token = this._create_token(TOKEN.COMMENT, resulting_string);
@@ -3005,8 +3011,8 @@ Tokenizer.prototype._read_open_handlebars = function(c, open_token) {
30053011
Tokenizer.prototype._read_control_flows = function(c, open_token) {
30063012
var resulting_string = '';
30073013
var token = null;
3008-
// Only check for control flows if angular templating is set AND indenting is set
3009-
if (!this._options.templating.includes('angular') || !this._options.indent_handlebars) {
3014+
// Only check for control flows if angular templating is set
3015+
if (!this._options.templating.includes('angular')) {
30103016
return token;
30113017
}
30123018

@@ -3099,14 +3105,29 @@ Tokenizer.prototype._is_content_unformatted = function(tag_name) {
30993105
this._options.unformatted.indexOf(tag_name) !== -1);
31003106
};
31013107

3102-
31033108
Tokenizer.prototype._read_raw_content = function(c, previous_token, open_token) { // jshint unused:false
31043109
var resulting_string = '';
31053110
if (open_token && open_token.text[0] === '{') {
31063111
resulting_string = this.__patterns.handlebars_raw_close.read();
31073112
} else if (previous_token.type === TOKEN.TAG_CLOSE &&
31083113
previous_token.opened.text[0] === '<' && previous_token.text[0] !== '/') {
31093114
// ^^ empty tag has no content
3115+
var tag_name = previous_token.opened.text.substr(1).toLowerCase();
3116+
if (this._is_content_unformatted(tag_name)) {
3117+
3118+
resulting_string = this._input.readUntil(new RegExp('</' + tag_name + '[\\n\\r\\t ]*?>', 'ig'));
3119+
}
3120+
}
3121+
3122+
if (resulting_string) {
3123+
return this._create_token(TOKEN.TEXT, resulting_string);
3124+
}
3125+
3126+
return null;
3127+
};
3128+
3129+
Tokenizer.prototype._read_script_and_style = function(c, previous_token) { // jshint unused:false
3130+
if (previous_token.type === TOKEN.TAG_CLOSE && previous_token.opened.text[0] === '<' && previous_token.text[0] !== '/') {
31103131
var tag_name = previous_token.opened.text.substr(1).toLowerCase();
31113132
if (tag_name === 'script' || tag_name === 'style') {
31123133
// Script and style tags are allowed to have comments wrapping their content
@@ -3116,17 +3137,12 @@ Tokenizer.prototype._read_raw_content = function(c, previous_token, open_token)
31163137
token.type = TOKEN.TEXT;
31173138
return token;
31183139
}
3119-
resulting_string = this._input.readUntil(new RegExp('</' + tag_name + '[\\n\\r\\t ]*?>', 'ig'));
3120-
} else if (this._is_content_unformatted(tag_name)) {
3121-
3122-
resulting_string = this._input.readUntil(new RegExp('</' + tag_name + '[\\n\\r\\t ]*?>', 'ig'));
3140+
var resulting_string = this._input.readUntil(new RegExp('</' + tag_name + '[\\n\\r\\t ]*?>', 'ig'));
3141+
if (resulting_string) {
3142+
return this._create_token(TOKEN.TEXT, resulting_string);
3143+
}
31233144
}
31243145
}
3125-
3126-
if (resulting_string) {
3127-
return this._create_token(TOKEN.TEXT, resulting_string);
3128-
}
3129-
31303146
return null;
31313147
};
31323148

@@ -3144,6 +3160,7 @@ Tokenizer.prototype._read_content_word = function(c, open_token) {
31443160
if (resulting_string) {
31453161
return this._create_token(TOKEN.TEXT, resulting_string);
31463162
}
3163+
return null;
31473164
};
31483165

31493166
module.exports.Tokenizer = Tokenizer;

js/lib/beautify.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3912,6 +3912,10 @@ TemplatablePattern.prototype.__set_templated_pattern = function() {
39123912
if (!this._disabled.handlebars) {
39133913
items.push(this.__patterns.handlebars._starting_pattern.source);
39143914
}
3915+
if (!this._disabled.angular) {
3916+
// Handlebars ('{{' and '}}') are also special tokens in Angular)
3917+
items.push(this.__patterns.handlebars._starting_pattern.source);
3918+
}
39153919
if (!this._disabled.erb) {
39163920
items.push(this.__patterns.erb._starting_pattern.source);
39173921
}

js/test/generated/beautify-html-tests.js

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9364,7 +9364,7 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
93649364
// Indenting angular control flow with default indent size
93659365
reset_options();
93669366
set_name('Indenting angular control flow with default indent size');
9367-
opts.templating = 'angular, handlebars';
9367+
opts.templating = 'angular';
93689368
bth(
93699369
'@if (a > b) {\n' +
93709370
'{{a}} is greater than {{b}}\n' +
@@ -9664,28 +9664,61 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
96649664
' max-width: 100%;\n' +
96659665
' }\n' +
96669666
' }\n' +
9667-
'</style>',
9668-
// -- output --
9667+
'</style>');
9668+
9669+
// CSS @media, the inside of <script> tag and control flows should be indented correctly
9670+
test_fragment(
9671+
'<head>\n' +
96699672
'<style type="text/css">\n' +
96709673
'@media only screen and (min-width:480px) {\n' +
9671-
' .mj-column-per-100\n' +
9672-
' {\n' +
9673-
' width:\n' +
9674-
' 100%\n' +
9675-
' !important;\n' +
9676-
' max-width:\n' +
9677-
' 100%;\n' +
9674+
'.mj-column-per-100 {\n' +
9675+
'width: 100% !important;\n' +
9676+
'max-width: 100%;\n' +
96789677
'}\n' +
9679-
' }\n' +
9680-
'</style>');
9678+
'}\n' +
9679+
'</style>\n' +
9680+
'<script>\n' +
9681+
'if(someExpression) {\n' +
9682+
'callFunc();\n' +
9683+
'}\n' +
9684+
'</script>\n' +
9685+
'</head>\n' +
9686+
'<body>\n' +
9687+
'<div>\n' +
9688+
'@if(someOtherExpression) {\n' +
9689+
'Text\n' +
9690+
'}\n' +
9691+
'</div>\n' +
9692+
'</body>',
9693+
// -- output --
9694+
'<head>\n' +
9695+
' <style type="text/css">\n' +
9696+
' @media only screen and (min-width:480px) {\n' +
9697+
' .mj-column-per-100 {\n' +
9698+
' width: 100% !important;\n' +
9699+
' max-width: 100%;\n' +
9700+
' }\n' +
9701+
' }\n' +
9702+
' </style>\n' +
9703+
' <script>\n' +
9704+
' if (someExpression) {\n' +
9705+
' callFunc();\n' +
9706+
' }\n' +
9707+
' </script>\n' +
9708+
'</head>\n' +
9709+
'<body>\n' +
9710+
' <div>\n' +
9711+
' @if(someOtherExpression) {\n' +
9712+
' Text\n' +
9713+
' }\n' +
9714+
' </div>\n' +
9715+
'</body>');
96819716

96829717

96839718
//============================================================
9684-
// No indenting for angular control flow should be done if indent_handlebars is false
9719+
// No indenting for angular control flow should be done if angular templating is not set
96859720
reset_options();
9686-
set_name('No indenting for angular control flow should be done if indent_handlebars is false');
9687-
opts.templating = 'angular, handlebars';
9688-
opts.indent_handlebars = false;
9721+
set_name('No indenting for angular control flow should be done if angular templating is not set');
96899722
bth(
96909723
'@if (a > b) {\n' +
96919724
'{{a}} is greater than {{b}}\n' +

python/jsbeautifier/tests/generated/tests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ def unicode_char(value):
8484
bt('"\\x41\\x42\\x43\\x01"', '"\\x41\\x42\\x43\\x01"')
8585
bt('"\\u2022"', '"\\u2022"')
8686
bt('"\\u{2022}"', '"\\u{2022}"')
87-
bt('a = /\s+/')
87+
bt('a = /\\s+/')
8888
#bt('a = /\\x41/','a = /A/')
89-
bt('"\\u2022";a = /\s+/;"\\x41\\x42\\x43\\x01".match(/\\x41/);','"\\u2022";\na = /\s+/;\n"\\x41\\x42\\x43\\x01".match(/\\x41/);')
89+
bt('"\\u2022";a = /\\s+/;"\\x41\\x42\\x43\\x01".match(/\\x41/);','"\\u2022";\na = /\\s+/;\n"\\x41\\x42\\x43\\x01".match(/\\x41/);')
9090

9191
test_fragment('"\\x41\\x42\\x01\\x43"')
9292
test_fragment('"\\x41\\x42\\u0001\\x43"')
@@ -110,7 +110,7 @@ def unicode_char(value):
110110
test_fragment('"\\u0072\\u016B\\u0137\\u012B\\u0074\\u0069\\u0073"', six.u('"\u0072\u016B\u0137\u012B\u0074\u0069\u0073"'))
111111
test_fragment('"\\u{0072}\\u{016B}\\u{110000}\\u{137}\\u012B\\x74\\u{0000069}\\u{073}"', six.u('"\u0072\u016B\\u{110000}\u0137\u012B\u0074\u0069\u0073"'))
112112

113-
bt('a = /\s+/')
113+
bt('a = /\\s+/')
114114
test_fragment('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff"',
115115
'"\\"\\\'", \'\\"\\\'\', "\\\\", \'\\\\\', "\\xff", "unicode \\u0000 \\" \\\' \\\\ ' + unicode_char(0xffff) + '"')
116116

0 commit comments

Comments
 (0)