Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
diberry committed Mar 10, 2019
1 parent e4267fc commit 6fa40d9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
18 changes: 6 additions & 12 deletions lib/js-yaml/dumper.js
Expand Up @@ -194,14 +194,7 @@ function isPlainSafe(c) {
// where nb-char ::= c-printable - b-char - c-byte-order-mark.
return isPrintable(c) && c !== 0xFEFF
// - c-flow-indicator
&& c !== CHAR_COMMA
&& c !== CHAR_LEFT_SQUARE_BRACKET
&& c !== CHAR_RIGHT_SQUARE_BRACKET
&& c !== CHAR_LEFT_CURLY_BRACKET
&& c !== CHAR_RIGHT_CURLY_BRACKET
// - ":" - "#"
&& c !== CHAR_COLON
&& c !== CHAR_SHARP;
&& c !== CHAR_COLON;
}

// Simplified test for values allowed as the first character in plain style.
Expand Down Expand Up @@ -272,7 +265,7 @@ function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth, te
if (!isPrintable(char)) {
return STYLE_DOUBLE;
}
plain = plain && isPlainSafe(char);
plain = plain && (isPlainSafe(char) && !isWhitespace(string.charCodeAt(i + 1)));
}
} else {
// Case: block styles permitted.
Expand All @@ -291,12 +284,14 @@ function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth, te
} else if (!isPrintable(char)) {
return STYLE_DOUBLE;
}
plain = plain && isPlainSafe(char);
if (!isPlainSafe(char)) {
plain = plain && !isWhitespace(string.charCodeAt(i + 1));
}
}
// in case the end is missing a \n
hasFoldableLine = hasFoldableLine || (shouldTrackWidth &&
(i - previousLineBreak - 1 > lineWidth &&
string[previousLineBreak + 1] !== ' '));
string[previousLineBreak + 1] !== ' '));
}
// Although every style can represent \n without escaping, prefer block styles
// for multiline, since they're more readable and they don't add empty lines.
Expand Down Expand Up @@ -760,7 +755,6 @@ function writeNode(state, level, object, block, compact, iskey) {
state.dump = '!<' + state.tag + '> ' + state.dump;
}
}

return true;
}

Expand Down
46 changes: 46 additions & 0 deletions test/issues/0470.js
@@ -0,0 +1,46 @@
'use strict';

var assert = require('assert');
var yaml = require('../..');

test('should not unnecessaryly apply quotes', function () {

var expected = 'url: https://github.com/nodeca/js-yaml\n';
var actual = yaml.dump(
{
url: 'https://github.com/nodeca/js-yaml'
}
);

assert.strictEqual(actual, expected);
});
test('should not unnecessaryly apply quotes - ', function () {

var expected = 'url: https://github.com/nodeca/js-yaml\n';

var obj = {};
obj['url'] = 'https://github.com/nodeca/js-yaml';

var actual = yaml.dump(obj);

assert.strictEqual(actual, expected);
});

test('should not unnecessaryly apply quotes - space then /\n at end of value', function () {

var expected = 'url: \'https://github.com/nodeca/js-yaml \'\n';

var obj = {};
obj['url'] = 'https://github.com/nodeca/js-yaml ';

var actual = yaml.dump(obj);

assert.strictEqual(actual, expected);
});
test('should not unnecessaryly apply quotes - space after colon', function () {

var expected = 'url: \'https: //github.com/nodeca/js-yaml\'\n';
var actual = yaml.dump({ url: 'https: //github.com/nodeca/js-yaml' });

assert.strictEqual(actual, expected);
});

0 comments on commit 6fa40d9

Please sign in to comment.