Keeps inline comments with closing tag

This commit is contained in:
L. D. MacKrell 2018-07-02 15:46:38 -07:00
parent ca97daa8fa
commit f491716330
4 changed files with 72 additions and 2 deletions

View File

@ -1142,7 +1142,9 @@ function Beautifier(html_source, options, js_beautify, css_beautify) {
case 'TK_TAG_SINGLE':
// Don't add a newline before elements that should remain unformatted.
var tag_check = token.text.match(/^\s*<([a-z-]+)/i);
if (
if (token.tag_name === '!--' && multi_parser.last_token.is_closing_tag && token.text.indexOf('\n') === -1) {
//Do nothing. Leave comments on same line.
} else if (
!tag_check ||
!multi_parser.Utils.in_array(tag_check[1], inline_tags) &&
!multi_parser.Utils.in_array(tag_check[1], unformatted)

View File

@ -934,7 +934,9 @@ function Beautifier(html_source, options, js_beautify, css_beautify) {
case 'TK_TAG_SINGLE':
// Don't add a newline before elements that should remain unformatted.
var tag_check = token.text.match(/^\s*<([a-z-]+)/i);
if (
if (token.tag_name === '!--' && multi_parser.last_token.is_closing_tag && token.text.indexOf('\n') === -1) {
//Do nothing. Leave comments on same line.
} else if (
!tag_check ||
!multi_parser.Utils.in_array(tag_check[1], inline_tags) &&
!multi_parser.Utils.in_array(tag_check[1], unformatted)

View File

@ -2751,6 +2751,37 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
'</html>');
//============================================================
// Single line comment after closing tag
reset_options();
test_fragment(
'<div class="col">\n' +
' <div class="row">\n' +
' <div class="card">\n' +
'\n' +
' <h1>Some heading</h1>\n' +
' <p>Some text for the card.</p>\n' +
' <img src="some/image.jpg" alt="">\n' +
'\n' +
' </div> <!-- /.card -->\n' +
' </div>\n' +
' <!-- /.row -->\n' +
'</div> <!-- /.col -->',
// -- output --
'<div class="col">\n' +
' <div class="row">\n' +
' <div class="card">\n' +
'\n' +
' <h1>Some heading</h1>\n' +
' <p>Some text for the card.</p>\n' +
' <img src="some/image.jpg" alt="">\n' +
'\n' +
' </div> <!-- /.card -->\n' +
' </div>\n' +
' <!-- /.row -->\n' +
'</div> <!-- /.col -->');
//============================================================
// Regression Tests
reset_options();

View File

@ -832,6 +832,41 @@ exports.test_data = {
'</html>'
]
}, ]
}, {
name: "Single line comment after closing tag",
description: "Keep single line comments as they are after closing tags",
options: [],
tests: [{
fragment: true,
input: [
'<div class="col">',
' <div class="row">',
' <div class="card">',
'',
' <h1>Some heading</h1>',
' <p>Some text for the card.</p>',
' <img src="some/image.jpg" alt="">',
'',
' </div> <!-- /.card -->',
' </div>',
' <!-- /.row -->',
'</div> <!-- /.col -->'
],
output: [
'<div class="col">',
' <div class="row">',
' <div class="card">',
'',
' <h1>Some heading</h1>',
' <p>Some text for the card.</p>',
' <img src="some/image.jpg" alt="">',
'',
' </div> <!-- /.card -->',
' </div>',
' <!-- /.row -->',
'</div> <!-- /.col -->'
]
}, ]
}, {
name: "Regression Tests",
description: "Regression Tests",