diff --git a/HISTORY.md b/HISTORY.md index 2ec365f..6d9e0ce 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,8 @@ +unreleased +========== + + * Fix encoding unpaired surrogates at start/end of string + 1.0.0 / 2016-06-08 ================== diff --git a/index.js b/index.js index 6ac7738..ae77cc9 100644 --- a/index.js +++ b/index.js @@ -26,7 +26,7 @@ var ENCODE_CHARS_REGEXP = /(?:[^\x21\x25\x26-\x3B\x3D\x3F-\x5B\x5D\x5F\x61-\x7A\ * @private */ -var UNMATCHED_SURROGATE_PAIR_REGEXP = /([^\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF]([^\uDC00-\uDFFF])/g +var UNMATCHED_SURROGATE_PAIR_REGEXP = /(^|[^\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF]([^\uDC00-\uDFFF]|$)/g /** * String to replace unmatched surrogate pair with. diff --git a/test/test.js b/test/test.js index d5c543a..5b215ec 100644 --- a/test/test.js +++ b/test/test.js @@ -76,8 +76,18 @@ describe('encodeUrl(url)', function () { assert.equal(encodeUrl('http://localhost/\uD83D\uDC7B snow.html'), 'http://localhost/%F0%9F%91%BB%20snow.html') }) - it('should encode unpaired surrogate pairs as replacement character', function () { - assert.equal(encodeUrl('http://localhost/\uD83Dfoo\uDC7B <\uDC7B\uD83D>.html'), 'http://localhost/%EF%BF%BDfoo%EF%BF%BD%20%3C%EF%BF%BD%EF%BF%BD%3E.html') + describe('when unpaired', function () { + it('should encode as replacement character', function () { + assert.equal(encodeUrl('http://localhost/\uD83Dfoo\uDC7B <\uDC7B\uD83D>.html'), 'http://localhost/%EF%BF%BDfoo%EF%BF%BD%20%3C%EF%BF%BD%EF%BF%BD%3E.html') + }) + + it('should encode at end of string', function () { + assert.equal(encodeUrl('http://localhost/\uD83D'), 'http://localhost/%EF%BF%BD') + }) + + it('should encode at start of string', function () { + assert.equal(encodeUrl('\uDC7Bfoo'), '%EF%BF%BDfoo') + }) }) }) })