mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-07 12:18:39 +00:00
* Comments without terminating newlines in nonstrict mode Fix #1506 by allowing single-line comments (`%` without terminating newline) in nonstrict mode. `Lexer` and `MacroExpander` now store the `Settings` object, so the `Lexer` can complain about missing newline according to the `strict` setting. I filtered this out from the snapshot tests with a slightly different `replacer`. * Reimplement \href like \verb, add \url Major restructuring to lex URL arguments differently, e.g. to support `\href%{hello}` and `\href{http://foo.com/#test%}{hello}`. The new URL parsing code is simpler, but involves a special case in `parseSymbol` like `\verb`. Also add support for `\url` while we're here. * Cleanup * Fix flow errors and improve error messages * Add \url to documentation * Improve doc formatting
81 lines
1.9 KiB
JavaScript
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";
|
|
|
|
// Serializer support
|
|
|
|
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);
|
|
},
|
|
});
|