Skip to content

Commit

Permalink
Fix encoding unpaired surrogates at start/end of string
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 10, 2016
1 parent b3f2a3a commit 3868484
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

* Fix encoding unpaired surrogates at start/end of string

1.0.0 / 2016-06-08
==================

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 12 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
})
})

0 comments on commit 3868484

Please sign in to comment.