Skip to content

Commit

Permalink
Add 'aligned' option that align attributes after specific line length…
Browse files Browse the repository at this point in the history
… if wrap line length is set
  • Loading branch information
Ekaterina authored and bitwiseman committed Jun 30, 2018
1 parent 4b40e69 commit b894ba3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
6 changes: 4 additions & 2 deletions js/lib/beautify-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ function Beautifier(html_source, options, js_beautify, css_beautify) {
is_wrap_attributes_force,
is_wrap_attributes_force_expand_multiline,
is_wrap_attributes_force_aligned,
is_wrap_attributes_aligned,
end_with_newline,
extra_liners,
eol;
Expand Down Expand Up @@ -323,6 +324,7 @@ function Beautifier(html_source, options, js_beautify, css_beautify) {
is_wrap_attributes_force = wrap_attributes.substr(0, 'force'.length) === 'force';
is_wrap_attributes_force_expand_multiline = (wrap_attributes === 'force-expand-multiline');
is_wrap_attributes_force_aligned = (wrap_attributes === 'force-aligned');
is_wrap_attributes_aligned = (wrap_attributes === 'aligned');
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 @@ -659,9 +661,9 @@ function Beautifier(html_source, options, js_beautify, css_beautify) {
if (indentAttrs) {
has_wrapped_attrs = true;

//indent attributes an auto, forced, or forced-align line-wrap
//indent attributes an auto, forced, aligned or forced-align line-wrap
var alignment_size = wrap_attributes_indent_size;
if (is_wrap_attributes_force_aligned) {
if (is_wrap_attributes_force_aligned || is_wrap_attributes_aligned) {
alignment_size = content.indexOf(' ') + 1;
}

Expand Down
6 changes: 4 additions & 2 deletions js/src/html/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function Beautifier(html_source, options, js_beautify, css_beautify) {
is_wrap_attributes_force,
is_wrap_attributes_force_expand_multiline,
is_wrap_attributes_force_aligned,
is_wrap_attributes_aligned,
end_with_newline,
extra_liners,
eol;
Expand Down Expand Up @@ -115,6 +116,7 @@ function Beautifier(html_source, options, js_beautify, css_beautify) {
is_wrap_attributes_force = wrap_attributes.substr(0, 'force'.length) === 'force';
is_wrap_attributes_force_expand_multiline = (wrap_attributes === 'force-expand-multiline');
is_wrap_attributes_force_aligned = (wrap_attributes === 'force-aligned');
is_wrap_attributes_aligned = (wrap_attributes === 'aligned');
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 @@ -451,9 +453,9 @@ function Beautifier(html_source, options, js_beautify, css_beautify) {
if (indentAttrs) {
has_wrapped_attrs = true;

//indent attributes an auto, forced, or forced-align line-wrap
//indent attributes an auto, forced, aligned or forced-align line-wrap
var alignment_size = wrap_attributes_indent_size;
if (is_wrap_attributes_force_aligned) {
if (is_wrap_attributes_force_aligned || is_wrap_attributes_aligned) {
alignment_size = content.indexOf(' ') + 1;
}

Expand Down
39 changes: 39 additions & 0 deletions js/test/generated/beautify-html-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,45 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
' rel="stylesheet"\n' +
' type="text/css">');

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

// Attribute Wrap - (indent_attr = " ", indent_attr_first = " ", indent_end = "", indent_end_selfclosing = " ", indent_over80 = " ")
reset_options();
opts.wrap_attributes = 'aligned';
test_fragment('<div >This is some text</div>', '<div>This is some text</div>');
test_fragment('<div attr="123" >This is some text</div>', '<div attr="123">This is some text</div>');
test_fragment('<div attr0 attr1="123" 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>');
test_fragment('<img attr0 attr1="123" data-attr2="hello t here"/>', '<img attr0 attr1="123" data-attr2="hello t here" />');
test_fragment(
'<?xml version="1.0" encoding="UTF-8" ?><root attr1="foo" attr2="bar"/>',
// -- output --
'<?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_attr_first = " ", indent_end = "", indent_end_selfclosing = " ", indent_over80 = "\n ")
reset_options();
opts.wrap_attributes = 'force-aligned';
Expand Down
22 changes: 21 additions & 1 deletion test/data/html/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,26 @@ exports.test_data = {
indent_end: '',
indent_end_selfclosing: ' ',
indent_over80: '\n '
}, {
options: [
{ name: "wrap_attributes", value: "'aligned'" },
{ name: "wrap_line_length", value: "80" }
],
indent_attr: ' ',
indent_attr_first: ' ',
indent_end: '',
indent_attr_aligned: ' ',
indent_end_selfclosing: ' ',
indent_over80: '\n '
}, {
options: [
{ name: "wrap_attributes", value: "'aligned'" },
],
indent_attr: ' ',
indent_attr_first: ' ',
indent_end: '',
indent_end_selfclosing: ' ',
indent_over80: ' '
}, {
options: [
{ name: "wrap_attributes", value: "'force-aligned'" },
Expand Down Expand Up @@ -474,7 +494,7 @@ exports.test_data = {
}, {
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{{indent_attr_first}}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"{{indent_end}}>'
output: '<link{{indent_attr_first}}href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,600,700,300&amp;subset=latin"{{indent_over80}}{{indent_attr_faligned}}{{indent_attr_aligned}}rel="stylesheet"{{indent_attr}}{{indent_attr_faligned}}type="text/css"{{indent_end}}>'
}]
}, {
name: "Handlebars Indenting Off",
Expand Down

0 comments on commit b894ba3

Please sign in to comment.