Add unformatted_content_delimiter

Fixes #1560
This commit is contained in:
Liam Newman 2019-01-13 12:45:52 -08:00
parent 8131d2713f
commit 579ea06ca1
6 changed files with 73 additions and 2 deletions

View File

@ -337,6 +337,7 @@ HTML Beautifier Options:
-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
--indent_scripts Sets indent level inside script tags ("normal", "keep", "separate")
--unformatted_content_delimiter Keep text content together between this string [""]
```
## Directives to Ignore or Preserve sections (Javascript beautifier only)

View File

@ -107,6 +107,7 @@ var path = require('path'),
"indent_handlebars": [Boolean],
"indent_scripts": ["keep", "separate", "normal"],
"extra_liners": [String, Array],
"unformatted_content_delimiter": String,
// CLI
"version": Boolean,
"help": Boolean,
@ -382,6 +383,7 @@ function usage(err) {
msg.push(' -U, --unformatted List of tags (defaults to inline) that should not be reformatted');
msg.push(' -T, --content_unformatted List of tags (defaults to pre) whose content should not be reformatted');
msg.push(' -E, --extra_liners List of tags (defaults to [head,body,/html] that should have an extra newline');
msg.push(' --unformatted_content_delimiter Keep text content together between this string [""]');
break;
case "css":
msg.push(' -L, --selector-separator-newline Add a newline between multiple selectors.');

View File

@ -73,6 +73,7 @@ function Options(options) {
this.content_unformatted = this._get_array('content_unformatted', [
'pre', 'textarea'
]);
this.unformatted_content_delimiter = this._get_characters('unformatted_content_delimiter');
this.indent_scripts = this._get_selection('indent_scripts', ['normal', 'keep', 'separate']);
}
Options.prototype = new BaseOptions();

View File

@ -55,6 +55,13 @@ var Tokenizer = function(input_string, options) {
// Words end at whitespace or when a tag starts
// if we are indenting handlebars, they are considered tags
this._word_pattern = this._options.indent_handlebars ? /[\n\r\t <]|{{/g : /[\n\r\t <]/g;
this._unformatted_content_delimiter = null;
if (this._options.unformatted_content_delimiter) {
this._unformatted_content_delimiter =
new RegExp(this._options.unformatted_content_delimiter
.replace(/([[\\^$.|?*+()])/g, '\\$1'), 'g');
}
};
Tokenizer.prototype = new BaseTokenizer();
@ -246,8 +253,16 @@ Tokenizer.prototype._read_raw_content = function(previous_token, open_token) { /
};
Tokenizer.prototype._read_content_word = function() {
// if we get here and we see handlebars treat them as plain text
var resulting_string = this._input.readUntil(this._word_pattern);
var resulting_string;
if (this._unformatted_content_delimiter) {
resulting_string = this._input.read(this._unformatted_content_delimiter);
}
if (resulting_string) {
resulting_string += this._input.readUntilAfter(this._unformatted_content_delimiter);
} else {
// if we get here and we see handlebars treat them as plain text
resulting_string = this._input.readUntil(this._word_pattern);
}
if (resulting_string) {
return this._create_token(TOKEN.TEXT, resulting_string);
}

View File

@ -732,6 +732,34 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
' type="button">Save</button>');
//============================================================
// unformatted_content_delimiter ^^
reset_options();
set_name('unformatted_content_delimiter ^^');
opts.wrap_line_length = 80;
opts.unformatted_content_delimiter = '^^';
test_fragment(
'<span>0 0001 0002 0003 0004 0005 0006 0007 0008 ^^09 0010 0011 0012 0013 0014 0015 ^^16 0017 0018 0019 0020</span>',
// -- output --
'<span>0 0001 0002 0003 0004 0005 0006 0007 0008\n' +
' ^^09 0010 0011 0012 0013 0014 0015 ^^16 0017 0018 0019 0020</span>');
test_fragment(
'<span>0 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 0016 0017 0018 0019 0020</span>',
// -- output --
'<span>0 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014\n' +
' 0015 0016 0017 0018 0019 0020</span>');
test_fragment(
'<span>0 0001 0002 0003 0004 0005 0006 0007 0008 0009 ^^10 0011 0012 0013 0014 0015 0016 0^^7 0018 0019 0020</span>',
// -- output --
'<span>0 0001 0002 0003 0004 0005 0006 0007 0008 0009\n' +
' ^^10 0011 0012 0013 0014 0015 0016 0^^7 0018 0019 0020</span>');
test_fragment(
'<span>0 0001 0002 0003 0004 0005 0006 0007 0008 0009 0^^0 0011 0012 0013 0014 0015 0016 0^^7 0018 0019 0020</span>',
// -- output --
'<span>0 0001 0002 0003 0004 0005 0006 0007 0008 0009 0^^0 0011 0012 0013 0014\n' +
' 0015 0016 0^^7 0018 0019 0020</span>');
//============================================================
// Attribute Wrap - (wrap_attributes = ""force"")
reset_options();

View File

@ -517,6 +517,30 @@ exports.test_data = {
' type="button">Save</button>'
]
}]
}, {
name: "unformatted_content_delimiter ^^",
description: "keep delimited together",
options: [
{ name: "wrap_line_length", value: "80" },
{ name: "unformatted_content_delimiter", value: "'^^'" }
],
tests: [{
fragment: true,
input: '<span>0 0001 0002 0003 0004 0005 0006 0007 0008 ^^09 0010 0011 0012 0013 0014 0015 ^^16 0017 0018 0019 0020</span>',
output: '<span>0 0001 0002 0003 0004 0005 0006 0007 0008\n ^^09 0010 0011 0012 0013 0014 0015 ^^16 0017 0018 0019 0020</span>'
}, {
fragment: true,
input: '<span>0 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 0016 0017 0018 0019 0020</span>',
output: '<span>0 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014\n 0015 0016 0017 0018 0019 0020</span>'
}, {
fragment: true,
input: '<span>0 0001 0002 0003 0004 0005 0006 0007 0008 0009 ^^10 0011 0012 0013 0014 0015 0016 0^^7 0018 0019 0020</span>',
output: '<span>0 0001 0002 0003 0004 0005 0006 0007 0008 0009\n ^^10 0011 0012 0013 0014 0015 0016 0^^7 0018 0019 0020</span>'
}, {
fragment: true,
input: '<span>0 0001 0002 0003 0004 0005 0006 0007 0008 0009 0^^0 0011 0012 0013 0014 0015 0016 0^^7 0018 0019 0020</span>',
output: '<span>0 0001 0002 0003 0004 0005 0006 0007 0008 0009 0^^0 0011 0012 0013 0014\n 0015 0016 0^^7 0018 0019 0020</span>'
}]
}, {
name: "Attribute Wrap",
description: "Wraps attributes inside of html tags",