Skip to content

Commit 0817bde

Browse files
committed
chore(trim): use consistent whitespaces
1 parent 657871f commit 0817bde

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

spec/selection.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,17 @@ describe('Selection', function () {
406406
const html = getHtml(underlineTags[0])
407407
expect(html).to.equal('<u class="bar"> foobar </u>')
408408
})
409+
410+
it('trims a range with special whitespaces', function () {
411+
// at the beginning we have U+2002, U+2005 and U+2006 in the end a normal whitespace
412+
const wordWithSpecialWhitespaces = createElement('<div>   bar </div>')
413+
const range = rangy.createRange()
414+
range.selectNodeContents(wordWithSpecialWhitespaces.firstChild)
415+
const selection = new Selection(wordWithSpecialWhitespaces, range)
416+
selection.trimRange()
417+
expect(selection.range.startOffset).to.equal(3)
418+
expect(selection.range.endOffset).to.equal(6)
419+
})
409420
})
410421

411422
describe('inherits form Cursor', function () {

src/selection.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,17 @@ export default class Selection extends Cursor {
8787
link.setAttribute(key, value)
8888
}
8989
}
90-
if (config.linkMarkup.trim) this.trimRangeWhitespaces()
90+
if (config.linkMarkup.trim) this.trimRange()
9191

9292
this.forceWrap(link)
9393
}
9494

9595
// trims whitespaces on the left and right of a selection, i.e. what you want in case of links
96-
trimRangeWhitespaces () {
96+
trimRange () {
9797
const textToLink = this.range.toString()
9898
const whitespacesOnTheLeft = textToLink.search(/\S|$/)
99-
const whitespacesOnTheRight = textToLink.length - whitespacesOnTheLeft - textToLink.trim().length
99+
const lastNonWhitespace = textToLink.search(/\S[\s]+$/)
100+
const whitespacesOnTheRight = lastNonWhitespace === -1 ? 0 : textToLink.length - (lastNonWhitespace + 1)
100101
this.range.setStart(this.range.startContainer, this.range.startOffset + whitespacesOnTheLeft)
101102
this.range.setEnd(this.range.endContainer, this.range.endOffset - whitespacesOnTheRight)
102103
}
@@ -160,37 +161,37 @@ export default class Selection extends Cursor {
160161

161162
makeBold () {
162163
const bold = this.createElement(config.boldMarkup.name, config.boldMarkup.attribs)
163-
if (config.boldMarkup.trim) this.trimRangeWhitespaces()
164+
if (config.boldMarkup.trim) this.trimRange()
164165
this.forceWrap(bold)
165166
}
166167

167168
toggleBold () {
168169
const bold = this.createElement(config.boldMarkup.name, config.boldMarkup.attribs)
169-
if (config.boldMarkup.trim) this.trimRangeWhitespaces()
170+
if (config.boldMarkup.trim) this.trimRange()
170171
this.toggle(bold)
171172
}
172173

173174
giveEmphasis () {
174175
const em = this.createElement(config.italicMarkup.name, config.italicMarkup.attribs)
175-
if (config.italicMarkup.trim) this.trimRangeWhitespaces()
176+
if (config.italicMarkup.trim) this.trimRange()
176177
this.forceWrap(em)
177178
}
178179

179180
toggleEmphasis () {
180181
const em = this.createElement(config.italicMarkup.name, config.italicMarkup.attribs)
181-
if (config.italicMarkup.trim) this.trimRangeWhitespaces()
182+
if (config.italicMarkup.trim) this.trimRange()
182183
this.toggle(em)
183184
}
184185

185186
makeUnderline () {
186187
const u = this.createElement(config.underlineMarkup.name, config.underlineMarkup.attribs)
187-
if (config.underlineMarkup.trim) this.trimRangeWhitespaces()
188+
if (config.underlineMarkup.trim) this.trimRange()
188189
this.forceWrap(u)
189190
}
190191

191192
toggleUnderline () {
192193
const u = this.createElement(config.underlineMarkup.name, config.underlineMarkup.attribs)
193-
if (config.underlineMarkup.trim) this.trimRangeWhitespaces()
194+
if (config.underlineMarkup.trim) this.trimRange()
194195
this.toggle(u)
195196
}
196197

0 commit comments

Comments
 (0)