Skip to content

Commit 579ea06

Browse files
committed
Add unformatted_content_delimiter
Fixes beautifier#1560
1 parent 8131d27 commit 579ea06

File tree

6 files changed

+73
-2
lines changed

6 files changed

+73
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ HTML Beautifier Options:
337337
-E, --extra_liners List of tags (defaults to [head,body,/html] that should have an extra newline before them.
338338
--editorconfig Use EditorConfig to set up the options
339339
--indent_scripts Sets indent level inside script tags ("normal", "keep", "separate")
340+
--unformatted_content_delimiter Keep text content together between this string [""]
340341
```
341342

342343
## Directives to Ignore or Preserve sections (Javascript beautifier only)

js/src/cli.js

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

js/src/html/options.js

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ function Options(options) {
7373
this.content_unformatted = this._get_array('content_unformatted', [
7474
'pre', 'textarea'
7575
]);
76+
this.unformatted_content_delimiter = this._get_characters('unformatted_content_delimiter');
7677
this.indent_scripts = this._get_selection('indent_scripts', ['normal', 'keep', 'separate']);
7778
}
7879
Options.prototype = new BaseOptions();

js/src/html/tokenizer.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ var Tokenizer = function(input_string, options) {
5555
// Words end at whitespace or when a tag starts
5656
// if we are indenting handlebars, they are considered tags
5757
this._word_pattern = this._options.indent_handlebars ? /[\n\r\t <]|{{/g : /[\n\r\t <]/g;
58+
this._unformatted_content_delimiter = null;
59+
60+
if (this._options.unformatted_content_delimiter) {
61+
this._unformatted_content_delimiter =
62+
new RegExp(this._options.unformatted_content_delimiter
63+
.replace(/([[\\^$.|?*+()])/g, '\\$1'), 'g');
64+
}
5865
};
5966
Tokenizer.prototype = new BaseTokenizer();
6067

@@ -246,8 +253,16 @@ Tokenizer.prototype._read_raw_content = function(previous_token, open_token) { /
246253
};
247254

248255
Tokenizer.prototype._read_content_word = function() {
249-
// if we get here and we see handlebars treat them as plain text
250-
var resulting_string = this._input.readUntil(this._word_pattern);
256+
var resulting_string;
257+
if (this._unformatted_content_delimiter) {
258+
resulting_string = this._input.read(this._unformatted_content_delimiter);
259+
}
260+
if (resulting_string) {
261+
resulting_string += this._input.readUntilAfter(this._unformatted_content_delimiter);
262+
} else {
263+
// if we get here and we see handlebars treat them as plain text
264+
resulting_string = this._input.readUntil(this._word_pattern);
265+
}
251266
if (resulting_string) {
252267
return this._create_token(TOKEN.TEXT, resulting_string);
253268
}

js/test/generated/beautify-html-tests.js

+28
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,34 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
732732
' type="button">Save</button>');
733733

734734

735+
//============================================================
736+
// unformatted_content_delimiter ^^
737+
reset_options();
738+
set_name('unformatted_content_delimiter ^^');
739+
opts.wrap_line_length = 80;
740+
opts.unformatted_content_delimiter = '^^';
741+
test_fragment(
742+
'<span>0 0001 0002 0003 0004 0005 0006 0007 0008 ^^09 0010 0011 0012 0013 0014 0015 ^^16 0017 0018 0019 0020</span>',
743+
// -- output --
744+
'<span>0 0001 0002 0003 0004 0005 0006 0007 0008\n' +
745+
' ^^09 0010 0011 0012 0013 0014 0015 ^^16 0017 0018 0019 0020</span>');
746+
test_fragment(
747+
'<span>0 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 0016 0017 0018 0019 0020</span>',
748+
// -- output --
749+
'<span>0 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014\n' +
750+
' 0015 0016 0017 0018 0019 0020</span>');
751+
test_fragment(
752+
'<span>0 0001 0002 0003 0004 0005 0006 0007 0008 0009 ^^10 0011 0012 0013 0014 0015 0016 0^^7 0018 0019 0020</span>',
753+
// -- output --
754+
'<span>0 0001 0002 0003 0004 0005 0006 0007 0008 0009\n' +
755+
' ^^10 0011 0012 0013 0014 0015 0016 0^^7 0018 0019 0020</span>');
756+
test_fragment(
757+
'<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>',
758+
// -- output --
759+
'<span>0 0001 0002 0003 0004 0005 0006 0007 0008 0009 0^^0 0011 0012 0013 0014\n' +
760+
' 0015 0016 0^^7 0018 0019 0020</span>');
761+
762+
735763
//============================================================
736764
// Attribute Wrap - (wrap_attributes = ""force"")
737765
reset_options();

test/data/html/tests.js

+24
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,30 @@ exports.test_data = {
517517
' type="button">Save</button>'
518518
]
519519
}]
520+
}, {
521+
name: "unformatted_content_delimiter ^^",
522+
description: "keep delimited together",
523+
options: [
524+
{ name: "wrap_line_length", value: "80" },
525+
{ name: "unformatted_content_delimiter", value: "'^^'" }
526+
],
527+
tests: [{
528+
fragment: true,
529+
input: '<span>0 0001 0002 0003 0004 0005 0006 0007 0008 ^^09 0010 0011 0012 0013 0014 0015 ^^16 0017 0018 0019 0020</span>',
530+
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>'
531+
}, {
532+
fragment: true,
533+
input: '<span>0 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 0016 0017 0018 0019 0020</span>',
534+
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>'
535+
}, {
536+
fragment: true,
537+
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>',
538+
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>'
539+
}, {
540+
fragment: true,
541+
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>',
542+
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>'
543+
}]
520544
}, {
521545
name: "Attribute Wrap",
522546
description: "Wraps attributes inside of html tags",

0 commit comments

Comments
 (0)