From 614fa4b3abc5166517b95342ff7c7c059f366418 Mon Sep 17 00:00:00 2001 From: Taneli Lahtela Date: Fri, 14 Aug 2026 16:00:29 +0300 Subject: [PATCH] Add null guards to the selectionchange anchor walk in TextLayerBuilder Safari can report a selection boundary whose backwards walk leaves the document (range end on / with endOffset === 0, or a boundary in a disconnected subtree). The walk then reads previousSibling of a null parentNode and throws TypeError: null is not an object (evaluating 'anchor.previousSibling') in the selectionchange listener. Guard the three reads of anchor; when the walk escapes the document, parentTextLayer is undefined and the endOfContent handling is skipped, which is correct because the selection end is not in a text layer. --- web/text_layer_builder.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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;