Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/js-yaml/dumper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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:
Expand Down