diff --git a/web/text_layer_builder.js b/web/text_layer_builder.js index 0275e961bacb5..3957647dc3279 100644 --- a/web/text_layer_builder.js +++ b/web/text_layer_builder.js @@ -351,14 +351,14 @@ class TextLayerBuilder { } if (!modifyStart && range.endOffset === 0) { do { - while (!anchor.previousSibling) { + while (anchor && !anchor.previousSibling) { anchor = anchor.parentNode; } - anchor = anchor.previousSibling; - } while (!anchor.childNodes.length); + anchor = anchor?.previousSibling; + } while (anchor && !anchor.childNodes.length); } - const parentTextLayer = anchor.parentElement?.closest(".textLayer"); + const parentTextLayer = anchor?.parentElement?.closest(".textLayer"); const endDiv = this.#textLayers.get(parentTextLayer); if (endDiv) { endDiv.style.width = parentTextLayer.style.width;