Skip to content

Commit eacfc38

Browse files
authored
Merge pull request #717 from mathjax/option-warnings
Provide switch for warn/error for invalid options
2 parents ed17439 + 20af442 commit eacfc38

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

ts/components/startup.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {OutputJax} from '../core/OutputJax.js';
3535
import {CommonOutputJax} from '../output/common/OutputJax.js';
3636
import {DOMAdaptor} from '../core/DOMAdaptor.js';
3737
import {PrioritizedList} from '../util/PrioritizedList.js';
38-
import {OptionList} from '../util/Options.js';
38+
import {OptionList, OPTIONS} from '../util/Options.js';
3939

4040
import {TeX} from '../input/tex.js';
4141

@@ -54,6 +54,8 @@ export interface MathJaxConfig extends MJConfig {
5454
typeset?: boolean; // Perform initial typeset?
5555
ready?: () => void; // Function to perform when components are ready
5656
pageReady?: () => void; // Function to perform when page is ready
57+
invalidOption?: 'fatal' | 'warn'; // Do invalid options produce a warning, or throw an error?
58+
optionError?: (message: string, key: string) => void, // Function to report invalid options
5759
[name: string]: any; // Other configuration blocks
5860
};
5961
}
@@ -534,8 +536,8 @@ export const MathJax = MJGlobal as MathJaxObject;
534536

535537
/*
536538
* If the startup module hasn't been added to the MathJax variable,
537-
* Add the startup configuration and data objects, and create
538-
* the initial typeset and conversion calls.
539+
* Add the startup configuration and data objects, and
540+
* set the method for handling invalid options, if provided.
539541
*/
540542
if (typeof MathJax._.startup === 'undefined') {
541543

@@ -555,6 +557,13 @@ if (typeof MathJax._.startup === 'undefined') {
555557
options: {}
556558
});
557559

560+
if (MathJax.config.startup.invalidOption) {
561+
OPTIONS.invalidOption = MathJax.config.startup.invalidOption;
562+
}
563+
if (MathJax.config.startup.optionError) {
564+
OPTIONS.optionError = MathJax.config.startup.optionError;
565+
}
566+
558567
}
559568

560569
/**

ts/util/Options.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,27 @@ export const APPEND = '[+]';
6868
*/
6969
export const REMOVE = '[-]';
7070

71+
72+
/**
73+
* Provides options for the option utlities.
74+
*/
75+
export const OPTIONS = {
76+
invalidOption: 'warn' as ('fatal' | 'warn'),
77+
/**
78+
* Function to report messages for invalid options
79+
*
80+
* @param {string} message The message for the invalid parameter.
81+
* @param {string} key The invalid key itself.
82+
*/
83+
optionError: (message: string, _key: string) => {
84+
if (OPTIONS.invalidOption === 'fatal') {
85+
throw new Error(message);
86+
}
87+
console.warn('MathJax: ' + message);
88+
}
89+
};
90+
91+
7192
/**
7293
* A Class to use for options that should not produce warnings if an undefined key is used
7394
*/
@@ -163,7 +184,8 @@ export function insert(dst: OptionList, src: OptionList, warn: boolean = true):
163184
if (typeof key === 'symbol') {
164185
key = (key as symbol).toString();
165186
}
166-
throw new Error('Invalid option "' + key + '" (no default value).');
187+
OPTIONS.optionError(`Invalid option "${key}" (no default value).`, key);
188+
continue;
167189
}
168190
//
169191
// Shorthands for the source and destination values

0 commit comments

Comments
 (0)