Skip to content

Commit

Permalink
Merge pull request #2034 from zalsaedy/handlebars-1988
Browse files Browse the repository at this point in the history
[Issue #1988] Added support to recognize Handlebars block with whitespace control
  • Loading branch information
bitwiseman committed Apr 13, 2022
2 parents 73e57ee + eb8233c commit 4df0273
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
16 changes: 12 additions & 4 deletions js/src/html/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,11 @@ var TagOpenParserToken = function(parent, raw_token) {
tag_check_match = raw_token.text.match(/^<([^\s>]*)/);
this.tag_check = tag_check_match ? tag_check_match[1] : '';
} else {
tag_check_match = raw_token.text.match(/^{{(?:[\^]|#\*?)?([^\s}]+)/);
tag_check_match = raw_token.text.match(/^{{~?(?:[\^]|#\*?)?([^\s}]+)/);
this.tag_check = tag_check_match ? tag_check_match[1] : '';

// handle "{{#> myPartial}}
if (raw_token.text === '{{#>' && this.tag_check === '>' && raw_token.next !== null) {
// handle "{{#> myPartial}}" or "{{~#> myPartial}}"
if ((raw_token.text === '{{#>' || raw_token.text === '{{~#>') && this.tag_check === '>' && raw_token.next !== null) {
this.tag_check = raw_token.next.text.split(' ')[0];
}
}
Expand All @@ -626,9 +626,17 @@ var TagOpenParserToken = function(parent, raw_token) {
this.is_end_tag = !this.is_start_tag ||
(raw_token.closed && raw_token.closed.text === '/>');

// if whitespace handler ~ included (i.e. {{~#if true}}), handlebars tags start at pos 3 not pos 2
var handlebar_starts = 2;
if (this.tag_start_char === '{' && this.text.length >= 3) {
if (this.text.charAt(2) === '~') {
handlebar_starts = 3;
}
}

// handlebars tags that don't start with # or ^ are single_tags, and so also start and end.
this.is_end_tag = this.is_end_tag ||
(this.tag_start_char === '{' && (this.text.length < 3 || (/[^#\^]/.test(this.text.charAt(2)))));
(this.tag_start_char === '{' && (this.text.length < 3 || (/[^#\^]/.test(this.text.charAt(handlebar_starts)))));
}
};

Expand Down
63 changes: 63 additions & 0 deletions test/data/html/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3528,6 +3528,69 @@ exports.test_data = {
'</script>'
]
}]
}, {
name: "Recognize handlebars with whitespace control",
description: "Maintains handlebar properties even when whitespace control ~ is at the start of handlebar statements",
template: "^^^ $$$",
options: [
{ name: "indent_handlebars", value: "true" }
],
tests: [{
input: [
'{{#if true}}<div><div>',
'{{~#if true ~}}<p>true</p>{{/if}}',
'</div></div>{{/if}}'
],
output: [
'{{#if true}}',
' <div>',
' <div>',
' {{~#if true ~}}',
' <p>true</p>',
' {{/if}}',
' </div>',
' </div>',
'{{/if}}'
]
}, {
input: [
'{{~#*inline "MyInlinePartial"}}',
'{{MyIdentifier}}',
'{{/inline}}'
],
output: [
'{{~#*inline "MyInlinePartial"}}',
' {{MyIdentifier}}',
'{{/inline}}'
]
}, {
input: [
'{{~#> myPartial }}',
'<span>format correctly</span>',
'{{/myPartial}}'
],
output: [
'{{~#> myPartial }}',
' <span>format correctly</span>',
'{{/myPartial}}'
]
}, {
unchanged: [
'{{#if callOn}}',
' {{translate "onText"}}',
'{{~else if (eq callOn false)}}',
' {{translate "offText"}}',
'{{/if}}'
]
}, {
unchanged: [
'{{~#if callOn}}',
' {{translate "onText"}}',
'{{~else if (eq callOn false)}}',
' {{translate "offText"}}',
'{{/if}}'
]
}]
}, {
name: "New Test Suite"
}]
Expand Down

0 comments on commit 4df0273

Please sign in to comment.