mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-06 20:10:24 +08:00
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:
@@ -399,23 +399,27 @@ export function parseTokens(text: string): string[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const charWidth = (() => {
|
export const charWidth = (() => {
|
||||||
const cachedCharWidth: Record<string, Array<number>> = {};
|
const cachedCharWidth = new Map<string, Array<number>>();
|
||||||
|
|
||||||
const calculate = (char: string, font: string) => {
|
const calculate = (char: string, font: string) => {
|
||||||
const ascii = char.charCodeAt(0);
|
const ascii = char.charCodeAt(0);
|
||||||
if (!cachedCharWidth[font]) {
|
|
||||||
cachedCharWidth[font] = [];
|
let fontCache = cachedCharWidth.get(font);
|
||||||
}
|
if (!fontCache) {
|
||||||
if (!cachedCharWidth[font][ascii]) {
|
fontCache = [];
|
||||||
const width = getLineWidth(char, font);
|
cachedCharWidth.set(font, fontCache);
|
||||||
cachedCharWidth[font][ascii] = width;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return cachedCharWidth[font][ascii];
|
if (fontCache[ascii] === undefined) {
|
||||||
|
const width = getLineWidth(char, font);
|
||||||
|
fontCache[ascii] = width;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fontCache[ascii];
|
||||||
};
|
};
|
||||||
|
|
||||||
const getCache = (font: string) => {
|
const getCache = (font: string) => {
|
||||||
return cachedCharWidth[font];
|
return cachedCharWidth.get(font);
|
||||||
};
|
};
|
||||||
return {
|
return {
|
||||||
calculate,
|
calculate,
|
||||||
|
|||||||
Reference in New Issue
Block a user