Skip to content

Commit

Permalink
fix: Don't wrap a collapsed selection with tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ajwild committed Dec 7, 2023
1 parent 14a013a commit fa09d39
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
20 changes: 20 additions & 0 deletions spec/selection.spec.js
Expand Up @@ -462,6 +462,26 @@ describe('Selection', function () {
const html = getHtml(spanTags[0])
expect(html).to.equal('<span class="foo">foobar</span>')
})

it('does not apply tags to whitespace when toggling', function () {
const range = createRange()
range.setStart(this.wordWithWhitespace.firstChild, 0)
range.setEnd(this.wordWithWhitespace.firstChild, 1)
const selection = new Selection(this.wordWithWhitespace, range)
selection.toggleBold()
expect(selection.toString()).to.equal('')
expect(this.wordWithWhitespace.innerHTML).to.equal(' foobar ')
})

it('does not apply tags to whitespace when wrapping', function () {
const range = createRange()
range.setStart(this.wordWithWhitespace.firstChild, 0)
range.setEnd(this.wordWithWhitespace.firstChild, 1)
const selection = new Selection(this.wordWithWhitespace, range)
selection.makeBold()
expect(selection.toString()).to.equal('')
expect(this.wordWithWhitespace.innerHTML).to.equal(' foobar ')
})
})

describe('inherits form Cursor', function () {
Expand Down
2 changes: 2 additions & 0 deletions src/selection.js
Expand Up @@ -156,6 +156,7 @@ export default class Selection extends Cursor {
// e.g. toggle('<em>')
toggle (elem) {
if (block.isPlainTextBlock(this.host)) return
if (this.range.collapsed) return
elem = this.adoptElement(elem)
this.range = content.toggleTag(this.host, this.range, elem)
this.setVisibleSelection()
Expand Down Expand Up @@ -320,6 +321,7 @@ export default class Selection extends Cursor {
// remove first.
forceWrap (elem) {
if (block.isPlainTextBlock(this.host)) return
if (this.range.collapsed) return
elem = this.adoptElement(elem)
this.range = content.forceWrap(this.host, this.range, elem)
this.setVisibleSelection()
Expand Down

0 comments on commit fa09d39

Please sign in to comment.