Skip to content

Commit

Permalink
fix(speculative.ts): check if temp node could be created
Browse files Browse the repository at this point in the history
  • Loading branch information
BearToCode committed Jul 17, 2024
1 parent 9f717c5 commit 42a57dc
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/carta-md/src/lib/internal/speculative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 42a57dc

Please sign in to comment.