From 56e49a6c55e196eb840ae2ad3c5cc6bdff686de1 Mon Sep 17 00:00:00 2001 From: Sakura Knoll Date: Tue, 11 Jul 2023 04:10:34 +0900 Subject: [PATCH] Fix tokenize --- src/components/v2-pre/FormattedText.tsx | 2 +- src/lib/v2-pre/data/formatText.spec.ts | 18 ++++++++++++++++ src/lib/v2-pre/data/formatText.ts | 2 +- src/lib/v2-pre/formattedText/tokenize.spec.ts | 21 +++++++++++++++++++ src/lib/v2-pre/formattedText/tokenize.ts | 8 +++++-- 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/components/v2-pre/FormattedText.tsx b/src/components/v2-pre/FormattedText.tsx index a14587c9..61cea8a6 100644 --- a/src/components/v2-pre/FormattedText.tsx +++ b/src/components/v2-pre/FormattedText.tsx @@ -7,7 +7,7 @@ interface FormattedTextProps { const FormattedText = ({ children }: FormattedTextProps) => { if (children == null) { - children = '' + return <> } const tokens = tokenize(children) diff --git a/src/lib/v2-pre/data/formatText.spec.ts b/src/lib/v2-pre/data/formatText.spec.ts index 93627482..3ce3197a 100644 --- a/src/lib/v2-pre/data/formatText.spec.ts +++ b/src/lib/v2-pre/data/formatText.spec.ts @@ -37,4 +37,22 @@ describe('formatSubSkillInfo', () => { expect(result).toBe('전투 중 초기 SP가 40.0 증가하며, 오픈월드에서는 10분에 1회 발동한다.') }) + it('prepends 0 if the calculated value below 0', () => { + const params = { + info: '파티원이 출혈 상태의 적 공격 시, 출혈 상태 스택마다 크리티컬률이 #1[f1]% 증가하고, 캐릭터 자신의 물리 대미지가 #2[f1]% 증가한다.', + maxLv: 11, + paramBase1: 0.004, + paramBase2: 0.0025, + paramBase3: 0, + paramAdd1: 0.0003, + paramAdd2: 0.00025, + paramAdd3: 0 + } + + const result = formatSubSkillInfo(params) + + expect(result).toBe( + '필살기 꿈 세계 창조는 추가로 SP를 소모해 물리 대미지가 추가 소모 SP*0.70%만큼 증가하고, 크리티컬률이 추가 소모 SP*0.50%만큼 증가한다(최대 추가 소모 SP: 50pt). 해당 효과는 피니쉬가 종료될 때까지 지속되며, 캐릭터가 사망하거나 퇴장 시 해제된다.' + ) + }) }) diff --git a/src/lib/v2-pre/data/formatText.ts b/src/lib/v2-pre/data/formatText.ts index 69075b9c..242f5bbb 100644 --- a/src/lib/v2-pre/data/formatText.ts +++ b/src/lib/v2-pre/data/formatText.ts @@ -66,7 +66,7 @@ export function formatSubSkillInfo({ export function replaceNewLine(value: string) { if (value == null) { - value = '' + return '' } return value.replace(/\\n/g, '\n') } diff --git a/src/lib/v2-pre/formattedText/tokenize.spec.ts b/src/lib/v2-pre/formattedText/tokenize.spec.ts index 30dc38b8..788fdc73 100644 --- a/src/lib/v2-pre/formattedText/tokenize.spec.ts +++ b/src/lib/v2-pre/formattedText/tokenize.spec.ts @@ -1,6 +1,27 @@ import { tokenize } from './tokenize' describe('tokenize', () => { + it('still tokenize even when a closing tag is missing', () => { + const rawString = '분기' + + const result = tokenize(rawString) + + expect(result).toEqual([ + { + type: 'element', + tagName: 'color', + attributes: { + color: '#FFC741FF' + }, + children: [ + { + type: 'text', + text: '분기' + } + ] + } + ]) + }) it('tokenize', () => { const rawString = '기본 공격 또는 분기 공격이 적에게 연소 게이지를 4.0pt 누적한다. 발동 간격: 6.0초. 기본 공격과 분기 공격이 가하는 화염 원소 대미지가 30.0% 증가한다.' diff --git a/src/lib/v2-pre/formattedText/tokenize.ts b/src/lib/v2-pre/formattedText/tokenize.ts index d805d91a..6b323aa5 100644 --- a/src/lib/v2-pre/formattedText/tokenize.ts +++ b/src/lib/v2-pre/formattedText/tokenize.ts @@ -18,7 +18,7 @@ export function tokenize(value: string) { let currentValue = value.replace(/{{/g, '').replace(/}}/g, '') while (currentValue.length > 0) { - const openingBracketStartIndex = currentValue.indexOf('<') + const openingBracketStartIndex = currentValue.indexOf('', nextClosingBracketStartIndex) - eat(nextClosingBracketEndIndex + 1) + if (nextClosingBracketEndIndex < 0) { + eat(currentValue.length) + } else { + eat(nextClosingBracketEndIndex + 1) + } } return tokens