Skip to content

Commit 7118df5

Browse files
committed
fix(trim): add for custom elements
1 parent 0817bde commit 7118df5

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

spec/selection.spec.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ describe('Selection', function () {
394394

395395
it('trims whitespaces from range when linking', function () {
396396
this.selection.link('https://livingdocs.io')
397-
console.log('tag', this.wordWithWhitespace)
398397
const linkTags = this.selection.getTagsByName('a')
399398
const html = getHtml(linkTags[0])
400399
expect(html).to.equal('<a class="foo bar" href="https://livingdocs.io">foobar</a>')
@@ -417,6 +416,22 @@ describe('Selection', function () {
417416
expect(selection.range.startOffset).to.equal(3)
418417
expect(selection.range.endOffset).to.equal(6)
419418
})
419+
420+
it('does trim if only a whitespace is selected', function () {
421+
const whitespaceOnly = createElement('<div> </div>')
422+
const range = rangy.createRange()
423+
range.selectNodeContents(whitespaceOnly.firstChild)
424+
const selection = new Selection(whitespaceOnly, range)
425+
selection.trimRange()
426+
expect(selection.toString()).to.equal('')
427+
})
428+
429+
it('trims a custom element if the param is given', function () {
430+
this.selection.toggleCustom({tagName: 'span', attributes: {class: 'foo'}, trim: true})
431+
const spanTags = this.selection.getTagsByName('span')
432+
const html = getHtml(spanTags[0])
433+
expect(html).to.equal('<span class="foo">foobar</span>')
434+
})
420435
})
421436

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

src/selection.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,15 @@ export default class Selection extends Cursor {
149149
this.setSelection()
150150
}
151151

152-
toggleCustom ({tagName, attributes}) {
152+
toggleCustom ({tagName, attributes, trim = false}) {
153153
const customElem = this.createElement(tagName, attributes)
154+
if (trim) this.trimRange()
154155
this.toggle(customElem)
155156
}
156157

157-
makeCustom ({tagName, attributes}) {
158+
makeCustom ({tagName, attributes, trim = false}) {
158159
const customElem = this.createElement(tagName, attributes)
160+
if (trim) this.trimRange()
159161
this.forceWrap(customElem)
160162
}
161163

0 commit comments

Comments
 (0)