Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

punycode: Update to v1.1.1 #3561

Closed
wants to merge 2 commits into from

2 participants

@mathiasbynens

The only change is that this version supports orphaned surrogates in ucs2encode and ucs2decode. For example, require('punycode').ucs2.encode([0xD800]) now returns '\uD800' rather than throwing an error.

@bnoordhuis
Owner

Mathias, can you add a regression test for that? Otherwise LGTM.

@mathiasbynens

Sure, but where/how? Any pointers? (There are tests for this in punycode.js repo, FWIW.)

@bnoordhuis
Owner

There are tests in test/simple/test-punycode.js. One or two new asserts should be sufficient.

@bnoordhuis
Owner

Thanks Mathias, landed in 2ba9645.

By the way, if you're ever in Amsterdam, come by the office and you can buy me a beer.

(Okay, okay - I'll buy the first round.)

@bnoordhuis bnoordhuis closed this
@mathiasbynens

I’ll hold you to that promise :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 27, 2012
  1. @mathiasbynens
Commits on Jul 3, 2012
  1. @mathiasbynens
This page is out of date. Refresh to see the latest.
Showing with 22 additions and 11 deletions.
  1. +9 −11 lib/punycode.js
  2. +13 −0 test/simple/test-punycode.js
View
20 lib/punycode.js
@@ -35,8 +35,6 @@
/** Error messages */
errors = {
'overflow': 'Overflow: input needs wider integers to process.',
- 'ucs2decode': 'UCS-2(decode): illegal sequence',
- 'ucs2encode': 'UCS-2(encode): illegal value',
'not-basic': 'Illegal input >= 0x80 (not a basic code point)',
'invalid-input': 'Invalid input'
},
@@ -112,14 +110,17 @@
extra;
while (counter < length) {
value = string.charCodeAt(counter++);
- if ((value & 0xF800) == 0xD800) {
+ if ((value & 0xF800) == 0xD800 && counter < length) {
+ // high surrogate, and there is a next character
extra = string.charCodeAt(counter++);
- if ((value & 0xFC00) != 0xD800 || (extra & 0xFC00) != 0xDC00) {
- error('ucs2decode');
+ if ((extra & 0xFC00) == 0xDC00) { // low surrogate
+ output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
+ } else {
+ output.push(value, extra);
}
- value = ((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000;
+ } else {
+ output.push(value);
}
- output.push(value);
}
return output;
}
@@ -135,9 +136,6 @@
function ucs2encode(array) {
return map(array, function(value) {
var output = '';
- if ((value & 0xF800) == 0xD800) {
- error('ucs2encode');
- }
if (value > 0xFFFF) {
value -= 0x10000;
output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800);
@@ -472,7 +470,7 @@
* @memberOf punycode
* @type String
*/
- 'version': '1.0.0',
+ 'version': '1.1.1',
/**
* An object of methods to convert from JavaScript's internal character
* representation (UCS-2) to decimal Unicode code points, and back.
View
13 test/simple/test-punycode.js
@@ -179,4 +179,17 @@ for (var encoded in tests) {
}
}
+// BMP code point
+assert.equal(punycode.ucs2.encode([0x61]), 'a');
+// supplementary code point (surrogate pair)
+assert.equal(punycode.ucs2.encode([0x1D306]), '\uD834\uDF06');
+// high surrogate
+assert.equal(punycode.ucs2.encode([0xD800]), '\uD800');
+// high surrogate followed by non-surrogates
+assert.equal(punycode.ucs2.encode([0xD800, 0x61, 0x62]), '\uD800ab');
+// low surrogate
+assert.equal(punycode.ucs2.encode([0xDC00]), '\uDC00');
+// low surrogate followed by non-surrogates
+assert.equal(punycode.ucs2.encode([0xDC00, 0x61, 0x62]), '\uDC00ab');
+
assert.equal(errors, 0);
Something went wrong with that request. Please try again.