Skip to content

Commit

Permalink
Added option "force-expand-multiline" for "wrap_attributes" (#1070)
Browse files Browse the repository at this point in the history
* introduce "wrap_attributes": "force-all"

result:
<tag
  attribute="hugo"
  other-attribute="sepp"
/>

* introduce "wrap_attributes": "force-all"

fix bug with indendation of previous wrap options

* introduce "wrap_attributes": "force-all"

cleanup

* introduce "wrap_attributes": "force-all"

add unit tests

* rename "wrap_attributes": "force-all" to "wrap_attributes": "force-expand-multiline"
  • Loading branch information
Tobias Zucali authored and bitwiseman committed Dec 16, 2016
1 parent 797e312 commit a32d9d8
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ 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|force-aligned] ["auto"]
-A, --wrap-attributes Wrap attributes to new lines [auto|force|force-aligned|force-expand-multiline] ["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.
Expand Down
54 changes: 41 additions & 13 deletions js/lib/beautify-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
wrap_attributes,
wrap_attributes_indent_size,
is_wrap_attributes_force,
is_wrap_attributes_force_expand_multiline,
is_wrap_attributes_force_aligned,
end_with_newline,
extra_liners,
eol;
Expand Down Expand Up @@ -141,6 +143,8 @@
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';
is_wrap_attributes_force_expand_multiline = (wrap_attributes === 'force-expand-multiline');
is_wrap_attributes_force_aligned = (wrap_attributes === 'force-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 @@ -361,10 +365,13 @@
comment = '',
space = false,
first_attr = true,
has_wrapped_attrs = false,
tag_start, tag_end,
tag_start_char,
orig_pos = this.pos,
orig_line_char_count = this.line_char_count;
orig_line_char_count = this.line_char_count,
is_tag_closed = false,
tail;

peek = peek !== undefined ? peek : false;

Expand All @@ -388,38 +395,57 @@
if (input_char === "'" || input_char === '"') {
input_char += this.get_unformatted(input_char);
space = true;

}

if (input_char === '=') { //no space before =
space = false;
}

tail = this.input.substr(this.pos - 1);
if (is_wrap_attributes_force_expand_multiline && has_wrapped_attrs && !is_tag_closed && (input_char === '>' || input_char === '/')) {
if (tail.match(/^\/?\s*>/)) {
space = false;
is_tag_closed = true;
this.print_newline(false, content);
this.print_indentation(content);
}
}
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 !== '/' && !is_wrap_attributes_force;
space = false;
if (!first_attr && is_wrap_attributes_force && input_char !== '/') {
this.print_newline(false, content);
this.print_indentation(content);
indentAttrs = true;

if (is_wrap_attributes_force && input_char !== '/') {
var force_first_attr_wrap = false;
if (is_wrap_attributes_force_expand_multiline && first_attr) {
var is_only_attribute = tail.match(/^\S*(="([^"]|\\")*")?\s*\/?\s*>/) !== null;
force_first_attr_wrap = !is_only_attribute;
}
if (!first_attr || force_first_attr_wrap) {
this.print_newline(false, content);
this.print_indentation(content);
indentAttrs = true;
}
}
if (indentAttrs) {
has_wrapped_attrs = true;

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

for (var count = 0; count < alignment_size; count++) {
content.push(indent_character);
}
}
for (var i = 0; i < content.length; i++) {
if (content[i] === ' ') {
first_attr = false;
break;
if (first_attr) {
for (var i = 0; i < content.length; i++) {
if (content[i] === ' ') {
first_attr = false;
break;
}
}
}
}
Expand Down Expand Up @@ -478,7 +504,9 @@
var tag_index;
var tag_offset;

if (tag_complete.indexOf(' ') !== -1) { //if there's whitespace, thats where the tag name ends
if (tag_complete.indexOf('\n') !== -1) { //if there's a line break, thats where the tag name ends
tag_index = tag_complete.indexOf('\n');
} else if (tag_complete.indexOf(' ') !== -1) { //if there's whitespace, thats where the tag name ends
tag_index = tag_complete.indexOf(' ');
} else if (tag_complete.charAt(0) === '{') {
tag_index = tag_complete.indexOf('}');
Expand Down
Loading

0 comments on commit a32d9d8

Please sign in to comment.