Skip to content

Commit 2ba9645

Browse files
mathiasbynensbnoordhuis
authored andcommitted
punycode: update to v1.1.1
1 parent 41b129f commit 2ba9645

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

lib/punycode.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
/** Error messages */
3636
errors = {
3737
'overflow': 'Overflow: input needs wider integers to process.',
38-
'ucs2decode': 'UCS-2(decode): illegal sequence',
39-
'ucs2encode': 'UCS-2(encode): illegal value',
4038
'not-basic': 'Illegal input >= 0x80 (not a basic code point)',
4139
'invalid-input': 'Invalid input'
4240
},
@@ -112,14 +110,17 @@
112110
extra;
113111
while (counter < length) {
114112
value = string.charCodeAt(counter++);
115-
if ((value & 0xF800) == 0xD800) {
113+
if ((value & 0xF800) == 0xD800 && counter < length) {
114+
// high surrogate, and there is a next character
116115
extra = string.charCodeAt(counter++);
117-
if ((value & 0xFC00) != 0xD800 || (extra & 0xFC00) != 0xDC00) {
118-
error('ucs2decode');
116+
if ((extra & 0xFC00) == 0xDC00) { // low surrogate
117+
output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
118+
} else {
119+
output.push(value, extra);
119120
}
120-
value = ((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000;
121+
} else {
122+
output.push(value);
121123
}
122-
output.push(value);
123124
}
124125
return output;
125126
}
@@ -135,9 +136,6 @@
135136
function ucs2encode(array) {
136137
return map(array, function(value) {
137138
var output = '';
138-
if ((value & 0xF800) == 0xD800) {
139-
error('ucs2encode');
140-
}
141139
if (value > 0xFFFF) {
142140
value -= 0x10000;
143141
output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800);
@@ -472,7 +470,7 @@
472470
* @memberOf punycode
473471
* @type String
474472
*/
475-
'version': '1.0.0',
473+
'version': '1.1.1',
476474
/**
477475
* An object of methods to convert from JavaScript's internal character
478476
* representation (UCS-2) to decimal Unicode code points, and back.

test/simple/test-punycode.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,17 @@ for (var encoded in tests) {
179179
}
180180
}
181181

182+
// BMP code point
183+
assert.equal(punycode.ucs2.encode([0x61]), 'a');
184+
// supplementary code point (surrogate pair)
185+
assert.equal(punycode.ucs2.encode([0x1D306]), '\uD834\uDF06');
186+
// high surrogate
187+
assert.equal(punycode.ucs2.encode([0xD800]), '\uD800');
188+
// high surrogate followed by non-surrogates
189+
assert.equal(punycode.ucs2.encode([0xD800, 0x61, 0x62]), '\uD800ab');
190+
// low surrogate
191+
assert.equal(punycode.ucs2.encode([0xDC00]), '\uDC00');
192+
// low surrogate followed by non-surrogates
193+
assert.equal(punycode.ucs2.encode([0xDC00, 0x61, 0x62]), '\uDC00ab');
194+
182195
assert.equal(errors, 0);

0 commit comments

Comments
 (0)