Skip to content

Commit 796408d

Browse files
committed
fix: Selection.getTextRange respects newlines
1 parent 22c6a08 commit 796408d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/selection.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,17 @@ export default class Selection extends Cursor {
5353
}
5454

5555
getTextRange () {
56-
return this.range.toCharacterRange(this.host)
56+
const rangeUntilSelection = this.range.cloneRange()
57+
rangeUntilSelection.setStart(this.host, 0)
58+
rangeUntilSelection.setEnd(this.range.startContainer, this.range.startOffset)
59+
60+
const numNewLinesBefore = $('<div>' + rangeUntilSelection.toHtml() + '</div>').find('br').length
61+
const numNewLinesWithin = $('<div>' + this.range.toHtml() + '</div>').find('br').length
62+
const textRange = this.range.toCharacterRange(this.host)
63+
return {
64+
start: textRange.start + numNewLinesBefore,
65+
end: textRange.end + numNewLinesWithin + numNewLinesBefore
66+
}
5767
}
5868

5969
// Get the ClientRects of this selection.

0 commit comments

Comments
 (0)