feat: add cached string width function for performance optimization (#7850)

Co-authored-by: lifeloating <imshuazi@126.com>
This commit is contained in:
Jacob Richman
2025-09-10 21:20:40 -07:00
committed by GitHub
parent 8cc787f304
commit 5b2176770e
3 changed files with 380 additions and 166 deletions
@@ -1393,6 +1393,69 @@ Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
expect(getBufferState(result).text).toBe('Pasted Text');
});
it('should sanitize large text (>5000 chars) and strip unsafe characters', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
const unsafeChars = '\x07\x08\x0B\x0C';
const largeTextWithUnsafe =
'safe text'.repeat(600) + unsafeChars + 'more safe text';
expect(largeTextWithUnsafe.length).toBeGreaterThan(5000);
act(() =>
result.current.handleInput({
name: '',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: largeTextWithUnsafe,
}),
);
const resultText = getBufferState(result).text;
expect(resultText).not.toContain('\x07');
expect(resultText).not.toContain('\x08');
expect(resultText).not.toContain('\x0B');
expect(resultText).not.toContain('\x0C');
expect(resultText).toContain('safe text');
expect(resultText).toContain('more safe text');
});
it('should sanitize large ANSI text (>5000 chars) and strip escape codes', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
const largeTextWithAnsi =
'\x1B[31m' +
'red text'.repeat(800) +
'\x1B[0m' +
'\x1B[32m' +
'green text'.repeat(200) +
'\x1B[0m';
expect(largeTextWithAnsi.length).toBeGreaterThan(5000);
act(() =>
result.current.handleInput({
name: '',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: largeTextWithAnsi,
}),
);
const resultText = getBufferState(result).text;
expect(resultText).not.toContain('\x1B[31m');
expect(resultText).not.toContain('\x1B[32m');
expect(resultText).not.toContain('\x1B[0m');
expect(resultText).toContain('red text');
expect(resultText).toContain('green text');
});
it('should not strip popular emojis', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),