Skip to content

Commit

Permalink
Patch IE8 compat fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdyer committed Nov 30, 2015
2 parents c614d6b + a0ee2fe commit 10be667
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 78 deletions.
78 changes: 60 additions & 18 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ var some = function some(array, fun /*, thisp */) {
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
var indexOf = function indexOf(array, sought /*, fromIndex */ ) {
var fromIndex = arguments[2];
if (Array.prototype.indexOf) {
if (Array.prototype.indexOf || typeof array === "string") {
return array.indexOf(sought, fromIndex);
}
var self = splitString && _toString(array) == "[object String]" ? array.split("") : toObject(array),
Expand Down Expand Up @@ -243,6 +243,48 @@ var create = function create(o) {
return new F();
};

// From: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON
if (!window.JSON) {
window.JSON = {
parse: function(sJSON) { return eval('(' + sJSON + ')'); },
stringify: (function () {
var toString = Object.prototype.toString;
var isArray = Array.isArray || function (a) { return toString.call(a) === '[object Array]'; };
var escMap = {'"': '\\"', '\\': '\\\\', '\b': '\\b', '\f': '\\f', '\n': '\\n', '\r': '\\r', '\t': '\\t'};
var escFunc = function (m) { return escMap[m] || '\\u' + (m.charCodeAt(0) + 0x10000).toString(16).substr(1); };
var escRE = /[\\"\u0000-\u001F\u2028\u2029]/g;
return function stringify(value) {
if (value == null) {
return 'null';
} else if (typeof value === 'number') {
return isFinite(value) ? value.toString() : 'null';
} else if (typeof value === 'boolean') {
return value.toString();
} else if (typeof value === 'object') {
if (typeof value.toJSON === 'function') {
return stringify(value.toJSON());
} else if (isArray(value)) {
var res = '[';
for (var i = 0; i < value.length; i++)
res += (i ? ', ' : '') + stringify(value[i]);
return res + ']';
} else if (toString.call(value) === '[object Object]') {
var tmp = [];
for (var k in value) {
if (value.hasOwnProperty(k))
tmp.push(stringify(k) + ': ' + stringify(value[k]));
}
return '{' + tmp.join(', ') + '}';
}
}
return '"' + value.toString().replace(escRE, escFunc) + '"';
};
})()
};
}





/* -*- Mode: js; js-indent-level: 2; indent-tabs-mode: nil; tab-width: 2 -*- */
Expand Down Expand Up @@ -890,7 +932,7 @@ var Model = (function () {
DEGREE: "degree",
BACKSLASH: "backslash",
MATHBF: "mathbf",
NONE: "none",
NONE: "none"
};

forEach(keys(OpStr), function (v, i) {
Expand Down Expand Up @@ -1221,7 +1263,7 @@ var Model = (function () {
} else {
// If the character matches the last separator or, if not, last is undefiend
// and character is in the provided list, return the character.
if (ch === last || !last && separators.indexOf(ch) >= 0) {
if (ch === last || !last && indexOf(separators, ch) >= 0) {
return ch;
} else {
return "";
Expand All @@ -1241,15 +1283,15 @@ var Model = (function () {
assert(decimalSeparator.length === 1, message(1002));
var separator = decimalSeparator;
if (thousandsSeparators instanceof Array &&
thousandsSeparators.indexOf(separator) >= 0) {
indexOf(thousandsSeparators, separator) >= 0) {
// There is a conflict between the decimal separator and the
// thousands separator.
assert(false, message(2008, [separator]));
return '.';
}
return separator;
}
if (thousandsSeparators instanceof Array && thousandsSeparators.indexOf('.') >= 0) {
if (thousandsSeparators instanceof Array && indexOf(thousandsSeparators, '.') >= 0) {
// Period is used as a thousands separator, so cannot be used as a
// decimal separator.
assert(false, message(2008));
Expand Down Expand Up @@ -1332,7 +1374,7 @@ var Model = (function () {
hasThousandsSeparator: separatorCount !== 0,
numberFormat: numberFormat,
hasLeadingZero: hasLeadingZero,
hasTrailingZero: hasTrailingZero,
hasTrailingZero: hasTrailingZero
}
}
// Construct a multiply node.
Expand Down Expand Up @@ -1445,7 +1487,7 @@ var Model = (function () {
var tbl = matrixExpr();
eat(TK_END);
braceExpr();
if (figure.args[0].indexOf("matrix") >= 0) {
if (indexOf(figure.args[0], "matrix") >= 0) {
e = newNode(Model.MATRIX, [tbl]);
} else {
assert(false, "Unrecognized LaTeX name");
Expand Down Expand Up @@ -1974,8 +2016,8 @@ var Model = (function () {
var n1 = expr.args[0];
n1 = numberNode("." + n1.args[0]);
n1.isRepeating = true;
if (n0.args[0].indexOf(".") >= 0) {
var decimalPlaces = n0.args[0].length - n0.args[0].indexOf(".")- 1;
if (indexOf(n0.args[0], ".") >= 0) {
var decimalPlaces = n0.args[0].length - indexOf(n0.args[0], ".")- 1;
n1 = multiplyNode([n1, binaryNode(Model.POW, [numberNode("10"), numberNode("-" + decimalPlaces)])]);
}
expr = binaryNode(Model.ADD, [n0, n1]);
Expand Down Expand Up @@ -2044,7 +2086,7 @@ var Model = (function () {
if (args.length === 1) {
// 1.2, 10^2
if (args[0].op === Model.NUM &&
(args[0].args[0].length === 1 || args[0].args[0].indexOf(getDecimalSeparator()) === 1)) {
(args[0].args[0].length === 1 || indexOf(args[0].args[0], getDecimalSeparator()) === 1)) {
return true;
} else if (args[0].op === Model.POW &&
args[0].args[0].op === Model.NUM && args[0].args[0].args[0] === "10" &&
Expand All @@ -2057,7 +2099,7 @@ var Model = (function () {
var a = args[0];
var e = args[1];
if (a.op === Model.NUM &&
(a.args[0].length === 1 || a.args[0].indexOf(getDecimalSeparator()) === 1) &&
(a.args[0].length === 1 || indexOf(a.args[0], getDecimalSeparator()) === 1) &&
e.op === Model.POW &&
e.args[0].op === Model.NUM && e.args[0].args[0] === "10" &&
e.args[1].numberFormat === "integer") {
Expand Down Expand Up @@ -2301,7 +2343,7 @@ var Model = (function () {
"\\underset": TK_UNDERSET,
"\\backslash": TK_BACKSLASH,
"\\mathbf": TK_MATHBF,
"\\abs": TK_ABS,
"\\abs": TK_ABS
};
var identifiers = keys(env);
function isAlphaCharCode(c) {
Expand All @@ -2326,7 +2368,7 @@ var Model = (function () {
case 13: // carriage return
continue;
case 38: // ampersand (new column or entity)
if (src.substring(curIndex).indexOf("nbsp;") === 0) {
if (indexOf(src.substring(curIndex), "nbsp;") === 0) {
// Skip &nbsp;
curIndex += 5;
continue;
Expand Down Expand Up @@ -2451,7 +2493,7 @@ var Model = (function () {
var ch = String.fromCharCode(c);
var prefix = lexeme + ch;
var match = some(identifiers, function (u) {
return u.indexOf(prefix) === 0;
return indexOf(u, prefix) === 0;
});
if (!match) {
break;
Expand All @@ -2476,11 +2518,11 @@ var Model = (function () {
lexeme = String.fromCharCode(c);
} else if (c === '%'.charCodeAt(0)) {
lexeme += String.fromCharCode(c);
} else if ([' '.charCodeAt(0),
} else if (indexOf([' '.charCodeAt(0),
':'.charCodeAt(0),
';'.charCodeAt(0),
','.charCodeAt(0),
'!'.charCodeAt(0)].indexOf(c) >= 0) {
'!'.charCodeAt(0)], c) >= 0) {
lexeme = "\\ ";
} else {
while (isAlphaCharCode(c)) {
Expand All @@ -2502,7 +2544,7 @@ var Model = (function () {
var c = src.charCodeAt(curIndex++);
while (c && c !== "}".charCodeAt(0)) {
var ch = String.fromCharCode(c);
if (ch === "&" && src.substring(curIndex).indexOf("nbsp;") === 0) {
if (ch === "&" && indexOf(src.substring(curIndex), "nbsp;") === 0) {
// Skip &nbsp;
curIndex += 5;
} else if (ch === " " || ch === "\t") {
Expand All @@ -2525,7 +2567,7 @@ var Model = (function () {
return {
start : start ,
lexeme : function () { return lexeme } ,
pos: function() { return curIndex; },
pos: function() { return curIndex; }
}
}
}
Expand Down
Loading

0 comments on commit 10be667

Please sign in to comment.