summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2014-10-18 16:38:19 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2014-10-18 16:39:39 -0700
commitd571c769791d600bf1756fc0df6ca4abd9535e5e (patch)
tree335ee12d04bff6167bbb91baa40c8b166ab9c63d
parentfb143fbc1705bb4d59b5d4ebbb5454866cf1b76c (diff)
Change from-code-point.js so it doesn't alter String prototype.
-rw-r--r--js/lib/from-code-point.js110
-rw-r--r--js/lib/html5-entities.js4
-rwxr-xr-xjs/lib/index.js8
3 files changed, 54 insertions, 68 deletions
diff --git a/js/lib/from-code-point.js b/js/lib/from-code-point.js
index bf1dd99..94eca65 100644
--- a/js/lib/from-code-point.js
+++ b/js/lib/from-code-point.js
@@ -1,65 +1,49 @@
-// polyfill for fromCodePoint:
-// https://github.com/mathiasbynens/String.fromCodePoint
+// derived from https://github.com/mathiasbynens/String.fromCodePoint
/*! http://mths.be/fromcodepoint v0.2.1 by @mathias */
-if (!String.fromCodePoint) {
- (function() {
- var defineProperty = (function() {
- // IE 8 only supports `Object.defineProperty` on DOM elements
- try {
- var object = {};
- var $defineProperty = Object.defineProperty;
- var result = $defineProperty(object, object, object) && $defineProperty;
- } catch(error) {}
- return result;
- }());
- 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
- ) {
- return String.fromCharCode(0xFFFD);
- }
- 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 (defineProperty) {
- defineProperty(String, 'fromCodePoint', {
- 'value': fromCodePoint,
- 'configurable': true,
- 'writable': true
- });
- } else {
- String.fromCodePoint = fromCodePoint;
- }
- }());
-}
+if (String.fromCodePoint) {
+
+ module.exports = String.fromCodePoint;
+} else {
+
+ 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
+ ) {
+ return String.fromCharCode(0xFFFD);
+ }
+ 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;
+ };
+ module.exports = fromCodePoint;
+}
diff --git a/js/lib/html5-entities.js b/js/lib/html5-entities.js
index 4257ba0..235fc66 100644
--- a/js/lib/html5-entities.js
+++ b/js/lib/html5-entities.js
@@ -1,3 +1,5 @@
+var fromCodePoint = require('./from-code-point');
+
var entities = { AAacute: 'Á',
aacute: 'á',
Abreve: 'Ă',
@@ -2135,7 +2137,7 @@ var entityToChar = function(m) {
} else {
num = parseInt(m.slice(2,-1), 10);
}
- uchar = String.fromCodePoint(num);
+ uchar = fromCodePoint(num);
} else {
uchar = entities[m.slice(1,-1)];
}
diff --git a/js/lib/index.js b/js/lib/index.js
index 0dbeaae..55db200 100755
--- a/js/lib/index.js
+++ b/js/lib/index.js
@@ -9,7 +9,7 @@
// var renderer = new stmd.HtmlRenderer();
// console.log(renderer.render(parser.parse('Hello *world*')));
- require('./from-code-point.js');
+ var fromCodePoint = require('./from-code-point.js');
var entityToChar = require('./html5-entities.js').entityToChar;
// Constants for character codes:
@@ -284,7 +284,7 @@
if (cc_after === -1) {
char_after = '\n';
} else {
- char_after = String.fromCodePoint(cc_after);
+ char_after = fromCodePoint(cc_after);
}
var can_open = numdelims > 0 && numdelims <= 3 && !(/\s/.test(char_after));
@@ -316,7 +316,7 @@
var startpos = this.pos;
var c ;
var first_close = 0;
- c = String.fromCodePoint(cc);
+ c = fromCodePoint(cc);
var numdelims;
var numclosedelims;
@@ -732,7 +732,7 @@
}
if (!res) {
this.pos += 1;
- inlines.push({t: 'Str', c: String.fromCodePoint(c)});
+ inlines.push({t: 'Str', c: fromCodePoint(c)});
}
if (memoize) {