Skip to content

Commit 305a0c0

Browse files
authored
Merge pull request #2502 from JessenReinhart/fix/handlebars-tag-stack-2045
fix(html): prevent Handlebars tags from corrupting HTML tag stack
2 parents 9de77aa + 92ff87e commit 305a0c0

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

js/src/html/beautifier.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,15 @@ Beautifier.prototype._do_optional_end_element = function(parser_token) {
816816
// are handled automatically by the beautifier.
817817
// It assumes parent or ancestor close tag closes all children.
818818
// https://www.w3.org/TR/html5/syntax.html#optional-tags
819+
// Handlebars tags ({{#tag}}, {{/tag}}, etc.) must not participate in
820+
// HTML optional end-element logic — otherwise a Handlebars helper like
821+
// {{#tr}} is mistaken for the HTML <tr> tag and incorrectly pops it
822+
// from the tag stack, breaking indentation inside table rows.
823+
if (parser_token.tag_start_char === '{') {
824+
parser_token.parent = this._tag_stack.get_parser_token();
825+
return;
826+
}
827+
819828
if (parser_token.is_empty_element || !parser_token.is_start_tag || !parser_token.parent) {
820829
return;
821830

test/data/html/tests.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,58 @@ exports.test_data = {
17361736
unchanged: '<span>{{condition < 0 ? "result1" : "result2"}}</span>'
17371737
}, {
17381738
unchanged: '<span>{{condition1 && condition2 && condition3 && condition4 < 0 ? "resForTrue" : "resForFalse"}}</span>'
1739+
}, {
1740+
comment: "Issue #2045 - {{#tr}} helper inside a table must not pop the HTML <tr> from the tag stack",
1741+
input_: [
1742+
'<table>',
1743+
'<tr>',
1744+
'<td>',
1745+
'{{#tr}}translated{{/tr}}',
1746+
'</td>',
1747+
'</tr>',
1748+
'</table>'
1749+
],
1750+
output: [
1751+
'<table>',
1752+
' <tr>',
1753+
' <td>',
1754+
' {{#tr}}translated{{/tr}}',
1755+
' </td>',
1756+
' </tr>',
1757+
'</table>'
1758+
]
1759+
}, {
1760+
comment: "Issue #2045 - {{#li}} helper inside a list item must not pop the HTML <li> from the tag stack",
1761+
input_: [
1762+
'<ul>',
1763+
'<li>',
1764+
'{{#li}}item{{/li}}',
1765+
'</li>',
1766+
'</ul>'
1767+
],
1768+
output: [
1769+
'<ul>',
1770+
' <li>',
1771+
' {{#li}}item{{/li}}',
1772+
' </li>',
1773+
'</ul>'
1774+
]
1775+
}, {
1776+
comment: "Issue #2045 - {{#option}} helper inside an option must not pop the HTML <option> from the tag stack",
1777+
input_: [
1778+
'<select>',
1779+
'<option>',
1780+
'{{#option}}text{{/option}}',
1781+
'</option>',
1782+
'</select>'
1783+
],
1784+
output: [
1785+
'<select>',
1786+
' <option>',
1787+
' {{#option}}text{{/option}}',
1788+
' </option>',
1789+
'</select>'
1790+
]
17391791
}
17401792
]
17411793
}, {

0 commit comments

Comments
 (0)