Commit
Closes #97
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -904,7 +904,8 @@ function parseEntity (parser) { | |
strictFail(parser, "Invalid character entity") | ||
return "&"+parser.entity + ";" | ||
} | ||
return String.fromCharCode(num) | ||
|
||
return String.fromCodePoint(num) | ||
} | ||
|
||
function write (chunk) { | ||
|
@@ -1352,4 +1353,58 @@ function write (chunk) { | |
return parser | ||
} | ||
|
||
/*! http://mths.be/fromcodepoint v0.1.0 by @mathias */ | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
isaacs
Author
Owner
|
||
if (!String.fromCodePoint) { | ||
(function() { | ||
var stringFromCharCode = String.fromCharCode; | ||
var floor = Math.floor; | ||
var fromCodePoint = function() { | ||
var MAX_SIZE = 0x4000; | ||
var codeUnits = []; | ||
var highSurrogate; | ||
var lowSurrogate; | ||
var index = -1; | ||
var length = arguments.length; | ||
if (!length) { | ||
return ''; | ||
} | ||
var result = ''; | ||
while (++index < length) { | ||
var codePoint = Number(arguments[index]); | ||
if ( | ||
!isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity` | ||
codePoint < 0 || // not a valid Unicode code point | ||
codePoint > 0x10FFFF || // not a valid Unicode code point | ||
floor(codePoint) != codePoint // not an integer | ||
) { | ||
throw RangeError('Invalid code point: ' + codePoint); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mathiasbynens
|
||
} | ||
if (codePoint <= 0xFFFF) { // BMP code point | ||
codeUnits.push(codePoint); | ||
} else { // Astral code point; split in surrogate halves | ||
// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae | ||
codePoint -= 0x10000; | ||
highSurrogate = (codePoint >> 10) + 0xD800; | ||
lowSurrogate = (codePoint % 0x400) + 0xDC00; | ||
codeUnits.push(highSurrogate, lowSurrogate); | ||
} | ||
if (index + 1 == length || codeUnits.length > MAX_SIZE) { | ||
result += stringFromCharCode.apply(null, codeUnits); | ||
codeUnits.length = 0; | ||
} | ||
} | ||
return result; | ||
}; | ||
if (Object.defineProperty) { | ||
Object.defineProperty(String, 'fromCodePoint', { | ||
'value': fromCodePoint, | ||
'configurable': true, | ||
'writable': true | ||
}); | ||
} else { | ||
String.fromCodePoint = fromCodePoint; | ||
} | ||
}()); | ||
} | ||
|
||
})(typeof exports === "undefined" ? sax = {} : exports) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// split high-order numeric attributes into surrogate pairs | ||
require(__dirname).test | ||
( { xml : '<a>🔥</a>' | ||
, expect : | ||
[ [ 'opentag', { name: 'A', attributes: {}, isSelfClosing: false } ] | ||
, [ 'text', '\ud83d\udd25' ] | ||
, [ 'closetag', 'A' ] | ||
] | ||
, strict : false | ||
, opt : {} | ||
} | ||
) |
Perhaps a more npm-ish way to do this would be:
npm install string.prototype.fromcodepoint --save
require('string.prototype.fromcodepoint');
Let me know if you’d like a pull request for that.