fix(editor): prototype polluting in gfx util (#11831)

This fixes the security edge case where the font key happens to be `__proto__` or `constructor`
This commit is contained in:
doodlewind
2025-04-21 04:00:59 +00:00
parent 6d7cd6dd99
commit 1146f4d5a9

View File

@@ -399,23 +399,27 @@ export function parseTokens(text: string): string[] {
}
export const charWidth = (() => {
const cachedCharWidth: Record<string, Array<number>> = {};
const cachedCharWidth = new Map<string, Array<number>>();
const calculate = (char: string, font: string) => {
const ascii = char.charCodeAt(0);
if (!cachedCharWidth[font]) {
cachedCharWidth[font] = [];
}
if (!cachedCharWidth[font][ascii]) {
const width = getLineWidth(char, font);
cachedCharWidth[font][ascii] = width;
let fontCache = cachedCharWidth.get(font);
if (!fontCache) {
fontCache = [];
cachedCharWidth.set(font, fontCache);
}
return cachedCharWidth[font][ascii];
if (fontCache[ascii] === undefined) {
const width = getLineWidth(char, font);
fontCache[ascii] = width;
}
return fontCache[ascii];
};
const getCache = (font: string) => {
return cachedCharWidth[font];
return cachedCharWidth.get(font);
};
return {
calculate,