Browse Source

Fixed broken surrogates replacer

pull/41/head
Vitaly Puzrin 10 years ago
parent
commit
47b71bae94
  1. 5
      lib/common/utils.js
  2. 26
      test/misc.js

5
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;

26
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'), '<p>foo\uFFFDbar</p>\n');

Loading…
Cancel
Save