diff --git a/lib/common/utils.js b/lib/common/utils.js index 4a2cea7..7665587 100644 --- a/lib/common/utils.js +++ b/lib/common/utils.js @@ -148,7 +148,7 @@ function replaceBadSurrogate(ch, pos, orig) { // low surrogate if (pos === 0) { return '\uFFFD'; } code = orig.charCodeAt(pos - 1); - if (code < 0xD900 || code > 0xDBFF) { return '\uFFFD'; } + if (code < 0xD800 || code > 0xDBFF) { return '\uFFFD'; } return ch; } @@ -287,3 +287,6 @@ exports.isWhiteSpace = isWhiteSpace; exports.isMdAsciiPunct = isMdAsciiPunct; exports.isPunctChar = isPunctChar; exports.escapeRE = escapeRE; + +// for testing only +exports.fixBrokenSurrogates = fixBrokenSurrogates; diff --git a/test/misc.js b/test/misc.js index 94234ee..d307c51 100644 --- a/test/misc.js +++ b/test/misc.js @@ -49,6 +49,30 @@ describe('Utils', function () { }); }); + it('fixBrokenSurrogates', function () { + var fixBrokenSurrogates = require('../lib/common/utils').fixBrokenSurrogates; + + // Bad + assert.strictEqual(fixBrokenSurrogates('\uD800foo'), '\uFFFDfoo'); + assert.strictEqual(fixBrokenSurrogates('foo\uD800'), 'foo\uFFFD'); + assert.strictEqual(fixBrokenSurrogates('\uDC00foo'), '\uFFFDfoo'); + assert.strictEqual(fixBrokenSurrogates('foo\uDC00'), 'foo\uFFFD'); + + // Good + assert.strictEqual(fixBrokenSurrogates('\uD800\uDC00'), '\uD800\uDC00'); + }); + + it('normalizeLink', function () { + var normalizeLink = require('../lib/common/utils').normalizeLink; + + // broken surrogates sequence (encodeURI should not throw) + assert.strictEqual(normalizeLink('/\uD800foo'), '/%EF%BF%BDfoo'); + assert.strictEqual(normalizeLink('/\uD900foo'), '/%EF%BF%BDfoo'); + + // broken utf-8 encoding (catch decodeURI exception) + assert.strictEqual(normalizeLink('\u0025test'), '%25test'); + }); + }); @@ -181,7 +205,7 @@ describe('API', function () { describe('Misc', function () { - it('Should strip (or replace) NULL characters', function () { + it('Should replace NULL characters', function () { var md = markdownit(); assert.strictEqual(md.render('foo\u0000bar'), '

foo\uFFFDbar

\n');