- html = html.replace(/([\x80-\uffff])/g, function (a, b) {
- var cc = b.charCodeAt();
- if (
+ html = html.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]|[\u0000-\u001f\u0080-\uFFFF]/g, function(match) {
+ var high, low, charValue, rep
+ var cc = match.charCodeAt();
+
+ // Get the character value, handling surrogate pairs
+ if (match.length == 2) {
+ // It's a surrogate pair, calculate the Unicode code point
+ high = match.charCodeAt(0) - 0xD800;
+ low = match.charCodeAt(1) - 0xDC00;
+ charValue = (high * 0x400) + low + 0x10000;
+ } else if (