OutputStream: Added support for quote_char option.

Can be either ', ", or null (null means choose the one with fewest occurrences in the string, like the previous behavior).

Use case: Serializing data-bind attributes (Knockout.js) into doublequote delimited attributes while avoiding an excessive number of " entities.
This commit is contained in:
Andreas Lind Petersen 2013-03-26 00:18:46 +01:00
parent 7628bcac01
commit 690d65ec51

View File

@ -60,7 +60,8 @@ function OutputStream(options) {
bracketize : false,
semicolons : true,
comments : false,
preserve_line : false
preserve_line : false,
quote_char : null
}, true);
var indentation = 0;
@ -95,7 +96,8 @@ function OutputStream(options) {
return s;
});
if (options.ascii_only) str = to_ascii(str);
if (dq > sq) return "'" + str.replace(/\x27/g, "\\'") + "'";
var quote_char = options.quote_char || (dq > sq ? "'" : '"');
if (quote_char === "'") return "'" + str.replace(/\x27/g, "\\'") + "'";
else return '"' + str.replace(/\x22/g, '\\"') + '"';
};