Skip to content

Commit

Permalink
Add tests for fixing issue #2219
Browse files Browse the repository at this point in the history
  • Loading branch information
gergely-gyorgy-both committed Nov 15, 2023
1 parent 08ecdd3 commit 1ffd900
Show file tree
Hide file tree
Showing 4 changed files with 312 additions and 13 deletions.
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@

<!-- Codemirror from https://cdnjs.com/libraries/codemirror -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.48.4/codemirror.min.css" integrity="sha512-9VgBnmEYUpZ041Fz5LvsEsFlEzAlUw6Ku5uPhNSKgqY4DgM+g8BRoYvfbfDcTqs3BPsREBB3ccVVlkCSLZ5T+Q==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.48.4/theme/darcula.min.css" integrity="sha512-zIJ2sSi2TEonQc5q1RJCbqSNElihVM3cJvRyUDAmympMCz8sOWFeczVXrdKO0ENpX6EKD51C5pztzAPZ3d/d9A==" crossorigin="anonymous" referrerpolicy="no-referrer" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.48.4/codemirror.min.js" integrity="sha512-wQgeja/1fclPgUWIHnv/MVPCdiL56SK0P8zLgtpOMhUYtvpaZTr/Q/IV2gkvzEYvMc3ZY2PaxarEj0m3CkOtfw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.48.4/theme/darcula.min.css" integrity="sha512-zIJ2sSi2TEonQc5q1RJCbqSNElihVM3cJvRyUDAmympMCz8sOWFeczVXrdKO0ENpX6EKD51C5pztzAPZ3d/d9A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.48.4/codemirror.min.js" integrity="sha512-wQgeja/1fclPgUWIHnv/MVPCdiL56SK0P8zLgtpOMhUYtvpaZTr/Q/IV2gkvzEYvMc3ZY2PaxarEj0m3CkOtfw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<!-- Codemirror Modes -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.48.4/mode/xml/xml.min.js" integrity="sha512-KU6R9MYnyy7/YS6zdRhTpYZJqQmBSEywURvPS9yqLNEXVU/LSkVupFYzekqKmERPF6KQmg1cYlDg+pCOZu3sZQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Expand Down
10 changes: 5 additions & 5 deletions js/src/html/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Printer.prototype.indent = function() {
};

