Skip to content

Commit

Permalink
Only quote []{} in block, not in flow context
Browse files Browse the repository at this point in the history
  • Loading branch information
rlidwka committed Dec 17, 2020
1 parent 91f9662 commit eb08b0e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/dumper.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,12 @@ function isNsCharOrWhitespace(c) {
// [130] ns-plain-char(c) ::= ( ns-plain-safe(c) - “:” - “#” )
// | ( /* An ns-char preceding */ “#” )
// | ( “:” /* Followed by an ns-plain-safe(c) */ )
function isPlainSafe(c, prev, flowLevel) {
function isPlainSafe(c, prev, inblock) {
var cIsNsCharOrWhitespace = isNsCharOrWhitespace(c);
var cIsNsChar = cIsNsCharOrWhitespace && !isWhitespace(c);
return (
// ns-plain-safe
flowLevel < 0 ? // c = flow-in
inblock ? // c = flow-in
cIsNsCharOrWhitespace
: cIsNsCharOrWhitespace
// - c-flow-indicator
Expand Down Expand Up @@ -314,7 +314,7 @@ var STYLE_PLAIN = 1,
// 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, quotingType, forceQuotes, flowLevel) {
testAmbiguousType, quotingType, forceQuotes, inblock) {

var i;
var char = 0;
Expand All @@ -334,7 +334,7 @@ function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth,
if (!isPrintable(char)) {
return STYLE_DOUBLE;
}
plain = plain && isPlainSafe(char, prevChar, flowLevel);
plain = plain && isPlainSafe(char, prevChar, inblock);
prevChar = char;
}
} else {
Expand All @@ -354,7 +354,7 @@ function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth,
} else if (!isPrintable(char)) {
return STYLE_DOUBLE;
}
plain = plain && isPlainSafe(char, prevChar, flowLevel);
plain = plain && isPlainSafe(char, prevChar, inblock);
prevChar = char;
}
// in case the end is missing a \n
Expand Down Expand Up @@ -391,7 +391,7 @@ function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth,
// • No ending newline => unaffected; already using strip "-" chomping.
// • Ending newline => removed then restored.
// Importantly, this keeps the "+" chomp indicator from gaining an extra line.
function writeScalar(state, string, level, iskey) {
function writeScalar(state, string, level, iskey, inblock) {
state.dump = (function () {
if (string.length === 0) {
return state.quotingType === QUOTING_TYPE_DOUBLE ? '""' : "''";
Expand Down Expand Up @@ -422,7 +422,7 @@ function writeScalar(state, string, level, iskey) {
}

switch (chooseScalarStyle(string, singleLineOnly, state.indent, lineWidth,
testAmbiguity, state.quotingType, state.forceQuotes && !iskey, state.flowLevel)) {
testAmbiguity, state.quotingType, state.forceQuotes && !iskey, inblock)) {

case STYLE_PLAIN:
return string;
Expand Down Expand Up @@ -766,6 +766,7 @@ function writeNode(state, level, object, block, compact, iskey, isblockseq) {
}

var type = _toString.call(state.dump);
var inblock = block;

if (block) {
block = (state.flowLevel < 0 || state.flowLevel > level);
Expand Down Expand Up @@ -820,7 +821,7 @@ function writeNode(state, level, object, block, compact, iskey, isblockseq) {
}
} else if (type === '[object String]') {
if (state.tag !== '?') {
writeScalar(state, state.dump, level, iskey);
writeScalar(state, state.dump, level, iskey, inblock);
}
} else {
if (state.skipInvalid) return false;
Expand Down
34 changes: 34 additions & 0 deletions test/issues/0521.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,37 @@ parameter#fallback: 'quotes #required'
required
);
});


it('Quote []{} in block-level scalars, but not in flow', function () {
var required = `
key1: a[]b
key2: a{}b
nested:
key1: a[]b
key2: a{}b
nested: {key1: 'a[]b', key2: 'a{}b', nested: {key1: 'a[]b', key2: 'a{}b'}}
`.replace(/^\n/, '');

var sample = {
key1: 'a[]b',
key2: 'a{}b',
nested: {
key1: 'a[]b',
key2: 'a{}b',
nested: {
key1: 'a[]b',
key2: 'a{}b',
nested: {
key1: 'a[]b',
key2: 'a{}b'
}
}
}
};

assert.strictEqual(
yaml.dump(sample, { flowLevel: 2 }),
required
);
});

0 comments on commit eb08b0e

Please sign in to comment.