Skip to content

Commit

Permalink
Fix newline insertion for optional end tags
Browse files Browse the repository at this point in the history
Fixes #1213
  • Loading branch information
bitwiseman committed Jan 30, 2019
1 parent f6ed35d commit bbc0e8e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 23 deletions.
56 changes: 33 additions & 23 deletions js/src/html/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,15 @@ Beautifier.prototype._set_tag_position = function(printer, raw_token, parser_tok
} else { // it's a start-tag
// check if this tag is starting an element that has optional end element
// and do an ending needed
this._do_optional_end_element(parser_token);
if (this._do_optional_end_element(parser_token)) {
if (!parser_token.is_inline_element) {
if (parser_token.parent) {
parser_token.parent.multiline_content = true;
}
printer.print_newline(false);
}

}

this._tag_stack.record_tag(parser_token); //push it on the tag stack

Expand Down Expand Up @@ -686,6 +694,7 @@ Beautifier.prototype._set_tag_position = function(printer, raw_token, parser_tok
//var p_closers = ['address', 'article', 'aside', 'blockquote', 'details', 'div', 'dl', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hr', 'main', 'nav', 'ol', 'p', 'pre', 'section', 'table', 'ul'];

Beautifier.prototype._do_optional_end_element = function(parser_token) {
var result = null;
// NOTE: cases of "if there is no more content in the parent element"
// are handled automatically by the beautifier.
// It assumes parent or ancestor close tag closes all children.
Expand All @@ -695,52 +704,52 @@ Beautifier.prototype._do_optional_end_element = function(parser_token) {

} else if (parser_token.tag_name === 'body') {
// A head element’s end tag may be omitted if the head element is not immediately followed by a space character or a comment.
this._tag_stack.try_pop('head');
result = result || this._tag_stack.try_pop('head');

//} else if (parser_token.tag_name === 'body') {
// DONE: A body element’s end tag may be omitted if the body element is not immediately followed by a comment.

} else if (parser_token.tag_name === 'li') {
// An li element’s end tag may be omitted if the li element is immediately followed by another li element or if there is no more content in the parent element.
this._tag_stack.try_pop('li', ['ol', 'ul']);
result = result || this._tag_stack.try_pop('li', ['ol', 'ul']);

} else if (parser_token.tag_name === 'dd' || parser_token.tag_name === 'dt') {
// A dd element’s end tag may be omitted if the dd element is immediately followed by another dd element or a dt element, or if there is no more content in the parent element.
// A dt element’s end tag may be omitted if the dt element is immediately followed by another dt element or a dd element.
this._tag_stack.try_pop('dt', ['dl']);
this._tag_stack.try_pop('dd', ['dl']);
result = result || this._tag_stack.try_pop('dt', ['dl']);
result = result || this._tag_stack.try_pop('dd', ['dl']);

//} else if (p_closers.indexOf(parser_token.tag_name) !== -1) {
//TODO: THIS IS A BUG FARM. We are not putting this into 1.8.0 as it is likely to blow up.
//A p element’s end tag may be omitted if the p element is immediately followed by an address, article, aside, blockquote, details, div, dl, fieldset, figcaption, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hr, main, nav, ol, p, pre, section, table, or ul element, or if there is no more content in the parent element and the parent element is an HTML element that is not an a, audio, del, ins, map, noscript, or video element, or an autonomous custom element.
//this._tag_stack.try_pop('p', ['body']);
//result = result || this._tag_stack.try_pop('p', ['body']);

} else if (parser_token.tag_name === 'rp' || parser_token.tag_name === 'rt') {
// An rt element’s end tag may be omitted if the rt element is immediately followed by an rt or rp element, or if there is no more content in the parent element.
// An rp element’s end tag may be omitted if the rp element is immediately followed by an rt or rp element, or if there is no more content in the parent element.
this._tag_stack.try_pop('rt', ['ruby', 'rtc']);
this._tag_stack.try_pop('rp', ['ruby', 'rtc']);
result = result || this._tag_stack.try_pop('rt', ['ruby', 'rtc']);
result = result || this._tag_stack.try_pop('rp', ['ruby', 'rtc']);

} else if (parser_token.tag_name === 'optgroup') {
// An optgroup element’s end tag may be omitted if the optgroup element is immediately followed by another optgroup element, or if there is no more content in the parent element.
// An option element’s end tag may be omitted if the option element is immediately followed by another option element, or if it is immediately followed by an optgroup element, or if there is no more content in the parent element.
this._tag_stack.try_pop('optgroup', ['select']);
//this._tag_stack.try_pop('option', ['select']);
result = result || this._tag_stack.try_pop('optgroup', ['select']);
//result = result || this._tag_stack.try_pop('option', ['select']);

} else if (parser_token.tag_name === 'option') {
// An option element’s end tag may be omitted if the option element is immediately followed by another option element, or if it is immediately followed by an optgroup element, or if there is no more content in the parent element.
this._tag_stack.try_pop('option', ['select', 'datalist', 'optgroup']);
result = result || this._tag_stack.try_pop('option', ['select', 'datalist', 'optgroup']);

} else if (parser_token.tag_name === 'colgroup') {
// DONE: A colgroup element’s end tag may be omitted if the colgroup element is not immediately followed by a space character or a comment.
// A caption element's end tag may be ommitted if a colgroup, thead, tfoot, tbody, or tr element is started.
this._tag_stack.try_pop('caption', ['table']);
result = result || this._tag_stack.try_pop('caption', ['table']);

} else if (parser_token.tag_name === 'thead') {
// A colgroup element's end tag may be ommitted if a thead, tfoot, tbody, or tr element is started.
// A caption element's end tag may be ommitted if a colgroup, thead, tfoot, tbody, or tr element is started.
this._tag_stack.try_pop('caption', ['table']);
this._tag_stack.try_pop('colgroup', ['table']);
result = result || this._tag_stack.try_pop('caption', ['table']);
result = result || this._tag_stack.try_pop('colgroup', ['table']);

//} else if (parser_token.tag_name === 'caption') {
// DONE: A caption element’s end tag may be omitted if the caption element is not immediately followed by a space character or a comment.
Expand All @@ -750,10 +759,10 @@ Beautifier.prototype._do_optional_end_element = function(parser_token) {
// A tbody element’s end tag may be omitted if the tbody element is immediately followed by a tbody or tfoot element, or if there is no more content in the parent element.
// A colgroup element's end tag may be ommitted if a thead, tfoot, tbody, or tr element is started.
// A caption element's end tag may be ommitted if a colgroup, thead, tfoot, tbody, or tr element is started.
this._tag_stack.try_pop('caption', ['table']);
this._tag_stack.try_pop('colgroup', ['table']);
this._tag_stack.try_pop('thead', ['table']);
this._tag_stack.try_pop('tbody', ['table']);
result = result || this._tag_stack.try_pop('caption', ['table']);
result = result || this._tag_stack.try_pop('colgroup', ['table']);
result = result || this._tag_stack.try_pop('thead', ['table']);
result = result || this._tag_stack.try_pop('tbody', ['table']);

//} else if (parser_token.tag_name === 'tfoot') {
// DONE: A tfoot element’s end tag may be omitted if there is no more content in the parent element.
Expand All @@ -762,15 +771,15 @@ Beautifier.prototype._do_optional_end_element = function(parser_token) {
// A tr element’s end tag may be omitted if the tr element is immediately followed by another tr element, or if there is no more content in the parent element.
// A colgroup element's end tag may be ommitted if a thead, tfoot, tbody, or tr element is started.
// A caption element's end tag may be ommitted if a colgroup, thead, tfoot, tbody, or tr element is started.
this._tag_stack.try_pop('caption', ['table']);
this._tag_stack.try_pop('colgroup', ['table']);
this._tag_stack.try_pop('tr', ['table', 'thead', 'tbody', 'tfoot']);
result = result || this._tag_stack.try_pop('caption', ['table']);
result = result || this._tag_stack.try_pop('colgroup', ['table']);
result = result || this._tag_stack.try_pop('tr', ['table', 'thead', 'tbody', 'tfoot']);

} else if (parser_token.tag_name === 'th' || parser_token.tag_name === 'td') {
// A td element’s end tag may be omitted if the td element is immediately followed by a td or th element, or if there is no more content in the parent element.
// A th element’s end tag may be omitted if the th element is immediately followed by a td or th element, or if there is no more content in the parent element.
this._tag_stack.try_pop('td', ['tr']);
this._tag_stack.try_pop('th', ['tr']);
result = result || this._tag_stack.try_pop('td', ['tr']);
result = result || this._tag_stack.try_pop('th', ['tr']);
}

// Start element omission not handled currently
Expand All @@ -781,6 +790,7 @@ Beautifier.prototype._do_optional_end_element = function(parser_token) {
// Fix up the parent of the parser token
parser_token.parent = this._tag_stack.get_parser_token();

return result;
};

module.exports.Beautifier = Beautifier;
26 changes: 26 additions & 0 deletions js/test/generated/beautify-html-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6284,6 +6284,32 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
' <td>\n' +
' <td>✔\n' +
'</table>');

// Regression test for #1213
bth(
'<ul><li>ab<li>cd</li><li>cd</li></ul><dl><dt>ef<dt>gh</dt><dt>gh</dt></dl>\n' +
'<ul><li>ab</li><li>cd<li>cd</li></ul><dl><dt>ef</dt><dt>gh<dt>gh</dt></dl>',
// -- output --
'<ul>\n' +
' <li>ab\n' +
' <li>cd</li>\n' +
' <li>cd</li>\n' +
'</ul>\n' +
'<dl>\n' +
' <dt>ef\n' +
' <dt>gh</dt>\n' +
' <dt>gh</dt>\n' +
'</dl>\n' +
'<ul>\n' +
' <li>ab</li>\n' +
' <li>cd\n' +
' <li>cd</li>\n' +
'</ul>\n' +
'<dl>\n' +
' <dt>ef</dt>\n' +
' <dt>gh\n' +
' <dt>gh</dt>\n' +
'</dl>');


//============================================================
Expand Down
28 changes: 28 additions & 0 deletions test/data/html/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,34 @@ exports.test_data = {
' <td>✔',
'</table>'
]
}, {
comment: 'Regression test for #1213',
input: [
'<ul><li>ab<li>cd</li><li>cd</li></ul><dl><dt>ef<dt>gh</dt><dt>gh</dt></dl>',
'<ul><li>ab</li><li>cd<li>cd</li></ul><dl><dt>ef</dt><dt>gh<dt>gh</dt></dl>'
],
output: [
'<ul>',
' <li>ab',
' <li>cd</li>',
' <li>cd</li>',
'</ul>',
'<dl>',
' <dt>ef',
' <dt>gh</dt>',
' <dt>gh</dt>',
'</dl>',
'<ul>',
' <li>ab</li>',
' <li>cd',
' <li>cd</li>',
'</ul>',
'<dl>',
' <dt>ef</dt>',
' <dt>gh',
' <dt>gh</dt>',
'</dl>'
]
}]
}, {
name: "Unformatted tags",
Expand Down

0 comments on commit bbc0e8e

Please sign in to comment.