Printer.prototype.deindent = function() {
if (this.indent_level > 0 ) {
if (this.indent_level > 0) {
this.indent_level--;
this._output.set_indent(this.indent_level, this.alignment_size);
}
Expand Down Expand Up @@ -312,9 +312,9 @@ Beautifier.prototype.beautify = function() {
parser_token = this._handle_tag_close(printer, raw_token, last_tag_token);
} else if (raw_token.type === TOKEN.TEXT) {
parser_token = this._handle_text(printer, raw_token, last_tag_token);
} else if(raw_token.type === TOKEN.CONTROL_FLOW_OPEN) {
} else if (raw_token.type === TOKEN.CONTROL_FLOW_OPEN) {
parser_token = this._handle_control_flow_open(printer, raw_token);
} else if(raw_token.type === TOKEN.CONTROL_FLOW_CLOSE) {
} else if (raw_token.type === TOKEN.CONTROL_FLOW_CLOSE) {
parser_token = this._handle_control_flow_close(printer, raw_token);
} else {
// This should never happen, but if it does. Print the raw token
Expand All @@ -336,7 +336,7 @@ Beautifier.prototype._handle_control_flow_open = function(printer, raw_token) {
type: raw_token.type
};
printer.set_space_before_token(raw_token.newlines || raw_token.whitespace_before !== '', true);
if(raw_token.newlines) {
if (raw_token.newlines) {
printer.print_preserved_newlines(raw_token);
} else {
printer.set_space_before_token(raw_token.newlines || raw_token.whitespace_before !== '', true);
Expand All @@ -353,7 +353,7 @@ Beautifier.prototype._handle_control_flow_close = function(printer, raw_token) {
};

printer.deindent();
if(raw_token.newlines) {
if (raw_token.newlines) {
printer.print_preserved_newlines(raw_token);
} else {
printer.set_space_before_token(raw_token.newlines || raw_token.whitespace_before !== '', true);
Expand Down
14 changes: 7 additions & 7 deletions js/src/html/tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Tokenizer.prototype._is_closing = function(current_token, open_token) {
(open_token && (
((current_token.text === '>' || current_token.text === '/>') && open_token.text[0] === '<') ||
(current_token.text === '}}' && open_token.text[0] === '{' && open_token.text[1] === '{')))
) || (current_token.type === TOKEN.CONTROL_FLOW_CLOSE &&
) || (current_token.type === TOKEN.CONTROL_FLOW_CLOSE &&
(current_token.text === '}' && open_token.text.endsWith('{')));
};

Expand Down Expand Up @@ -226,11 +226,11 @@ Tokenizer.prototype._read_open_handlebars = function(c, open_token) {
return token;
};

Tokenizer.prototype._read_control_flows = function (c, open_token) {
Tokenizer.prototype._read_control_flows = function(c, open_token) {
var resulting_string = '';
var token = null;
// Only check for control flows if angular templating is set
if(!this._options.templating.includes('angular')) {
if (!this._options.templating.includes('angular')) {
return token;
}

Expand All @@ -239,13 +239,13 @@ Tokenizer.prototype._read_control_flows = function (c, open_token) {
var closing_parentheses_count = 0;
// The opening brace of the control flow is where the number of opening and closing parentheses equal
// e.g. @if({value: true} !== null) {
while(!(resulting_string.endsWith('{') && opening_parentheses_count === closing_parentheses_count)) {
while (!(resulting_string.endsWith('{') && opening_parentheses_count === closing_parentheses_count)) {
var next_char = this._input.next();
if(next_char === null) {
if (next_char === null) {
break;
} else if(next_char === '(') {
} else if (next_char === '(') {
opening_parentheses_count++;
} else if(next_char === ')') {
} else if (next_char === ')') {
closing_parentheses_count++;
}
resulting_string += next_char;
Expand Down
298 changes: 298 additions & 0 deletions test/data/html/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3839,6 +3839,304 @@ exports.test_data = {
'</span>'
]
}]
}, {
name: "Indenting angular control flow with indent size 2",
description: "https://github.com/beautify-web/js-beautify/issues/2219",
template: "^^^ $$$",
options: [
{ name: "templating", value: "'angular'" },
{ name: "indent_size", value: "2" }
],
tests: [{
input: [
'@if (a > b) {',
'{{a}} is greater than {{b}}',
'}',
'',
'@if (a > b) {',
'{{a}} is greater than {{b}}',
'} @else if (b > a) {',
'{{a}} is less than {{b}}',
'} @else {',
'{{a}} is equal to {{b}}',
'}',
'',
'@for (item of items; track item.name) {',
'<li> {{ item.name }} </li>',
'} @empty {',
'<li> There are no items. </li>',
'}',
'',
'@switch (condition) {',
'@case (caseA) { ',
'Case A.',
'}',
'@case (caseB) {',
'Case B.',
'}',
'@default {',
'Default case.',
'}',
'}'
],
output: [
'@if (a > b) {',
' {{a}} is greater than {{b}}',
'}',
'',
'@if (a > b) {',
' {{a}} is greater than {{b}}',
'} @else if (b > a) {',
' {{a}} is less than {{b}}',
'} @else {',
' {{a}} is equal to {{b}}',
'}',
'',
'@for (item of items; track item.name) {',
' <li> {{ item.name }} </li>',
'} @empty {',
' <li> There are no items. </li>',
'}',
'',
'@switch (condition) {',
' @case (caseA) {',
' Case A.',
' }',
' @case (caseB) {',
' Case B.',
' }',
' @default {',
' Default case.',
' }',
'}'
]
}]
}, {
name: "Indenting angular control flow with default indent size",
description: "https://github.com/beautify-web/js-beautify/issues/2219",
template: "^^^ $$$",
options: [
{ name: "templating", value: "'angular, handlebars'" }
],
tests: [{
input: [
'@if (a > b) {',
'{{a}} is greater than {{b}}',
'}',
'',
'@if (a > b) {',
'{{a}} is greater than {{b}}',
'} @else if (b > a) {',
'{{a}} is less than {{b}}',
'} @else {',
'{{a}} is equal to {{b}}',
'}',
'',
'@for (item of items; track item.name) {',
'<li> {{ item.name }} </li>',
'} @empty {',
'<li> There are no items. </li>',
'}',
'',
'@switch (condition) {',
'@case (caseA) { ',
'Case A.',
'}',
'@case (caseB) {',
'Case B.',
'}',
'@default {',
'Default case.',
'}',
'}'
],
output: [
'@if (a > b) {',
' {{a}} is greater than {{b}}',
'}',
'',
'@if (a > b) {',
' {{a}} is greater than {{b}}',
'} @else if (b > a) {',
' {{a}} is less than {{b}}',
'} @else {',
' {{a}} is equal to {{b}}',
'}',
'',
'@for (item of items; track item.name) {',
' <li> {{ item.name }} </li>',
'} @empty {',
' <li> There are no items. </li>',
'}',
'',
'@switch (condition) {',
' @case (caseA) {',
' Case A.',
' }',
' @case (caseB) {',
' Case B.',
' }',
' @default {',
' Default case.',
' }',
'}'
]
}, {
input: [
'@if (a > b) {',
' {{a}} is greater than {{b}}',
' }',
'',
' @if (a > b) {',
' {{a}} is greater than {{b}}',
' } @else if (b > a) {',
' {{a}} is less than {{b}}',
' } @else {',
' {{a}} is equal to {{b}}',
'}',
'',
' @for (item of items; track item.name) {',
' <li> {{ item.name }} </li>',
' } @empty {',
' <li> There are no items. </li>',
' }',
'',
' @switch (condition) {',
'@case (caseA) { ',
'Case A.',
' }',
' @case (caseB) {',
'Case B.',
' }',
' @default {',
'Default case.',
'}',
' }'
],
output: [
'@if (a > b) {',
' {{a}} is greater than {{b}}',
'}',
'',
'@if (a > b) {',
' {{a}} is greater than {{b}}',
'} @else if (b > a) {',
' {{a}} is less than {{b}}',
'} @else {',
' {{a}} is equal to {{b}}',
'}',
'',
'@for (item of items; track item.name) {',
' <li> {{ item.name }} </li>',
'} @empty {',
' <li> There are no items. </li>',
'}',
'',
'@switch (condition) {',
' @case (caseA) {',
' Case A.',
' }',
' @case (caseB) {',
' Case B.',
' }',
' @default {',
' Default case.',
' }',
'}'
]
}, {
input: [
'@if( {value: true}; as val) {',
'<div>{{val.value}}</div>',
'}'
],
output: [
'@if( {value: true}; as val) {',
' <div>{{val.value}}</div>',
'}'
]
}, {
input: [
'@if( {value: true}; as val) {',
'<div>',
'@defer {',
'{{val.value}}',
'}',
'</div>',
'}'
],
output: [
'@if( {value: true}; as val) {',
' <div>',
' @defer {',
' {{val.value}}',
' }',
' </div>',
'}'
]
}, {
unchanged: [
'<div> @if(true) { {{"{}" + " }"}} } </div>'
]
}, {
input: [
'<div>',
'@for (item of items; track item.id; let idx = $index, e = $even) {',
'Item #{{ idx }}: {{ item.name }}',
'<p>',
'Item #{{ idx }}: {{ item.name }}',
'</p>',
'}',
'</div>'
],
output: [
'<div>',
' @for (item of items; track item.id; let idx = $index, e = $even) {',
' Item #{{ idx }}: {{ item.name }}',
' <p>',
' Item #{{ idx }}: {{ item.name }}',
' </p>',
' }',
'</div>'
]
}, {
input: [
'<div>',
'@for (item of items; track item.id; let idx = $index, e = $even) {',
'{{{value: true} | json}}',
'<p>',
'Item #{{ idx }}: {{ item.name }}',
'</p>',
'{{ {value: true} }}',
'<div>',
'@if(true) {',
'{{ {value: true} }}',
' }',
'',
'Placeholder',
'</div>',
'}',
'</div>'
],
output: [
'<div>',
' @for (item of items; track item.id; let idx = $index, e = $even) {',
' {{{value: true} | json}}',
' <p>',
' Item #{{ idx }}: {{ item.name }}',
' </p>',
' {{ {value: true} }}',
' <div>',
' @if(true) {',
' {{ {value: true} }}',
' }',
'',
' Placeholder',
' </div>',
' }',
'</div>'
]
}]
}, {
name: "New Test Suite"
}]
Expand Down

0 comments on commit 1ffd900

Please sign in to comment.