Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added flag and code to forcestyleliteral #515

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/js-yaml/dumper.js
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