From bd3e953af36680f916b7d0be3381bbf02cd1e354 Mon Sep 17 00:00:00 2001 From: TJ Kandala Date: Fri, 9 Apr 2021 17:47:33 -0400 Subject: [PATCH] fix: exclude end position in findElementWithOffset --- src/token_position.test.ts | 10 +++++----- src/token_position.ts | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/token_position.test.ts b/src/token_position.test.ts index 374913fd..65f42d62 100644 --- a/src/token_position.test.ts +++ b/src/token_position.test.ts @@ -180,13 +180,13 @@ describe('token_positions', () => { const content = `${tabChar}if rv := contextGet(r, routeKey); rv != nil {` const ranges = [ - { offsetStart: 2, offsetEnd: 3, textContent: 'if' }, + { offsetStart: 2, offsetEnd: 4, textContent: 'if' }, + { offsetStart: 2, offsetEnd: 5, textContent: 'if ' }, // Intentional limitation: match whole text node at a given offset // since that is much simpler to highlight. - { offsetStart: 3, offsetEnd: 5, textContent: 'if rv' }, - { offsetStart: 2, offsetEnd: 5, textContent: 'if rv' }, + { offsetStart: 3, offsetEnd: 5, textContent: 'if ' }, { offsetStart: 2, offsetEnd: 6, textContent: 'if rv' }, - { offsetStart: 11, offsetEnd: 33, textContent: 'contextGet(r, routeKey)' }, + { offsetStart: 11, offsetEnd: 34, textContent: 'contextGet(r, routeKey)' }, // If offsetEnd is less or equal to offsetStart, range should be treated as a position (offsetStart) { offsetStart: 11, offsetEnd: 4, textContent: 'contextGet' }, ] @@ -244,7 +244,7 @@ describe('token_positions', () => { const offsets = [ { offsetStart: content.length + 1, offsetEnd: content.length + 2 }, - { offsetStart: 1, offsetEnd: content.length + 1 }, + { offsetStart: 1, offsetEnd: content.length + 2 }, ] const elem = dom.createElementFromString(content) diff --git a/src/token_position.ts b/src/token_position.ts index 6cbd2711..c61d7f84 100644 --- a/src/token_position.ts +++ b/src/token_position.ts @@ -289,7 +289,8 @@ export function findElementWithOffset( } // offsetEnd should be greater than offsetStart, so only check for it after targetStartNode has been found if (targetStartNode) { - if (offsetStep <= offsetEnd && offsetStep + text.length > offsetEnd) { + // End position of range is exclusive + if (offsetStep < offsetEnd && offsetStep + text.length >= offsetEnd) { targetEndNode = node break }