Skip to content

Commit

Permalink
fix(Text#fromAttributed): Fix StyleRun assuming that the `startInde…
Browse files Browse the repository at this point in the history
…x` and `length` always exist

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
  • Loading branch information
jonz94 committed Jan 5, 2025
1 parent f3c777b commit ac7c262
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/parser/classes/misc/Text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export default class Text {
static fromAttributed(data: AttributedText) {
const {
content,
styleRuns: style_runs,
commandRuns: command_runs,
attachmentRuns: attachment_runs
} = data;
Expand All @@ -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) {
Expand Down Expand Up @@ -271,7 +280,7 @@ interface ResponseRun {
length: number;
}

interface StyleRun extends ResponseRun {
interface StyleRun extends Partial<ResponseRun> {
italic?: boolean;
weightLabel?: string;
strikethrough?: string;
Expand Down

0 comments on commit ac7c262

Please sign in to comment.