Merge pull request #1571 from MacKLess/preserve

Add preserve and preserve_align to html
This commit is contained in:
Liam Newman 2018-10-10 11:12:59 -07:00 committed by GitHub
commit 2f8e4f0502
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 80 additions and 13 deletions

View File

@ -328,7 +328,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|force-expand-multiline|aligned-multiple] ["auto"]
-A, --wrap-attributes Wrap attributes to new lines [auto|force|force-aligned|force-expand-multiline|aligned-multiple|preserve|preserve-aligned] ["auto"]
-i, --wrap-attributes-indent-size Indent wrapped attributes to after N characters [indent-size] (ignored if wrap-attributes is "aligned")
-d, --inline List of tags to be considered inline tags
-U, --unformatted List of tags (defaults to inline) that should not be reformatted

View File

@ -85,7 +85,7 @@ var path = require('path'),
"keep_array_indentation": Boolean,
"unescape_strings": Boolean,
"wrap_line_length": Number,
"wrap_attributes": ["auto", "force", "force-aligned", "force-expand-multiline", "aligned-multiple"],
"wrap_attributes": ["auto", "force", "force-aligned", "force-expand-multiline", "aligned-multiple", "preserve", "preserve-aligned"],
"wrap_attributes_indent_size": Number,
"e4x": Boolean,
"end_with_newline": Boolean,
@ -373,7 +373,7 @@ function usage(err) {
msg.push(' -H, --indent-handlebars Indent handlebars. Default is false.');
msg.push(' -S, --indent-scripts [keep|separate|normal] ["normal"]');
msg.push(' -w, --wrap-line-length Wrap lines at next opportunity after N characters [0]');
msg.push(' -A, --wrap-attributes Wrap html tag attributes to new lines [auto|force] ["auto"]');
msg.push(' -A, --wrap-attributes Wrap html tag attributes to new lines [auto|force|force-aligned|force-expand-multiline|aligned-multiple|preserve|preserve-aligned] ["auto"]');
msg.push(' -i, --wrap-attributes-indent-size Indent wrapped tags to after N characters [indent-level]');
msg.push(' -p, --preserve-newlines Preserve line-breaks (--no-preserve-newlines disables)');
msg.push(' -m, --max-preserve-newlines Number of line-breaks to be preserved in one chunk [10]');

View File

@ -243,6 +243,8 @@ function Beautifier(source_text, options, js_beautify, css_beautify) {
this._is_wrap_attributes_force_expand_multiline = (this._options.wrap_attributes === 'force-expand-multiline');
this._is_wrap_attributes_force_aligned = (this._options.wrap_attributes === 'force-aligned');
this._is_wrap_attributes_aligned_multiple = (this._options.wrap_attributes === 'aligned-multiple');
this._is_wrap_attributes_preserve = this._options.wrap_attributes.substr(0, 'preserve'.length) === 'preserve';
this._is_wrap_attributes_preserve_aligned = (this._options.wrap_attributes === 'preserve-aligned');
}
Beautifier.prototype.beautify = function() {
@ -359,12 +361,19 @@ Beautifier.prototype._handle_inside_tag = function(printer, raw_token, last_tag_
}
if (printer._output.space_before_token && last_tag_token.tag_start_char === '<') {
// Allow the current attribute to wrap
// Set wrapped to true if the line is wrapped
var wrapped = printer.print_space_or_wrap(raw_token.text);
if (raw_token.type === TOKEN.ATTRIBUTE) {
var indentAttrs = wrapped && !this._is_wrap_attributes_force;
if (this._is_wrap_attributes_preserve || this._is_wrap_attributes_preserve_aligned) {
printer.traverse_whitespace(raw_token);
wrapped = wrapped || raw_token.newlines !== 0;
}
// Save whether we have wrapped any attributes
last_tag_token.has_wrapped_attrs = last_tag_token.has_wrapped_attrs || wrapped;
if (this._is_wrap_attributes_force) {
var force_first_attr_wrap = false;
var force_attr_wrap = last_tag_token.attr_count > 1;
if (this._is_wrap_attributes_force_expand_multiline && last_tag_token.attr_count === 1) {
var is_only_attribute = true;
var peek_index = 0;
@ -378,17 +387,14 @@ Beautifier.prototype._handle_inside_tag = function(printer, raw_token, last_tag_
peek_index += 1;
} while (peek_index < 4 && peek_token.type !== TOKEN.EOF && peek_token.type !== TOKEN.TAG_CLOSE);
force_first_attr_wrap = !is_only_attribute;
force_attr_wrap = !is_only_attribute;
}
if (last_tag_token.attr_count > 1 || force_first_attr_wrap) {
if (force_attr_wrap) {
printer.print_newline(false);
indentAttrs = true;
last_tag_token.has_wrapped_attrs = true;
}
}
if (indentAttrs) {
last_tag_token.has_wrapped_attrs = true;
}
}
}
printer.print_token(raw_token.text);
@ -472,7 +478,7 @@ Beautifier.prototype._handle_tag_open = function(printer, raw_token, last_tag_to
}
//indent attributes an auto, forced, aligned or forced-align line-wrap
if (this._is_wrap_attributes_force_aligned || this._is_wrap_attributes_aligned_multiple) {
if (this._is_wrap_attributes_force_aligned || this._is_wrap_attributes_aligned_multiple || this._is_wrap_attributes_preserve_aligned) {
parser_token.alignment_size = raw_token.text.length + 1;
}

View File

@ -39,7 +39,7 @@ function Options(options) {
this.indent_handlebars = this._get_boolean('indent_handlebars', true);
this.wrap_attributes = this._get_selection('wrap_attributes',
['auto', 'force', 'force-aligned', 'force-expand-multiline', 'aligned-multiple']);
['auto', 'force', 'force-aligned', 'force-expand-multiline', 'aligned-multiple', 'preserve', 'preserve-aligned']);
this.wrap_attributes_indent_size = this._get_number('wrap_attributes_indent_size', this.indent_size);
this.extra_liners = this._get_array('extra_liners', ['head', 'body', '/html']);

View File

@ -1824,6 +1824,38 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
'</button>');
//============================================================
// Issue #1125 -- Add preserve and preserve_aligned attribute options - (wrap_attributes = ""preserve-aligned"")
reset_options();
set_name('Issue #1125 -- Add preserve and preserve_aligned attribute options - (wrap_attributes = ""preserve-aligned"")');
opts.wrap_attributes = 'preserve-aligned';
bth(
'<input type="text" class="form-control" autocomplete="off"\n' +
'[(ngModel)]="myValue" [disabled]="isDisabled" [placeholder]="placeholder"\n' +
'[typeahead]="suggestionsSource" [typeaheadOptionField]="suggestionValueField" [typeaheadItemTemplate]="suggestionTemplate" [typeaheadWaitMs]="300"\n' +
'(typeaheadOnSelect)="onSuggestionSelected($event)" />',
// -- output --
'<input type="text" class="form-control" autocomplete="off"\n' +
' [(ngModel)]="myValue" [disabled]="isDisabled" [placeholder]="placeholder"\n' +
' [typeahead]="suggestionsSource" [typeaheadOptionField]="suggestionValueField" [typeaheadItemTemplate]="suggestionTemplate" [typeaheadWaitMs]="300"\n' +
' (typeaheadOnSelect)="onSuggestionSelected($event)" />');
// Issue #1125 -- Add preserve and preserve_aligned attribute options - (wrap_attributes = ""preserve"")
reset_options();
set_name('Issue #1125 -- Add preserve and preserve_aligned attribute options - (wrap_attributes = ""preserve"")');
opts.wrap_attributes = 'preserve';
bth(
'<input type="text" class="form-control" autocomplete="off"\n' +
'[(ngModel)]="myValue" [disabled]="isDisabled" [placeholder]="placeholder"\n' +
'[typeahead]="suggestionsSource" [typeaheadOptionField]="suggestionValueField" [typeaheadItemTemplate]="suggestionTemplate" [typeaheadWaitMs]="300"\n' +
'(typeaheadOnSelect)="onSuggestionSelected($event)" />',
// -- output --
'<input type="text" class="form-control" autocomplete="off"\n' +
' [(ngModel)]="myValue" [disabled]="isDisabled" [placeholder]="placeholder"\n' +
' [typeahead]="suggestionsSource" [typeaheadOptionField]="suggestionValueField" [typeaheadItemTemplate]="suggestionTemplate" [typeaheadWaitMs]="300"\n' +
' (typeaheadOnSelect)="onSuggestionSelected($event)" />');
//============================================================
// Handlebars Indenting Off
reset_options();

View File

@ -751,6 +751,35 @@ exports.test_data = {
'</button>'
]
}]
}, {
name: "Issue #1125 -- Add preserve and preserve_aligned attribute options",
description: "",
template: "^^^ $$$",
matrix: [{
options: [
{ name: "wrap_attributes", value: "'preserve-aligned'" }
],
indent_attr: ' '
}, {
options: [
{ name: "wrap_attributes", value: "'preserve'" }
],
indent_attr: ' '
}],
tests: [{
input: [
'<input type="text" class="form-control" autocomplete="off"',
'[(ngModel)]="myValue" [disabled]="isDisabled" [placeholder]="placeholder"',
'[typeahead]="suggestionsSource" [typeaheadOptionField]="suggestionValueField" [typeaheadItemTemplate]="suggestionTemplate" [typeaheadWaitMs]="300"',
'(typeaheadOnSelect)="onSuggestionSelected($event)" />'
],
output: [
'<input type="text" class="form-control" autocomplete="off"',
'^^^indent_attr$$$[(ngModel)]="myValue" [disabled]="isDisabled" [placeholder]="placeholder"',
'^^^indent_attr$$$[typeahead]="suggestionsSource" [typeaheadOptionField]="suggestionValueField" [typeaheadItemTemplate]="suggestionTemplate" [typeaheadWaitMs]="300"',
'^^^indent_attr$$$(typeaheadOnSelect)="onSuggestionSelected($event)" />'
]
}]
}, {
name: "Handlebars Indenting Off",
description: "Test handlebar behavior when indenting is off",