Skip to content

Commit

Permalink
🐛 Fix the use of empty styledTextSegments array (#31)
Browse files Browse the repository at this point in the history
When a TextNode contains empty characters field, getStyledTextSegments returns empty array. This causes a TypeError, when we try to access the properties of the first element of this array.

This change adds a check if the first element of the styledTextSegments exists before using its properties.

Signed-off-by: Roma <[email protected]>
  • Loading branch information
syncroma authored Nov 3, 2023
1 parent c64895f commit 7dc7309
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,24 @@ function traverse(node): NodeData {

if (node.type == "TEXT") {
const styledTextSegments = node.getStyledTextSegments(["fontName", "fontSize", "fontWeight", "lineHeight", "letterSpacing", "textCase", "textDecoration", "fills"]);
let font = {
fontName: styledTextSegments[0].fontName,
fontSize: styledTextSegments[0].fontSize.toString(),
fontWeight: styledTextSegments[0].fontWeight.toString(),
characters: node.characters,
lineHeight: styledTextSegments[0].lineHeight,
letterSpacing: styledTextSegments[0].letterSpacing,
fills: styledTextSegments[0].fills,
textCase: styledTextSegments[0].textCase,
textDecoration: styledTextSegments[0].textDecoration,
textAlignHorizontal: node.textAlignHorizontal,
textAlignVertical: node.textAlignVertical,
children: styledTextSegments
};
result = {...result, ...font};

if (styledTextSegments[0]) {
let font = {
fontName: styledTextSegments[0].fontName,
fontSize: styledTextSegments[0].fontSize.toString(),
fontWeight: styledTextSegments[0].fontWeight.toString(),
characters: node.characters,
lineHeight: styledTextSegments[0].lineHeight,
letterSpacing: styledTextSegments[0].letterSpacing,
fills: styledTextSegments[0].fills,
textCase: styledTextSegments[0].textCase,
textDecoration: styledTextSegments[0].textDecoration,
textAlignHorizontal: node.textAlignHorizontal,
textAlignVertical: node.textAlignVertical,
children: styledTextSegments
};
result = {...result, ...font};
}
}

return result;
Expand Down

0 comments on commit 7dc7309

Please sign in to comment.