Skip to content

Commit d6df01d

Browse files
committed
Make sure length is a string before using match()
1 parent 0b3d89f commit d6df01d

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

mathjax3-ts/util/lengths.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,21 @@ export const MATHSPACE: {[name: string]: number} = {
7979

8080

8181
/*
82-
* @param{Property} length A dimension (giving number and units) to be converted to ems
83-
* @param{number} size The default size of the dimension (for percentage values)
84-
* @param{number} scale The current scaling factor (to handle absolute units)
85-
* @return{number} The dimension converted to ems
82+
* @param{string|number} length A dimension (giving number and units) to be converted to ems
83+
* @param{number} size The default size of the dimension (for percentage values)
84+
* @param{number} scale The current scaling factor (to handle absolute units)
85+
* @return{number} The dimension converted to ems
8686
*/
87-
export function length2em(length: string, size: number = 0, scale: number = 1, em: number = 16) {
87+
export function length2em(length: string | number, size: number = 0, scale: number = 1, em: number = 16) {
88+
if (typeof length !== 'string') {
89+
length = String(length);
90+
}
8891
if (length === '' || length == null) {
8992
return size;
9093
}
9194
if (MATHSPACE[length]) {
9295
return MATHSPACE[length];
9396
}
94-
9597
let match = length.match(/^\s*([-+]?(?:\.\d+|\d+(?:\.\d*)?))?(pt|em|ex|mu|px|pc|in|mm|cm|%)?/);
9698
if (!match) {
9799
return size;

0 commit comments

Comments
 (0)