Skip to content

Commit e1b2285

Browse files
chore: add tests and address PR comments
1 parent f32972b commit e1b2285

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

spec/selection.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,31 @@ describe('Selection', function () {
7878
})
7979
})
8080

81+
describe('deleteExactSurroundingTags:', function () {
82+
it('deletes the farest ancestor that exactly surrounds the selection', function () {
83+
const content = createElement('<p>text <strong><em>italic</em></strong> text</p>')
84+
const em = content.getElementsByTagName('em')[0]
85+
const range = rangy.createRange()
86+
range.setStart(em, 0)
87+
range.setEnd(em, 1)
88+
let selection = new Selection(content, range)
89+
selection = selection.deleteExactSurroundingTags()
90+
expect(selection.host.innerHTML).to.equal('text text')
91+
})
92+
})
93+
94+
describe('deleteContainedTags:', function () {
95+
it('deletes all the tags whose content is completely within the current selection: ', function () {
96+
const content = createElement('<p>text <strong>bold</strong> text')
97+
const range = rangy.createRange()
98+
range.setStart(content, 1)
99+
range.setEnd(content, 3)
100+
let selection = new Selection(content, range)
101+
selection = selection.deleteContainedTags()
102+
expect(selection.host.innerHTML).to.equal('text text')
103+
})
104+
})
105+
81106
describe('with a range', function () {
82107

83108
beforeEach(function () {

src/clipboard.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export function paste (element, cursor, clipboardContent) {
3030
element.setAttribute(config.pastingAttribute, true)
3131

3232
if (cursor.isSelection) {
33-
cursor = cursor.deleteExactSurroundingMarkups()
34-
.deleteContainedMarkupTags()
33+
cursor = cursor.deleteExactSurroundingTags()
34+
.deleteContainedTags()
3535
.deleteContent()
3636
}
3737

src/selection.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -248,27 +248,23 @@ export default class Selection extends Cursor {
248248
// Delete the farest ancestor that is an exact selection
249249
//
250250
// @return Selection instance
251-
deleteExactSurroundingMarkups () {
252-
const ancestorMarkupTags = this.getAncestorTags(
253-
(elem) => ['STRONG', 'EM'].includes(elem.nodeName)
254-
).reverse()
255-
for (const ancestorMarkupTag of ancestorMarkupTags) {
256-
if (this.isExactSelection(ancestorMarkupTag)) {
257-
ancestorMarkupTag.remove()
251+
deleteExactSurroundingTags () {
252+
const ancestorTags = this.getAncestorTags().reverse()
253+
for (const ancestorTag of ancestorTags) {
254+
if (this.isExactSelection(ancestorTag)) {
255+
ancestorTag.remove()
258256
break
259257
}
260258
}
261259
return new Selection(this.host, this.range)
262260
}
263261

264-
// Delete all the markups whose text is completely within the current selection.
262+
// Delete all the tags whose text is completely within the current selection.
265263
//
266264
// @return Selection instance
267-
deleteContainedMarkupTags () {
268-
const containedMarkupTags = this.getContainedTags(
269-
(elem) => ['STRONG', 'EM'].includes(elem.nodeName)
270-
)
271-
containedMarkupTags.forEach(containedMarkupTag => containedMarkupTag.remove())
265+
deleteContainedTags () {
266+
const containedTags = this.getContainedTags()
267+
containedTags.forEach(containedTag => containedTag.remove())
272268
return new Selection(this.host, this.range)
273269
}
274270

0 commit comments

Comments
 (0)