Skip to content

Commit 12ff339

Browse files
committed
fix(cursor): add textBefore and textAfter methods
1 parent c11445c commit 12ff339

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

spec/cursor.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ describe('Cursor', function () {
100100
})
101101
})
102102

103+
describe('textBefore()', () => {
104+
it('gets the text before', () => {
105+
const textBefore = this.cursor.textBefore()
106+
expect(textBefore).toEqual('foobar')
107+
})
108+
})
109+
103110
describe('beforeHtml()', () => {
104111
it('gets the content before', () => {
105112
expect(this.cursor.beforeHtml()).toEqual('foobar')
@@ -113,6 +120,13 @@ describe('Cursor', function () {
113120
})
114121
})
115122

123+
describe('textAfter()', () => {
124+
it('gets the text after', () => {
125+
var textAfter = this.cursor.textAfter()
126+
expect(textAfter).toEqual('')
127+
})
128+
})
129+
116130
describe('afterHtml()', () => {
117131
it('gets the content before', () => {
118132
expect(this.cursor.afterHtml()).toEqual('')

spec/selection.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,29 @@ describe('Selection', function () {
4141
})
4242
})
4343

44+
describe('textBefore() / textAfter()', function () {
45+
46+
beforeEach(function () {
47+
// <div>a|b|c</div>
48+
const host = $('<div>abc</div>')[0]
49+
const range = rangy.createRange()
50+
range.setStart(host.firstChild, 1)
51+
range.setEnd(host.firstChild, 2)
52+
53+
this.selection = new Selection(host, range)
54+
})
55+
56+
it('returns the text before', function () {
57+
const textBefore = this.selection.textBefore()
58+
expect(textBefore).toEqual('a')
59+
})
60+
61+
it('returns the text after', function () {
62+
const textAfter = this.selection.textAfter()
63+
expect(textAfter).toEqual('c')
64+
})
65+
})
66+
4467
describe('with a range', function () {
4568

4669
beforeEach(function () {

src/cursor.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,18 @@ export default class Cursor {
109109
// @returns {Document Fragment} content before the cursor or selection.
110110
before () {
111111
const range = this.range.cloneRange()
112+
range.collapse(true)
112113
range.setStartBefore(this.host)
113114
return content.cloneRangeContents(range)
114115
}
115116

117+
textBefore () {
118+
const range = this.range.cloneRange()
119+
range.collapse(true)
120+
range.setStartBefore(this.host)
121+
return range.toString()
122+
}
123+
116124
// Same as before() but returns a string.
117125
beforeHtml () {
118126
return content.getInnerHtmlOfFragment(this.before())
@@ -127,10 +135,18 @@ export default class Cursor {
127135
// @returns {Document Fragment} content after the cursor or selection.
128136
after () {
129137
const range = this.range.cloneRange()
138+
range.collapse(false)
130139
range.setEndAfter(this.host)
131140
return content.cloneRangeContents(range)
132141
}
133142

143+
textAfter () {
144+
const range = this.range.cloneRange()
145+
range.collapse(false)
146+
range.setEndAfter(this.host)
147+
return range.toString()
148+
}
149+
134150
// Same as after() but returns a string.
135151
afterHtml () {
136152
return content.getInnerHtmlOfFragment(this.after())

0 commit comments

Comments
 (0)