Skip to content

Commit

Permalink
Add newline for block end tag with multiline child
Browse files Browse the repository at this point in the history
  • Loading branch information
ang-zeyu committed Jul 19, 2020
1 parent 491d69a commit 947566f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ <h2>Browser extensions and other uses</h2>
<li>A
<a href="javascript:(function(){s=document.getElementsByTagName('SCRIPT');tx='';sr=[];for(i=0;i<s.length;i++){with(s.item(i)){t=text;if(t){tx+=t;}else{sr.push(src)};}};with(window.open()){document.write('<textarea id=&quot;t&quot;>'+(sr.join(&quot;\n&quot;))+&quot;\n\n-----\n\n&quot;+tx+'</textarea><script src=&quot;https://beautifier.io/js/lib/beautify.js&quot;></script><script>with(document.getElementById(&quot;t&quot;)){value=js_beautify(value);with(style){width=&quot;99%&quot;;height=&quot;99%&quot;;borderStyle=&quot;none&quot;;}};</script>');document.close();}})();">
<strong>bookmarklet</strong></a>
(drag it to your bookmarks) by Ichiro Hiroshi to see all scripts used on the page,</li>
(drag it to your bookmarks) by Ichiro Hiroshi to see all scripts used on the page,
</li>

<li><strong>Chrome</strong>, in case the built-in CSS and javascript formatting isn't enough for you:<br>
<a href="https://chrome.google.com/webstore/detail/cfmcghennfbpmhemnnfjhkdmnbidpanb">Quick source viewer</a> by Tomi Mickelsson (<a href="https://github.com/tomimick/chrome-ext-view-src">github</a>, <a href="http://tomicloud.com/2012/07/viewsrc-chrome-ext">blog</a>),<br>
Expand Down
13 changes: 9 additions & 4 deletions js/src/html/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -734,12 +734,17 @@ Beautifier.prototype._set_tag_position = function(printer, raw_token, parser_tok
}
}

if (!parser_token.is_inline_element && last_token.type !== 'TK_CONTENT') {
if (parser_token.parent) {
parser_token.parent.multiline_content = true;
}
var do_set_parent_multiline;
if (parser_token.is_inline_element) {
do_set_parent_multiline = raw_token.newlines && parser_token.parent && !parser_token.parent.is_inline_element;
} else if (last_token.type !== 'TK_CONTENT') {
do_set_parent_multiline = parser_token.parent;
printer.print_newline(false);
}

if (do_set_parent_multiline) {
parser_token.parent.multiline_content = true;
}
}
};

Expand Down
15 changes: 15 additions & 0 deletions js/test/generated/beautify-html-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,21 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
'<svg xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="none" x="0" y="0" viewBox="0 0 900 710" width="100%" height="100%">\n' +
' <circle id="mycircle" cx="182.901" cy="91.4841" style="fill:rosybrown;stroke:black;stroke-width:1px;" r="48" />\n' +
'</svg>');
test_fragment(
'<div class="col-xs-2">\n' +
'<input type="radio" class="control-label" ng-disabled="!col" ng-model="col" value="2" class="form-control" id="coli" name="coli" />\n' +
'<label for="coli" class="control-label">Collision</label></div>',
// -- output --
'<div class="col-xs-2">\n' +
' <input type="radio" class="control-label" ng-disabled="!col" ng-model="col" value="2" class="form-control" id="coli" name="coli" />\n' +
' <label for="coli" class="control-label">Collision</label>\n' +
'</div>');
test_fragment(
'<label class="col-xs-2">Collision\n' +
'<input type="radio" class="control-label" ng-disabled="!col" ng-model="col" value="2" class="form-control" id="coli" name="coli" /></label>',
// -- output --
'<label class="col-xs-2">Collision\n' +
' <input type="radio" class="control-label" ng-disabled="!col" ng-model="col" value="2" class="form-control" id="coli" name="coli" /></label>');


//============================================================
Expand Down
25 changes: 25 additions & 0 deletions test/data/html/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,31 @@ exports.test_data = {
' <circle id="mycircle" cx="182.901" cy="91.4841" style="fill:rosybrown;stroke:black;stroke-width:1px;" r="48" />',
'</svg>'
]
}, {
// Issue #1365 -- Inline tags with newlines should break block parent ending tags into another line
fragment: true,
input: [
'<div class="col-xs-2">',
'<input type="radio" class="control-label" ng-disabled="!col" ng-model="col" value="2" class="form-control" id="coli" name="coli" />',
'<label for="coli" class="control-label">Collision</label></div>'
],
output: [
'<div class="col-xs-2">',
' <input type="radio" class="control-label" ng-disabled="!col" ng-model="col" value="2" class="form-control" id="coli" name="coli" />',
' <label for="coli" class="control-label">Collision</label>',
'</div>'
]
}, {
// Issue #1365 -- Inline tags with newlines should not break inline parent ending tags into another line
fragment: true,
input: [
'<label class="col-xs-2">Collision',
'<input type="radio" class="control-label" ng-disabled="!col" ng-model="col" value="2" class="form-control" id="coli" name="coli" /></label>'
],
output: [
'<label class="col-xs-2">Collision',
' <input type="radio" class="control-label" ng-disabled="!col" ng-model="col" value="2" class="form-control" id="coli" name="coli" /></label>'
]
}]
}, {
name: "End With Newline",
Expand Down

0 comments on commit 947566f

Please sign in to comment.