diff --git a/core/selection.ts b/core/selection.ts index f68d877d0b..642bf335eb 100644 --- a/core/selection.ts +++ b/core/selection.ts @@ -168,6 +168,17 @@ class Selection { let node: Node; let [leaf, offset] = this.scroll.leaf(index); if (leaf == null) return null; + if (length > 0 && offset === leaf.length()) { + const [next] = this.scroll.leaf(index + 1); + if (next) { + const [line] = this.scroll.line(index); + const [nextLine] = this.scroll.line(index + 1); + if (line === nextLine) { + leaf = next; + offset = 0; + } + } + } [node, offset] = leaf.position(offset, true); const range = document.createRange(); if (length > 0) { diff --git a/test/unit/core/selection.js b/test/unit/core/selection.js index 1d40b9d1cc..8c5d723a65 100644 --- a/test/unit/core/selection.js +++ b/test/unit/core/selection.js @@ -601,6 +601,22 @@ describe('Selection', function() { ); }); + it('selection starting at end of text node', function() { + this.div.style.width = `${this.reference.width * 4}px`; + const selection = this.initialize( + Selection, + ` +

+ 0000 + 0000 + 0000 +

`, + this.div, + ); + this.bounds = selection.getBounds(4, 1); + expect(this.bounds.width).toBeApproximately(this.reference.width, 1); + }); + it('multiple lines', function() { const selection = this.initialize( Selection,