Files
KaTeX/test/setup.js
Erik Demaine 647c661db0 Remove double encoding in MathML via Unicode MathML spaces (#1813)
* Remove double encoding in MathML via Unicode MathML spaces

We used to have a complex mechanic for escaping strings, marking them as
`needsEscape = false`, and then not escaping those strings (for
combining strings in `\operatorname`).  But this doesn't really work
with `render`ing directly to a node, as `document.createTextNode` can't
be stopped from escaping.

I've thus removed this mechanic, which required the following changes:

* Switch MathML "smart space" encoding to use Unicode instead of
  `&LongNames;` (which weren't working with `render` for the same reason).
* Hack our HTML/MathML serializer to not use `String.trim`, which wrecks
  havoc with emitted Unicode spaces.

Now `toText()` doesn't escape, so strings concatenate in unescaped form,
and `toHTML()` only does the necessary escaping.  Thus fix #1782.

* Fix src/utils.js

Co-Authored-By: edemaine <edemaine@mit.edu>

* Fix src/mathMLTree.js documentation

Co-Authored-By: edemaine <edemaine@mit.edu>

* Remove trim hack thanks to diffable-html@4.0.0

* Switch back to jest-serializer-html

* Update mathMLTree.js
2019-01-02 01:55:44 +09:00

81 lines
1.9 KiB
JavaScript

/* global expect: false */
import stringify from 'json-stable-stringify';
import Lexer from "../src/Lexer";
import ParseError from "../src/ParseError";
import {
Mode, ConsoleWarning,
expectKaTeX, expectEquivalent,
} from "./helpers";
// JSON serializer
const typeFirstCompare = (a, b) => {
if (a.key === 'type') {
return -1;
} else if (b.key === 'type') {
return 1;
} else {
return a.key < b.key ? -1 : 1;
}
};
const replacer = (key, value) => {
if (value instanceof Lexer) {
return {
input: value.input,
// omit value.settings
lastIndex: value.tokenRegex.lastIndex,
};
} else {
return value;
}
};
const serializer = {
print(val) {
return stringify(val, {
cmp: typeFirstCompare,
space: ' ',
replacer: replacer,
});
},
test(val) {
// Leave strings (e.g. XML) to other serializers
return typeof val !== "string";
},
};
expect.addSnapshotSerializer(serializer);
// Mock console.warn to throw an error
global.console.warn = x => { throw new ConsoleWarning(x); };
// Expect extensions
expect.extend({
toParse(expr, settings) {
return expectKaTeX(expr, settings, Mode.PARSE, this.isNot);
},
toFailWithParseError: function(expr, expected = ParseError) {
return expectKaTeX(expr, undefined, Mode.PARSE, this.isNot, expected);
},
toBuild(expr, settings) {
return expectKaTeX(expr, settings, Mode.BUILD, this.isNot);
},
toWarn(expr, settings) {
return expectKaTeX(expr, settings, Mode.BUILD, this.isNot, ConsoleWarning);
},
toParseLike(expr, expected, settings) {
return expectEquivalent(expr, expected, settings, Mode.PARSE, this.expand);
},
toBuildLike(expr, expected, settings) {
return expectEquivalent(expr, expected, settings, Mode.BUILD, this.expand);
},
});