js-beautify 1.9.1 → 1.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -601,7 +601,7 @@ Beautifier.prototype.print_token_line_indentation = function(current_token) {
601
601
  }
602
602
  };
603
603
 
604
- Beautifier.prototype.print_token = function(current_token, printable_token) {
604
+ Beautifier.prototype.print_token = function(current_token) {
605
605
  if (this._output.raw) {
606
606
  this._output.add_raw_token(current_token);
607
607
  return;
@@ -627,10 +627,9 @@ Beautifier.prototype.print_token = function(current_token, printable_token) {
627
627
  }
628
628
  }
629
629
 
630
- printable_token = printable_token || current_token.text;
631
630
  this.print_token_line_indentation(current_token);
632
631
  this._output.non_breaking_space = true;
633
- this._output.add_token(printable_token);
632
+ this._output.add_token(current_token.text);
634
633
  if (this._output.previous_token_wrapped) {
635
634
  this._flags.multiline_frame = true;
636
635
  }
@@ -884,6 +883,8 @@ Beautifier.prototype.handle_start_block = function(current_token) {
884
883
  if (this._flags.last_word === 'switch' && this._flags.last_token.type === TOKEN.END_EXPR) {
885
884
  this.set_mode(MODE.BlockStatement);
886
885
  this._flags.in_case_statement = true;
886
+ } else if (this._flags.case_body) {
887
+ this.set_mode(MODE.BlockStatement);
887
888
  } else if (second_token && (
888
889
  (in_array(second_token.text, [':', ',']) && in_array(next_token.type, [TOKEN.STRING, TOKEN.WORD, TOKEN.RESERVED])) ||
889
890
  (in_array(next_token.text, ['get', 'set', '...']) && in_array(second_token.type, [TOKEN.WORD, TOKEN.RESERVED]))
@@ -1069,11 +1070,12 @@ Beautifier.prototype.handle_word = function(current_token) {
1069
1070
 
1070
1071
  if (this._flags.in_case_statement && reserved_array(current_token, ['case', 'default'])) {
1071
1072
  this.print_newline();
1072
- if (this._flags.case_body || this._options.jslint_happy) {
1073
+ if (this._flags.last_token.type !== TOKEN.END_BLOCK && (this._flags.case_body || this._options.jslint_happy)) {
1073
1074
  // switch cases following one another
1074
1075
  this.deindent();
1075
- this._flags.case_body = false;
1076
1076
  }
1077
+ this._flags.case_body = false;
1078
+
1077
1079
  this.print_token(current_token);
1078
1080
  this._flags.in_case = true;
1079
1081
  return;
@@ -1371,11 +1373,16 @@ Beautifier.prototype.handle_operator = function(current_token) {
1371
1373
  }
1372
1374
 
1373
1375
  if (current_token.text === ':' && this._flags.in_case) {
1374
- this._flags.case_body = true;
1375
- this.indent();
1376
1376
  this.print_token(current_token);
1377
- this.print_newline();
1377
+
1378
1378
  this._flags.in_case = false;
1379
+ this._flags.case_body = true;
1380
+ if (this._tokens.peek().type !== TOKEN.START_BLOCK) {
1381
+ this.indent();
1382
+ this.print_newline();
1383
+ } else {
1384
+ this._output.space_before_token = true;
1385
+ }
1379
1386
  return;
1380
1387
  }
1381
1388
 
@@ -1538,8 +1545,12 @@ Beautifier.prototype.handle_block_comment = function(current_token, preserve_sta
1538
1545
  this.print_token(current_token);
1539
1546
  this._output.space_before_token = true;
1540
1547
  return;
1548
+ } else {
1549
+ this.print_block_commment(current_token, preserve_statement_flags);
1541
1550
  }
1551
+ };
1542
1552
 
1553
+ Beautifier.prototype.print_block_commment = function(current_token, preserve_statement_flags) {
1543
1554
  var lines = split_linebreaks(current_token.text);
1544
1555
  var j; // iterator for this case
1545
1556
  var javadoc = false;
@@ -1551,7 +1562,8 @@ Beautifier.prototype.handle_block_comment = function(current_token, preserve_sta
1551
1562
  this.print_newline(false, preserve_statement_flags);
1552
1563
 
1553
1564
  // first line always indented
1554
- this.print_token(current_token, lines[0]);
1565
+ this.print_token_line_indentation(current_token);
1566
+ this._output.add_token(lines[0]);
1555
1567
  this.print_newline(false, preserve_statement_flags);
1556
1568
 
1557
1569
 
@@ -1567,10 +1579,12 @@ Beautifier.prototype.handle_block_comment = function(current_token, preserve_sta
1567
1579
  for (j = 0; j < lines.length; j++) {
1568
1580
  if (javadoc) {
1569
1581
  // javadoc: reformat and re-indent
1570
- this.print_token(current_token, ltrim(lines[j]));
1582
+ this.print_token_line_indentation(current_token);
1583
+ this._output.add_token(ltrim(lines[j]));
1571
1584
  } else if (starless && lines[j]) {
1572
1585
  // starless: re-indent non-empty content, avoiding trim
1573
- this.print_token(current_token, lines[j].substring(lastIndentLength));
1586
+ this.print_token_line_indentation(current_token);
1587
+ this._output.add_token(lines[j].substring(lastIndentLength));
1574
1588
  } else {
1575
1589
  // normal comments output raw
1576
1590
  this._output.current_line.set_indent(-1);
@@ -1585,6 +1599,7 @@ Beautifier.prototype.handle_block_comment = function(current_token, preserve_sta
1585
1599
  }
1586
1600
  };
1587
1601
 
1602
+
1588
1603
  Beautifier.prototype.handle_comment = function(current_token, preserve_statement_flags) {
1589
1604
  if (current_token.newlines) {
1590
1605
  this.print_newline(false, preserve_statement_flags);
@@ -2365,6 +2380,11 @@ function Options(options, merge_child_field) {
2365
2380
  this.wrap_line_length = this._get_number('wrap_line_length', this._get_number('max_char'));
2366
2381
 
2367
2382
  this.indent_empty_lines = this._get_boolean('indent_empty_lines');
2383
+
2384
+ // valid templating languages ['django', 'erb', 'handlebars', 'php']
2385
+ // For now, 'auto' = all off for javascript, all on for html (and inline javascript).
2386
+ // other values ignored
2387
+ this.templating = this._get_selection_list('templating', ['auto', 'none', 'django', 'erb', 'handlebars', 'php'], ['auto']);
2368
2388
  }
2369
2389
 
2370
2390
  Options.prototype._get_array = function(name, default_value) {
@@ -2602,10 +2622,8 @@ var Tokenizer = function(input_string, options) {
2602
2622
  /\u2028\u2029/.source);
2603
2623
 
2604
2624
  var pattern_reader = new Pattern(this._input);
2605
- var templatable = new TemplatablePattern(this._input);
2606
- templatable = templatable.disable('handlebars');
2607
- templatable = templatable.disable('django');
2608
-
2625
+ var templatable = new TemplatablePattern(this._input)
2626
+ .read_options(this._options);
2609
2627
 
2610
2628
  this.__patterns = {
2611
2629
  template: templatable,
@@ -2774,7 +2792,7 @@ Tokenizer.prototype._read_non_javascript = function(c) {
2774
2792
 
2775
2793
  this._input.back();
2776
2794
 
2777
- } else if (c === '<') {
2795
+ } else if (c === '<' && this._is_first_token()) {
2778
2796
  resulting_string = this.__patterns.html_comment_start.read();
2779
2797
  if (resulting_string) {
2780
2798
  while (this._input.hasNext() && !this._input.testChar(acorn.newline)) {
@@ -3832,6 +3850,7 @@ function TemplatablePattern(input_scanner, parent) {
3832
3850
  var pattern = new Pattern(input_scanner);
3833
3851
  this.__patterns = {
3834
3852
  handlebars_comment: pattern.starting_with(/{{!--/).until_after(/--}}/),
3853
+ handlebars_unescaped: pattern.starting_with(/{{{/).until_after(/}}}/),
3835
3854
  handlebars: pattern.starting_with(/{{/).until_after(/}}/),
3836
3855
  php: pattern.starting_with(/<\?(?:[=]|php)/).until_after(/\?>/),
3837
3856
  erb: pattern.starting_with(/<%[^%]/).until_after(/[^%]%>/),
@@ -3858,6 +3877,15 @@ TemplatablePattern.prototype.disable = function(language) {
3858
3877
  return result;
3859
3878
  };
3860
3879
 
3880
+ TemplatablePattern.prototype.read_options = function(options) {
3881
+ var result = this._create();
3882
+ for (var language in template_names) {
3883
+ result._disabled[language] = options.templating.indexOf(language) === -1;
3884
+ }
3885
+ result._update();
3886
+ return result;
3887
+ };
3888
+
3861
3889
  TemplatablePattern.prototype.exclude = function(language) {
3862
3890
  var result = this._create();
3863
3891
  result._excluded[language] = true;
@@ -3933,6 +3961,8 @@ TemplatablePattern.prototype._read_template = function() {
3933
3961
  if (!this._disabled.handlebars && !this._excluded.handlebars) {
3934
3962
  resulting_string = resulting_string ||
3935
3963
  this.__patterns.handlebars_comment.read();
3964
+ resulting_string = resulting_string ||
3965
+ this.__patterns.handlebars_unescaped.read();
3936
3966
  resulting_string = resulting_string ||
3937
3967
  this.__patterns.handlebars.read();
3938
3968
  }
@@ -4215,6 +4245,9 @@ Beautifier.prototype.beautify = function() {
4215
4245
  isAfterSpace = whitespace !== '';
4216
4246
  previous_ch = topCharacter;
4217
4247
  this._ch = this._input.next();
4248
+ if (this._ch === '\\' && this._input.hasNext()) {
4249
+ this._ch += this._input.next();
4250
+ }
4218
4251
  topCharacter = this._ch;
4219
4252
 
4220
4253
  if (!this._ch) {
@@ -4347,7 +4380,7 @@ Beautifier.prototype.beautify = function() {
4347
4380
  }
4348
4381
  }
4349
4382
  } else if (this._ch === ":") {
4350
- if ((insideRule || enteringConditionalGroup) && !(this._input.lookBack("&") || this.foundNestedPseudoClass()) && !this._input.lookBack("(") && !insideAtExtend) {
4383
+ if ((insideRule || enteringConditionalGroup) && !(this._input.lookBack("&") || this.foundNestedPseudoClass()) && !this._input.lookBack("(") && !insideAtExtend && parenLevel === 0) {
4351
4384
  // 'property: value' delimiter
4352
4385
  // which could be in a conditional group query
4353
4386
  this.print_string(':');
@@ -4379,51 +4412,66 @@ Beautifier.prototype.beautify = function() {
4379
4412
  this.print_string(this._ch + this.eatString(this._ch));
4380
4413
  this.eatWhitespace(true);
4381
4414
  } else if (this._ch === ';') {
4382
- if (insidePropertyValue) {
4383
- this.outdent();
4384
- insidePropertyValue = false;
4385
- }
4386
- insideAtExtend = false;
4387
- insideAtImport = false;
4388
- this.print_string(this._ch);
4389
- this.eatWhitespace(true);
4390
-
4391
- // This maintains single line comments on the same
4392
- // line. Block comments are also affected, but
4393
- // a new line is always output before one inside
4394
- // that section
4395
- if (this._input.peek() !== '/') {
4396
- this._output.add_new_line();
4415
+ if (parenLevel === 0) {
4416
+ if (insidePropertyValue) {
4417
+ this.outdent();
4418
+ insidePropertyValue = false;
4419
+ }
4420
+ insideAtExtend = false;
4421
+ insideAtImport = false;
4422
+ this.print_string(this._ch);
4423
+ this.eatWhitespace(true);
4424
+
4425
+ // This maintains single line comments on the same
4426
+ // line. Block comments are also affected, but
4427
+ // a new line is always output before one inside
4428
+ // that section
4429
+ if (this._input.peek() !== '/') {
4430
+ this._output.add_new_line();
4431
+ }
4432
+ } else {
4433
+ this.print_string(this._ch);
4434
+ this.eatWhitespace(true);
4435
+ this._output.space_before_token = true;
4397
4436
  }
4398
4437
  } else if (this._ch === '(') { // may be a url
4399
4438
  if (this._input.lookBack("url")) {
4400
4439
  this.print_string(this._ch);
4401
4440
  this.eatWhitespace();
4441
+ parenLevel++;
4442
+ this.indent();
4402
4443
  this._ch = this._input.next();
4403
4444
  if (this._ch === ')' || this._ch === '"' || this._ch === '\'') {
4404
4445
  this._input.back();
4405
- parenLevel++;
4406
4446
  } else if (this._ch) {
4407
4447
  this.print_string(this._ch + this.eatString(')'));
4448
+ if (parenLevel) {
4449
+ parenLevel--;
4450
+ this.outdent();
4451
+ }
4408
4452
  }
4409
4453
  } else {
4410
- parenLevel++;
4411
4454
  this.preserveSingleSpace(isAfterSpace);
4412
4455
  this.print_string(this._ch);
4413
4456
  this.eatWhitespace();
4457
+ parenLevel++;
4458
+ this.indent();
4414
4459
  }
4415
4460
  } else if (this._ch === ')') {
4461
+ if (parenLevel) {
4462
+ parenLevel--;
4463
+ this.outdent();
4464
+ }
4416
4465
  this.print_string(this._ch);
4417
- parenLevel--;
4418
4466
  } else if (this._ch === ',') {
4419
4467
  this.print_string(this._ch);
4420
4468
  this.eatWhitespace(true);
4421
- if (this._options.selector_separator_newline && !insidePropertyValue && parenLevel < 1 && !insideAtImport) {
4469
+ if (this._options.selector_separator_newline && !insidePropertyValue && parenLevel === 0 && !insideAtImport) {
4422
4470
  this._output.add_new_line();
4423
4471
  } else {
4424
4472
  this._output.space_before_token = true;
4425
4473
  }
4426
- } else if ((this._ch === '>' || this._ch === '+' || this._ch === '~') && !insidePropertyValue && parenLevel < 1) {
4474
+ } else if ((this._ch === '>' || this._ch === '+' || this._ch === '~') && !insidePropertyValue && parenLevel === 0) {
4427
4475
  //handle combinator spacing
4428
4476
  if (this._options.space_around_combinator) {
4429
4477
  this._output.space_before_token = true;
@@ -4448,7 +4496,7 @@ Beautifier.prototype.beautify = function() {
4448
4496
  if (whitespaceChar.test(this._ch)) {
4449
4497
  this._ch = '';
4450
4498
  }
4451
- } else if (this._ch === '!') { // !important
4499
+ } else if (this._ch === '!' && !this._input.lookBack("\\")) { // !important
4452
4500
  this.print_string(' ');
4453
4501
  this.print_string(this._ch);
4454
4502
  } else {
@@ -5016,10 +5064,12 @@ Beautifier.prototype._handle_text = function(printer, raw_token, last_tag_token)
5016
5064
  Beautifier.prototype._print_custom_beatifier_text = function(printer, raw_token, last_tag_token) {
5017
5065
  var local = this;
5018
5066
  if (raw_token.text !== '') {
5019
- printer.print_newline(false);
5067
+
5020
5068
  var text = raw_token.text,
5021
5069
  _beautifier,
5022
- script_indent_level = 1;
5070
+ script_indent_level = 1,
5071
+ pre = '',
5072
+ post = '';
5023
5073
  if (last_tag_token.custom_beautifier_name === 'javascript' && typeof this._js_beautify === 'function') {
5024
5074
  _beautifier = this._js_beautify;
5025
5075
  } else if (last_tag_token.custom_beautifier_name === 'css' && typeof this._css_beautify === 'function') {
@@ -5031,7 +5081,6 @@ Beautifier.prototype._print_custom_beatifier_text = function(printer, raw_token,
5031
5081
  };
5032
5082
  }
5033
5083
 
5034
-
5035
5084
  if (this._options.indent_scripts === "keep") {
5036
5085
  script_indent_level = 0;
5037
5086
  } else if (this._options.indent_scripts === "separate") {
@@ -5044,24 +5093,67 @@ Beautifier.prototype._print_custom_beatifier_text = function(printer, raw_token,
5044
5093
  // we'll be adding one back after the text but before the containing tag.
5045
5094
  text = text.replace(/\n[ \t]*$/, '');
5046
5095
 
5047
- if (_beautifier) {
5096
+ // Handle the case where content is wrapped in a comment or cdata.
5097
+ if (last_tag_token.custom_beautifier_name !== 'html' &&
5098
+ text[0] === '<' && text.match(/^(<!--|<!\[CDATA\[)/)) {
5099
+ var matched = /^(<!--[^\n]*|<!\[CDATA\[)(\n?)([ \t\n]*)([\s\S]*)(-->|]]>)$/.exec(text);
5048
5100
 
5049
- // call the Beautifier if avaliable
5050
- var Child_options = function() {
5051
- this.eol = '\n';
5052
- };
5053
- Child_options.prototype = this._options.raw_options;
5054
- var child_options = new Child_options();
5055
- text = _beautifier(indentation + text, child_options);
5056
- } else {
5057
- // simply indent the string otherwise
5058
- var white = raw_token.whitespace_before;
5059
- if (white) {
5060
- text = text.replace(new RegExp('\n(' + white + ')?', 'g'), '\n');
5101
+ // if we start to wrap but don't finish, print raw
5102
+ if (!matched) {
5103
+ printer.add_raw_token(raw_token);
5104
+ return;
5105
+ }
5106
+
5107
+ pre = indentation + matched[1] + '\n';
5108
+ text = matched[4];
5109
+ if (matched[5]) {
5110
+ post = indentation + matched[5];
5111
+ }
5112
+
5113
+ // if there is at least one empty line at the end of this text, strip it
5114
+ // we'll be adding one back after the text but before the containing tag.
5115
+ text = text.replace(/\n[ \t]*$/, '');
5116
+
5117
+ if (matched[2] || matched[3].indexOf('\n') !== -1) {
5118
+ // if the first line of the non-comment text has spaces
5119
+ // use that as the basis for indenting in null case.
5120
+ matched = matched[3].match(/[ \t]+$/);
5121
+ if (matched) {
5122
+ raw_token.whitespace_before = matched[0];
5123
+ }
5061
5124
  }
5125
+ }
5126
+
5127
+ if (text) {
5128
+ if (_beautifier) {
5129
+
5130
+ // call the Beautifier if avaliable
5131
+ var Child_options = function() {
5132
+ this.eol = '\n';
5133
+ };
5134
+ Child_options.prototype = this._options.raw_options;
5135
+ var child_options = new Child_options();
5136
+ text = _beautifier(indentation + text, child_options);
5137
+ } else {
5138
+ // simply indent the string otherwise
5139
+ var white = raw_token.whitespace_before;
5140
+ if (white) {
5141
+ text = text.replace(new RegExp('\n(' + white + ')?', 'g'), '\n');
5142
+ }
5143
+
5144
+ text = indentation + text.replace(/\n/g, '\n' + indentation);
5145
+ }
5146
+ }
5062
5147
 
5063
- text = indentation + text.replace(/\n/g, '\n' + indentation);
5148
+ if (pre) {
5149
+ if (!text) {
5150
+ text = pre + post;
5151
+ } else {
5152
+ text = pre + text + '\n' + post;
5153
+ }
5064
5154
  }
5155
+
5156
+ printer.print_newline(false);
5065
5157
  if (text) {
5066
5158
  raw_token.text = text;
5067
5159
  raw_token.whitespace_before = '';
@@ -5409,6 +5501,9 @@ var BaseOptions = __webpack_require__(7).Options;
5409
5501
 
5410
5502
  function Options(options) {
5411
5503
  BaseOptions.call(this, options, 'html');
5504
+ if (this.templating.length === 1 && this.templating[0] === 'auto') {
5505
+ this.templating = ['django', 'erb', 'handlebars', 'php'];
5506
+ }
5412
5507
 
5413
5508
  this.indent_inner_html = this._get_boolean('indent_inner_html');
5414
5509
  this.indent_body_inner_html = this._get_boolean('indent_body_inner_html', true);
@@ -5456,6 +5551,7 @@ function Options(options) {
5456
5551
  ]);
5457
5552
  this.unformatted_content_delimiter = this._get_characters('unformatted_content_delimiter');
5458
5553
  this.indent_scripts = this._get_selection('indent_scripts', ['normal', 'keep', 'separate']);
5554
+
5459
5555
  }
5460
5556
  Options.prototype = new BaseOptions();
5461
5557
 
@@ -5527,7 +5623,7 @@ var Tokenizer = function(input_string, options) {
5527
5623
 
5528
5624
  // Words end at whitespace or when a tag starts
5529
5625
  // if we are indenting handlebars, they are considered tags
5530
- var templatable_reader = new TemplatablePattern(this._input);
5626
+ var templatable_reader = new TemplatablePattern(this._input).read_options(this._options);
5531
5627
  var pattern_reader = new Pattern(this._input);
5532
5628
 
5533
5629
  this.__patterns = {
@@ -5542,7 +5638,7 @@ var Tokenizer = function(input_string, options) {
5542
5638
  handlebars_open: pattern_reader.until(/[\n\r\t }]/),
5543
5639
  handlebars_raw_close: pattern_reader.until(/}}/),
5544
5640
  comment: pattern_reader.starting_with(/<!--/).until_after(/-->/),
5545
- cdata: pattern_reader.starting_with(/<!\[cdata\[/).until_after(/]]>/),
5641
+ cdata: pattern_reader.starting_with(/<!\[CDATA\[/).until_after(/]]>/),
5546
5642
  // https://en.wikipedia.org/wiki/Conditional_comment
5547
5643
  conditional_comment: pattern_reader.starting_with(/<!\[/).until_after(/]>/),
5548
5644
  processing: pattern_reader.starting_with(/<\?/).until_after(/\?>/)
@@ -5593,27 +5689,27 @@ Tokenizer.prototype._get_next_token = function(previous_token, open_token) { //
5593
5689
 
5594
5690
  token = token || this._read_open_handlebars(c, open_token);
5595
5691
  token = token || this._read_attribute(c, previous_token, open_token);
5596
- token = token || this._read_raw_content(previous_token, open_token);
5692
+ token = token || this._read_raw_content(c, previous_token, open_token);
5597
5693
  token = token || this._read_close(c, open_token);
5598
5694
  token = token || this._read_content_word(c);
5599
- token = token || this._read_comment(c);
5695
+ token = token || this._read_comment_or_cdata(c);
5696
+ token = token || this._read_processing(c);
5600
5697
  token = token || this._read_open(c, open_token);
5601
5698
  token = token || this._create_token(TOKEN.UNKNOWN, this._input.next());
5602
5699
 
5603
5700
  return token;
5604
5701
  };
5605
5702
 
5606
- Tokenizer.prototype._read_comment = function(c) { // jshint unused:false
5703
+ Tokenizer.prototype._read_comment_or_cdata = function(c) { // jshint unused:false
5607
5704
  var token = null;
5608
5705
  var resulting_string = null;
5609
5706
  var directives = null;
5610
5707
 
5611
5708
  if (c === '<') {
5612
5709
  var peek1 = this._input.peek(1);
5613
- //if we're in a comment, do something special
5614
5710
  // We treat all comments as literals, even more than preformatted tags
5615
- // we just look for the appropriate close tag
5616
- if (c === '<' && (peek1 === '!' || peek1 === '?')) {
5711
+ // we only look for the appropriate closing marker
5712
+ if (peek1 === '!') {
5617
5713
  resulting_string = this.__patterns.comment.read();
5618
5714
 
5619
5715
  // only process directive on html comments
@@ -5624,8 +5720,6 @@ Tokenizer.prototype._read_comment = function(c) { // jshint unused:false
5624
5720
  }
5625
5721
  } else {
5626
5722
  resulting_string = this.__patterns.cdata.read();
5627
- resulting_string = resulting_string || this.__patterns.conditional_comment.read();
5628
- resulting_string = resulting_string || this.__patterns.processing.read();
5629
5723
  }
5630
5724
  }
5631
5725
 
@@ -5638,6 +5732,27 @@ Tokenizer.prototype._read_comment = function(c) { // jshint unused:false
5638
5732
  return token;
5639
5733
  };
5640
5734
 
5735
+ Tokenizer.prototype._read_processing = function(c) { // jshint unused:false
5736
+ var token = null;
5737
+ var resulting_string = null;
5738
+ var directives = null;
5739
+
5740
+ if (c === '<') {
5741
+ var peek1 = this._input.peek(1);
5742
+ if (peek1 === '!' || peek1 === '?') {
5743
+ resulting_string = this.__patterns.conditional_comment.read();
5744
+ resulting_string = resulting_string || this.__patterns.processing.read();
5745
+ }
5746
+
5747
+ if (resulting_string) {
5748
+ token = this._create_token(TOKEN.COMMENT, resulting_string);
5749
+ token.directives = directives;
5750
+ }
5751
+ }
5752
+
5753
+ return token;
5754
+ };
5755
+
5641
5756
  Tokenizer.prototype._read_open = function(c, open_token) {
5642
5757
  var resulting_string = null;
5643
5758
  var token = null;
@@ -5729,19 +5844,27 @@ Tokenizer.prototype._is_content_unformatted = function(tag_name) {
5729
5844
  // script and style tags should always be read as unformatted content
5730
5845
  // finally content_unformatted and unformatted element contents are unformatted
5731
5846
  return this._options.void_elements.indexOf(tag_name) === -1 &&
5732
- (tag_name === 'script' || tag_name === 'style' ||
5733
- this._options.content_unformatted.indexOf(tag_name) !== -1 ||
5847
+ (this._options.content_unformatted.indexOf(tag_name) !== -1 ||
5734
5848
  this._options.unformatted.indexOf(tag_name) !== -1);
5735
5849
  };
5736
5850
 
5737
5851
 
5738
- Tokenizer.prototype._read_raw_content = function(previous_token, open_token) { // jshint unused:false
5852
+ Tokenizer.prototype._read_raw_content = function(c, previous_token, open_token) { // jshint unused:false
5739
5853
  var resulting_string = '';
5740
5854
  if (open_token && open_token.text[0] === '{') {
5741
5855
  resulting_string = this.__patterns.handlebars_raw_close.read();
5742
5856
  } else if (previous_token.type === TOKEN.TAG_CLOSE && (previous_token.opened.text[0] === '<')) {
5743
5857
  var tag_name = previous_token.opened.text.substr(1).toLowerCase();
5744
- if (this._is_content_unformatted(tag_name)) {
5858
+ if (tag_name === 'script' || tag_name === 'style') {
5859
+ // Script and style tags are allowed to have comments wrapping their content
5860
+ // or just have regular content.
5861
+ var token = this._read_comment_or_cdata(c);
5862
+ if (token) {
5863
+ token.type = TOKEN.TEXT;
5864
+ return token;
5865
+ }
5866
+ resulting_string = this._input.readUntil(new RegExp('</' + tag_name + '[\\n\\r\\t ]*?>', 'ig'));
5867
+ } else if (this._is_content_unformatted(tag_name)) {
5745
5868
  resulting_string = this._input.readUntil(new RegExp('</' + tag_name + '[\\n\\r\\t ]*?>', 'ig'));
5746
5869
  }
5747
5870
  }