mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-06 19:58:40 +00:00
* \newcommand, \renewcommand, \providecommand * Tests * Add comment * Add symbols to the set of already defined things * Add implicitCommands, catch \hline outside array * Add \relax * Move isDefined to be a method of MacroExpander * Namespace.has * Reword error messages * Add \hdashline given #1407
101 lines
2.9 KiB
JavaScript
101 lines
2.9 KiB
JavaScript
/* global expect: false */
|
|
|
|
import katex from "../katex";
|
|
import ParseError from "../src/ParseError";
|
|
import parseTree from "../src/parseTree";
|
|
import Settings from "../src/Settings";
|
|
|
|
export const nonstrictSettings = new Settings({strict: false});
|
|
export const strictSettings = new Settings({strict: true});
|
|
|
|
export const _getBuilt = function(expr, settings = new Settings()) {
|
|
let rootNode = katex.__renderToDomTree(expr, settings);
|
|
|
|
if (rootNode.classes.indexOf('katex-error') >= 0) {
|
|
return rootNode;
|
|
}
|
|
|
|
if (rootNode.classes.indexOf('katex-display') >= 0) {
|
|
rootNode = rootNode.children[0];
|
|
}
|
|
|
|
// grab the root node of the HTML rendering
|
|
// rootNode.children[0] is the MathML rendering
|
|
const builtHTML = rootNode.children[1];
|
|
|
|
// combine the non-strut children of all base spans
|
|
const children = [];
|
|
for (let i = 0; i < builtHTML.children.length; i++) {
|
|
children.push(...builtHTML.children[i].children.filter(
|
|
(node) => node.classes.indexOf("strut") < 0));
|
|
}
|
|
return children;
|
|
};
|
|
|
|
/**
|
|
* Return the root node of the rendered HTML.
|
|
* @param expr
|
|
* @param settings
|
|
* @returns {Object}
|
|
*/
|
|
export const getBuilt = function(expr, settings = new Settings()) {
|
|
expect(expr).toBuild(settings);
|
|
return _getBuilt(expr, settings);
|
|
};
|
|
|
|
/**
|
|
* Return the root node of the parse tree.
|
|
* @param expr
|
|
* @param settings
|
|
* @returns {Object}
|
|
*/
|
|
export const getParsed = function(expr, settings = new Settings()) {
|
|
expect(expr).toParse(settings);
|
|
return parseTree(expr, settings);
|
|
};
|
|
|
|
export const stripPositions = function(expr) {
|
|
if (typeof expr !== "object" || expr === null) {
|
|
return expr;
|
|
}
|
|
if (expr.loc && expr.loc.lexer && typeof expr.loc.start === "number") {
|
|
delete expr.loc;
|
|
}
|
|
Object.keys(expr).forEach(function(key) {
|
|
stripPositions(expr[key]);
|
|
});
|
|
return expr;
|
|
};
|
|
|
|
export const parseAndSetResult = function(expr, result,
|
|
settings = new Settings()) {
|
|
try {
|
|
return parseTree(expr, settings);
|
|
} catch (e) {
|
|
result.pass = false;
|
|
if (e instanceof ParseError) {
|
|
result.message = () =>
|
|
`'${expr}' failed parsing with error: ${e.message}`;
|
|
} else {
|
|
result.message = () =>
|
|
`'${expr}' failed parsing with unknown error: ${e.message}`;
|
|
}
|
|
}
|
|
};
|
|
|
|
export const buildAndSetResult = function(expr, result,
|
|
settings = new Settings()) {
|
|
try {
|
|
return _getBuilt(expr, settings);
|
|
} catch (e) {
|
|
result.pass = false;
|
|
if (e instanceof ParseError) {
|
|
result.message = () =>
|
|
`'${expr}' failed building with error: ${e.message}`;
|
|
} else {
|
|
result.message = () =>
|
|
`'${expr}' failed building with unknown error: ${e.message}`;
|
|
}
|
|
}
|
|
};
|