@@ -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
24382443Beautifier . 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) {
30053011Tokenizer . 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-
31033108Tokenizer . 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
31493166module . exports . Tokenizer = Tokenizer ;
0 commit comments