Add 'aligned' option that align attributes after specific line length if wrap line length is set

This commit is contained in:
Ekaterina 2017-11-23 23:17:56 +01:00 committed by Liam Newman
parent 4b40e69976
commit b894ba305d
4 changed files with 68 additions and 5 deletions

View File

@ -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;
@ -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') ?
@ -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;
}

View File

@ -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;
@ -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') ?
@ -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;
}

View File

@ -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';

View File

@ -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'" },
@ -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",