From 7dc730994f9a78c2dd4e6b764c5e70afe9891afe Mon Sep 17 00:00:00 2001 From: romachne <40444580+romachne@users.noreply.github.com> Date: Fri, 3 Nov 2023 13:35:17 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20the=20use=20of=20empty=20s?= =?UTF-8?q?tyledTextSegments=20array=20(#31)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/code.ts | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/code.ts b/src/code.ts index 13c2bed1..67c748c4 100644 --- a/src/code.ts +++ b/src/code.ts @@ -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;