Skip to content

Commit 371fd4f

Browse files
committed
fix(selection): Revert to jQuery-behavior for link attrs update
Revert to same behavior as jQuery's .attr() for updating attributes in link(): - null value removes attribute - undefined value does not modify attribute This reverts to the behavior before the migration away from jQuery (83e512f).
1 parent 859cb84 commit 371fd4f

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

spec/selection.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,27 @@ describe('Selection', function () {
269269
const linkTags = this.selection.getTagsByName('a')
270270
expect(linkTags.length).to.equal(0)
271271
})
272+
273+
it('sets class attribute', function () {
274+
this.selection.link('https://livingdocs.io', {class: 'baz'})
275+
const linkTags = this.selection.getTagsByName('a')
276+
const html = getHtml(linkTags[0])
277+
expect(html).to.equal('<a class="baz" href="https://livingdocs.io">foobar</a>')
278+
})
279+
280+
it('removes class attribute when set to null', function () {
281+
this.selection.link('https://livingdocs.io', {class: null})
282+
const linkTags = this.selection.getTagsByName('a')
283+
const html = getHtml(linkTags[0])
284+
expect(html).to.equal('<a href="https://livingdocs.io">foobar</a>')
285+
})
286+
287+
it('does not modify class attribute when set to undefined', function () {
288+
this.selection.link('https://livingdocs.io', {class: undefined})
289+
const linkTags = this.selection.getTagsByName('a')
290+
const html = getHtml(linkTags[0])
291+
expect(html).to.equal('<a class="foo bar" href="https://livingdocs.io">foobar</a>')
292+
})
272293
})
273294

274295
})

src/selection.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,15 @@ export default class Selection extends Cursor {
7878
link (href, attrs = {}) {
7979
if (href) attrs.href = href
8080
const link = this.createElement(config.linkMarkup.name, config.linkMarkup.attribs)
81-
for (const key in attrs) link.setAttribute(key, attrs[key])
81+
for (const key in attrs) {
82+
const value = attrs[key]
83+
if (value === undefined) continue
84+
if (value === null) {
85+
link.removeAttribute(key)
86+
} else {
87+
link.setAttribute(key, value)
88+
}
89+
}
8290
this.forceWrap(link)
8391
}
8492

0 commit comments

Comments
 (0)