Skip to content

Commit

Permalink
Merge branch 'master' into io-move
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed Sep 10, 2018
2 parents e287d16 + 7f36731 commit ee99c9b
Show file tree
Hide file tree
Showing 3 changed files with 296 additions and 23 deletions.
53 changes: 30 additions & 23 deletions js/src/html/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,25 @@ Printer.prototype.add_raw_token = function(token) {
this._output.add_raw_token(token);
};

Printer.prototype.traverse_whitespace = function(raw_token) {
if (raw_token.whitespace_before || raw_token.newlines) {
var newlines = 0;
Printer.prototype.print_preserved_newlines = function(raw_token) {
var newlines = 0;
if (raw_token.type !== TOKEN.TEXT && raw_token.previous.type !== TOKEN.TEXT) {
newlines = raw_token.newlines ? 1 : 0;
}

if (raw_token.type !== TOKEN.TEXT && raw_token.previous.type !== TOKEN.TEXT) {
newlines = raw_token.newlines ? 1 : 0;
}
if (this.preserve_newlines) {
newlines = raw_token.newlines < this.max_preserve_newlines + 1 ? raw_token.newlines : this.max_preserve_newlines + 1;
}
for (var n = 0; n < newlines; n++) {
this.print_newline(n > 0);
}

if (this.preserve_newlines) {
newlines = raw_token.newlines < this.max_preserve_newlines + 1 ? raw_token.newlines : this.max_preserve_newlines + 1;
}
return newlines !== 0;
};

if (newlines) {
for (var n = 0; n < newlines; n++) {
this.print_newline(n > 0);
}
} else {
Printer.prototype.traverse_whitespace = function(raw_token) {
if (raw_token.whitespace_before || raw_token.newlines) {
if (!this.print_preserved_newlines(raw_token)) {
this._output.space_before_token = true;
this.print_space_or_wrap(raw_token.text);
}
Expand Down Expand Up @@ -339,16 +341,21 @@ Beautifier.prototype._handle_inside_tag = function(printer, raw_token, last_tag_
printer.set_space_before_token(raw_token.newlines || raw_token.whitespace_before !== '');
if (last_tag_token.is_unformatted) {
printer.add_raw_token(raw_token);
} else if (last_tag_token.tag_start_char === '{' && raw_token.type === TOKEN.TEXT) {
// For the insides of handlebars allow newlines or a single space between open and contents
if (printer.print_preserved_newlines(raw_token)) {
printer.print_raw_text(raw_token.whitespace_before + raw_token.text);
} else {
printer.print_token(raw_token.text);
}
} else {
if (last_tag_token.tag_start_char === '<') {
if (raw_token.type === TOKEN.ATTRIBUTE) {
printer.set_space_before_token(true);
last_tag_token.attr_count += 1;
} else if (raw_token.type === TOKEN.EQUALS) { //no space before =
printer.set_space_before_token(false);
} else if (raw_token.type === TOKEN.VALUE && raw_token.previous.type === TOKEN.EQUALS) { //no space before value
printer.set_space_before_token(false);
}
if (raw_token.type === TOKEN.ATTRIBUTE) {
printer.set_space_before_token(true);
last_tag_token.attr_count += 1;
} else if (raw_token.type === TOKEN.EQUALS) { //no space before =
printer.set_space_before_token(false);
} else if (raw_token.type === TOKEN.VALUE && raw_token.previous.type === TOKEN.EQUALS) { //no space before value
printer.set_space_before_token(false);
}

if (printer._output.space_before_token && last_tag_token.tag_start_char === '<') {
Expand Down
226 changes: 226 additions & 0 deletions js/test/generated/beautify-html-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1859,6 +1859,22 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
'{{em-input label="Some Labe" property="amt" type="text" placeholder=""}}\n' +
'{{em-input label="Type*" property="type" type="text" placeholder="(LTD)"}}\n' +
'{{em-input label="Place*" property="place" type="text" placeholder=""}}');

// Issue #1469 - preserve newlines inside handlebars, including first one. Just treated as text here.
bth(
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
' {{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
' {{em-input label="Place*" property="place" type="text" placeholder=""}}',
// -- output --
'{{em-input\n' +
'label="Some Labe" property="amt"\n' +
'type="text" placeholder=""}}\n' +
'{{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
'{{em-input label="Place*" property="place" type="text" placeholder=""}}');
bth(
'{{#if callOn}}\n' +
'{{#unless callOn}}\n' +
Expand Down Expand Up @@ -1906,6 +1922,24 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
'{{ myHelper someValue}}\n' +
'{{field}}\n' +
'{{value-title}}');

// Issue #1469 - preserve newlines inside handlebars, including first one. BUG: does not fix indenting inside handlebars.
bth(
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'{{field}}\n' +
' {{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
' {{em-input label="Place*" property="place" type="text" placeholder=""}}',
// -- output --
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'{{field}}\n' +
'{{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
'{{em-input label="Place*" property="place" type="text" placeholder=""}}');
bth(
'{{em-input label="Some Labe" property="amt" type="text" placeholder=""}}\n' +
'{{field}}\n' +
Expand Down Expand Up @@ -2144,6 +2178,24 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
'{{ myHelper someValue}}\n' +
'{{em-input label="Some Labe" property="amt" type="text" placeholder=""}}\n' +
'{{value-title}}');

// Issue #1469 - preserve newlines inside handlebars, including first one. BUG: does not fix indenting inside handlebars.
bth(
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'{{em-input label="Some Labe" property="amt" type="text" placeholder=""}}\n' +
' {{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
' {{em-input label="Place*" property="place" type="text" placeholder=""}}',
// -- output --
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'{{em-input label="Some Labe" property="amt" type="text" placeholder=""}}\n' +
'{{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
'{{em-input label="Place*" property="place" type="text" placeholder=""}}');
bth(
'{{em-input label="Some Labe" property="amt" type="text" placeholder=""}}\n' +
'{{em-input label="Some Labe" property="amt" type="text" placeholder=""}}\n' +
Expand Down Expand Up @@ -2382,6 +2434,24 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
'{{ myHelper someValue}}\n' +
'{{! comment}}\n' +
'{{value-title}}');

// Issue #1469 - preserve newlines inside handlebars, including first one. BUG: does not fix indenting inside handlebars.
bth(
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'{{! comment}}\n' +
' {{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
' {{em-input label="Place*" property="place" type="text" placeholder=""}}',
// -- output --
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'{{! comment}}\n' +
'{{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
'{{em-input label="Place*" property="place" type="text" placeholder=""}}');
bth(
'{{em-input label="Some Labe" property="amt" type="text" placeholder=""}}\n' +
'{{! comment}}\n' +
Expand Down Expand Up @@ -2620,6 +2690,24 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
'{{ myHelper someValue}}\n' +
'{{!-- comment--}}\n' +
'{{value-title}}');

// Issue #1469 - preserve newlines inside handlebars, including first one. BUG: does not fix indenting inside handlebars.
bth(
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'{{!-- comment--}}\n' +
' {{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
' {{em-input label="Place*" property="place" type="text" placeholder=""}}',
// -- output --
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'{{!-- comment--}}\n' +
'{{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
'{{em-input label="Place*" property="place" type="text" placeholder=""}}');
bth(
'{{em-input label="Some Labe" property="amt" type="text" placeholder=""}}\n' +
'{{!-- comment--}}\n' +
Expand Down Expand Up @@ -2858,6 +2946,24 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
'{{ myHelper someValue}}\n' +
'{{Hello "woRld"}} {{!-- comment--}} {{heLloWorlD}}\n' +
'{{value-title}}');

// Issue #1469 - preserve newlines inside handlebars, including first one. BUG: does not fix indenting inside handlebars.
bth(
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'{{Hello "woRld"}} {{!-- comment--}} {{heLloWorlD}}\n' +
' {{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
' {{em-input label="Place*" property="place" type="text" placeholder=""}}',
// -- output --
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'{{Hello "woRld"}} {{!-- comment--}} {{heLloWorlD}}\n' +
'{{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
'{{em-input label="Place*" property="place" type="text" placeholder=""}}');
bth(
'{{em-input label="Some Labe" property="amt" type="text" placeholder=""}}\n' +
'{{Hello "woRld"}} {{!-- comment--}} {{heLloWorlD}}\n' +
Expand Down Expand Up @@ -3096,6 +3202,24 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
'{{ myHelper someValue}}\n' +
'{pre{{field1}} {{field2}} {{field3}}post\n' +
'{{value-title}}');

// Issue #1469 - preserve newlines inside handlebars, including first one. BUG: does not fix indenting inside handlebars.
bth(
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'{pre{{field1}} {{field2}} {{field3}}post\n' +
' {{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
' {{em-input label="Place*" property="place" type="text" placeholder=""}}',
// -- output --
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'{pre{{field1}} {{field2}} {{field3}}post\n' +
'{{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
'{{em-input label="Place*" property="place" type="text" placeholder=""}}');
bth(
'{{em-input label="Some Labe" property="amt" type="text" placeholder=""}}\n' +
'{pre{{field1}} {{field2}} {{field3}}post\n' +
Expand Down Expand Up @@ -3346,6 +3470,32 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
' with spacing\n' +
'}}\n' +
'{{value-title}}');

// Issue #1469 - preserve newlines inside handlebars, including first one. BUG: does not fix indenting inside handlebars.
bth(
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'{{! \n' +
' mult-line\n' +
'comment \n' +
' with spacing\n' +
'}}\n' +
' {{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
' {{em-input label="Place*" property="place" type="text" placeholder=""}}',
// -- output --
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'{{! \n' +
' mult-line\n' +
'comment \n' +
' with spacing\n' +
'}}\n' +
'{{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
'{{em-input label="Place*" property="place" type="text" placeholder=""}}');
bth(
'{{em-input label="Some Labe" property="amt" type="text" placeholder=""}}\n' +
'{{! \n' +
Expand Down Expand Up @@ -3755,6 +3905,32 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
' with spacing\n' +
'--}}\n' +
'{{value-title}}');

// Issue #1469 - preserve newlines inside handlebars, including first one. BUG: does not fix indenting inside handlebars.
bth(
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'{{!-- \n' +
' mult-line\n' +
'comment \n' +
' with spacing\n' +
'--}}\n' +
' {{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
' {{em-input label="Place*" property="place" type="text" placeholder=""}}',
// -- output --
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'{{!-- \n' +
' mult-line\n' +
'comment \n' +
' with spacing\n' +
'--}}\n' +
'{{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
'{{em-input label="Place*" property="place" type="text" placeholder=""}}');
bth(
'{{em-input label="Some Labe" property="amt" type="text" placeholder=""}}\n' +
'{{!-- \n' +
Expand Down Expand Up @@ -4173,6 +4349,38 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
' with spacing\n' +
' {{/ component}}--}}\n' +
'{{value-title}}');

// Issue #1469 - preserve newlines inside handlebars, including first one. BUG: does not fix indenting inside handlebars.
bth(
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'{{!-- \n' +
' mult-line\n' +
'comment \n' +
'{{#> component}}\n' +
' mult-line\n' +
'comment \n' +
' with spacing\n' +
' {{/ component}}--}}\n' +
' {{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
' {{em-input label="Place*" property="place" type="text" placeholder=""}}',
// -- output --
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'{{!-- \n' +
' mult-line\n' +
'comment \n' +
'{{#> component}}\n' +
' mult-line\n' +
'comment \n' +
' with spacing\n' +
' {{/ component}}--}}\n' +
'{{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
'{{em-input label="Place*" property="place" type="text" placeholder=""}}');
bth(
'{{em-input label="Some Labe" property="amt" type="text" placeholder=""}}\n' +
'{{!-- \n' +
Expand Down Expand Up @@ -4685,6 +4893,24 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
'{{ myHelper someValue}}\n' +
'content\n' +
'{{value-title}}');

// Issue #1469 - preserve newlines inside handlebars, including first one. BUG: does not fix indenting inside handlebars.
bth(
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'content\n' +
' {{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
' {{em-input label="Place*" property="place" type="text" placeholder=""}}',
// -- output --
'{{em-input\n' +
' label="Some Labe" property="amt"\n' +
' type="text" placeholder=""}}\n' +
'content\n' +
'{{em-input label="Type*"\n' +
'property="type" type="text" placeholder="(LTD)"}}\n' +
'{{em-input label="Place*" property="place" type="text" placeholder=""}}');
bth(
'{{em-input label="Some Labe" property="amt" type="text" placeholder=""}}\n' +
'content\n' +
Expand Down

0 comments on commit ee99c9b

Please sign in to comment.