From c7575b49f60e12e4d68f173b11423e64be2e8db7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Dziewo=C5=84ski?= Date: Wed, 27 May 2020 20:51:26 +0200 Subject: [PATCH] Work around invalid ranges in #getNativeRange Change-Id: I46a54654acb15fb1ef7b80495cf1be8103b7688f --- modules/utils.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/modules/utils.js b/modules/utils.js index 2963886a4..7b457bf9a 100644 --- a/modules/utils.js +++ b/modules/utils.js @@ -7,11 +7,20 @@ * @return {Range} */ function getNativeRange( comment ) { - var + var endContainer, endOffset, doc = comment.range.startContainer.ownerDocument, nativeRange = doc.createRange(); nativeRange.setStart( comment.range.startContainer, comment.range.startOffset ); - nativeRange.setEnd( comment.range.endContainer, comment.range.endOffset ); + // HACK: When the offset is outside the container, assume this is because of + // the 'mw:Entity' hack in parser#findTimestamps and adjust accordingly. + // TODO: The parser should produce valid ranges! + endContainer = comment.range.endContainer; + endOffset = comment.range.endOffset; + while ( endOffset > ( endContainer.length || endContainer.childNodes.length ) ) { + endOffset -= ( endContainer.length || endContainer.childNodes.length ); + endContainer = endContainer.nextSibling; + } + nativeRange.setEnd( endContainer, endOffset ); return nativeRange; }