Skip to content

Commit

Permalink
Process astral characters correctly
Browse files Browse the repository at this point in the history
According to YAML 1.2 spec, #xD800-#xDFFF unicode range is considered
non-printables. But unicode range #x10000-#x10FFFF, represented
in UTF-16 by the same characters is not.

NOTE: Non-printable characters are still allowed in quoted scalars,
added a pending test for this case.

Fixes: #191
  • Loading branch information
rlidwka committed May 13, 2015
1 parent ad21884 commit e26fe90
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/js-yaml/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var CHOMPING_STRIP = 2;
var CHOMPING_KEEP = 3;


var PATTERN_NON_PRINTABLE = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x84\x86-\x9F\uD800-\uDFFF\uFFFE\uFFFF]/;
var PATTERN_NON_PRINTABLE = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x84\x86-\x9F\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/;
var PATTERN_NON_ASCII_LINE_BREAKS = /[\x85\u2028\u2029]/;
var PATTERN_FLOW_INDICATORS = /[,\[\]\{\}]/;
var PATTERN_TAG_HANDLE = /^(?:!|!!|![a-z\-]+!)$/i;
Expand Down
25 changes: 25 additions & 0 deletions test/units/character-set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';


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


test('Allow astral characters', function () {
assert.deepEqual(yaml.load('π‘˜π‘’π‘¦: π‘£π‘Žπ‘™π‘’π‘’'), { 'π‘˜π‘’π‘¦': 'π‘£π‘Žπ‘™π‘’π‘’' });
});

test('Forbid non-printable characters', function () {
assert.throws(function () { yaml.load('\x01'); }, yaml.YAMLException);
assert.throws(function () { yaml.load('\x7f'); }, yaml.YAMLException);
assert.throws(function () { yaml.load('\x9f'); }, yaml.YAMLException);
});

test('Forbid lone surrogates', function () {
assert.throws(function () { yaml.load('\udc00\ud800'); }, yaml.YAMLException);
});

test.skip('Allow non-printable characters inside quoted scalars', function () {
assert.strictEqual(yaml.load('"\x7f\x9f\udc00\ud800"'), '\x7f\x9f\udc00\ud800');
});

0 comments on commit e26fe90

Please sign in to comment.