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:
Erik Demaine
2018-05-13 14:27:30 -04:00
committed by GitHub
parent 4801ab875a
commit 7ab4f76e16
10 changed files with 208 additions and 79 deletions

View File

@@ -8,7 +8,10 @@ import parseTree from "../src/parseTree";
import Settings from "../src/Settings";
import {scriptFromCodepoint, supportedCodepoint} from "../src/unicodeScripts";
const defaultSettings = new Settings({});
const defaultSettings = new Settings({
strict: false, // deal with warnings only when desired
});
const strictSettings = new Settings({strict: true});
const parseAndSetResult = function(expr, result, settings) {
try {
@@ -72,16 +75,16 @@ describe("unicode", function() {
'ÆÇÐØÞßæçðøþ}').toParse();
});
it("should not parse Latin-1 outside \\text{} without setting", function() {
it("should not parse Latin-1 outside \\text{} with strict", function() {
const chars = 'ÀÁÂÃÄÅÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåèéêëìíîïñòóôõöùúûüýÿÇÐÞçþ';
for (const ch of chars) {
expect(ch).toNotParse();
expect(ch).toNotParse(strictSettings);
}
});
it("should parse Latin-1 outside \\text{}", function() {
expect('ÀÁÂÃÄÅÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåèéêëìíîïñòóôõöùúûüýÿ' +
'ÇÐÞçðþ').toParse({unicodeTextInMathMode: true});
'ÇÐÞçðþ').toParse();
});
it("should parse all lower case Greek letters", function() {
@@ -96,8 +99,8 @@ describe("unicode", function() {
expect('\\text{БГДЖЗЙЛФЦШЫЮЯ}').toParse();
});
it("should not parse Cyrillic outside \\text{}", function() {
expect('БГДЖЗЙЛФЦШЫЮЯ').toNotParse();
it("should not parse Cyrillic outside \\text{} with strict", function() {
expect('БГДЖЗЙЛФЦШЫЮЯ').toNotParse(strictSettings);
});
it("should parse CJK inside \\text{}", function() {
@@ -105,33 +108,33 @@ describe("unicode", function() {
expect('\\text{여보세요}').toParse();
});
it("should not parse CJK outside \\text{}", function() {
expect('私はバナナです。').toNotParse();
expect('여보세요').toNotParse();
it("should not parse CJK outside \\text{} with strict", function() {
expect('私はバナナです。').toNotParse(strictSettings);
expect('여보세요').toNotParse(strictSettings);
});
it("should parse Devangari inside \\text{}", function() {
expect('\\text{नमस्ते}').toParse();
});
it("should not parse Devangari outside \\text{}", function() {
expect('नमस्ते').toNotParse();
it("should not parse Devangari outside \\text{} with strict", function() {
expect('नमस्ते').toNotParse(strictSettings);
});
it("should parse Georgian inside \\text{}", function() {
expect('\\text{გამარჯობა}').toParse();
});
it("should not parse Georgian outside \\text{}", function() {
expect('გამარჯობა').toNotParse();
it("should not parse Georgian outside \\text{} with strict", function() {
expect('გამარჯობა').toNotParse(strictSettings);
});
it("should parse extended Latin characters inside \\text{}", function() {
expect('\\text{ěščřžůřťďňőİı}').toParse();
});
it("should not parse extended Latin outside \\text{}", function() {
expect('ěščřžůřťďňőİı').toNotParse();
it("should not parse extended Latin outside \\text{} with strict", function() {
expect('ěščřžůřťďňőİı').toNotParse(strictSettings);
});
});