From 6fa40d98070dd4b5eff679a603f117444c31c716 Mon Sep 17 00:00:00 2001 From: Dina Berry Date: Sun, 10 Mar 2019 05:56:05 -0700 Subject: [PATCH] #470 fix --- lib/js-yaml/dumper.js | 18 ++++++----------- test/issues/0470.js | 46 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 test/issues/0470.js diff --git a/lib/js-yaml/dumper.js b/lib/js-yaml/dumper.js index 86f34794..356e107a 100644 --- a/lib/js-yaml/dumper.js +++ b/lib/js-yaml/dumper.js @@ -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. @@ -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. @@ -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. @@ -760,7 +755,6 @@ function writeNode(state, level, object, block, compact, iskey) { state.dump = '!<' + state.tag + '> ' + state.dump; } } - return true; } diff --git a/test/issues/0470.js b/test/issues/0470.js new file mode 100644 index 00000000..13d2a6c6 --- /dev/null +++ b/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); +});