diff --git a/lib/js-yaml/dumper.js b/lib/js-yaml/dumper.js index 86f34794..41119d48 100644 --- a/lib/js-yaml/dumper.js +++ b/lib/js-yaml/dumper.js @@ -116,7 +116,7 @@ function State(options) { this.noRefs = options['noRefs'] || false; this.noCompatMode = options['noCompatMode'] || false; this.condenseFlow = options['condenseFlow'] || false; - + this.forceStyleLiteral = options['forceStyleLiteral'] || false; this.implicitTypes = this.schema.compiledImplicit; this.explicitTypes = this.schema.compiledExplicit; @@ -254,7 +254,7 @@ var STYLE_PLAIN = 1, // STYLE_PLAIN or STYLE_SINGLE => no \n are in the string. // STYLE_LITERAL => no lines are suitable for folding (or lineWidth is -1). // STYLE_FOLDED => a line > lineWidth and can be folded (and lineWidth != -1). -function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth, testAmbiguousType) { +function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth, testAmbiguousType, forceStyleLiteral) { var i; var char; var hasLineBreak = false; @@ -270,6 +270,9 @@ function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth, te for (i = 0; i < string.length; i++) { char = string.charCodeAt(i); if (!isPrintable(char)) { + if (forceStyleLiteral) { + return STYLE_LITERAL; + } return STYLE_DOUBLE; } plain = plain && isPlainSafe(char); @@ -289,6 +292,9 @@ function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth, te previousLineBreak = i; } } else if (!isPrintable(char)) { + if (forceStyleLiteral) { + return STYLE_LITERAL; + } return STYLE_DOUBLE; } plain = plain && isPlainSafe(char); @@ -351,7 +357,8 @@ function writeScalar(state, string, level, iskey) { return testImplicitResolving(state, string); } - switch (chooseScalarStyle(string, singleLineOnly, state.indent, lineWidth, testAmbiguity)) { + switch (chooseScalarStyle(string, singleLineOnly, state.indent, lineWidth, testAmbiguity, + state.forceStyleLiteral)) { case STYLE_PLAIN: return string; case STYLE_SINGLE: