/
githubmirror
/
pixijs
Обзор
Документация
Войти
/
githubmirror
/
pixijs
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
dev
src/scene/text-split/__tests__/SegmentedText.test.ts
273 строки
8 KB
Zyie
fix: SplitText crash when text begins with whitespace (#12040) (#12041)
12 май 2026, 11:26
Не верифицирован
12 май 2026, 11:26
fd36778
Код
Авторство
О чём код?
import { Text } from '../../text/Text'; import { TextStyle } from '../../text/TextStyle'; import { BitmapText } from '../../text-bitmap/BitmapText'; import { SplitBitmapText } from '../SplitBitmapText'; import { SplitText } from '../SplitText'; describe('SegmentedText', () => { describe('constructor', () => { it('should create instance with default options', () => { const text = new SplitText({ text: 'Hello', style: { fontSize: 24 }, }); expect(text.text).toBe('Hello'); expect(text.chars).toBeInstanceOf(Array); expect(text.words).toBeInstanceOf(Array); expect(text.lines).toBeInstanceOf(Array); expect(text.style).toBeInstanceOf(TextStyle); }); it('should respect custom options', () => { const text = new SplitText({ text: 'Hello', style: { fontSize: 24 }, autoSplit: false, lineAnchor: 0.5, wordAnchor: { x: 0, y: 1 }, charAnchor: { x: 0.5, y: 0.5 }, }); expect(text.lineAnchor).toEqual(0.5); expect(text.wordAnchor).toEqual({ x: 0, y: 1 }); expect(text.charAnchor).toEqual({ x: 0.5, y: 0.5 }); }); }); describe('static from()', () => { it('should create from Text instance', () => { const sourceText = new Text({ text: 'Convert me', style: { fontSize: 24 }, }); const segmented = SplitText.from(sourceText, { lineAnchor: 0.5, }); expect(segmented).toBeInstanceOf(SplitText); expect(segmented.text).toBe('Convert me'); expect(segmented.lineAnchor).toEqual(0.5); }); it('should create from BitmapText instance', () => { const bitmapText = new BitmapText({ text: 'Bitmap', style: { fontFamily: 'Arial' }, }); const segmented = SplitBitmapText.from(bitmapText); expect(segmented).toBeInstanceOf(SplitBitmapText); expect(segmented.text).toBe('Bitmap'); }); }); describe('segmentation', () => { it('should segment text into chars, words, and lines', () => { const text = new SplitText({ text: 'Hello World', style: { fontSize: 24 }, }); expect(text.chars.length).toBeGreaterThan(0); expect(text.words.length).toBe(2); expect(text.lines.length).toBe(1); }); it('should handle multi-line text', () => { const text = new SplitText({ text: 'Hello\nWorld', style: { fontSize: 24 }, }); expect(text.lines.length).toBe(2); }); it('should respect text alignment', () => { const text = new SplitBitmapText({ text: 'Right\nAligned', style: { align: 'right', }, }); const firstWord = text.words[0]; expect(firstWord.x).not.toBe(0); // Should be offset for right alignment expect(text.lines.length).toBe(2); expect(text.words.length).toBe(2); expect(text.chars.length).toBe(12); // 'Right' + 'Aligned' }); it('should handle text beginning with whitespace', () => { const text = new SplitText({ text: ' Hello, world!', style: { fontSize: 24 }, }); expect(text.lines.length).toBe(1); expect(text.words.length).toBe(2); expect(text.chars.length).toBe(12); // 'Hello,' (6) + 'world!' (6); spaces are word breaks }); it('should handle a non-first line beginning with whitespace', () => { const text = new SplitText({ text: 'Foo\n Bar', style: { fontSize: 24 }, }); expect(text.lines.length).toBe(2); expect(text.words.length).toBe(2); // 'Foo' on line 1, 'Bar' on line 2 expect(text.chars.length).toBe(6); // 'Foo' (3) + 'Bar' (3) }); it('should respect autoSegment option', () => { const text = new SplitText({ text: 'Hello', style: { fontSize: 24 }, autoSplit: false, }); expect(text.chars.length).toBe(0); // No segmentation done text.split(); expect(text.chars.length).toBe(5); // Now segmented text.text = 'Changed'; expect(text.chars.length).toBe(0); // Should not auto-segment text.split(); expect(text.chars[0].text).toBe('C'); // Now updated }); }); describe('transform origins', () => { it('should set numeric origins', () => { const text = new SplitText({ text: 'Test', style: { fontSize: 24 }, }); text.lineAnchor = 0.5; expect(text.lineAnchor).toEqual(0.5); text.wordAnchor = 0.5; expect(text.wordAnchor).toEqual(0.5); text.charAnchor = 0.5; expect(text.charAnchor).toEqual(0.5); }); it('should set point origins', () => { const text = new SplitText({ text: 'Test', style: { fontSize: 24 }, }); const point = { x: 0.5, y: 1 }; text.lineAnchor = point; expect(text.lineAnchor).toEqual(point); }); it('should update origins after text changes', () => { const text = new SplitText({ text: 'Test', style: { fontSize: 24 }, charAnchor: 0.5, }); const originalOrigin = text.chars[0].origin.x; text.text = 'New'; expect(text.chars[0].origin.x).not.toBe(originalOrigin); }); }); describe('style updates', () => { it('should update style properties', () => { const text = new SplitText({ text: 'Test', style: { fontSize: 24 }, }); text.style = { fontSize: 48 }; expect(text.style.fontSize).toBe(48); }); it('should trigger segmentation on style change', () => { const text = new SplitText({ text: 'Test', style: { fontSize: 24 }, }); const char = text.chars[0]; const originalBounds = char.getBounds(); text.style = { fontSize: 48 }; const newChar = text.chars[0]; const newBounds = newChar.getBounds(); expect(newBounds.height).toBeGreaterThan(originalBounds.height); }); }); describe('destroy', () => { it('should clean up resources', () => { const text = new SplitText({ text: 'Test', style: { fontSize: 24 }, }); text.destroy({ children: true, texture: true, style: true }); expect(text.chars).toEqual([]); expect(text.words).toEqual([]); expect(text.lines).toEqual([]); expect(text.style).toBeNull(); }); it('should handle partial cleanup', () => { const text = new SplitText({ text: 'Test', style: { fontSize: 24 }, }); const style = text.style; text.destroy({ children: true, style: false }); expect(text.chars).toEqual([]); expect(style).not.toBeNull(); }); }); });