From 732a30c09f50a9d78d5d1686ffef88a3bde2e53e Mon Sep 17 00:00:00 2001 From: jonz94 Date: Sun, 12 Jan 2025 04:27:16 +0800 Subject: [PATCH] fix(Text#fromAttributed): Fix `StyleRun` assuming that the `startIndex` and `length` always exist (#862) In AttributedText, styleRuns may not always include the `startIndex` or `length` properties - If `startIndex` is missing, we assume the style applies from the beginning of the text - If `length` is missing, we assume the style applies to the entire text --- src/parser/classes/misc/Text.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/parser/classes/misc/Text.ts b/src/parser/classes/misc/Text.ts index b05fb64de..ca2260675 100644 --- a/src/parser/classes/misc/Text.ts +++ b/src/parser/classes/misc/Text.ts @@ -54,7 +54,6 @@ export default class Text { static fromAttributed(data: AttributedText) { const { content, - styleRuns: style_runs, commandRuns: command_runs, attachmentRuns: attachment_runs } = data; @@ -66,6 +65,16 @@ export default class Text { } ]; + // In AttributedText, styleRuns may not always include the `startIndex` or `length` properties + // - If `startIndex` is missing, we assume the style applies from the beginning of the text + // - If `length` is missing, we assume the style applies to the entire text + // The following code ensures default values are provided for these properties + const style_runs = data.styleRuns?.map((run) => ({ + ...run, + startIndex: run.startIndex ?? 0, + length: run.length ?? content.length + }) as StyleRun & ResponseRun); + if (style_runs || command_runs || attachment_runs) { if (style_runs) { for (const style_run of style_runs) { @@ -271,7 +280,7 @@ interface ResponseRun { length: number; } -interface StyleRun extends ResponseRun { +interface StyleRun extends Partial { italic?: boolean; weightLabel?: string; strikethrough?: string;