From 690d65ec513bd5371c3b7b981a4178d3ed1bac97 Mon Sep 17 00:00:00 2001 From: Andreas Lind Petersen Date: Tue, 26 Mar 2013 00:18:46 +0100 Subject: [PATCH] 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. --- lib/output.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/output.js b/lib/output.js index 42b3aad9..8d3f93be 100644 --- a/lib/output.js +++ b/lib/output.js @@ -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, '\\"') + '"'; };