Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[dumper] Implement simple scalar style detection.

  • Loading branch information...
commit ddadd7ae6dc7bfbfe55eb8e283f251d74ea86b16 1 parent a84ef06
Dervus Grim dervus authored
Showing with 85 additions and 4 deletions.
  1. +85 −4 lib/js-yaml/dumper.js
89 lib/js-yaml/dumper.js
View
@@ -11,6 +11,31 @@ var SAFE_SCHEMA = require('./schema/safe');
var _hasOwnProperty = Object.prototype.hasOwnProperty;
+var CHAR_TAB = 0x09; /* Tab */
+var CHAR_LINE_FEED = 0x0A; /* LF */
+var CHAR_CARRIAGE_RETURN = 0x0D; /* CR */
+var CHAR_SPACE = 0x20; /* Space */
+var CHAR_EXCLAMATION = 0x21; /* ! */
+var CHAR_DOUBLE_QUOTE = 0x22; /* " */
+var CHAR_SHARP = 0x23; /* # */
+var CHAR_PERCENT = 0x25; /* % */
+var CHAR_AMPERSAND = 0x26; /* & */
+var CHAR_SINGLE_QUOTE = 0x27; /* ' */
+var CHAR_ASTERISK = 0x2A; /* * */
+var CHAR_COMMA = 0x2C; /* , */
+var CHAR_MINUS = 0x2D; /* - */
+var CHAR_COLON = 0x3A; /* : */
+var CHAR_GREATER_THAN = 0x3E; /* > */
+var CHAR_QUESTION = 0x3F; /* ? */
+var CHAR_COMMERCIAL_AT = 0x40; /* @ */
+var CHAR_LEFT_SQUARE_BRACKET = 0x5B; /* [ */
+var CHAR_RIGHT_SQUARE_BRACKET = 0x5D; /* ] */
+var CHAR_GRAVE_ACCENT = 0x60; /* ` */
+var CHAR_LEFT_CURLY_BRACKET = 0x7B; /* { */
+var CHAR_VERTICAL_LINE = 0x7C; /* | */
+var CHAR_RIGHT_CURLY_BRACKET = 0x7D; /* } */
+
+
var ESCAPE_SEQUENCES = {};
ESCAPE_SEQUENCES[0x00] = '\\0';
@@ -117,15 +142,64 @@ function dump(input, settings) {
return '\n' + common.repeat(' ', indent * level);
}
- function writeQuotedScalar(object) {
- var checkpoint, position, length, character;
+ function testImplicitResolving(object) {
+ var index, length, type;
+
+ for (index = 0, length = implicitTypes.length; index < length; index += 1) {
+ type = implicitTypes[index];
+
+ if (null !== type.loader &&
+ NIL !== type.loader.resolver(object, false)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ function writeScalar(object) {
+ var isQuoted, checkpoint, position, length, character;
result = '';
+ isQuoted = false;
checkpoint = 0;
+ if (0 === object.length ||
+ CHAR_SPACE === object.charCodeAt(0) ||
+ CHAR_SPACE === object.charCodeAt(object.length - 1)) {
+ isQuoted = true;
+ }
+
for (position = 0, length = object.length; position < length; position += 1) {
character = object.charCodeAt(position);
+ if (!isQuoted) {
+ if (CHAR_TAB === character ||
+ CHAR_LINE_FEED === character ||
+ CHAR_CARRIAGE_RETURN === character ||
+ CHAR_COMMA === character ||
+ CHAR_LEFT_SQUARE_BRACKET === character ||
+ CHAR_RIGHT_SQUARE_BRACKET === character ||
+ CHAR_LEFT_CURLY_BRACKET === character ||
+ CHAR_RIGHT_CURLY_BRACKET === character ||
+ CHAR_SHARP === character ||
+ CHAR_AMPERSAND === character ||
+ CHAR_ASTERISK === character ||
+ CHAR_EXCLAMATION === character ||
+ CHAR_VERTICAL_LINE === character ||
+ CHAR_GREATER_THAN === character ||
+ CHAR_SINGLE_QUOTE === character ||
+ CHAR_DOUBLE_QUOTE === character ||
+ CHAR_PERCENT === character ||
+ CHAR_COMMERCIAL_AT === character ||
+ CHAR_GRAVE_ACCENT === character ||
+ CHAR_QUESTION === character ||
+ CHAR_COLON === character ||
+ CHAR_MINUS === character) {
+ isQuoted = true;
+ }
+ }
+
if (ESCAPE_SEQUENCES[character] ||
!((0x00020 <= character && character <= 0x00007E) ||
(0x00085 === character) ||
@@ -135,6 +209,7 @@ function dump(input, settings) {
result += object.slice(checkpoint, position);
result += ESCAPE_SEQUENCES[character] || encodeHex(character);
checkpoint = position + 1;
+ isQuoted = true;
}
}
@@ -142,7 +217,13 @@ function dump(input, settings) {
result += object.slice(checkpoint, position);
}
- result = '"' + result + '"';
+ if (!isQuoted && testImplicitResolving(result)) {
+ isQuoted = true;
+ }
+
+ if (isQuoted) {
+ result = '"' + result + '"';
+ }
}
function writeFlowSequence(level, object) {
@@ -329,7 +410,7 @@ function dump(input, settings) {
}
} else if ('string' === kind) {
if ('?' !== tag) {
- writeQuotedScalar(result);
+ writeScalar(result);
}
} else {
throw new YAMLException('unacceptabe kind of an object to dump (' + kind + ')');
Please sign in to comment.
Something went wrong with that request. Please try again.