KaTeX/utils.js
Ben Alpert 7df5b4bba8 Add code for generating HTML
Test Plan: Ran unit tests. Looked at `\blue{\displaystyle \left(\dfrac{a^\sigma}{\sin \theta}\right\Updownarrow \intop_{1/2}^{z^z} \sum_{i=0}^\infty x \,dx}` in Chrome and saw the future in my eyes.

Reviewers: emily

Reviewed By: emily

Subscribers: jessie

Differential Revision: http://phabricator.khanacademy.org/D13154
2014-09-12 17:59:26 -07:00

80 lines
1.6 KiB
JavaScript

var nativeIndexOf = Array.prototype.indexOf;
var indexOf = function(list, elem) {
if (list == null) {
return -1;
}
if (nativeIndexOf && list.indexOf === nativeIndexOf) {
return list.indexOf(elem);
}
var i = 0, l = list.length;
for (; i < l; i++) {
if (list[i] === elem) {
return i;
}
}
return -1;
};
var contains = function(list, elem) {
return indexOf(list, elem) !== -1;
};
// hyphenate and escape adapted from Facebook's React under Apache 2 license
var uppercase = /([A-Z])/g;
var hyphenate = function(str) {
return str.replace(uppercase, "-$1").toLowerCase();
};
var ESCAPE_LOOKUP = {
"&": "&amp;",
">": "&gt;",
"<": "&lt;",
"\"": "&quot;",
"'": "&#x27;"
};
var ESCAPE_REGEX = /[&><"']/g;
function escaper(match) {
return ESCAPE_LOOKUP[match];
}
/**
* Escapes text to prevent scripting attacks.
*
* @param {*} text Text value to escape.
* @return {string} An escaped string.
*/
function escape(text) {
return ('' + text).replace(ESCAPE_REGEX, escaper);
}
var setTextContent;
if (typeof document !== "undefined") {
var testNode = document.createElement("span");
if ("textContent" in testNode) {
setTextContent = function(node, text) {
node.textContent = text;
};
} else {
setTextContent = function(node, text) {
node.innerText = text;
};
}
}
function clearNode(node) {
setTextContent(node, "");
}
module.exports = {
contains: contains,
escape: escape,
hyphenate: hyphenate,
indexOf: indexOf,
setTextContent: setTextContent,
clearNode: clearNode
};