From 42a57dca64e99556187d59a63a9177d05c60d1a3 Mon Sep 17 00:00:00 2001 From: BearToCode Date: Wed, 17 Jul 2024 20:20:04 +0200 Subject: [PATCH] fix(speculative.ts): check if temp node could be created --- packages/carta-md/src/lib/internal/speculative.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/carta-md/src/lib/internal/speculative.ts b/packages/carta-md/src/lib/internal/speculative.ts index 44c71383..251303d4 100644 --- a/packages/carta-md/src/lib/internal/speculative.ts +++ b/packages/carta-md/src/lib/internal/speculative.ts @@ -14,7 +14,9 @@ export function speculativeHighlightUpdate(container: HTMLDivElement, from: stri const textNodes = textNodesUnder(container); if (textNodes.length == 0) { - textNodes.push(createTemporaryNode(container)); + const tempNode = createTemporaryNode(container); + if (!tempNode) return; // Could not create a temporary node + textNodes.push(tempNode); } let currentNodeIdx = 0; @@ -119,7 +121,8 @@ function textNodesUnder(elem: HTMLElement) { * @returns A new text node appended to the last line of the container. */ export function createTemporaryNode(container: HTMLDivElement) { - const span = Array.from(container.querySelectorAll('.line')).at(-1)!; + const span = Array.from(container.querySelectorAll('.line')).at(-1); + if (!span) return null; // Could not find a line const node = document.createTextNode(''); span.appendChild(node);