Skip to content

Commit

Permalink
Fix selecting control flow closing brace if it is not preceded by whi…
Browse files Browse the repository at this point in the history
…tespace
  • Loading branch information
gergely-gyorgy-both committed Dec 4, 2023
1 parent 3d53f6d commit cc5429f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
8 changes: 5 additions & 3 deletions js/src/html/tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var Tokenizer = function(input_string, options) {

this.__patterns = {
word: templatable_reader.until(/[\n\r\t <]/),
word_control_flow_close_excluded: templatable_reader.until(/[\n\r\t <}]/),
single_quote: templatable_reader.until_after(/'/),
double_quote: templatable_reader.until_after(/"/),
attribute: templatable_reader.until(/[\n\r\t =>]|\/>/),
Expand All @@ -82,6 +83,7 @@ var Tokenizer = function(input_string, options) {

if (this._options.indent_handlebars) {
this.__patterns.word = this.__patterns.word.exclude('handlebars');
this.__patterns.word_control_flow_close_excluded = this.__patterns.word_control_flow_close_excluded.exclude('handlebars');
}

this._unformatted_content_delimiter = null;
Expand Down Expand Up @@ -130,7 +132,7 @@ Tokenizer.prototype._get_next_token = function(previous_token, open_token) { //
token = token || this._read_close(c, open_token);
token = token || this._read_control_flows(c, open_token);
token = token || this._read_raw_content(c, previous_token, open_token);
token = token || this._read_content_word(c);
token = token || this._read_content_word(c, open_token);
token = token || this._read_comment_or_cdata(c);
token = token || this._read_processing(c);
token = token || this._read_open(c, open_token);
Expand Down Expand Up @@ -355,7 +357,7 @@ Tokenizer.prototype._read_raw_content = function(c, previous_token, open_token)
return null;
};

Tokenizer.prototype._read_content_word = function(c) {
Tokenizer.prototype._read_content_word = function(c, open_token) {
var resulting_string = '';
if (this._options.unformatted_content_delimiter) {
if (c === this._options.unformatted_content_delimiter[0]) {
Expand All @@ -364,7 +366,7 @@ Tokenizer.prototype._read_content_word = function(c) {
}

if (!resulting_string) {
resulting_string = this.__patterns.word.read();
resulting_string = (open_token && open_token.type === TOKEN.CONTROL_FLOW_OPEN) ? this.__patterns.word_control_flow_close_excluded.read() : this.__patterns.word.read();
}
if (resulting_string) {
return this._create_token(TOKEN.TEXT, resulting_string);
Expand Down
24 changes: 24 additions & 0 deletions test/data/html/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4154,6 +4154,30 @@ exports.test_data = {
'</p>',
'}'
]
}, {
comment: 'Check if control flow is indented well if we have oneliners with no space before the closing token',
input: [
'{{b}} @if (a > b) {is less than}@else{is greater than or equal to} {{a}}',
'<div>',
'Hello there',
'</div>',
'<div>',
'{{b}} @if (a > b) {is less than}@else{',
'is greater than or equal to} {{a}}',
'Hello there',
'</div>'
],
output: [
'{{b}} @if (a > b) {is less than}@else{is greater than or equal to} {{a}}',
'<div>',
' Hello there',
'</div>',
'<div>',
' {{b}} @if (a > b) {is less than}@else{',
' is greater than or equal to} {{a}}',
' Hello there',
'</div>'
]
}]
}, {
name: "New Test Suite"
Expand Down

0 comments on commit cc5429f

Please sign in to comment.