@@ -5,6 +5,7 @@ import buildTree from "../src/buildTree";
55import katex from "../katex" ;
66import parseTree from "../src/parseTree" ;
77import Options from "../src/Options" ;
8+ import ParseError from "../src/ParseError" ;
89import Settings from "../src/Settings" ;
910import Style from "../src/Style" ;
1011import {
@@ -3060,6 +3061,53 @@ describe("A parser that does not throw on unsupported commands", function() {
30603061 } ) ;
30613062} ) ;
30623063
3064+ describe ( "ParseError properties" , function ( ) {
3065+ it ( "should contain affected position and length information" , function ( ) {
3066+ try {
3067+ katex . renderToString ( "1 + \\fraq{}{}" ) ;
3068+
3069+ // Render is expected to throw, so this should not be called.
3070+ expect ( true ) . toBe ( false ) ;
3071+ } catch ( error ) {
3072+ expect ( error ) . toBeInstanceOf ( ParseError ) ;
3073+ expect ( error . message ) . toBe ( "KaTeX parse error: Undefined control sequence: \\fraq at position 5: 1 + \\̲f̲r̲a̲q̲{}{}" ) ;
3074+ expect ( error . rawMessage ) . toBe ( "Undefined control sequence: \\fraq" ) ;
3075+ expect ( error . position ) . toBe ( 4 ) ;
3076+ expect ( error . length ) . toBe ( 5 ) ;
3077+ }
3078+ } ) ;
3079+
3080+ it ( "should contain position and length information at end of input" , function ( ) {
3081+ try {
3082+ katex . renderToString ( "\\frac{}" ) ;
3083+
3084+ // Render is expected to throw, so this should not be called.
3085+ expect ( true ) . toBe ( false ) ;
3086+ } catch ( error ) {
3087+ expect ( error ) . toBeInstanceOf ( ParseError ) ;
3088+ expect ( error . message ) . toBe ( "KaTeX parse error: Unexpected end of input in a macro argument, expected '}' at end of input: \\frac{}" ) ;
3089+ expect ( error . rawMessage ) . toBe ( "Unexpected end of input in a macro argument, expected '}'" ) ;
3090+ expect ( error . position ) . toBe ( 7 ) ;
3091+ expect ( error . length ) . toBe ( 0 ) ;
3092+ }
3093+ } ) ;
3094+
3095+ it ( "should contain no position and length information if unavailable" , function ( ) {
3096+ try {
3097+ katex . renderToString ( "\\verb|hello\nworld|" ) ;
3098+
3099+ // Render is expected to throw, so this should not be called.
3100+ expect ( true ) . toBe ( false ) ;
3101+ } catch ( error ) {
3102+ expect ( error ) . toBeInstanceOf ( ParseError ) ;
3103+ expect ( error . message ) . toBe ( "KaTeX parse error: \\verb ended by end of line instead of matching delimiter" ) ;
3104+ expect ( error . rawMessage ) . toBe ( "\\verb ended by end of line instead of matching delimiter" ) ;
3105+ expect ( error . position ) . toBeUndefined ( ) ;
3106+ expect ( error . length ) . toBeUndefined ( ) ;
3107+ }
3108+ } ) ;
3109+ } ) ;
3110+
30633111describe ( "The symbol table integrity" , function ( ) {
30643112 it ( "should treat certain symbols as synonyms" , function ( ) {
30653113 expect `<` . toBuildLike `\lt` ;
0 commit comments