Skip to content

Commit

Permalink
Merge pull request #1015 from Lukinos/master
Browse files Browse the repository at this point in the history
Add force-aligned feature
  • Loading branch information
bitwiseman authored Sep 6, 2016
2 parents 9a2eb4e + b58562b commit 56be690
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ HTML Beautifier Options:
-b, --brace-style [collapse-preserve-inline|collapse|expand|end-expand|none] ["collapse"]
-S, --indent-scripts [keep|separate|normal] ["normal"]
-w, --wrap-line-length Maximum characters per line (0 disables) [250]
-A, --wrap-attributes Wrap attributes to new lines [auto|force] ["auto"]
-i, --wrap-attributes-indent-size Indent wrapped attributes to after N characters [indent-size]
-A, --wrap-attributes Wrap attributes to new lines [auto|force|force-aligned] ["auto"]
-i, --wrap-attributes-indent-size Indent wrapped attributes to after N characters [indent-size] (ignored if wrap-attributes is "force-aligned")
-U, --unformatted List of tags (defaults to inline) that should not be reformatted
-E, --extra_liners List of tags (defaults to [head,body,/html] that should have an extra newline before them.
--editorconfig Use EditorConfig to set up the options
Expand Down
15 changes: 11 additions & 4 deletions js/lib/beautify-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
indent_handlebars,
wrap_attributes,
wrap_attributes_indent_size,
is_wrap_attributes_force,
end_with_newline,
extra_liners,
eol;
Expand Down Expand Up @@ -139,6 +140,7 @@
indent_handlebars = (options.indent_handlebars === undefined) ? false : options.indent_handlebars;
wrap_attributes = (options.wrap_attributes === undefined) ? 'auto' : options.wrap_attributes;
wrap_attributes_indent_size = (isNaN(parseInt(options.wrap_attributes_indent_size, 10))) ? indent_size : parseInt(options.wrap_attributes_indent_size, 10);
is_wrap_attributes_force = wrap_attributes.substr(0, 'force'.length) === 'force';
end_with_newline = (options.end_with_newline === undefined) ? false : options.end_with_newline;
extra_liners = (typeof options.extra_liners === 'object') && options.extra_liners ?
options.extra_liners.concat() : (typeof options.extra_liners === 'string') ?
Expand Down Expand Up @@ -396,16 +398,21 @@
if (content.length && content[content.length - 1] !== '=' && input_char !== '>' && space) {
//no space after = or before >
var wrapped = this.space_or_wrap(content);
var indentAttrs = wrapped && input_char !== '/' && wrap_attributes !== 'force';
var indentAttrs = wrapped && input_char !== '/' && !is_wrap_attributes_force;
space = false;
if (!first_attr && wrap_attributes === 'force' && input_char !== '/') {
if (!first_attr && is_wrap_attributes_force && input_char !== '/') {
this.print_newline(false, content);
this.print_indentation(content);
indentAttrs = true;
}
if (indentAttrs) {
//indent attributes an auto or forced line-wrap
for (var count = 0; count < wrap_attributes_indent_size; count++) {
//indent attributes an auto, forced, or forced-align line-wrap
var alignment_size = wrap_attributes_indent_size;
if (wrap_attributes === 'force-aligned') {
alignment_size = content.indexOf(' ') + 1;
}

for (var count = 0; count < alignment_size; count++) {
content.push(indent_character);
}
}
Expand Down
2 changes: 1 addition & 1 deletion js/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var fs = require('fs'),
"keep_array_indentation": Boolean,
"unescape_strings": Boolean,
"wrap_line_length": Number,
"wrap_attributes": ["auto", "force"],
"wrap_attributes": ["auto", "force", "force-aligned"],
"wrap_attributes_indent_size": Number,
"e4x": Boolean,
"end_with_newline": Boolean,
Expand Down
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 @@ -313,6 +313,32 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
test_fragment('<?xml version="1.0" encoding="UTF-8" ?><root attr1="foo" attr2="bar"/>', '<?xml version="1.0" encoding="UTF-8" ?>\n<root attr1="foo" attr2="bar" />');
test_fragment('<link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,600,700,300&amp;subset=latin" rel="stylesheet" type="text/css">');

// Attribute Wrap - (indent_attr = "\n ", indent_attr_faligned = " ", indent_over80 = "\n ")
opts.wrap_attributes = 'force-aligned';
test_fragment('<div attr0 attr1="123" data-attr2="hello t here">This is some text</div>', '<div attr0\n attr1="123"\n data-attr2="hello t here">This is some text</div>');
test_fragment('<div lookatthissuperduperlongattributenamewhoahcrazy0="true" attr0 attr1="123" data-attr2="hello t here" heymanimreallylongtoowhocomesupwiththesenames="false">This is some text</div>', '<div lookatthissuperduperlongattributenamewhoahcrazy0="true"\n attr0\n attr1="123"\n data-attr2="hello t here"\n heymanimreallylongtoowhocomesupwiththesenames="false">This is some text</div>');
test_fragment('<img attr0 attr1="123" data-attr2="hello t here"/>', '<img attr0\n attr1="123"\n data-attr2="hello t here" />');
test_fragment('<?xml version="1.0" encoding="UTF-8" ?><root attr1="foo" attr2="bar"/>', '<?xml version="1.0" encoding="UTF-8" ?>\n<root attr1="foo"\n attr2="bar" />');
test_fragment('<link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,600,700,300&amp;subset=latin" rel="stylesheet" type="text/css">', '<link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,600,700,300&amp;subset=latin"\n rel="stylesheet"\n type="text/css">');

// Attribute Wrap - (indent_attr = "\n ", indent_attr_faligned = " ", indent_over80 = "\n ")
opts.wrap_attributes = 'force-aligned';
opts.wrap_line_length = 80;
test_fragment('<div attr0 attr1="123" data-attr2="hello t here">This is some text</div>', '<div attr0\n attr1="123"\n data-attr2="hello t here">This is some text</div>');
test_fragment('<div lookatthissuperduperlongattributenamewhoahcrazy0="true" attr0 attr1="123" data-attr2="hello t here" heymanimreallylongtoowhocomesupwiththesenames="false">This is some text</div>', '<div lookatthissuperduperlongattributenamewhoahcrazy0="true"\n attr0\n attr1="123"\n data-attr2="hello t here"\n heymanimreallylongtoowhocomesupwiththesenames="false">This is some text</div>');
test_fragment('<img attr0 attr1="123" data-attr2="hello t here"/>', '<img attr0\n attr1="123"\n data-attr2="hello t here" />');
test_fragment('<?xml version="1.0" encoding="UTF-8" ?><root attr1="foo" attr2="bar"/>', '<?xml version="1.0" encoding="UTF-8" ?>\n<root attr1="foo"\n attr2="bar" />');
test_fragment('<link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,600,700,300&amp;subset=latin" rel="stylesheet" type="text/css">', '<link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,600,700,300&amp;subset=latin"\n rel="stylesheet"\n type="text/css">');

// Attribute Wrap - (indent_attr = "\n ", indent_attr_faligned = " ", indent_over80 = "\n ")
opts.wrap_attributes = 'force-aligned';
opts.wrap_attributes_indent_size = 8;
test_fragment('<div attr0 attr1="123" data-attr2="hello t here">This is some text</div>', '<div attr0\n attr1="123"\n data-attr2="hello t here">This is some text</div>');
test_fragment('<div lookatthissuperduperlongattributenamewhoahcrazy0="true" attr0 attr1="123" data-attr2="hello t here" heymanimreallylongtoowhocomesupwiththesenames="false">This is some text</div>', '<div lookatthissuperduperlongattributenamewhoahcrazy0="true"\n attr0\n attr1="123"\n data-attr2="hello t here"\n heymanimreallylongtoowhocomesupwiththesenames="false">This is some text</div>');
test_fragment('<img attr0 attr1="123" data-attr2="hello t here"/>', '<img attr0\n attr1="123"\n data-attr2="hello t here" />');
test_fragment('<?xml version="1.0" encoding="UTF-8" ?><root attr1="foo" attr2="bar"/>', '<?xml version="1.0" encoding="UTF-8" ?>\n<root attr1="foo"\n attr2="bar" />');
test_fragment('<link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,600,700,300&amp;subset=latin" rel="stylesheet" type="text/css">', '<link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,600,700,300&amp;subset=latin"\n rel="stylesheet"\n type="text/css">');


reset_options();
//============================================================
Expand Down
29 changes: 26 additions & 3 deletions test/data/html/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ exports.test_data = {
}, {
options: [
{ name: "wrap_attributes", value: "'force'" },
{ name: "wrap_attributes_indent_size", value: "8" },
{ name: "wrap_attributes_indent_size", value: "8" }
],
indent_attr: '\\n ',
indent_over80: '\\n '
Expand All @@ -300,6 +300,29 @@ exports.test_data = {
],
indent_attr: ' ',
indent_over80: ' '
}, {
options: [
{ name: "wrap_attributes", value: "'force-aligned'" }
],
indent_attr: '\\n ',
indent_attr_faligned: ' ',
indent_over80: '\\n '
}, {
options: [
{ name: "wrap_attributes", value: "'force-aligned'" },
{ name: "wrap_line_length", value: "80" }
],
indent_attr: '\\n ',
indent_attr_faligned: ' ',
indent_over80: '\\n '
}, {
options: [
{ name: "wrap_attributes", value: "'force-aligned'" },
{ name: "wrap_attributes_indent_size", value: "8" }
],
indent_attr: '\\n ',
indent_attr_faligned: ' ',
indent_over80: '\\n '
}],
tests: [{
fragment: true,
Expand All @@ -316,11 +339,11 @@ exports.test_data = {
}, {
fragment: true,
input: '<?xml version="1.0" encoding="UTF-8" ?><root attr1="foo" attr2="bar"/>',
output: '<?xml version="1.0" encoding="UTF-8" ?>\n<root attr1="foo"{{indent_attr}}attr2="bar" />'
output: '<?xml version="1.0" encoding="UTF-8" ?>\n<root attr1="foo"{{indent_attr}}{{indent_attr_faligned}}attr2="bar" />'
}, {
fragment: true,
input: '<link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,600,700,300&amp;subset=latin" rel="stylesheet" type="text/css">',
output: '<link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,600,700,300&amp;subset=latin"{{indent_over80}}rel="stylesheet"{{indent_attr}}type="text/css">'
output: '<link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,600,700,300&amp;subset=latin"{{indent_over80}}{{indent_attr_faligned}}rel="stylesheet"{{indent_attr}}{{indent_attr_faligned}}type="text/css">'
}]
}, {
name: "Handlebars Indenting Off",
Expand Down

0 comments on commit 56be690

Please sign in to comment.