Skip to content

Commit

Permalink
[sync] Add "eol" option to support non-newline line endings
Browse files Browse the repository at this point in the history
  • Loading branch information
Infocatcher committed Mar 24, 2015
1 parent c13dcc9 commit 53942f2
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion jsBeautifier.js
Expand Up @@ -6,7 +6,7 @@
// Version: 0.2.7 - 2015-01-10
// Author: Infocatcher
// Based on scripts from http://jsbeautifier.org/
// [built from https://github.com/beautify-web/js-beautify/tree/master 2015-03-16 20:29:32 UTC]
// [built from https://github.com/beautify-web/js-beautify/tree/master 2015-03-23 22:11:19 UTC]

//===================
//// JavaScript unpacker and beautifier, also can unpack HTML with scripts and styles inside
Expand Down Expand Up @@ -574,6 +574,7 @@ function detectXMLType(str) {

opt.indent_size = options.indent_size ? parseInt(options.indent_size, 10) : 4;
opt.indent_char = options.indent_char ? options.indent_char : ' ';
opt.eol = options.eol ? options.eol : '\n';
opt.preserve_newlines = (options.preserve_newlines === undefined) ? true : options.preserve_newlines;
opt.break_chained_methods = (options.break_chained_methods === undefined) ? false : options.break_chained_methods;
opt.max_preserve_newlines = (options.max_preserve_newlines === undefined) ? 0 : parseInt(options.max_preserve_newlines, 10);
Expand All @@ -600,6 +601,8 @@ function detectXMLType(str) {
opt.indent_size = 1;
}

opt.eol = opt.eol.replace(/\\r/, '\r').replace(/\\n/, '\n')

//----------------------------------
indent_string = '';
while (opt.indent_size > 0) {
Expand Down Expand Up @@ -664,6 +667,10 @@ function detectXMLType(str) {
sweet_code += '\n';
}

if (opt.eol != '\n') {
sweet_code = sweet_code.replace(/[\r]?[\n]/mg, opt.eol);
}

return sweet_code;
};

Expand Down Expand Up @@ -2097,6 +2104,11 @@ function detectXMLType(str) {
(esc || (input.charAt(parser_pos) !== sep &&
(sep === '`' || !acorn.newline.test(input.charAt(parser_pos)))))) {
resulting_string += input.charAt(parser_pos);
// Handle \r\n linebreaks after escapes or in template strings
if (input.charAt(parser_pos) === '\r' && input.charAt(parser_pos + 1) === '\n') {
parser_pos += 1;
resulting_string += '\n';
}
if (esc) {
if (input.charAt(parser_pos) === 'x' || input.charAt(parser_pos) === 'u') {
has_char_escapes = true;
Expand Down Expand Up @@ -4104,6 +4116,13 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
wrapped_input = '{\n' + input.replace(/^(.+)$/mg, ' $1') + '\n foo = bar;\n}';
wrapped_expectation = '{\n' + expectation.replace(/^(.+)$/mg, ' $1') + '\n foo = bar;\n}';
test_fragment(wrapped_input, wrapped_expectation);

// Everywhere we do newlines, they should be replaced with opts.eol
opts.eol = '\r\\n';
wrapped_input = wrapped_input.replace(/[\n]/mg, '\r\n');
wrapped_expectation = wrapped_expectation.replace(/[\n]/mg, '\r\n');
test_fragment(wrapped_input, wrapped_expectation);
opts.eol = '\n';
}

}
Expand Down

0 comments on commit 53942f2

Please sign in to comment.