mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-12 06:28:40 +00:00
Implement strict mode (replacing unicodeTextInMathMode) (#1278)
* Implement strict mode (replacing unicodeTextInMathMode) Add new "strict" setting (default value false) that can take a boolean (whether to throw an error or silently ignore), string ("ignore", "warn", or "error"), or a function possibly returning such a value. This enables a variety of ways of handling or ignoring transgressions from "true" LaTeX behavior, making KaTeX easier to use while still providing the ability for strict LaTeX adherance. Resolve #1226, implementing that spec, for two existing transgressions from regular LaTeX: * src/functions/kern.js had some errors and warnings about use of (units in) math vs. text mode commands. * The former setting unicodeTextInMathMode (not in any released version) needed to be set to true to enable Unicode text symbols in math mode. Now these are controlled by the strict setting. By default, KaTeX is now very permissive, but if desired, the user can request warnings or errors. * Rewrite strict description * Add tests for strict functions * Stricter type for strict * Switch default strict setting to "warn" * Fix new flow error * Fix another flow bug
This commit is contained in:
@@ -1,20 +1,28 @@
|
||||
// @flow
|
||||
/* eslint no-console:0 */
|
||||
/**
|
||||
* This is a module for storing settings passed into KaTeX. It correctly handles
|
||||
* default settings.
|
||||
*/
|
||||
|
||||
import utils from "./utils";
|
||||
import ParseError from "./ParseError.js";
|
||||
import ParseNode from "./ParseNode";
|
||||
import {Token} from "./Token";
|
||||
|
||||
import type { MacroMap } from "./macros";
|
||||
|
||||
export type StrictFunction =
|
||||
(errorCode: string, errorMsg: string, token?: Token | ParseNode<*>) =>
|
||||
?(boolean | string);
|
||||
|
||||
export type SettingsOptions = {
|
||||
displayMode?: boolean;
|
||||
throwOnError?: boolean;
|
||||
errorColor?: string;
|
||||
macros?: MacroMap;
|
||||
colorIsTextColor?: boolean;
|
||||
unicodeTextInMathMode?: boolean;
|
||||
strict?: boolean | "ignore" | "warn" | "error" | StrictFunction;
|
||||
maxSize?: number;
|
||||
};
|
||||
|
||||
@@ -34,7 +42,7 @@ class Settings {
|
||||
errorColor: string;
|
||||
macros: MacroMap;
|
||||
colorIsTextColor: boolean;
|
||||
unicodeTextInMathMode: boolean;
|
||||
strict: boolean | "ignore" | "warn" | "error" | StrictFunction;
|
||||
maxSize: number;
|
||||
|
||||
constructor(options: SettingsOptions) {
|
||||
@@ -45,10 +53,37 @@ class Settings {
|
||||
this.errorColor = utils.deflt(options.errorColor, "#cc0000");
|
||||
this.macros = options.macros || {};
|
||||
this.colorIsTextColor = utils.deflt(options.colorIsTextColor, false);
|
||||
this.unicodeTextInMathMode =
|
||||
utils.deflt(options.unicodeTextInMathMode, false);
|
||||
this.strict = utils.deflt(options.strict, "warn");
|
||||
this.maxSize = Math.max(0, utils.deflt(options.maxSize, Infinity));
|
||||
}
|
||||
|
||||
/**
|
||||
* Report nonstrict (non-LaTeX-compatible) input.
|
||||
* Can safely not be called if `this.strict` is false in JavaScript.
|
||||
*/
|
||||
nonstrict(errorCode: string, errorMsg: string, token?: Token | ParseNode<*>) {
|
||||
let strict = this.strict;
|
||||
if (typeof strict === "function") {
|
||||
// Allow return value of strict function to be boolean or string
|
||||
// (or null/undefined, meaning no further processing).
|
||||
strict = strict(errorCode, errorMsg, token);
|
||||
}
|
||||
if (!strict || strict === "ignore") {
|
||||
return;
|
||||
} else if (strict === true || strict === "error") {
|
||||
throw new ParseError(
|
||||
"LaTeX-incompatible input and strict mode is set to 'error': " +
|
||||
`${errorMsg} [${errorCode}]`, token);
|
||||
} else if (strict === "warn") {
|
||||
typeof console !== "undefined" && console.warn(
|
||||
"LaTeX-incompatible input and strict mode is set to 'warn': " +
|
||||
`${errorMsg} [${errorCode}]`);
|
||||
} else { // won't happen in type-safe code
|
||||
typeof console !== "undefined" && console.warn(
|
||||
"LaTeX-incompatible input and strict mode is set to " +
|
||||
`unrecognized '${strict}': ${errorMsg} [${errorCode}]`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default Settings;
|
||||
|
Reference in New Issue
Block a user